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

## Description
Draws the trend of a series as a line with no axes, in a table cell or beside a figure, as a picture with a name.

`import { Sparkline } from "@/components/cobalt/ui/sparkline"`, source `registry/cobalt/ui/sparkline.tsx`, status `ready-with-gaps`.
Foundation: `hand-rolled`. Traps checked: `trap/loading-layout-shift`, `trap/color-only-status`, `trap/illustration-announced`, `trap/tooltip-essential` (n/a: there is no tooltip; the figure beside the line carries the value), `trap/tooltip-interactive` (n/a: nothing to hover), `trap/touch-hover-flash` (n/a: nothing reacts to the pointer), `trap/hover-unguarded` (n/a: no hover style), `trap/reduced-motion-ignored` (n/a: nothing moves), `trap/motion-transition-all` (n/a: no transition), `trap/demo-on-timers` (n/a: every example holds fixed data), `trap/loop-offscreen` (n/a: nothing loops).

### Foundation
Not applicable: hand-rolled.

| Given by the foundation | Added by the team |
|---|---|
| `d3-shape` for the line and the area path, and `decimate`, `valueDomain` and `formatValue` from `chart-math.ts` | The whole picture: an `svg` with `role="img"` named by its `label`, the first and the last value and, for a single value, that value; a fixed 100 by 24 `viewBox` with `preserveAspectRatio="none"` and a non-scaling 1.5px stroke, so it stretches to its container (`h-6 w-24` by default) with no measuring and a table of a thousand rows draws a thousand sparklines in one pass; a gap for each `null`; past 480 values, a decimation to 240 columns that keeps each column's first, lowest, highest and last value; a flat grey line when no value is known; not focusable, with no tooltip and no frame. The figure beside it carries the value |

## Examples
Twelve months of assets beside the latest figure.

Real uses, 0 call sites (`rg -n "<Sparkline\b" app registry docs/system/examples --glob '!docs/system/examples/sparkline/*'`): the showcase chrome renders no Sparkline of its own, and the examples folder does not count.

Uses inside the registry: none. `registry/cobalt/ui/sparkline.test.tsx` renders it for the tests.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/sparkline/default.tsx` | default | Twelve months of assets beside the latest figure |
| `docs/system/examples/sparkline/in-table.tsx` | composition:Table | A sparkline in each row of a table of advisers, beside the latest figure it ends on |

Example: Twelve months of assets beside the latest figure (`docs/system/examples/sparkline/default.tsx`)

```tsx
import { Sparkline } from "@/components/cobalt/ui/sparkline";
import { compactMoney } from "@/lib/cobalt/format";

const assets = [4.12, 4.18, 4.05, 4.21, 4.34, 4.29, 4.41, 4.38, 4.52, 4.47, 4.6, 4.71].map((v) => v * 1e9);

export default function Example() {
  return (
    <div className="flex items-center gap-3 text-sm">
      <span className="font-medium tabular-nums">{compactMoney(assets[assets.length - 1] ?? 0)}</span>
      <Sparkline values={assets} label="Gross assets over 12 months" format="money" area />
    </div>
  );
}
```

Example: A sparkline in each row of a table of advisers, beside the latest figure it ends on (`docs/system/examples/sparkline/in-table.tsx`)

```tsx
import { Sparkline } from "@/components/cobalt/ui/sparkline";
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/cobalt/ui/table";
import { compactMoney } from "@/lib/cobalt/format";

// Assets at the end of each of the last 12 months, in dollars.
const advisers = [
  { name: "Northwind Capital", trend: [4.1, 4.2, 4.0, 4.3, 4.4, 4.3, 4.5, 4.4, 4.6, 4.5, 4.6, 4.7].map((b) => b * 1e9) },
  { name: "Harbor Point Advisors", trend: [2.6, 2.5, 2.5, 2.4, 2.4, 2.3, 2.3, 2.2, 2.2, 2.1, 2.2, 2.1].map((b) => b * 1e9) },
  { name: "Juniper Partners", trend: [700, 720, 750, 740, 780, 800, 790, 830, 850, 840, 860, 880].map((m) => m * 1e6) },
];

