Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Chart examples · Line

A line chart with a custom label for React

A line chart labeling each point with its browser's name from the chart config.

Framework
UI library

Use it when each point stands for a named thing rather than a moment. Each point is a browser, and a LabelList turns its key into the label from the chart config, so the line reads Chrome, Safari, Firefox instead of numbers.

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 do the points say Chrome above them?

LabelList sets dataKey to browser and formatter to formatBrowser. That function looks the key up in chartConfig and returns label, so chrome prints as Chrome. position is top, offset is 12, and fontSize is 12.

How is that label text styled?

LabelList uses className fill-foreground. LineChart sets margin to 24 on the top, left, and right so the names fit. This chart has no XAxis. The browser name is the label above the point.

What is drawn on each point?

Line sets dot to an object whose fill is var(--color-visitors), and activeDot to { r: 6 }. dataKey is visitors, type is natural, stroke is var(--color-visitors), and strokeWidth is 2.

Where does the line color come from?

config gives visitors color var(--chart-2) and the label Visitors. The keys chrome through other have their own colors and labels. formatBrowser uses those labels for the text.

What does the tooltip show?

ChartTooltipContent sets hideLabel, indicator to line, and nameKey to visitors. cursor is false. The row is named Visitors from config.

Can I move along the points with the keyboard?

Yes. LineChart sets accessibilityLayer. CartesianGrid sets vertical to false. The visible names are the LabelList text, and the counts are the visitors values: 275, 200, 187, 173, and 90.