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
"use client"
import { Bar, BarChart, CartesianGrid, XAxis } from "recharts"
import { ChartContainer, ChartTooltip, ChartTooltipContent, type ChartConfig } from "@/components/ui/chart"
const months = [
{ month: "Jun", closed: 42 },
{ month: "Jul", closed: 58 },
{ month: "Aug", closed: 37 },
{ month: "Sep", closed: 64 },
{ month: "Oct", closed: 71 },
{ month: "Nov", closed: 49 },
]
const config = {
closed: { label: "Closed", color: "var(--chart-3)" },
} satisfies ChartConfig
export default function ChartBarChart() {
return (
<figure className="flex w-full max-w-md flex-col gap-3">
<ChartContainer config={config}>
<BarChart accessibilityLayer data={months} title="Tasks closed each month">
<CartesianGrid vertical={false} />
<XAxis dataKey="month" tickLine={false} axisLine={false} tickMargin={8} />
<ChartTooltip content={<ChartTooltipContent />} />
<Bar dataKey="closed" fill="var(--color-closed)" />
</BarChart>
</ChartContainer>
{/* The chart is drawn in the browser: the caption is in the page from the start, with the values. */}
<figcaption className="text-center text-sm text-muted-foreground">
Tasks closed each month
<span className="sr-only">: June 42, July 58, August 37, September 64, October 71, November 49.</span>
</figcaption>
</figure>
)
}
Lines with a legend
"use client"
import { CartesianGrid, Line, LineChart, XAxis } from "recharts"
import {
ChartContainer,
ChartLegend,
ChartLegendContent,
ChartTooltip,
ChartTooltipContent,
type ChartConfig,
} from "@/components/ui/chart"
const weeks = [
{ week: "W41", opened: 18, closed: 12 },
{ week: "W42", opened: 24, closed: 19 },
{ week: "W43", opened: 21, closed: 26 },
{ week: "W44", opened: 30, closed: 22 },
{ week: "W45", opened: 17, closed: 28 },
{ week: "W46", opened: 14, closed: 20 },
]
const config = {
opened: { label: "Opened", color: "var(--chart-2)" },
closed: { label: "Closed", color: "var(--chart-5)" },
} satisfies ChartConfig
export default function ChartLinesWithALegend() {
return (
<figure className="flex w-full max-w-md flex-col gap-3">
<ChartContainer config={config}>
<LineChart
accessibilityLayer
data={weeks}
margin={{ left: 12, right: 12 }}
title="Tasks opened and closed each week"
>
<CartesianGrid vertical={false} />
<XAxis dataKey="week" tickLine={false} axisLine={false} tickMargin={8} />
<ChartTooltip content={<ChartTooltipContent indicator="line" />} />
<ChartLegend content={<ChartLegendContent />} />
<Line dataKey="opened" type="monotone" stroke="var(--color-opened)" strokeWidth={2} dot={false} />
<Line dataKey="closed" type="monotone" stroke="var(--color-closed)" strokeWidth={2} dot={false} />
</LineChart>
</ChartContainer>
<figcaption className="text-center text-sm text-muted-foreground">
Tasks opened and closed each week
<span className="sr-only">
, weeks 41 to 46. Opened: 18, 24, 21, 30, 17, 14. Closed: 12, 19, 26, 22, 28, 20.
</span>
</figcaption>
</figure>
)
}
Donut chart
"use client"
import { Pie, PieChart } from "recharts"
import {
ChartContainer,
ChartLegend,
ChartLegendContent,
ChartTooltip,
ChartTooltipContent,
type ChartConfig,
} from "@/components/ui/chart"
const hours = [
{ team: "design", hours: 120, fill: "var(--color-design)" },
{ team: "product", hours: 86, fill: "var(--color-product)" },
{ team: "platform", hours: 164, fill: "var(--color-platform)" },
{ team: "support", hours: 52, fill: "var(--color-support)" },
]
// Categories that are not a scale take the categorical tokens.
const config = {
hours: { label: "Hours" },
design: { label: "Design", color: "var(--chart-cat-1)" },
product: { label: "Product", color: "var(--chart-cat-2)" },
platform: { label: "Platform", color: "var(--chart-cat-3)" },
support: { label: "Support", color: "var(--chart-cat-4)" },
} satisfies ChartConfig
export default function ChartDonutChart() {
return (
<figure className="flex w-full max-w-64 flex-col gap-3">
<ChartContainer config={config} className="aspect-square">
<PieChart accessibilityLayer title="Hours logged by team in November">
<ChartTooltip content={<ChartTooltipContent hideLabel nameKey="team" />} />
<ChartLegend content={<ChartLegendContent nameKey="team" />} />
<Pie data={hours} dataKey="hours" nameKey="team" innerRadius="55%" />
</PieChart>
</ChartContainer>
<figcaption className="text-center text-sm text-muted-foreground">
Hours logged by team in November
<span className="sr-only">: Platform 164, Design 120, Product 86, Support 52.</span>
</figcaption>
</figure>
)
}
A colour per theme
"use client"
import { Area, AreaChart, CartesianGrid, XAxis } from "recharts"
import { ChartContainer, ChartTooltip, ChartTooltipContent, type ChartConfig } from "@/components/ui/chart"
const months = [
{ month: "Jun", invoiced: 18400 },
{ month: "Jul", invoiced: 21300 },
{ month: "Aug", invoiced: 19800 },
{ month: "Sep", invoiced: 24600 },
{ month: "Oct", invoiced: 27100 },
{ month: "Nov", invoiced: 24350 },
]
// `theme` gives a series one token on light and another on dark.
const config = {
invoiced: { label: "Invoiced (EUR)", theme: { light: "var(--chart-4)", dark: "var(--chart-1)" } },
} satisfies ChartConfig
export default function ChartAColourPerTheme() {
return (
<figure className="flex w-full max-w-md flex-col gap-3">
<ChartContainer config={config}>
<AreaChart
accessibilityLayer
data={months}
margin={{ left: 12, right: 12 }}
title="Amount invoiced each month"
>
<CartesianGrid vertical={false} />
<XAxis dataKey="month" tickLine={false} axisLine={false} tickMargin={8} />
<ChartTooltip content={<ChartTooltipContent indicator="dashed" />} />
<Area
dataKey="invoiced"
type="monotone"
stroke="var(--color-invoiced)"
strokeWidth={2}
fill="var(--color-invoiced)"
fillOpacity={0.15}
/>
</AreaChart>
</ChartContainer>
<figcaption className="text-center text-sm text-muted-foreground">
Amount invoiced each month
<span className="sr-only">
: June 18,400, July 21,300, August 19,800, September 24,600, October 27,100, November 24,350 euros.
</span>
</figcaption>
</figure>
)
}
Installation
npx shadcn@latest add @khaeli/chartbunx --bun shadcn@latest add @khaeli/chartInstall @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
svgwithrole="application". Name it with the chart'stitle, which becomes thetitleelement of thesvg. - 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