-
Notifications
You must be signed in to change notification settings - Fork 0
fix(charts): descriptive aria labels for all chart types (a11y) #1108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import { render, screen, cleanup } from "@testing-library/react"; | ||
| import { describe, it, expect, vi, afterEach } from "vitest"; | ||
|
|
||
| // One place that verifies every chart gives BaseChart a descriptive aria-label | ||
| // (was the generic "Chart visualization" fallback) and honours a caller | ||
| // override. echarts/core + container size are stubbed so the charts mount. | ||
| vi.mock("echarts/core", () => { | ||
| const init = vi.fn(() => ({ | ||
| setOption: vi.fn(), | ||
| resize: vi.fn(), | ||
| dispose: vi.fn(), | ||
| on: vi.fn(), | ||
| off: vi.fn(), | ||
| showLoading: vi.fn(), | ||
| hideLoading: vi.fn(), | ||
| })); | ||
| const stub = { | ||
| use: vi.fn(), | ||
| init, | ||
| registerTheme: vi.fn(), | ||
| registerMap: vi.fn(), | ||
| getMap: vi.fn(() => ({})), | ||
| format: { encodeHTML: (s: string) => s }, | ||
| }; | ||
| return { ...stub, default: stub }; | ||
| }); | ||
| vi.mock("@/hooks/useContainerSize", () => ({ | ||
| useContainerSize: () => ({ width: 600, height: 400, containerRef: vi.fn() }), | ||
| })); | ||
|
|
||
| import { PieChart } from "../pie-chart"; | ||
| import { RadarChart } from "../radar-chart"; | ||
| import { GaugeChart } from "../gauge-chart"; | ||
| import { SankeyChart } from "../sankey-chart"; | ||
| import { TreemapChart } from "../treemap-chart"; | ||
| import { SunburstChart } from "../sunburst-chart"; | ||
| import { CirclePackingChart } from "../circle-packing-chart"; | ||
| import { GanttChart } from "../gantt-chart"; | ||
| import { ChoroplethChart } from "../choropleth-chart"; | ||
|
|
||
| const cases: Array<[string, React.ReactElement, RegExp]> = [ | ||
| [ | ||
| "pie", | ||
| <PieChart data={[{ name: "A", value: 1 }]} />, | ||
| /Pie chart with 1 segments/, | ||
| ], | ||
| [ | ||
| "radar", | ||
| <RadarChart | ||
| data={{ | ||
| indicators: [{ name: "X", max: 10 }], | ||
| series: [{ name: "S", values: [5] }], | ||
| }} | ||
| />, | ||
| /Radar chart comparing 1 series across 1 axes/, | ||
| ], | ||
| [ | ||
| "gauge", | ||
| <GaugeChart data={[{ value: 50 }]} />, | ||
| /Gauge showing 50 of 0 to 100/, | ||
| ], | ||
| [ | ||
| "sankey", | ||
| <SankeyChart | ||
| data={{ | ||
| nodes: [{ name: "A" }, { name: "B" }], | ||
| links: [{ source: "A", target: "B", value: 1 }], | ||
| }} | ||
| />, | ||
| /Sankey diagram with 2 nodes and 1 links/, | ||
| ], | ||
| [ | ||
| "treemap", | ||
| <TreemapChart data={[{ name: "A", value: 1 }]} />, | ||
| /Treemap with 1 top-level items/, | ||
| ], | ||
| [ | ||
| "sunburst", | ||
| <SunburstChart data={[{ name: "A", value: 1 }]} />, | ||
| /Sunburst chart with 1 top-level segments/, | ||
| ], | ||
| [ | ||
| "circle-packing", | ||
| <CirclePackingChart data={[{ name: "A", value: 1 }]} />, | ||
| /Circle-packing chart with 1 top-level groups/, | ||
| ], | ||
| [ | ||
| "gantt", | ||
| <GanttChart data={[{ task: "T", start: 0, end: 1 }]} />, | ||
| /Gantt chart with 1 tasks/, | ||
| ], | ||
| [ | ||
| "choropleth", | ||
| <ChoroplethChart data={[{ name: "United States", value: 1 }]} />, | ||
| /Choropleth map with 1 regions/, | ||
| ], | ||
| ]; | ||
|
|
||
| afterEach(cleanup); | ||
|
|
||
| describe("chart aria descriptions", () => { | ||
| it.each(cases)("%s gets a descriptive aria-label", (_name, el, re) => { | ||
| render(el); | ||
| expect(screen.getByTestId("base-chart").getAttribute("aria-label")).toMatch( | ||
| re, | ||
| ); | ||
| }); | ||
|
|
||
| it("honours a caller-provided ariaDescription override", () => { | ||
| render( | ||
| <PieChart data={[{ name: "A", value: 1 }]} ariaDescription="Custom" />, | ||
| ); | ||
| expect(screen.getByTestId("base-chart")).toHaveAttribute( | ||
| "aria-label", | ||
| "Custom", | ||
| ); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -60,6 +60,7 @@ function PieChart({ | |||||||||||||||
| donutCenterText, | ||||||||||||||||
| stylingRules, | ||||||||||||||||
| paramValues, | ||||||||||||||||
| ariaDescription, | ||||||||||||||||
| ...rest | ||||||||||||||||
| }: PieChartProps) { | ||||||||||||||||
| const { width, height, containerRef } = useContainerSize(); | ||||||||||||||||
|
|
@@ -187,7 +188,13 @@ function PieChart({ | |||||||||||||||
|
|
||||||||||||||||
| return ( | ||||||||||||||||
| <div ref={containerRef} className="h-full w-full"> | ||||||||||||||||
| <BaseChart options={options} {...rest} /> | ||||||||||||||||
| <BaseChart | ||||||||||||||||
| options={options} | ||||||||||||||||
| ariaDescription={ | ||||||||||||||||
| ariaDescription ?? `Pie chart with ${data.length} segments` | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+193
to
+195
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use rendered segment count in the fallback aria description. Line 194 uses Suggested fix- ariaDescription={
- ariaDescription ?? `Pie chart with ${data.length} segments`
- }
+ ariaDescription={
+ ariaDescription ??
+ `Pie chart with ${topN > 0 && data.length > topN ? topN + 1 : data.length} segments`
+ }📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||
| {...rest} | ||||||||||||||||
| /> | ||||||||||||||||
| </div> | ||||||||||||||||
| ); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallback group count can be wrong for single-root hierarchical input.
At Line 272,
data.lengthreports1when input is a single root node with many children, so the aria label undercounts top-level groups.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents