Answer a comparison question with a bar chart you can check
Task
Which product category brings in the most revenue? Calculate the answer, then draw it as a sorted bar chart in SVG, written by your own script, and explain why a bar chart answers this question better than a pie or a line.
Steps
- Calculate the net revenue of valid orders (quantity above zero) for each category in
data/products.csv, and savelab/chart/by-category.csvwith headercategory,net_revenue, sorted from largest to smallest, to two decimal places. - Write
lab/chart/bars.pyto read that file and writelab/chart/by-category.svg, containing one<rect class="bar" data-label="CATEGORY" data-value="VALUE" height="H">per category in the same order, where H is the value divided by the largest value times 300, to one decimal place. - Give the SVG a
<title>naming the question, and a<text class="axis-label">for each of the two axes. - Write in
lab/chart/why.txtwhy a bar chart, and not a pie or a line.
Verify
These checks run in a POSIX shell with Python 3: Terminal on macOS or Linux, and on Windows Git Bash (it comes with Git for Windows) or WSL. On Windows, python3 is often a Microsoft Store placeholder that prints “Python was not found”; run alias python3=py in Git Bash first.
head -2 lab/chart/by-category.csv
grep -o '<rect class="bar"' lab/chart/by-category.svg | wc -l
python3 -c "import re; h = [float(x) for x in re.findall('class=.bar.[^>]*height=.([0-9.]+)', open('lab/chart/by-category.svg').read())]; print('sorted:', h == sorted(h, reverse=True), 'tallest:', max(h))"
grep -c 'class="axis-label"' lab/chart/by-category.svg
Five bars, sorted, the tallest 300.0, and both axes labelled. Stationery leads with 95956.59, but Toys is close behind at 92179.83 -- a gap of 4% that sorted bars on a common baseline show at a glance, and that two neighbouring pie slices would hide. A line would imply the categories are in some order, which they are not.
This is an independent study companion for CompTIA Data+ DA0-002 and is not produced by or endorsed by CompTIA.