<!-- generated by scripts/gen-docs.mjs from docs/system/line-chart.md. Edit the source and rerun. -->
# LineChart

## Description
Draws a value over time, or over any ordered position, as one line per series, so a person reads the shape of the change and any point on it.

`import { LineChart } from "@/components/cobalt/ui/line-chart"`, source `registry/cobalt/ui/line-chart.tsx`, status `ready-with-gaps`.
Foundation: `hand-rolled`. Traps checked: `trap/loading-layout-shift`, `trap/color-only-status`, `trap/live-region-double`, `trap/reduced-motion-ignored`, `trap/motion-transition-all`, `trap/motion-layout-property`, `trap/numbers-shift`, `trap/tooltip-essential`, `trap/tooltip-interactive`, `trap/touch-hover-flash`, `trap/hover-unguarded` (n/a: no CSS hover reveals anything; the overlay follows the hover store), `trap/demo-on-timers` (n/a: every example holds fixed data), `trap/illustration-announced` (n/a: the marks are `aria-hidden` and the table carries the data), `trap/loop-offscreen` (n/a: nothing loops).

### Foundation
Not applicable: hand-rolled.

| Given by the foundation | Added by the team |
|---|---|
| `d3-shape` `line` and its three curves (linear, monotone X, step) for the path maths only | A thin wrapper over the shared XY engine in `chart.tsx`, set to `mark: "line"`: the engine gives the frame (fixed box, hover store, tooltip, keyboard, states, summary and table), the dashed grey comparison series drawn behind the others, gaps where a value is `null`, the value axis kept at the data's own range with negative values included, and min and max per pixel column once a series holds more than two points a pixel; `LineChart` adds only `curve` and `points` |

## Examples
A fund's gross assets each month for three years, against the same month a year before.

Real uses, 0 call sites (`rg -n "<LineChart\b" app docs/system/examples --glob '!docs/system/examples/line-chart/*'`): no showcase page draws one outside its examples yet, and the examples folder does not count.

Uses inside the registry: none beyond its tests. `ChartCard` is the parent a page puts it in (see in-chart-card.tsx).

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/line-chart/default.tsx` | default | A fund's gross assets each month for three years, against the same month a year before |
| Not applicable: linear is the default, the line in default.tsx | curve=linear | Straight segments between points |
| `docs/system/examples/line-chart/curve-monotone.tsx` | curve=monotone | Occupancy in three regions each week of the summer, smoothed without overshooting a reading; a legend name hides its region |
| `docs/system/examples/line-chart/curve-step.tsx` | curve=step | A fee rate that holds until it changes, drawn in steps, with a dot on each filing |
| `docs/system/examples/line-chart/in-chart-card.tsx` | composition:ChartCard | In a chart card the headline figure, its change and the note follow the hovered month, so the chart needs no tooltip |
| `docs/system/examples/line-chart/layers-reference-line.tsx` | composition:ChartReferenceLine | A fund's gross assets against its $4.5B target, a reference line drawn in `layers` with the chart's own scale |
| `docs/system/examples/line-chart/layers-reference-band.tsx` | composition:ChartReferenceBand | A fund's cash as a share of its assets each month against its 2% to 5% target, a band drawn in `layers` with the chart's own scale |

Example: A fund's gross assets each month for three years, against the same month a year before (`docs/system/examples/line-chart/default.tsx`)

```tsx
import { LineChart } from "@/components/cobalt/ui/line-chart";

// Gross assets at the end of each month, in billions of dollars.
const assets = [
  3.12, 3.18, 3.05, 3.21, 3.34, 3.29, 3.41, 3.38, 3.52, 3.47, 3.6, 3.71, 3.66, 3.58, 3.49, 3.62, 3.75, 3.81,
  3.79, 3.94, 4.02, 3.97, 4.11, 4.2, 4.16, 4.09, 4.25, 4.31, 4.28, 4.42, 4.51, 4.47, 4.39, 4.55, 4.63, 4.71,
];
const months = assets.map((value, i) => ({
  month: `${2023 + Math.floor(i / 12)}-${String((i % 12) + 1).padStart(2, "0")}-01`,
  assets: value * 1e9,
  yearBefore: i >= 12 ? (assets[i - 12] ?? 0) * 1e9 : null,
}));

