Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Chart examples · Line

An interactive line chart for React

A line chart of three months of daily page views with dates on the axis and in the tooltip.

Framework
UI library

Use it for daily data over weeks or months. Three months of daily page views are one monotone line at a fixed height, minTickGap keeps the date ticks apart, and the tooltip shows the full date. The series on show is set by one constant.

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 I chart the mobile series?

activeChart is "desktop". Set it to "mobile". Line uses dataKey={activeChart} and a stroke of var(--color- plus that key. config maps desktop to var(--chart-1) and mobile to var(--chart-2).

How is the line drawn?

Line sets type="monotone", dot={false}, and strokeWidth={2}. monotone curves between the daily points, and dot={false} leaves the stroke without markers. Only the activeChart key is plotted.

How do the axis and the tooltip format dates?

XAxis sets dataKey="date", minTickGap={32}, and tickFormatter={formatDateTick}, a short month and day. labelFormatter is formatDateLabel, which adds the year, so the tooltip title reads like Apr 1, 2024. The content also sets a fixed width, w-[150px].

Why does the tooltip say Page Views?

ChartTooltipContent sets nameKey="views". The views entry in config has the label Page Views, and that becomes the row name for the series on the chart.

How is the height fixed?

ChartContainer sets aspect-auto h-[250px] w-full. aspect-auto clears the default 16:9 box so the 250px height holds. CartesianGrid sets vertical={false}. LineChart sets accessibilityLayer and a margin of left: 12 and right: 12.

What is the line that follows the pointer?

ChartTooltip doesn't set cursor to false, so the line chart keeps its default cursor, a vertical line on the hovered date. The tooltip row is Page Views and that day's value.