Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Chart examples · Area

An interactive area chart for React

A stacked area chart of three months of daily visitors with gradient fills and a legend.

Framework
UI library

Use it for daily data over weeks or months. Three months of daily desktop and mobile visitors are stacked with gradient fills, minTickGap thins the date ticks so they don't overlap, and a legend names both series.

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 axis skip dates?

XAxis sets minTickGap={32} and dataKey="date". Ticks closer than 32 pixels are skipped, so the daily rows from April through June don't pile up. tickFormatter is formatDateTick, which prints a short month and the day, like Apr 1.

How is the tooltip date formatted?

ChartTooltipContent sets labelFormatter to formatDateLabel. That calls toLocaleDateString with a short month and a numeric day, and it does not add the year. indicator is dot. cursor={false} turns the hover cursor off.

How do the fills fade?

Two linearGradient elements use the ids interactiveFillDesktop and interactiveFillMobile, from y1="0" to y2="1". Each fades from stopOpacity={0.8} at 5% to stopOpacity={0.1} at 95%. The areas set fill to url(#interactiveFillMobile) and url(#interactiveFillDesktop).

How are desktop and mobile stacked?

Both Area components set stackId="a" and type="natural". Mobile is first, on the baseline. Desktop is second, on top. Strokes are var(--color-mobile) and var(--color-desktop), from var(--chart-2) and var(--chart-1) in config.

What does the legend list?

ChartLegend renders ChartLegendContent, which prints Desktop and Mobile from config with a color swatch. verticalAlign stays bottom. There is no icon on these entries.

Why is the height fixed?

ChartContainer sets aspect-auto h-[250px] w-full. aspect-auto clears the default 16:9 box so the h-[250px] height holds, and the width follows the max-w-xl frame. CartesianGrid sets vertical={false}.