const series = [
  { key: "assets", label: "Gross assets", value: "assets" as const },
  { key: "yearBefore", label: "A year before", value: "yearBefore" as const, kind: "previous" as const },
];

export default function Example() {
  return <LineChart label="Gross assets" data={months} x="month" series={series} format="money" />;
}
```

Example: In a chart card the headline figure, its change and the note follow the hovered month, so the chart needs no tooltip (`docs/system/examples/line-chart/in-chart-card.tsx`)

```tsx
import { useState } from "react";

import { ChartCard } from "@/components/cobalt/ui/chart-card";
import { LineChart } from "@/components/cobalt/ui/line-chart";
import { pointDate } from "@/lib/cobalt/chart-math";
import { compactMoney } from "@/lib/cobalt/format";

// Gross assets at the end of each month, in billions of dollars.
const assets = [
  3.12, 3.18, 3.05, 3.21, 3.34, 3.29, 3.41, 3.38, 3.52, 3.47, 3.6, 3.71, 3.66, 3.58, 3.49, 3.62, 3.75, 3.81,
  3.79, 3.94, 4.02, 3.97, 4.11, 4.2, 4.16, 4.09, 4.25, 4.31, 4.28, 4.42, 4.51, 4.47, 4.39, 4.55, 4.63, 4.71,
];
const months = assets.map((value, i) => ({
  month: `${2023 + Math.floor(i / 12)}-${String((i % 12) + 1).padStart(2, "0")}-01`,
  assets: value * 1e9,
  yearBefore: i >= 12 ? (assets[i - 12] ?? 0) * 1e9 : null,
}));

const series = [
  { key: "assets", label: "Gross assets", value: "assets" as const },
  { key: "yearBefore", label: "A year before", value: "yearBefore" as const, kind: "previous" as const },
];

export default function Example() {
  const [active, setActive] = useState<number | null>(null);
  const point = months[active ?? months.length - 1]!;
  const before = point.yearBefore;
  const month = pointDate(Date.parse(point.month), 0, "en-US", "month");
  return (
    <ChartCard
      className="w-full max-w-xl"
      title="Gross assets"
      value={compactMoney(point.assets)}
      change={before ? ((point.assets - before) / before) * 100 : undefined}
      note={
        before
          ? `${month}, against ${compactMoney(before)} a year before`
          : `${month}, with no year before to compare`
      }

    >
      <LineChart
        label="Gross assets"
        height="sm"
        data={months}
        x="month"
        series={series}
        format="money"
        tooltip={false}
        onActiveChange={setActive}
      />
    </ChartCard>
  );
}
```

Example: A fund's gross assets against its $4.5B target, a reference line drawn in `layers` with the chart's own scale (`docs/system/examples/line-chart/layers-reference-line.tsx`)

```tsx
import { ChartReferenceLine } from "@/components/cobalt/ui/chart";
import { LineChart } from "@/components/cobalt/ui/line-chart";

// Gross assets at the end of each month, in billions of dollars.
const assets = [
  3.12, 3.18, 3.05, 3.21, 3.34, 3.29, 3.41, 3.38, 3.52, 3.47, 3.6, 3.71, 3.66, 3.58, 3.49, 3.62, 3.75, 3.81,
  3.79, 3.94, 4.02, 3.97, 4.11, 4.2, 4.16, 4.09, 4.25, 4.31, 4.28, 4.42, 4.51, 4.47, 4.39, 4.55, 4.63, 4.71,
];
const months = assets.map((value, i) => ({
  month: `${2023 + Math.floor(i / 12)}-${String((i % 12) + 1).padStart(2, "0")}-01`,
  assets: value * 1e9,
}));
const series = [{ key: "assets", label: "Gross assets", value: "assets" as const }];

