shadcn.io is not affiliated with official shadcn/ui
About
Add a legend once the colors alone don't say which series is which: two series a reader meets for the first time, or three and more. With a single series, the card title does that job. If the series should be recognized at a glance, the Area Chart with Icons puts an icon in each entry.
The two series stack, mobile under desktop, as smooth curves. Under the plot, a centered row shows a small square in each series' color and its label, and the tooltip marks each row with a line.
Installation
Usage
<script lang="ts">
import ChartAreaLegend from "@/components/charts/chart-area-legend.svelte";
</script><ChartAreaLegend />Customize
ChartLegendsits insideChartContainer, after the chart. It reads the container'schartConfigand lists each entry with a color.- The legend is HTML, not part of the SVG, so its labels wrap, translate and select like the rest of the card.
keyspicks and orders the entries;verticalAlign="top"moves the row above the chart.- The stack is the Stacked Area Chart's:
fold, thenstackRowsYwithorder: ["mobile", "desktop"], then oneareaYfromy1toy2.
FAQ
How do I put the legend above the chart?
Set verticalAlign="top" on ChartLegend. The container is a column, and the legend moves above the chart with its padding underneath.
Where do the legend's labels come from?
From label in each chartConfig entry, with var(--color-<key>) for the swatch. Rename Desktop there and the legend and the tooltip both change.
How do I change which series the legend lists?
ChartLegend lists every config entry with a color, in config order. Pass keys to pick and order them, for example ["mobile", "desktop"] to match the stack from the bottom up.
Can I click a legend item to hide its series?
Not with ChartLegend: it's a plain row. Keep the hidden keys in state, leave those series out of the rows you pass to the mark, and render each legend item as a button with aria-pressed.
API Reference
ChartContainer
<div> data-slot="chart"
configChartConfig
Each series' label, icon and color, read by the tooltip and the legend.
definitionChartDefinition
The chart: defineChart(spec, chartTooltip()), its data, marks, scales and axes, and the tooltip's focus.
ariaLabelstring
The chart's accessible name: the card's title.
ariaDescription?string
The chart's accessible description.
class?string
The container's size: aspect-auto h-[250px] w-full, mx-auto aspect-square max-h-[250px]. It's aspect-video by default.
children?Snippet
What sits under the chart inside the container: a ChartLegend.
tooltip?(ChartTooltipContentOptions & { class?: string }) | false
ChartTooltipContent's options (indicator, hideLabel, labelFormatter…) for the default body, or false for TanStack's own tooltip body.
- Type
(ChartTooltipContentOptions & { class?: string }) | false
renderTooltip?Snippet<[ChartTooltipBodySnippetContext]>
A whole tooltip body of your own, in place of ChartTooltipContent: it gets the focused points (each with its datum, group and color). A snippet.
- Type
Snippet<[ChartTooltipBodySnippetContext]>
defaultIndex?number
The category whose tooltip shows before any pointer, as Recharts' defaultIndex.
height?number
A fixed height in pixels for the chart, in place of the container's.
aspectRatio?number
A width-to-height ratio for the chart, in place of the container's height.
initialWidth?number
The width the chart renders at before it's measured.
onFocusChange?(point: ChartPoint | null) => void
Called as the focused point changes: a pointer, a key or a tap.
- Type
(point: ChartPoint | null) => void
onFocusGroupChange?(points: readonly ChartPoint[]) => void
Called with every point at the focused category (a stacked or grouped chart's).
- Type
(points: readonly ChartPoint[]) => void
onSelect?(point: ChartPoint | null) => void
Called when a point is clicked or picked with Enter.
- Type
(point: ChartPoint | null) => void
--color-<key>?color
Each config key's color (its theme color in light and dark), set on the container: the marks paint with var(--color-<key>).
ChartTooltipContent
<div>
pointsreadonly ChartPoint[]
The focused points, from the tooltip's context. ChartContainer passes them to its default body; a body of your own passes its context's.
indicator?"dot" | "line" | "dashed"
The indicator's shape beside each row.
- Type
"dot" | "line" | "dashed"- Default
"dot"
hideLabel?boolean
Hides the label above the rows (the focused category).
- Default
false
hideIndicator?boolean
Hides each row's color indicator.
- Default
false
label?string
The label's text in place of the focused category, looked up in config.
labelKey?string
The datum field (or config key) the label is read from, looked up in config.
nameKey?string
The datum field (or config key) each row's series is read from: a pie's browser.
valueKey?string
The datum field each row's value is read from, for points that carry geometry rather than values (a pie's, from pie()).
color?string
One indicator color for every row, in place of each series' own.
labelFormatter?(label: string, points: readonly ChartPoint[]) => string
Formats the label (the config's label or the category), with the rows' points: a date written out, a unit.
- Type
(label: string, points: readonly ChartPoint[]) => string
formatter?Snippet<[{ index, item, name, value }]>
A row's content in place of its indicator, name and value, as shadcn's formatter: a unit, a currency, a total row. A snippet.
- Type
Snippet<[{ index, item, name, value }]>
class?string
Classes for the tooltip's card.
ChartLegend
<div>
config?ChartConfig
The series to list: the container's config by default.
keys?readonly string[]
The series keys to list, in order: every key with a color by default.
hideIcon?boolean
Hides each series' icon (from config), leaving its color swatch.
- Default
false
verticalAlign?"top" | "bottom"
Above the chart or under it.
- Type
"top" | "bottom"- Default
"bottom"
class?string
Classes for the legend's row.
ChartConfig
label?string
The series' name in the tooltip and the legend.
icon?Component
An icon beside its name in the legend and the tooltip (a lucide-svelte icon).
color?string
Its color in both themes: a token (var(--chart-1)) or any CSS color.
theme?{ light: string; dark: string }
A color for each theme, in place of color.
Helpers
chartTooltip?(settings?: ChartTooltipSettings) => ChartDefinitionOptions
defineChart's second argument for a chart with a tooltip: what a pointer or a key focuses (group-x, each category's series, by default), the focus ring, and the host ChartContainer draws its body in.
- Type
(settings?: ChartTooltipSettings) => ChartDefinitionOptions
chartTheme?ChartTheme
defineChart's theme in the site's tokens: the text, the grid and the palette (--chart-1 to --chart-5).
chartColors?(config: ChartConfig, keys?: readonly string[]) => { domain: string[]; range: string[] }
defineChart's color: each series key (in config order, or keys') painted var(--color-<key>), for marks with a color channel.
- Type
(config: ChartConfig, keys?: readonly string[]) => { domain: string[]; range: string[] }
chartLinearScale?() => LinearScale
A value axis drawn as Recharts draws its default one: zero in the domain, five nice ticks. Pass it as a factory: y: { scale: chartLinearScale }.
- Type
() => LinearScale
curveNatural?CurveFactory
D3's natural curve, for a smooth line or area: curve: d3Curve(curveNatural).
curveStep?CurveFactory
D3's step curve, for a line or area that holds each value: curve: d3Curve(curveStep).
curveMonotoneX?CurveFactory
D3's monotone curve, smooth without overshooting a point: curve: d3Curve(curveMonotoneX).
curveLinearClosed?CurveFactory
D3's closed linear curve: a radar's polygon (curve: curveLinearClosed on radialLine and radialArea).
useChart?() => { config: ChartConfig }
The config of the ChartContainer around a component of your own (a custom tooltip body or legend).
- Type
() => { config: ChartConfig }
Related
Area Chart with Icons
Area chartsStacked areas with an icon beside each series in the legend.
Stacked Area Chart
Area chartsTwo series stacked into one total, each band its own color.
Stacked Bar Chart with Legend
Bar chartsTwo series stacked in each bar, with a legend.
Pie Chart with Legend
Pie chartsA pie with a legend of its slices underneath.
Radar Chart with Legend
Radar chartsTwo radar series with a legend naming them.
Multiple Line Chart
Line chartsTwo series as lines on the same axes.