Skip to content

Chart

Recharts containers, tooltips and legends that read their colours from --chart-* tokens.

ChartContainer takes a config that gives each series a label and a colour, and exposes the colour to the chart as --color- and the series key. Take colours from the tokens: --chart-1 to --chart-5 for steps of one scale, --chart-cat-1 to --chart-cat-5 for categories. A chart is a client component, so mark the file "use client". It is drawn in the browser: the server sends an empty box of the chart's shape, so nothing jumps when it arrives. Its motion stops when the person prefers reduced motion.

Bar chart

Tasks closed each month: June 42, July 58, August 37, September 64, October 71, November 49.

Lines with a legend

Tasks opened and closed each week, weeks 41 to 46. Opened: 18, 24, 21, 30, 17, 14. Closed: 12, 19, 26, 22, 28, 20.

Donut chart

Hours logged by team in November: Platform 164, Design 120, Product 86, Support 52.

A colour per theme

Amount invoiced each month: June 18,400, July 21,300, August 19,800, September 24,600, October 27,100, November 24,350 euros.

Installation

npx shadcn@latest add @khaeli/chart

Install @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.

Installs with

Registry items
npm packages
  • recharts 3.8.0

Keyboard

Tab
Moves to the chart, which is one tab stop, and shows the tooltip of the first point. Tab again leaves it.
ArrowLeftArrowRight
In a bar, line or area chart: move the tooltip to the previous or next point. In a pie or donut chart the arrows show nothing.
Escape
Hides the tooltip.

Accessible name

  • Recharts draws the chart as an svg with role="application". Name it with the chart's title, which becomes the title element of the svg.
  • The values are only in the tooltip, which a screen reader does not read and which is not in the page before the chart is drawn. State them in text beside the chart: a figcaption, visually hidden if need be, or a @khaeli/table. A pie or donut chart depends on this, since its tooltip opens for a pointer only.
  • The legend is text, so colour is never the only label of a series. Name every series in config.

Registry JSON

The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/chart.json