Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Chart examples · Area

An area chart with a legend for React

A stacked area chart of desktop and mobile visitors with a legend below it.

Framework
UI library

Use a legend once a chart has more than one series. Desktop and mobile are stacked areas, and ChartLegend names both with their colors under the chart.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

How does the legend get Desktop and Mobile?

ChartLegend sets content to ChartLegendContent. That reads label from config for each series and draws a color swatch. These entries have no icon, and this file passes no onClick. verticalAlign stays at bottom, which adds space above the legend.

How are the areas stacked under that legend?

Both Area components set stackId="a". Mobile is first, on the baseline. Desktop is second, on top. Both use type="natural" and fillOpacity={0.4}, and they fill var(--color-mobile) and var(--color-desktop).

How do I move the legend above the chart?

Set verticalAlign="top" on ChartLegend. ChartLegendContent then uses the top spacing, pb-3, under the legend. The default bottom uses pt-3 above it. The labels still come from config.

What does the tooltip show?

ChartTooltipContent sets indicator="line". The title is the month, and the rows are Mobile and Desktop with their values. cursor={false} on ChartTooltip turns the hover cursor off.

How are the month ticks shortened?

XAxis uses dataKey="month" and tickFormatter={formatMonth}, which returns value.slice(0, 3). axisLine and tickLine are false, tickMargin is 8, and CartesianGrid sets vertical={false}.

Where do the swatch colors come from?

config sets mobile to var(--chart-2) and desktop to var(--chart-1). ChartContainer writes --color-mobile and --color-desktop. The areas read those variables, and the legend swatch uses the same series color. AreaChart also sets accessibilityLayer.