Atoms · Sparkline
Sparkline
A small trend line with no axes. It sits beside a Stat or in a table cell. It's plain SVG (Scalable Vector Graphics, the web's format for drawn shapes): one image named by a sentence, with an optional safe band behind it and a square on the last value.
- Best for
- You want the shape of a recent series, not its values. Think of a reading over the last hour.
- Not for
- Values people need to read, compare or hover over. Use ChartPanel for those.
This page as Markdown, a plain-text copy for a coding agent (an AI tool that writes code).
Examples
Beside the value it trends
4.2 °C
412
Code
tsx
<div className="flex items-center gap-3"> <span className="font-mono text-sm tabular-nums">4.2 °C</span> <Sparkline values={[3.1, 3.2, 3.2, 3.4, 3.3, 3.6, 3.8, 3.7, 4.0, 4.2]} label="Cold room 2, last hour: 3.1 to 4.2 °C, rising" band={{ from: 2, to: 8 }} showLast /></div><div className="flex items-center gap-3"> <span className="font-mono text-sm tabular-nums">412</span> <Sparkline values={[380, 365, 402, 398, 371, 420, 412]} label="Orders, last 7 days: 365 to 420, steady" tone="signal" showLast /></div>Tones
Code
tsx
<Sparkline values={[5, 6, 5, 7, 6, 8]} label="Neutral trend, rising" /><Sparkline values={[5, 6, 5, 7, 6, 8]} label="Healthy trend, rising" tone="ok" /><Sparkline values={[5, 6, 5, 7, 6, 8]} label="Degraded trend, rising" tone="warning" /><Sparkline values={[5, 6, 5, 7, 6, 8]} label="Failing trend, rising" tone="hostile" /><Sparkline values={[5, 6, 5, 7, 6, 8]} label="Informational trend, rising" tone="info" />Stretched to its container, with a safe band from 2 to 8 °C
Code
tsx
<div className="w-full max-w-md"> <Sparkline values={[4.1, 4.3, 4.0, 5.2, 6.8, 8.4, 9.1, 8.7, 7.2, 5.9, 5.0, 4.6]} label="Cold room 3, last 12 hours: 4.0 to 9.1 °C, above the safe band for 3 hours, now back inside it" tone="warning" band={{ from: 2, to: 8 }} min={0} max={10} height={40} className="w-full" showLast /></div>No values, one value, a flat run
Code
tsx
<Sparkline values={[]} label="No readings yet" /><Sparkline values={[12]} label="One reading: 12" showLast /><Sparkline values={[7, 7, 7, 7]} label="Flat at 7" showLast />Anatomy
| Part | What it is |
|---|---|
| Frame | An inline box. It's 96 x 24 by default, or the container's width with className="w-full". |
| Image | An svg with role="img", named by label. Screen readers (software that reads the page aloud) don't get the points one by one. |
| Band | Optional. A hairline-filled range behind the line, such as a safe range. |
| Line | 1.5px (pixels) in the tone colour. The stroke doesn't get thicker when the frame stretches. |
| Last value | An optional 6px square on the newest point. It's aria-hidden, a setting that hides it from screen readers. |
Props
- valuesreadonly number[]
- Required. Oldest first. Non-finite values (such as NaN or Infinity) are dropped.
- labelstring
- Required. The chart as one sentence: "Temperature, last hour: 3.1 to 4.2 °C, rising".
- tone'neutral' | 'signal' | 'ok' | 'warning' | 'hostile' | 'info'Default 'neutral'
- The line's colour. Each tone means what it means on Badge.
- minnumber
- A fixed bottom for the scale. Without it, the scale follows the data (and the band).
- maxnumber
- A fixed top for the scale. Without it, the scale follows the data (and the band).
- band{ from: number; to: number }
- A range drawn behind the line.
- showLastbooleanDefault false
- Puts a square on the last value.
- widthnumberDefault 96
- In CSS px, the pixel unit of Cascading Style Sheets. className="w-full" overrides it.
- heightnumberDefault 24
- In CSS px.
- classNamestring
- Goes on the frame.
- refRef<HTMLSpanElement>
- Forwarded to the frame.
Keyboard
You can't reach this component with the keyboard. It has no keyboard behaviour of its own.
Do and don't
Do
- Write the label as the reading a person takes from the shape: the range and the direction.
- Fix min and max when several sparklines sit side by side and have to share a scale.
- Put the current value beside it in text. The line shows the trend, not the number.
- Use a tone for what the trend means (hostile for out of range), and say it in text too.
Don't
- Don't use a sparkline where someone has to read values off it. That's a job for ChartPanel.
- Don't leave the label as a title like "Temperature". It's the only thing a screen reader hears.
- Don't colour every sparkline on a page. Neutral is the default for a reason.