More accessible line graphs
Tags: webdev, a11y, design
Published: 2024-06-22 18:34:12

Earlier this week, the [State of JS 2023 survey results](https://2023.stateofjs.com/en-US/) were published and, as always, the results are presented with a variety of different charts and graphs. Upon reviewing the line graphs, I noticed some common, easily fixable accessibility issues. In this post, I will discuss these issues and provide suggestions for improvement.

## Use of colour alone

![Graph from State of JS 2023 showing a line graph of ratios of frontend frameworks over time](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69jprkjfkaghdi9sb4ur.png)

The line graphs are presented with a title, a legend, and the graph itself. However, these graphs rely on color alone to convey information. This is problematic because around 1 in 12 men (approx. 8% of the population) and 1 in 200 women (approx. 0.5% of the population) have a red-green color vision deficiency. Other types of color vision deficiencies mean that about 1 in 10 people may have difficulty distinguishing all the colors used.

We can emulate color deficiencies in Chrome to demonstrate how the graphs appear to people with these conditions.

### Emulated protanopia (no red) 
![Line graph with emulated protanopia](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d09vcedy91mp15a0knnh.png)
Without red, distinguishing between reds, oranges, yellows, and greens is challenging.

### Emulated deuteranopia (no green) 
![Line graph with emulated deuteranopia](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iyreroc7vooh0r9iq0xe.png)
Without green, it's also difficult to distinguish between reds, oranges, yellows and greens.

### Emulated tritanopia (no blue) 
![Line graph with emulated tritanopia](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/im5mk5je1k7el4nykipb.png)
Without blue, the blues and greens become difficult to distinguish and the reds and oranges also become harder to tell apart.

### Emulated achromatopsia (no colour) 
![Line graph with emulated achromatopsia](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ca4rny09426dlflxwher.png)
Without colour, all lines become a shade of grey.

### What's the solution?
There are several ways to improve these graphs to ensure they do not rely on color alone.

#### Different Line Styles  
Different line styles (e.g., dotted, dashed, and solid lines) can be used to differentiate the lines. Additionally, various shapes (e.g., circles, squares, triangles) can mark data points.

Here's an example using different shapes for each data point (albeit poorly photoshopped):

![Line graph where each category has a different shape marking its data points and a legend above to match the shape to the category](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0q9x6e37lrqtepb4j69.png)

There are still some downsides with relying on a legend to communicate the relationship between data points and their category. Legends can be difficult to use for people with certain eye conditions or cognitive disabilities. 

#### Label the Lines Directly
Labeling the lines directly removes the need for a legend, making it easier for users to identify each line. This approach may require more space on the y-axis to accommodate the labels, especially where lines overlap.

Here's an example with labels directly on the lines (again, a rough edit):

![Line graph where each category is labelled directly next to the end of the line](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gq8esxnvvx4o809oljt2.png)


## Alternative text and text alternative
I tested the graph with NVDA, a screen reader, and found that it was an unusable experience. The screen reader read out each element of the y-axis first, then the x-axis, but it didn't specify what these axes represented.

When it got to the graph, it was an unusable experience for anyone using a screen reader. Each part of the graph is made up of many paths in an SVG. NVDA went wild, reading "Graphic, clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable clickable..." and any normal person is left thinking "please make it stop!!!"


![NVDA speech viewer showing the output of the line graph](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/652vklf55jf7c789pjmj.png)

The SVG should be given a `role="img"` and then an alternative text between two `<title>` tags which describes the graph. And because these graphs present a lot of information, it should ideally be presented in another way such as in a table. Screen reader users can navigate through tables to get information but presenting info in another way is also useful for many other people. 

Graphs are not the easiest things for many people to read. They require quite a lot of cognitive energy to work out what the different x and y axes are and what is being shown. Giving people the option of seeing data presented in different formats helps people to understand the way they can best.

## In summary
A few small tweaks can significantly improve the accessibility of these line graphs:

- Differentiate lines by more than just color.
- Label lines directly whenever possible.
- Avoid using legends if possible.
- Present information in multiple formats, such as tables.

Implementing these changes will make the graphs more accessible and ensure that a broader audience can benefit from the valuable insights they provide.