export default function Example() {
  return (
    <LineChart
      label="Gross assets"
      data={months}
      x="month"
      series={series}
      format="money"
      layers={(scope) => <ChartReferenceLine scope={scope} y={4.5e9} label="Target $4.5B" />}
    />
  );
}
```

Example: A fund's cash as a share of its assets each month against its 2% to 5% target, a band drawn in `layers` with the chart's own scale (`docs/system/examples/line-chart/layers-reference-band.tsx`)

```tsx
import { ChartReferenceBand } from "@/components/cobalt/ui/chart";
import { LineChart } from "@/components/cobalt/ui/line-chart";

// Cash held at the end of each month, as a percentage of net assets.
const cash = [3.1, 2.8, 2.4, 1.9, 1.6, 2.2, 2.9, 3.6, 4.4, 5.3, 5.8, 4.9, 4.1, 3.5, 3.2, 2.7, 2.5, 3.0, 3.8, 4.2, 4.6, 4.0, 3.4, 3.1];
const months = cash.map((share, i) => ({
  month: `${2025 + Math.floor(i / 12)}-${String((i % 12) + 1).padStart(2, "0")}-01`,
  cash: share,
}));
const series = [{ key: "cash", label: "Cash", value: "cash" as const }];

export default function Example() {
  return (
    <LineChart
      label="Cash as a share of assets"
      data={months}
      x="month"
      series={series}
      format="percent"
      domain={[0, 7]}
      layers={(scope) => <ChartReferenceBand scope={scope} y={[2, 5]} label="Target 2% to 5%" />}
    />
  );
}
```

## Variants

### curve
- `linear`: straight segments between points, the default, for a value that moves between readings. Used at 0 call sites.
- `monotone`: a smoothed line that never rises above or falls below a data point, for a value that changes gradually and where the corners distract. Used at 0 call sites.
- `step`: a flat run that jumps at the next reading, for a value that holds until it is changed, such as a fee rate. Used at 0 call sites.

Example: Occupancy in three regions each week of the summer, smoothed without overshooting a reading; a legend name hides its region (`docs/system/examples/line-chart/curve-monotone.tsx`)

```tsx
import { LineChart } from "@/components/cobalt/ui/line-chart";

// The share of rooms booked each week, in percent, from Monday 1 June 2026.
const coast = [71, 74, 78, 83, 86, 89, 91, 92, 90, 88, 84, 79, 73];
const city = [82, 80, 77, 75, 72, 70, 69, 71, 74, 78, 81, 84, 85];
const mountains = [48, 52, 57, 61, 66, 70, 73, 72, 68, 63, 58, 54, 51];
const weeks = coast.map((value, i) => ({
  week: new Date(Date.UTC(2026, 5, 1) + i * 7 * 86_400_000).toISOString().slice(0, 10),
  coast: value,
  city: city[i] ?? null,
  mountains: mountains[i] ?? null,
}));

const series = [
  { key: "coast", label: "Coast", value: "coast" as const },
  { key: "city", label: "City", value: "city" as const },
  { key: "mountains", label: "Mountains", value: "mountains" as const },
];

export default function Example() {
  return (
    <LineChart label="Occupancy by region" curve="monotone" data={weeks} x="week" series={series} format="percent" />
  );
}
```

Example: A fee rate that holds until it changes, drawn in steps, with a dot on each filing (`docs/system/examples/line-chart/curve-step.tsx`)

```tsx
import { LineChart } from "@/components/cobalt/ui/line-chart";

// The management fee in percent, as each annual filing reported it.
const filings = [
  { filed: "2019-03-29", fee: 1.5 },
  { filed: "2020-03-30", fee: 1.5 },
  { filed: "2021-03-31", fee: 1.25 },
  { filed: "2022-03-30", fee: 1.25 },
  { filed: "2023-03-31", fee: 1.0 },
  { filed: "2024-03-29", fee: 0.9 },
  { filed: "2025-03-31", fee: 0.9 },
];
const series = [{ key: "fee", label: "Management fee", value: "fee" as const }];

