Wrapping other charts
ChartFrame gives a chart raster doesn’t draw, such as your own SVG or canvas or another library’s chart, the same accessible structure as raster’s own charts, in the server-rendered HTML before any JavaScript runs:
- a
role="figure"named by the visible title - a text summary describing what the chart shows
- the chart, rendered as it is
- a legend, if you pass one
- a Show data table disclosure with a captioned table of the values
Line chart, 7 points. Mon to Sun. Values from 340 to 610.
Show data table
| Day | Views |
|---|---|
| Mon | 420 |
| Tue | 512 |
| Wed | 498 |
| Thu | 560 |
| Fri | 610 |
| Sat | 380 |
| Sun | 340 |
import { ChartFrame, describeChart } from "@eekodigital/raster/frame";
<ChartFrame
title="Views this week (canvas)"
summary={describeChart({ type: "line", series: 1, points: 7, x: ["Mon", "Sun"], y: ["340", "610"] })}
table={{
headers: ["Day", "Views"],
rows: days.map((day, i) => ({ key: day, cells: [day, views[i]] })),
}}
>
<canvas aria-hidden="true" … />
</ChartFrame>ChartFrame, ChartDataTable and describeChart are at @eekodigital/raster/frame (about 1.6 KB) and in the main entry.
- Table cells are shown as given. Format numbers before passing them (
value.toLocaleString(locale)), so the table matches the summary. - React Server Components:
@eekodigital/raster/framestarts with"use client", so a Server Component can renderChartFramedirectly, and it’s still server-rendered. To build the summary on the server, importdescribeChartfrom@eekodigital/raster/labels, which isn’t marked. See Server rendering and React Server Components. - Export:
exportSVGandexportPNGfrom/exportonly export raster’s own charts. They leave a wrapped chart alone rather than export whatever SVG they find; use that library’s own export.
With TanStack Charts
Section titled “With TanStack Charts”TanStack Charts renders its marks after hydration, with the chart exposed as one role="img" whose name you pass in ariaLabel. The values aren’t in the server HTML. Wrapping it adds the summary and the data table:
import { ChartFrame, describeChart } from "@eekodigital/raster/frame";import { Chart } from "@tanstack/charts/react";
<ChartFrame title="Revenue by month" summary={describeChart({ type: "line", series: 1, points: 12, x: ["Jan", "Dec"], y: ["£12k", "£48k"], })} table={{ headers: ["Month", "Revenue"], rows: months.map((m, i) => ({ key: m, cells: [m, revenue[i]] })), }}> <Chart definition={definition} aspectRatio={16 / 9} ariaLabel="Revenue by month, zoomable" /></ChartFrame>;Hidden or exposed?
Section titled “Hidden or exposed?”- Interactive charts (keyboard focus, zoom, selection): leave them exposed. Hiding a focusable element with
aria-hiddenleaves keyboard users on something screen readers can’t see. Give it a short name that adds to the title, such as “zoomable”, rather than repeating it. - Static charts (a canvas, an image, an SVG with no focus): hide them with
aria-hidden="true". The figure’s title, summary and table already carry everything, and the drawing would only be announced as an unlabelled graphic.
describeChart
Section titled “describeChart”describeChart(parts, labels?) writes the summary in raster’s words, from the same parts raster’s charts use: type, series, points, and the x and y ranges already formatted. type is one of raster’s chart types, worded by labels.chartNames. For any other chart, give a name instead: describeChart({ name: "Heatmap", series: 1, points: 120 }) gives “Heatmap, 120 points.”. Counts are formatted in labels.locale, and labels.summary rewords or translates it, exactly as for raster’s charts. See Accessibility.
| Prop | Type | Description |
|---|---|---|
title |
string |
Required. Visible title: names the figure and captions the table. |
summary |
string |
Required. The text alternative: what the chart shows. |
children |
ReactNode |
Required. The chart, rendered as it is. |
table |
{ headers, rows, caption?, rowHeaders? } |
The data. caption defaults to “Data for {title}”; the first cell of each row is a row header. |
dataTable |
"disclosure" | "visually-hidden" | "none" |
How the table is offered. Default "disclosure". |
actions |
ReactNode |
Shown on the same row as the Show data table button, such as download buttons. |
hideTitle |
boolean |
Keep the title for assistive technology but hide it visually. |
legend |
ReactNode |
Shown under the chart. |
labels |
ChartLabelOverrides |
Generated strings and the number locale. |
ref |
Ref<HTMLDivElement> |
The figure element. |
className |
string |
Class on the figure. |
ChartDataTable takes caption, headers, rows, rowHeaders, labels, mode and describedBy on its own, if a product only needs the table.