Skip to content
easeweb
Search topics

Chart legends a keyboard can't toggle

Clicking a legend entry hides its series, but the entries are drawn on the canvas or are list items, so Tab skips them and nothing says they can be pressed.

WCAG 2.2
SC 2.1.1ASC 4.1.2A
Required by
Section 508 · EN 301 549 (EU)
Broken li.onclick = () => {
The accessibility problem illustrated: Motor:How do I hide last week?
01

The problem

Keyboard and screen reader users can't hide a series to read the others, and aren't told that the legend does anything.

02

Try it

Hide last week’s bars with the mouse, then try it with only the keyboard.

  • Broken 1: click “Last week” at the top of the chart. Then put the mouse aside and press Tab: focus goes past the legend to “View all orders”.

  • Broken 2: the legend is now under the chart. Click “Last week”, then try Tab, and read the panel: is it read as a button, and does anything say it is off?

  • Apply a fix. Tab to “Last week” and press Space, or Enter on a toggle button. The panel shows what a screen reader says for each entry.

Broken Can you fix this?

Orders per day

View all orders

Reading on from the chart, a screen reader says:

  1. View all orders, link

A recreated demo. The broken version is intentionally inaccessible.

03

The fix

Make each legend entry a control that says if it's on.

Before

The legend Chart.js draws on the canvas

<section class="card">  <h2>Orders per day</h2>  <canvas id="orders" role="img" aria-label="…"></canvas>  <a href="/orders">View all orders</a></section>
What is wrong2 points
  • The legend is painted on the canvas, so Tab never stops on "This week" or "Last week", and no key hides a series.
  • The entries are pixels, with no name, no role and no state for a screen reader to read.
After

A toggle button for each series

<section class="card">  <h2>Orders per day</h2>  <canvas id="orders" role="img" aria-label="…"></canvas>  <ul class="legend" id="legend" aria-label="Series shown"></ul>  <a href="/orders">View all orders</a></section>
How it is fixed5 steps
  1. Turn off the legend Chart.js draws on the canvas with plugins.legend.display: false, so the only legend is the one in the page.
  2. Put a button in each list item, named by its series alone, "This week" or "Last week". Tab reaches it, and Enter and Space press it.
  3. Give it aria-pressed="true", and on every press set it to whether the series is now shown. A screen reader can say "Last week, toggle button, pressed", and "not pressed" once its bars are hidden.
  4. Draw the line through a hidden series from aria-pressed="false" instead of an inline style, so what is drawn and what is said come from one value.
  5. Name the list "Series shown", so the buttons are heard as a set that says which series are on.

This fixes both Befores and passes 2.1.1 and 4.1.2. The legend keeps its look: a swatch and a name, struck through when off.

Why this fix: Use native HTML first

04

Verify the fix

4 checks, no mouse.

Test with: Keyboard, Screen reader

0 of 4 checked

Record the OS, browser and assistive-technology versions, the build, the date, and the actual result of each step. Repeat whenever the chart component or its library is updated.

How screen readers handle aria-pressed

Limits

This is a starting pattern, not a guarantee for every chart. Chart is Chart.js, and setDatasetVisibility its own way to hide a series; another library's equivalent goes in its place. Screen readers word aria-pressed differently, and some say "toggle button" while others say "button" with "pressed" or "not pressed" after it. Test the complete dashboard with a keyboard and your target assistive technology.

This is a recreated teaching example, not a finding about a named client. The code is a starting pattern: test it in your own product. No completed assistive-technology test is claimed here.

EaseWeb service

Fixed it? Keep it from coming back.

I turn guides like this one into rules your coding agents follow and checks your CI runs. Then I retest by hand with a keyboard and screen readers, periodically.

See the service