export default function Example() {
  return (
    <LineChart label="Management fee" curve="step" points data={filings} x="filed" series={series} format="percent" />
  );
}
```

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| idle | data with at least one value that is not zero | read the lines, hover, tab in | the lines, the axes, the legend when there are two series or a comparison | screenshot |
| hover | the pointer over the box, snapped to the nearest point | read the tooltip, move along | a 1px crosshair, a marker per series (the 3px dot with a 2px ring of the card), the tooltip 6px above the plot's top edge, centred on the crosshair and kept over the plot where it fits, so it covers neither the line nor the value axis; a month's figures dated the 1st title it "May 2024" | screenshot |
| focus | Tab reaches the box | step with the arrow keys | the system focus outline on the box, the last point shown, the live line read aloud | by hand |
| gap | a row whose series value is `null`, or `missing(reason)` from `registry/cobalt/lib/missing.ts` | read the break, find the row in the table | the line breaks there; the table cell says "Unknown", or the reason's words such as "Withheld", in the muted ink through MissingValue, never "None", which reads as zero | test |
| pending | `status="pending"` with nothing drawn yet | wait | `aria-busy`; nothing for 250ms, then a Skeleton block at the box's size | test |
| refreshing | `status="pending"` with marks on screen | read the old marks | `aria-busy`; after 250ms the marks dim to 55% and the hover stops; the new marks fade in when they arrive | by hand |
| empty | no value, or every value zero, and not pending | read why | the sentence "Nothing to show for this period." centred in the box | test |
| failure | `status="failure"` | press Retry | the title "Could not load the chart" announced once as an alert, and Retry when `onRetry` is passed | test |

### State precedence
- Failure and pending: failure wins. The box shows the failure and its Retry.
- Pending and empty: pending wins. The box stays blank, then shows the Skeleton after the slow wait; the empty sentence never flashes.
- Gap and hover: both show. The crosshair snaps to the nearest point that has a value, and the tooltip row for the gapped series reads "Unknown", or its reason's words, in the tooltip's muted ink.
- Hover and focus: the last one wins. The pointer and the keys move the same marker, so one point shows at a time.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| the crosshair, markers and tooltip appear or leave | announce | `popup-motion` at `--t-press` 140ms on `--curve`, leaving at 70% | opacity | the same fade, which the base layer keeps |
| the hovered point changes | input | none: snaps with no transition | transform | the same snap |
| a legend item brings its series forward | announce | `duration-press` on `--curve` | opacity | the same fade |

## Props

<!-- Props generated from registry/cobalt/ui/line-chart.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `data` | `readonly T[]` |  |  |
| `label` | `string` |  |  |
| `series` | `readonly ChartSeries<T>[]` |  |  |
| `x` | `Accessor<T, string \| number \| Date>` |  | The position along the bottom: a date (Date or ISO string), a number or a name. |
| `active?` | `number` |  |  |
| `className?` | `string` |  |  |
| `config?` | `ChartConfig` |  | Names and colours by series key, as shadcn's `ChartConfig`. |
| `curve?` | `"linear" \| "monotone" \| "step"` | `"linear"` | How the line runs between points: straight, smoothed without overshoot, or in steps. |
| `domain?` | `readonly [number, number]` |  | A fixed value range, in place of the one read from the data. |
| `format?` | `ValueFormat` |  | How values read on the axis and in the tooltip. |
| `height?` | `ChartHeight` |  |  |
| `labels?` | `Partial<ChartLabels>` |  |  |
| `layers?` | `((scope: ChartScope) => ReactNode)` |  | Your own SVG, drawn over the marks with the chart's scales: a reference line, a band, a note. |
| `legend?` | `"auto" \| "none"` |  | "auto" shows it for two series or more, or a comparison; "none" hides it. |
| `locale?` | `string` |  |  |
| `onActiveChange?` | `((index: number) => void)` |  |  |
| `onRetry?` | `(() => void)` |  |  |
| `points?` | `boolean` | `false` | A dot on every point, for a short series read point by point. |
| `status?` | `Status` |  |  |
| `summary?` | `string` |  | Your own sentence for screen readers; one is written from the data otherwise. |
| `tooltip?` | `boolean` |  |  |
| `xFormat?` | `((value: string \| number \| Date) => string)` |  | How an x value reads in the tooltip and table, in place of the default date or name. |

`curve` is `linear`, `monotone` or `step`. `points` puts a dot on every point, for a series read point by point. A series with `mark: "area"` fills under its own line, from zero. Everything else comes from the shared frame: `data`, `x`, `series` (each with a `key`, a `label`, which falls back to `config[key].label` and then the key, a `value`, an optional `kind: "previous"` for the comparison, its own `format`, and an optional `mark`), `format`, `xFormat`, `domain`, `legend`, `config`, `layers`, `label`, `summary`, `status`, `onRetry`, `height`, `labels`, `tooltip`, `active`, `onActiveChange` and `locale`; `Chart` documents them.

## Usage

### When to use
- A value changes over time and the shape of the change is the point, such as a fund's gross assets each month.
- A value is a level that moves in a narrow band, such as a unit price, where the line should keep the data's own range instead of starting at zero.
- A period sits beside its comparison, such as this year against the same month a year before.

### When not to use
- The values are amounts per group that a person compares by length. Use BarChart instead.
- The amount accumulates and the area under it is the point. Use AreaChart instead.
- Only a trend is needed inside a row or a card header. Use Sparkline instead.
- One figure and its change, with no history worth a picture. Use Stat instead.

### Rules
- `rule/line-chart-line-for-levels`: When a value is a level that moves in a narrow band, such as a unit price, draw it with `LineChart` instead of `BarChart`, because a line keeps the data's own range while a bar starts at zero, so the bars would all look the same height. Evidence: principle heuristic: match between system and real world; single use registry/cobalt/ui/line-chart.tsx:14-15. Check: test `chart-math.test.ts` expects a line to keep its own range and bars and areas to keep zero.
  - Don't: `<BarChart label="Unit price" data={prices} x="day" series={price} />`
  - Do: `<LineChart label="Unit price" data={prices} x="day" series={price} />`
- `rule/line-chart-comparison-is-a-kind`: When a line sits beside the same measure from an earlier period, pass the second series with `kind: "previous"` instead of giving it a colour of its own, because the comparison is drawn grey, dashed and behind, so the difference is a shape and colour keeps one job. Evidence: principle wcag: 1.4.1 Use of color; person D13. Check: test `line-chart.test.tsx` expects the legend with a dashed swatch and "A year before".
  - Don't: `series={[{ key: "now", label: "Now", value: "now" }, { key: "before", label: "Before", value: "before" }]}`
  - Do: `series={[{ key: "now", label: "Now", value: "now" }, { key: "before", label: "A year before", value: "before", kind: "previous" }]}`
- `rule/line-chart-step-for-held-values`: When a value holds until it is changed, such as a fee rate or a rating, set `curve="step"` instead of the linear default, because a diagonal between two filings says the value moved gradually on the days between them. Evidence: principle heuristic: match between system and real world; single use docs/system/examples/line-chart/curve-step.tsx:18. Check: review of every `LineChart` whose rows are filings or decisions, for `curve="step"`.
  - Don't: `<LineChart label="Management fee" data={filings} x="filed" series={series} />`
  - Do: `<LineChart label="Management fee" curve="step" points data={filings} x="filed" series={series} />`

### Content
- Follows `rule/writing-no-invented-numbers`.
- Follows `rule/chart-label-names-the-data`.
- `rule/line-chart-series-label-names-the-measure`: When a series has a `label`, write the measure, such as "Gross assets", and for a comparison the period it stands for, such as "A year before", instead of "Series 1" or "Previous", because the label is the legend entry, the tooltip row and the table column. Evidence: principle wcag: 1.1.1 Non-text content; principle heuristic: match between system and real world. Check: test `line-chart.test.tsx` expects "A year before" in the legend.
  - Don't: `{ key: "before", label: "Previous", value: "before", kind: "previous" }`
  - Do: `{ key: "before", label: "A year before", value: "before", kind: "previous" }`

### Anti-slop
- `rule/line-chart-gaps-stay-gaps`: When a row has no reading for a series, leave its value `null` instead of filling in 0 or the last reading, because the line breaks at a `null` and the table and the tooltip say "Unknown", or the reason given with `missing("withheld")`, while a zero draws a fall that never happened. Evidence: principle heuristic: error prevention, a missing value is not zero. Check: test `line-chart.test.tsx` expects `<td>None</td>` for a missing value.
  - Don't: `{ month: "2025-01-01", assets: 3.9e9, before: 0 }`
  - Do: `{ month: "2025-01-01", assets: 3.9e9, before: null }`

### Limits
- `rule/line-chart-decimates-past-two-per-pixel`: When a series holds more than 2 points per pixel of plot width, pass all the rows to `LineChart` instead of thinning them yourself, because the chart draws the first, lowest, highest and last point of each pixel column, so every peak and dip stays and hover still snaps to the real rows. Evidence: measured decimation 1.8ms for 1,000,000 points, docs/research/2026-10-17-charts.md; measured hover median 3ms and layout shift 0 on the huge line, .design-system/evidence/charts/hover-cost.jsonl. Check: test `chart-math.test.ts` expects decimation to keep every peak and dip and at most four points a pixel.
  - Don't: `<LineChart data={rows.filter((_, i) => i % 50 === 0)} x="at" series={series} label="Hourly value" />`
  - Do: `<LineChart data={rows} x="at" series={series} label="Hourly value" />`

## Accessibility
Rests on the Chart frame: a focusable `div` with `role="group"` and `aria-roledescription="chart"`, one tab stop. The lines are `aria-hidden`; the data is a visually hidden sentence (`aria-describedby`) and a visually hidden `table`, sampled to 200 evenly spaced rows with a note past that. The summary names the highest, lowest and latest of the first series. Series colours hold 3:1 on the card in both themes (test). Screen reader pass with VoiceOver: NEEDS REVIEW.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the chart | focuses the box and shows the last point | the box | by hand |
| Right, Left | the box | the next or previous point | the box | by hand |
| Home, End | the box | the first or the last point | the box | by hand |
| Page Down, Page Up | the box | a tenth of the points on or back, at least 2 | the box | by hand |
| Escape | the box, or the pointer over it | hides the crosshair and tooltip | stays | by hand |
| Tab | the box | leaves the chart; the legend buttons come next | the next control | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| box | group | `label`, through `aria-label`; `aria-roledescription` from `labels.chart` | `aria-describedby` the summary; `aria-busy` while pending | the name, "chart" and the summary when it is reached | test |
| live line | none, `aria-live="polite"` | nothing | none | the point and each series' value as the keys move; pointer moves are silent | by hand |
| table | table | its caption, the `label` | a row header per point | read on request from the table list | test |
| legend item | button | its series name | `aria-pressed` | the name and pressed or not | test |
| failure | alert | its title | none | "Could not load the chart" once | test |

## Tokens
| Part | State | Token |
|---|---|---|
| series | all | `--chart-1`, `--chart-2`, `--chart-3`, `--chart-4`, `--chart-5`, `--chart-6` |
| comparison series | all | `--chart-previous` |
| gridlines | all | `--chart-grid` |
| zero line | all | `--border` |
| axis labels | all | `--muted-foreground` |
| crosshair | hover | `--foreground` at 15% |
| marker ring | hover | `--card` |
| tooltip | hover | `--tip`, `--tip-foreground`, `--r-md`, `--t-press`, `--curve` |
| legend swatch | all | `--r-xs` |

## Related
- Chart: the frame every chart type draws in, with the shared states, keys and table.
- AreaChart: the same data filled down to zero, or stacked into parts of a whole.
- BarChart: amounts per period or group compared by length.
- Sparkline: a trend with no axes, inside a row or a card header.
- ChartCard: a card with the chart's title and headline figure.
- BarList: ranked names with their values, each row filled to its share of the largest.
- Heatmap: values on a grid or a calendar, shaded by size.
- Histogram: how many rows fall into each range of a value.
- ScatterChart: two measures of many things, one dot each.
- WaterfallChart: how a total moved from one amount to another, step by step.