export default function Example() {
  return (
    <Table className="w-full max-w-lg">
      <TableHeader>
        <TableRow>
          <TableHead>Adviser</TableHead>
          <TableHead className="text-right">Assets</TableHead>
          <TableHead>Last 12 months</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        {advisers.map((adviser) => (
          <TableRow key={adviser.name}>
            <TableCell>{adviser.name}</TableCell>
            <TableCell className="text-right tabular-nums">
              {compactMoney(adviser.trend[adviser.trend.length - 1] ?? 0)}
            </TableCell>
            <TableCell>
              <Sparkline values={adviser.trend} label={`${adviser.name} assets, last 12 months`} format="money" />
            </TableCell>
          </TableRow>
        ))}
      </TableBody>
    </Table>
  );
}
```

## Variants
Not applicable: Sparkline has no variant axis. It is one line in the first chart colour; `area` only adds a 12 percent fill under it, and the size is set by `className`.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| idle | `values` with at least two known numbers | read the shape | the line, and the label "from" the first "to" the last value | test |
| gap | a `null`, `undefined` or non-finite value in `values` | read the shape | the line breaks at that place; later points keep their position | test |
| single | exactly one known value | read the label | one point at the middle of the width and a label with that value alone | by hand |
| empty | no values, or none known | read the label | a flat grey line across the middle and the label "no data" | test |
| decimated | more than 480 values | read the shape | the same shape drawn with at most 4 points in each of 240 columns | test |

### State precedence
- Gap and decimated: both show. Decimation skips unknown values, so a gap stays a gap in the thinned line.
- Empty and idle: Not applicable: a series either has a known number or it has none.

### Motion
Not applicable: no motion

## Props

<!-- Props generated from registry/cobalt/ui/sparkline.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `label` | `string` |  | What it shows, read with its first and last value: "Assets over 12 months". |
| `values` | `readonly (number)[]` |  | The values in order; null leaves a gap. |
| `area?` | `boolean` | `false` | Fill the area under the line, faintly. |
| `format?` | `((string \| number) & ValueFormat)` | `"number"` |  |
| `locale?` | `string` |  |  |

Also accepts every native `<svg>` attribute.

`values` are the numbers in order, `null` for a gap. `label` names what it shows and is read with the first and last value. `format` shapes those two values, `area` fills under the line, and `locale` shapes the numbers. The other `svg` attributes pass through, `className` sets the size.

## Usage

### When to use
- A trend at a glance in a table cell, such as assets for the last 12 months in each row of a table of advisers.
- A trend beside a headline figure that already says the number.

### When not to use
- A trend a person reads by value, with axes and a tooltip. Use LineChart instead.
- One figure and its change, with no history worth a picture. Use Stat instead.
- A share of a fixed limit. Use Meter instead.

### Rules
- `rule/sparkline-figure-beside-the-line`: When a `Sparkline` shows a number a person needs, write the figure as text beside it instead of leaving the line to carry it, because the line has no axis, no tooltip and no focus stop, so its height tells a direction and not an amount. Evidence: principle heuristic: recognition rather than recall, the figure is read and the line is glanced at; single use registry/cobalt/ui/sparkline.tsx:14-16. Check: review of every `Sparkline` for a figure in the same row or cell.
  - Don't: `<TableCell><Sparkline values={trend} label="Assets, last 12 months" /></TableCell>`
  - Do: `<TableCell className="tabular-nums">$4.7B</TableCell><TableCell><Sparkline values={trend} label="Assets, last 12 months" /></TableCell>`
- `rule/sparkline-null-for-a-gap`: When a month has no value, put `null` in its place in `values` instead of dropping it or writing 0, because `null` leaves a gap where it falls, while a 0 draws a fall to nothing that did not happen and a dropped month pulls every later point left. Evidence: measured aria-label "Assets: from $4.1B to $4.7B" with a `null` between, registry/cobalt/ui/sparkline.test.tsx. Check: test `sparkline.test.tsx` expects the label to skip the `null`.
  - Don't: `<Sparkline values={[4.1e9, 0, 4.7e9]} label="Assets" format="money" />`
  - Do: `<Sparkline values={[4.1e9, null, 4.7e9]} label="Assets" format="money" />`

### Content
- Follows `rule/writing-no-invented-numbers`.
- `rule/sparkline-label-names-the-trace`: When a sparkline is placed, give `label` the name of what it traces with its period, such as "Gross assets over 12 months", because the label opens the picture's accessible name and the first and last value follow it. Evidence: principle wcag: 1.1.1 Non-text content; principle wcag: 4.1.2 Name, role, value. Check: test `sparkline.test.tsx` expects `aria-label="Assets: from $4.1B to $4.7B"`.
  - Don't: `<Sparkline values={assets} label="Trend" />`
  - Do: `<Sparkline values={assets} label="Gross assets over 12 months" format="money" />`

### Anti-slop
- `rule/sparkline-no-hover-or-axes`: When a trend needs an axis, a tooltip or a value at a point, use `LineChart` instead of adding a pointer handler or axis marks to a `Sparkline`, because the sparkline stretches with `preserveAspectRatio="none"` and never measures its box, so it has no pixel position to read a point from. Evidence: single use registry/cobalt/ui/sparkline.tsx:79; person D36. Check: test `sparkline.test.tsx` expects `preserveAspectRatio="none"` and a non-scaling stroke.
  - Don't: `<Sparkline values={assets} label="Assets" onPointerMove={showTip} />`
  - Do: `<LineChart label="Assets" data={months} x="month" series={[{ key: "assets", label: "Assets", value: "assets" }]} />`

### Limits
- `rule/sparkline-thinned-past-480`: When a series holds more than 480 values, pass all of them to `Sparkline` instead of thinning them by hand, because it keeps each pixel column's first, lowest, highest and last value, so a spike survives, and 100,000 values draw under 1,000 path points. Evidence: measured under 1,000 path points for 100,000 values, registry/cobalt/ui/sparkline.test.tsx. Check: test `sparkline.test.tsx` expects fewer than 1,000 points for 100,000 values.
  - Don't: `<Sparkline values={readings.filter((_, i) => i % 100 === 0)} label="Load" />`
  - Do: `<Sparkline values={readings} label="Load" />`

## Accessibility
Rests on a native `svg` with `role="img"`. The picture is named by `label` with its first and last known value, for example "Assets: from $4.1B to $4.7B", and is not focusable. The line is the first chart colour, which holds 3:1 on the card in both themes (`chart-math.test.ts`). Screen reader pass with VoiceOver: NEEDS REVIEW.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the picture | skips it, it is not focusable | the next control | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| picture | img | `label` and the first and last known value, through `aria-label` | none | "Assets: from $4.1B to $4.7B, image" when a reader reaches it | test |
| empty picture | img | `label` and "no data" | none | "Assets: no data, image" | test |

## Tokens
| Part | State | Token |
|---|---|---|
| line | all | `--chart-1` |
| area | all | `--chart-1` at 12% |
| empty line | empty | `--chart-grid` |

## Related
- LineChart: a trend read by value, with axes and a tooltip.
- Stat: one figure and its change, with no history.
- Meter: a share of a fixed limit.
- DataTable: the table a sparkline most often sits in.
- AreaChart: an amount over time filled to zero, stacked or not.
