Build an accessible bar chart
Turn class survey results into an SVG bar chart that everyone can read, with a title, a description, direct labels and a data table for screen readers.
Ages 10 to 14, about 45 minutes. You will practice: How to turn numbers into bar heights with a scale, How to draw axes, gridlines and labels in SVG, How to add a title and description that screen readers can use, Why a data table makes a chart more useful for everyone.
Goal: make a bar chart of class survey results that works for everyone, including people who cannot see the picture.
Charts turn numbers into shapes, so we can compare them at a glance. But a chart that is only a picture leaves some people out. A person who is blind, or who uses a screen reader (a tool that reads a page aloud), cannot see the bars. So we will add words and a table as well.
The numbers in this project are an example. A class asked everyone to pick a favorite fruit snack by raising a hand, and an adult counted the hands. The class had 30 children. You do not need names, only counts. You can replace the numbers with your own class results at the end. If you are new to SVG, start with What is SVG?.
Step 1: Turn a number into a length
Every bar chart has a scale. We decide that one child is 20 units of height. The Apple bar has 6 children, so it is 6 times 20, which is 120 units tall. The baseline, where all bars start, is at y="240". SVG measures down from the top, so the top of the bar is at 240 minus 120, which is y="120".
The small square shows one child, 20 units tall. Doing the sum on paper first helps you spot mistakes later.
Try this: make a bar for 3 children. What is its height? What is its y? Check: the height is 60 and the y is 180.
Step 2: Axes and gridlines
Now draw the frame. The vertical axis is a line at x="50". The baseline runs along the bottom. Light gridlines at 2, 4, 6, 8 and 10 children help the eye measure a bar. The numbers go on the left, and each number sits level with its gridline. The top gridline is the 10 mark at y="40".
Try this: add a light gridline for the 5 mark. Which y value does it need? Hint: 240 minus 5 times 20.
Step 3: The bars
Each bar is one <rect>. They are 40 wide and start 60 apart, at x of 70, 130, 190, 250 and 310, so there is a gap between them. The height is the count times 20, and y is 240 minus the height. All the bars use the same color. A dark blue has strong contrast against the white background, so it is easy to see.
| Fruit snack | Children | Height | y |
|---|---|---|---|
| Apple | 6 | 120 | 120 |
| Banana | 4 | 80 | 160 |
| Grapes | 8 | 160 | 80 |
| Orange | 3 | 60 | 180 |
| Strawberry | 9 | 180 | 60 |
Try this: change the Orange bar to 5 children. Work out the new height and y before you type them.
Step 4: Labels
A bar with no label is a mystery. Name each bar under its base. Write the value above each bar too, so nobody has to guess the number by looking along a gridline. This also means the chart does not rely on color or on good eyesight alone to be understood. Keep text at 12 units or larger so it stays readable. Add a label for the vertical axis, turned on its side with rotate(-90 14 140).
Try this: add an x-axis label, such as Fruit snack, at x="200" y="287". Where else might extra words help a reader?
Step 5: A title and a description
Now the part that helps screen reader users most. Add two elements as the first things inside the <svg>:
<title>is a short name for the chart.<desc>explains what the chart shows, including the main finding, in a sentence or two.
Link them with aria-labelledby="chart-title chart-desc" and matching id values. A visible title is also good for everyone, so we draw it in the picture too, at the top. Do not repeat the entire list of numbers in the description. The data table in the next step does that job.
Try this: change the description so it names the two most popular snacks. Then ask a friend to read only the description aloud. Can they tell what the chart is about?
Step 6: Add a data table
The table is the most useful extra for accessibility. It gives the exact numbers in a form a screen reader can step through row by row. It also helps people who want to copy the numbers, or who find charts hard to read. Put it right after the chart on your page, with a caption, and make sure the numbers match the bars.
| Fruit snack | Number of children |
|---|---|
| Apple | 6 |
| Banana | 4 |
| Grapes | 8 |
| Orange | 3 |
| Strawberry | 9 |
| Total | 30 |
Table: Favorite fruit snack in our class survey (example numbers).
In a real web page, you would write this table in HTML with a <caption>, a header row and <th> cells. Here we also help the reader find the main point. The tallest bar is darker, with a short note and a line pointing to it. The note is plain text, so the point does not depend on color alone.
Try this: replace the numbers with your own class results. Change each bar, each value label and the table. Then check the three places agree: the bars, the labels and the table.
For teachers and families
Ask: “Who might not be able to read this chart as a picture, and what did we add for them? Which was more useful to you, the bars or the table?” As a classroom extension, run your own survey with a topic that does not need names, such as favorite season, and count by raised hands. Then compare two versions of the chart: one with only bars, and one with labels, a description and a table. Talk about which questions each version answers best. A good extension is to check that the totals in the table add up to the number of children who voted.