Change over time as lines for comparing series, with dashed targets and a gliding crosshair and tooltip.
| Revenue | Target | |
|---|---|---|
| Jan | $36K | $36K |
| Feb | $43K | $39K |
| Mar | $44K | $41K |
| Apr | $44K | $44K |
| May | $49K | $46K |
| Jun | $52K | $49K |
| Jul | $54K | $51K |
| Aug | $59K | $54K |
| Sep | $64K | $56K |
| Oct | $67K | $59K |
| Nov | $72K | $61K |
| Dec | $76K | $64K |
Give it rows of data, the field that places each row along the bottom (index) and a config that names the series to draw. Each series is a 2px line in the next colour of the chart palette; a dashed series reads as a target or a forecast. Point at the chart, drag a finger across it or tab to it and use the arrow keys: a crosshair finds the nearest row and a tooltip lists every series there. The legend hides a series when pressed and spotlights it when pointed at.
The lines draw in from the start edge the first time the chart is on screen, and morph to new values when the data changes. Line Chart is part of the Fabricator style. It shares its plot with Area Chart, which adds the wash under each line. It's drawn in SVG, so the same component ships for every base, and its layout comes from the same model as the React Native version.
Installation#
bunx --bun fabricator-ui@latest add line-chart
Usage#
import { type ChartConfig } from "@/components/ui/chart-kit"
import { LineChart } from "@/components/ui/line-chart"const data = [
{ month: "Jan", actual: 36_400, target: 36_000 },
{ month: "Feb", actual: 43_000, target: 38_500 },
{ month: "Mar", actual: 43_900, target: 41_000 },
]
const config = {
actual: { label: "Revenue" },
target: { label: "Target", color: "muted-foreground", dashed: true },
} satisfies ChartConfig
<LineChart data={data} index="month" config={config} label="Revenue" />Each key of config is a field of the rows. A missing or non-numeric value leaves a gap in the line. The chart fills its container's width and is 18rem tall until you give it a height with className.
Values print in full (1,234) until you pass formatValue. It also formats the hidden table, which renders on the server, so use a formatter that writes the same text in Node and in the browser: formatChartNumber(value, { compact: true }) from lib/chart-model gives 43.9K on both, where Intl's compact notation can differ between them.
Examples#
Several series#
Point at a series in the legend to spotlight it, so the others step back; press it to hide it. The others keep their colours, and the value axis rescales to what is left.
| API | Web | Worker | Cron | |
|---|---|---|---|---|
| Mon | 182 ms | 240 ms | 96 ms | 41 ms |
| Tue | 176 ms | 251 ms | 104 ms | 44 ms |
| Wed | 214 ms | 233 ms | 131 ms | 39 ms |
| Thu | 198 ms | 262 ms | 122 ms | 52 ms |
| Fri | 241 ms | 288 ms | 158 ms | 47 ms |
| Sat | 132 ms | 174 ms | 88 ms | 45 ms |
| Sun | 121 ms | 162 ms | 79 ms | 43 ms |
Curves and stacking#
Line Chart takes the same curve and stack as Area Chart: smooth, linear or step, and none, stacked or expand.
Colours#
Series take the chart palette in the order of config, and keep their colour when another is hidden. Give a series its own with color: a palette slot, an upstream chart token (chart-1 … chart-5), a role (primary, info, success, …), a CSS variable (var(--brand)) or any CSS colour.
| Token | Hue | Token | Hue |
|---|---|---|---|
--chart-series-1 | blue | --chart-series-5 | magenta |
--chart-series-2 | orange | --chart-series-6 | green |
--chart-series-3 | aqua | --chart-series-7 | violet |
--chart-series-4 | amber | --chart-series-8 | red |
The palette ships with the foundations, chosen separately for light and dark. Every slot keeps 3:1 against the page and the card, and each colour stays apart from its neighbours for readers with red-green colour blindness. Slots never repeat: a ninth series is grey, so fold small series into an "Other" one before that.
Motion#
- Draw-in: the first time the chart scrolls into view, the lines sweep in from the start edge on the
revealrole, once per mount. - Morph: when
data,config,stackor the visible series change, each point moves to its new place on themoverole. A series being shown grows from its baseline; one being hidden shrinks into it. Data with a different number of rows redraws instead. - Glide: the crosshair, dots and tooltip glide between rows on the
gliderole.
All three follow the motion speed of the region they sit in. Reduce Motion and the instant speed draw the chart at once and drop the glide; animate={false} does the same for one chart.
API Reference#
LineChart#
All other props go to the root <div>.
| Prop | Type | Default |
|---|---|---|
data | TDatum[] | required |
config | ChartConfig | required |
index | keyof TDatum | required |
label | string | required: the accessible name |
curve | "smooth" | "linear" | "step" | "smooth" |
stack | "none" | "stacked" | "expand" | "none" |
formatValue | (value: number, key: string) => string | 1,234 |
formatIndex | (value: string, datum: TDatum) => string | the field as text |
legend | boolean | on for two or more series |
tooltip | boolean | true |
animate | boolean | true |
loading | boolean | false |
empty | ReactNode | "No data" |
onActiveChange | (active: ChartActive<TDatum> | null) => void | - |
ChartConfig#
| Field | Type | Default |
|---|---|---|
label | string | required |
color | ChartColor | the next palette slot |
dashed | boolean | false |
Data attributes#
| Attribute | Element | Present when |
|---|---|---|
data-slot="line-chart" | root | always |
data-loading | root | loading is true |
data-slot="chart-plot" | the plot | always |
data-slot="chart-series" | a series | always; data-dimmed while another is spotlit |
data-slot="chart-legend" | the legend | the legend shows |
data-slot="chart-tooltip" | the tooltip | a row is inspected |
Accessibility#
- The chart's plot is a slider named after
label("Monthly revenue: inspect values"). Tab to it, then Left and Right step through the rows, Home and End jump to the first and last, and Escape lets go. Its value text reads the tooltip: the row and every series' value. - Every value is also in a table hidden from sight, so a screen reader can read the data without inspecting it.
- With two or more series the legend always shows, so no series is told apart by colour alone. Its items are toggle buttons with
aria-pressed. - Text never takes a series colour: values and labels stay in the text colours, beside a coloured key.
loadingsetsaria-busyon the root and dims the last drawing until new data arrives.