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

## Description
Draws an amount over time as a line with the area under it filled from zero, so a person reads how much there is and how it changes, alone or piled into parts of a whole.

`import { AreaChart } from "@/components/cobalt/ui/area-chart"`, source `registry/cobalt/ui/area-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`, `area` and its three curves for the path maths only | A thin wrapper over the shared XY engine in `chart.tsx`, set to `mark: "area"`: the engine gives the frame (fixed box, hover store, tooltip, keyboard, states, summary and table), the fill measured from zero, a stack that piles the series so the top edge is their sum with values below zero stacking downward, and min and max per pixel column once a series holds more than two points a pixel; `AreaChart` adds only `stacked` and `curve` |

## Examples
Capital called from investors since the fund's first close, filled down to zero as it builds toward the $800M committed.

Real uses, 0 call sites (`rg -n "<AreaChart\b" app docs/system/examples --glob '!docs/system/examples/area-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. The in-card.tsx example puts it in a `Card` with a section title.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/area-chart/default.tsx` | default | Capital called from investors since the fund's first close, filled down to zero as it builds toward the $800M committed |
| `docs/system/examples/area-chart/stacked-true.tsx` | stacked=true | Assets by strategy each quarter, stacked so the top edge is the fund's total |
| Not applicable: false is the default, the single filled area in default.tsx | stacked=false | Each series filled down to zero on its own |
| `docs/system/examples/area-chart/in-card.tsx` | composition:Card | Nights booked each month by channel in a chart card; the headline is the latest month's total and its change on the month before |

Example: Capital called from investors since the fund's first close, filled down to zero as it builds toward the $800M committed (`docs/system/examples/area-chart/default.tsx`)

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

// Capital called in each quarter, in millions of dollars; the chart shows the running total.
const calls = [64, 88, 52, 97, 71, 43, 80, 66, 58, 39, 47, 31];
let called = 0;
const quarters = calls.map((amount, i) => {
  called += amount;
  return {
    quarter: new Date(Date.UTC(2023, 3 * i + 3, 0)).toISOString().slice(0, 10),
    called: called * 1e6,
  };
});
const series = [{ key: "called", label: "Capital called", value: "called" as const }];

export default function Example() {
  return (
    <AreaChart
      label="Capital called"
      data={quarters}
      x="quarter"
      series={series}
      format="money"
      domain={[0, 1e9]}
      layers={(scope) => <ChartReferenceLine scope={scope} y={800e6} label="Committed $800M" />}
    />
  );
}
```

Example: Nights booked each month by channel in a chart card; the headline is the latest month's total and its change on the month before (`docs/system/examples/area-chart/in-card.tsx`)

```tsx
import { AreaChart } from "@/components/cobalt/ui/area-chart";
import { ChartCard } from "@/components/cobalt/ui/chart-card";
import { pointDate } from "@/lib/cobalt/chart-math";
import { integer } from "@/lib/cobalt/format";

// Nights booked across 42 hotels each month, by the channel the booking came through.
const months = [
  [4120, 2890, 1340], [3980, 2760, 1410], [3610, 2420, 1180], [3770, 2510, 1290],
  [4260, 2980, 1460], [4590, 3240, 1520], [4980, 3610, 1490], [5410, 3920, 1380],
  [5720, 4180, 1310], [5530, 4010, 1470], [4870, 3460, 1590], [4640, 3220, 1620],
].map(([direct, agencies, corporate], i) => ({
  month: new Date(Date.UTC(2025, 9 + i, 1)).toISOString().slice(0, 10),
  direct: direct ?? 0,
  agencies: agencies ?? 0,
  corporate: corporate ?? 0,
}));
const series = [
  { key: "direct", label: "Direct", value: "direct" as const },
  { key: "agencies", label: "Agencies", value: "agencies" as const },
  { key: "corporate", label: "Corporate", value: "corporate" as const },
];

const total = (row: (typeof months)[number]) => row.direct + row.agencies + row.corporate;
const latest = months[months.length - 1]!;
const before = months[months.length - 2]!;
const monthOf = (row: (typeof months)[number]) => pointDate(Date.parse(row.month), 0, "en-US", "month");

export default function Example() {
  return (
    <ChartCard
      className="w-full max-w-xl"
      title="Nights booked"
      value={integer(total(latest))}
      change={((total(latest) - total(before)) / total(before)) * 100}
      note={`${monthOf(latest)}, against ${monthOf(before)}`}
    >
      <AreaChart label="Nights booked by channel" stacked data={months} x="month" series={series} />
    </ChartCard>
  );
}
```

## Variants

### stacked
- `true`: the series pile, so the top edge is their sum and a value below zero stacks downward from it, for parts of one whole. Used at 0 call sites.
- `false`: each series is filled from zero on its own, the default, for one series or series that overlap. Used at 0 call sites.

Example: Assets by strategy each quarter, stacked so the top edge is the fund's total (`docs/system/examples/area-chart/stacked-true.tsx`)

```tsx
import { AreaChart } from "@/components/cobalt/ui/area-chart";

// Assets by strategy at the end of each quarter, in millions of dollars.
const quarters = [
  { quarter: "2024-03-31", equity: 1840, credit: 920, real: 410 },
  { quarter: "2024-06-30", equity: 1910, credit: 960, real: 430 },
  { quarter: "2024-09-30", equity: 1870, credit: 1010, real: 455 },
  { quarter: "2024-12-31", equity: 2030, credit: 1060, real: 470 },
  { quarter: "2025-03-31", equity: 2110, credit: 1120, real: 468 },
  { quarter: "2025-06-30", equity: 2060, credit: 1180, real: 492 },
  { quarter: "2025-09-30", equity: 2240, credit: 1210, real: 515 },
  { quarter: "2025-12-31", equity: 2320, credit: 1260, real: 530 },
].map((row) => ({ ...row, equity: row.equity * 1e6, credit: row.credit * 1e6, real: row.real * 1e6 }));
const series = [
  { key: "equity", label: "Equity", value: "equity" as const },
  { key: "credit", label: "Credit", value: "credit" as const },
  { key: "real", label: "Real assets", value: "real" as const },
];

export default function Example() {
  return <AreaChart label="Assets by strategy" stacked data={quarters} x="quarter" series={series} format="money" />;
}
```

## 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 areas, hover, tab in | the areas, the axes, the legend when there are two series or a comparison | screenshot |
| hover | the pointer over the box, snapped to the nearest point or group | 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 | 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 |
| 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.
- Hover and focus: the last one wins. The pointer and the keys move the same marker, so one point shows at a time.
- Stacked and comparison series: stacked wins. The comparison series is not stacked and not drawn; it stays in the legend and the table.

### 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/area-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 edge 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)` |  |  |
| `stacked?` | `boolean` | `false` | Stack the series, so the top edge is their sum: parts of a whole over time. |
| `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. |

`stacked` piles the series into one total. A series with `mark: "line"` draws as a line with no fill and stays out of the stack. `curve` is `linear`, `monotone` or `step`. 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
- An amount accumulates over time and the size of what lies under the line is the point, such as net asset value each month.
- Series are parts of one total over time, such as assets by strategy, and the top edge should be the total.

### When not to use
- The value is a level that moves in a narrow band far above zero. Use LineChart instead.
- Amounts are compared period by period by length. Use BarChart instead.
- Only a trend is needed inside a row or a card header. Use Sparkline instead.

### Rules
- `rule/area-chart-stack-parts-of-a-whole`: When series are parts of one total, such as assets by strategy, set `stacked` instead of overlaying filled areas, because overlapping fills hide the series behind them and the top edge of a stack reads as the total. Evidence: principle heuristic: match between system and real world; single use docs/system/examples/area-chart/stacked-true.tsx:22. Check: test `area-chart.test.tsx` expects the table to list each series' own value, negatives included, stacked or not.
  - Don't: `<AreaChart label="Assets by strategy" data={quarters} x="quarter" series={series} />`
  - Do: `<AreaChart label="Assets by strategy" stacked data={quarters} x="quarter" series={series} />`
- `rule/area-chart-zero-is-the-baseline`: When the series moves in a narrow band far above zero, draw it with `LineChart` instead of `AreaChart`, because the fill is measured from zero and the movement shows as a thin sliver at the top of a tall block. Evidence: principle heuristic: match between system and real world; single use registry/cobalt/ui/area-chart.tsx:13. Check: test `chart-math.test.ts` expects bars and areas to keep zero and a line to keep its own range.
  - Don't: `<AreaChart label="Unit price" data={prices} x="day" series={price} />`
  - Do: `<LineChart label="Unit price" data={prices} x="day" series={price} />`
- `rule/area-chart-comparison-beside-not-stacked`: When a stacked area needs a comparison with an earlier period, draw the comparison in a `LineChart` beside it instead of passing a `kind: "previous"` series, because a stacked area leaves the comparison series out of the stack and does not draw it. Evidence: single use registry/cobalt/ui/chart.tsx:1927; principle heuristic: visibility of system status. Check: review of every stacked `AreaChart` for a series with `kind: "previous"`.
  - Don't: `<AreaChart stacked series={[...parts, before]} ... />`
  - Do: `<LineChart series={[total, before]} ... />`

### Content
- Follows `rule/writing-no-invented-numbers`.
- Follows `rule/chart-label-names-the-data`.
- `rule/area-chart-series-label-names-the-part`: When series are stacked, give each `label` the name of the part, such as "Equity" or "Credit", instead of "Series 1", because the label is the legend entry, the tooltip row and the table column, and the stack is read part by part. Evidence: principle wcag: 1.1.1 Non-text content; principle heuristic: match between system and real world. Check: test `area-chart.test.tsx` expects a legend of two toggles named for the series.
  - Don't: `{ key: "equity", label: "Series 1", value: "equity" }`
  - Do: `{ key: "equity", label: "Equity", value: "equity" }`

### Anti-slop
- `rule/area-chart-no-stack-of-separate-things`: When series are not parts of one total, such as two funds' assets, leave `stacked` off instead of stacking them for the look, because the top edge of a stack is read as a sum, and a sum of two funds is a number nobody asked for. Evidence: principle heuristic: match between system and real world. Check: review of every `AreaChart` with `stacked`, asking whether its top edge is a total someone would name.
  - Don't: `<AreaChart label="Two funds" stacked data={rows} x="month" series={[fundA, fundB]} />`
  - Do: `<LineChart label="Two funds" data={rows} x="month" series={[fundA, fundB]} />`

### Limits
- `rule/area-chart-decimates-past-two-per-pixel`: When a series holds more than 2 points per pixel of plot width, pass all the rows to `AreaChart` 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 hover median 3.3ms, p95 4.2ms and layout shift 0 on the huge area chart, .design-system/evidence/charts/hover-cost.jsonl; measured decimation 1.8ms for 1,000,000 points, docs/research/2026-10-17-charts.md. Check: test `chart-math.test.ts` expects decimation to keep every peak and dip and at most four points a pixel.
  - Don't: `<AreaChart data={rows.filter((_, i) => i % 50 === 0)} x="at" series={series} label="Hourly value" />`
  - Do: `<AreaChart 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 areas are `aria-hidden`; the data is a visually hidden sentence (`aria-describedby`) and a visually hidden `table` that lists each series' own value, never the stacked sum, sampled to 200 evenly spaced rows with a note past that. 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.
- LineChart: a level that moves in a narrow band, with no fill.
- 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.
- WaterfallChart: how a total moved from one amount to another, step by step.
- CompositionBar: the parts of one whole in a single bar, with each part's amount and share.
