Interactive Web Charts: Library, Accessibility, Data
skillImplement an accessible, responsive, fast chart in a web app: pick Recharts, Plotly, ECharts, Chart.js, Vega-Lite, Plot or D3. Use for browser chart code.
agents that use this skill
instructions
Overview
Use this skill when a chart is implemented in the code of a web application. The outcome is a chart that fits its container at every width, can be read without a mouse or a screen, follows light and dark themes, stays fast on the real data size, and shows labels from data as text. The deliverable is the working component and the checks that were run on it. Chart form, colour choice and labelling are decided by general chart design guidance; this skill is about the implementation.
Mental model
- A web chart has four parts: the data transform, a drawing layer (SVG elements, canvas pixels or WebGL), the interaction layer, and the text alternative. The library mostly decides the drawing layer and how much accessibility you get free.
- SVG puts every mark in the DOM, so each mark can carry ARIA attributes. Canvas draws pixels: Chart.js notes that canvas content is not accessible to screen readers, so the text alternative is your own work.
- Drawing is cheap until the mark count grows. ECharts recommends canvas over SVG above roughly 1,000 data items.
Rules
Choose the library
If the project already has a chart library, keep it unless it fails a requirement below.
| Situation | Pick | Reason from its docs |
|---|---|---|
| React app, common chart types | Recharts | Built with React and D3, native SVG, accessibility layer on by default |
| Declarative spec, linked selections, or SVG rendered without a browser | Vega-Lite | A grammar of interactive graphics that compiles to Vega; a view can run headless and export SVG |
| Quick analysis charts in few lines | Observable Plot | D3's high-level sister library, recommended by D3 for quick dashboards; returns SVG |
| Many chart types, statistical, 3D | Plotly.js | SVG for most, WebGL for 3D and scattergl |
| Large series | ECharts or Chart.js | ECharts has canvas and SVG renderers and series sampling; Chart.js draws on canvas and has a decimation plugin |
| Bespoke marks and interaction | D3 | Low-level modules with no concept of charts; draws to SVG or canvas |
Plotly figures that use WebGL consume several of the 8 to 16 WebGL contexts a browser allows per page, so expect 4 to 8 such figures on one page.
Size and respond
- Give the container a size in CSS before the chart measures it. Chart.js needs a relatively positioned container used only for its canvas; set maintainAspectRatio to false to size by the container height. Recharts needs a parent with its own size; ResponsiveContainer measures it with ResizeObserver. ECharts needs a sized container before init, and resize() on window resize and on container changes via ResizeObserver. Plotly takes responsive: true in config. Vega-Lite takes "container" for width and height, with a container whose size is set outside the chart. Plot defaults to a width of 640, so pass the measured width.
- Test at 320 CSS pixels wide (WCAG 1.4.10 Reflow, Level AA). A chart that needs its two-dimensional layout may be exempt, but the text, controls and table around it must still reflow.
- Remove the chart when its component unmounts (ECharts dispose, Vega finalize).
Accessibility
WCAG 1.1.1 (Level A) wants a short text alternative plus a long description for a chart: its type, a summary of the data, the trends and implications, and the data in a table where practical. WCAG 2.1.1 (Level A) wants every function usable from the keyboard, hover included. WCAG 1.4.13 (AA) wants a custom tooltip to be dismissible, hoverable and persistent.
| Library | Provided | You add |
|---|---|---|
| Recharts | accessibilityLayer, default true, for keyboard and screen readers | Description and table |
| ECharts | aria.enabled, off by default; label built from title, series and data; decal patterns; import AriaComponent in tree-shaken builds | aria.description when there are many items |
| Chart.js | Nothing; canvas needs role="img" with aria-label, or fallback content between the canvas tags | Description and table; the default "browser does not support canvas" text fails |
| Observable Plot | aria-label on each mark; ariaLabel and ariaDescription options on the plot, default null | Both options |
| Vega-Lite | aria and description properties on marks, SVG output only | Text outside the chart |
| Plotly.js | None documented in its configuration reference | Description and table outside the figure |
Render one real table from the same data array as the chart, visible or behind a toggle, so the two never disagree.
Tooltips that find the nearest point
- Chart.js: interaction mode nearest with intersect false, or mode index with axis x for time series. Intersect defaults to true, which needs the pointer on the mark.
- Observable Plot: the pointer transform with a tip mark; pointerX for time series. maxRadius defaults to 40 pixels.
- D3: sorted x values use bisector.center with a comparator that returns a signed distance; scatter points use Delaunay find.
- Show the same tooltip on keyboard focus, and let Escape dismiss it.
Colours from CSS custom properties
Define each series colour once as a custom property with light-dark(), with color-scheme: light dark set on :root. A canvas library cannot read CSS, so read the resolved value with getComputedStyle from an element whose color uses the property, pass it in the options, and rebuild when the matchMedia change event for prefers-color-scheme fires.
Format with Intl
Create one Intl.NumberFormat and one Intl.DateTimeFormat per quantity and reuse them for axis, tooltip and table. Notation compact prints 1.2M for 1234567 in en-US. Pass timeZone and locale explicitly: React lists a current date and different data on server and client as hydration mismatch causes, and locale or time zone differences change the text the same way.
Labels from data are text
Category names, series names and tooltip fields are untrusted. OWASP names textContent as the safe sink and innerHTML, outerHTML and document.write as sinks that parse markup. Chart.js tooltip callbacks return plain strings, but its external tooltip example assigns innerHTML; ECharts tooltips default to an HTML element. Build such tooltips with textContent.
Large data
- Reduce points before drawing and say so in the chart note.
- Chart.js decimation: algorithm lttb for trends, min-max to keep peaks. It needs a line dataset, indexAxis x, a linear or time x scale, parsing false and more points than its threshold (4 times the canvas width by default).
- Chart.js also benefits from parsing false with normalized true on sorted unique indices, fixed scale min and max, and animation false.
- ECharts line series: sampling lttb, average, min, max, minmax or sum; it is off by default.
- Plotly: scattergl for dense scatter, mindful of the context limit above.
Server rendering and hydration
- The first client render must produce the server HTML. React lists window checks, browser-only APIs such as matchMedia, dates and different data on the two sides as mismatch causes. Render the chart after mount, or give both sides the same locale, time zone and data.
- In Next.js, ssr false with next/dynamic is allowed only inside Client Components.
- ECharts renders to an SVG string with init(null, null, {renderer: 'svg', ssr: true, width, height}) and renderToSVGString; set animation false. The string carries no aria-label even with aria enabled, so add the label and description in your own markup.
Test
- Render the spec or component headless where the library allows it, and assert on the output text. A Vega-Lite spec renders with npx -y -p vega-lite -p vega-cli vl2svg spec.vl.json out.svg.
- In Playwright, set viewport to 320 wide, colorScheme to dark, and fixed locale and timezoneId; check the fallback table with toMatchAriaSnapshot. toHaveScreenshot baselines are per platform, so run them where they were made.
- Feed a label of and confirm it shows as visible text.
- Tab to the chart and reach every tooltip without a mouse.
When the user cannot be asked, assume the project's current framework and chart library and say so.
Examples
Hover-only to reachable. Bad: tooltips open only when the pointer is exactly on a 4 px dot. Good: nearest mode with intersect false, the same tooltip on focus, Escape closes it.
Markup to text. Bad: tooltip HTML built with a template string from a series name. Good: the name written with textContent, the test label rendered as visible text.
Raw to decimated. Bad: 200,000 points drawn at 600 px wide. Good: lttb decimation to the canvas width, a note "downsampled", the full values in the table export.
Output format
- The component path, library and version, and why it was chosen.
- Accessibility provided by the library and added by you.
- Checks run, with results; checks not run.
Checklist
- Library choice follows the table, or the existing library meets every rule.
- The container has a CSS size; the chart resizes and is cleaned up on unmount.
- Short alternative, long description and a data table exist; keyboard reaches every tooltip.
- Colours come from custom properties with light and dark values; canvas charts rebuild on theme change.
- One formatter per quantity, with explicit locale and time zone.
- Data labels are rendered as text; the test label showed as text.
- Large data is reduced before drawing and disclosed.
- Server render and hydration run without mismatch warnings.
Composition / References
Scope limits: choice of chart form and palette, statistics, spreadsheet charts and maps are separate jobs.
- Chart.js: https://www.chartjs.org/docs/latest/general/accessibility.html , https://www.chartjs.org/docs/latest/general/performance.html , https://www.chartjs.org/docs/latest/configuration/decimation.html , https://www.chartjs.org/docs/latest/configuration/interactions.html , https://www.chartjs.org/docs/latest/configuration/responsive.html , https://www.chartjs.org/docs/latest/configuration/tooltip.html
- ECharts handbook: https://echarts.apache.org/handbook/en/best-practices/aria/ , https://echarts.apache.org/handbook/en/best-practices/canvas-vs-svg/ , https://echarts.apache.org/handbook/en/how-to/cross-platform/server/ , https://echarts.apache.org/handbook/en/concepts/chart-size/
- Plotly.js: https://plotly.com/javascript/webgl-vs-svg/ , https://plotly.com/javascript/responsive-fluid-layout/
- Vega-Lite and Vega: https://vega.github.io/vega-lite/docs/size.html , https://vega.github.io/vega-lite/docs/mark.html , https://vega.github.io/vega/docs/api/view/
- Observable Plot: https://observablehq.com/plot/interactions/pointer , https://observablehq.com/plot/features/accessibility
- Recharts: https://recharts.github.io/en-US/api/LineChart/ , https://recharts.github.io/en-US/api/ResponsiveContainer/
- D3: https://d3js.org/what-is-d3 , https://d3js.org/d3-array/bisect , https://d3js.org/d3-delaunay/delaunay
- OWASP, DOM based XSS Prevention Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/DOM_based_XSS_Prevention_Cheat_Sheet.html
- WCAG 2.2 Understanding: https://www.w3.org/WAI/WCAG22/Understanding/non-text-content.html , https://www.w3.org/WAI/WCAG22/Understanding/keyboard.html , https://www.w3.org/WAI/WCAG22/Understanding/content-on-hover-or-focus.html , https://www.w3.org/WAI/WCAG22/Understanding/reflow.html
- MDN: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value/light-dark , https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle , https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList/change_event , https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Optimizing_canvas
- React, hydrateRoot: https://react.dev/reference/react-dom/client/hydrateRoot ; Next.js lazy loading: https://nextjs.org/docs/app/guides/lazy-loading
- Playwright: https://playwright.dev/docs/emulation , https://playwright.dev/docs/aria-snapshots , https://playwright.dev/docs/test-snapshots