Skip to content

Sparkline

A word-sized picture of a trend, with no axes, no grid and no legend. It goes beside a number, inside a sentence or in a table cell, and says which way something has been going.

tsx
import { Sparkline } from 'neba';

<Sparkline data={[18, 22, 19, 27, 24, 31, 29, 36]} label="Signups, last eight weeks" endDot />;

It is not a small chart. Every number it could label is one the surrounding text already has, which is why it labels none of them, and why it takes data directly instead of a series array.

data is an array of NebaChartDatum, the same shape every chart takes: a number, a null for a gap, or a point. See LineChart for the full definition.

Props

PropTypeDefaultDescription
data * NebaChartDatum[]The values. null is a gap, exactly as it is on every other chart
shape'line' | 'area' | 'bar''line'Which mark. A line for a trend, an area for a quantity, bars for a count of discrete things
curve'linear' | 'smooth' | 'step''linear'How it gets from one point to the next
sizeshared'xs' | 'sm' | 'md' | 'lg' | 'xl''md'The height of the strip, on a ladder measured against the line of text beside it rather than against the page
color'primary' | 'secondary' | 'success' | 'warning' | 'danger' | 'info' | string첫 번째 chart slot / the first chart slotThe mark's colour, taken directly unlike the full charts: a sparkline has one series and no legend for a palette to hand out
endDotbooleanfalsePuts a dot on the last point: the one direct label a strip this small has room for
baselinenumberDraws a rule across the strip at this value: a target, a budget, last year's average
minnumberThe bottom of the scale. Left out, the strip fills itself with its own range: two side by side are only comparable if both are given the same one
maxnumberThe top of the scale
widthnumber | string'100%'How wide. Fills its container by default
labelstringA name for the strip. With one, the values are exposed to assistive technology; without one it is hidden entirely

Every native <div> attribute passes through. See prop conventions for the shared axes.

Examples

shape · curve · endDot · baseline

shape picks the mark: a line for a trend, an area for a quantity, bars for a count of discrete things. endDot puts a dot on the last point: the one direct label a strip this small has room for. baseline draws a rule across it at a target or a budget.

min · max

A sparkline fills itself with its own range, which is what makes it legible at twenty pixels tall, and it is also the trap. Two sparklines side by side are drawn on two different scales unless they are given the same min and max; pass those and a column of them becomes a small-multiples chart.

size · color · width

size sets the height, on a ladder measured against the line of text beside it rather than against the page. width defaults to filling the container; a number pins it.

color takes a NebaColor family or any CSS colour: unlike the full charts, this one takes it directly, since a sparkline has one series and no legend for a palette to hand out.

tsx
<Sparkline data={data} size="xs" color="success" width={72} />

Accessibility

  • A Sparkline with a label renders its values as visually hidden text and is exposed as an image with that name. Without a label it is hidden from assistive technology entirely: which is correct when the strip sits beside a Statistic that already says the number, and wrong anywhere else.
  • Nothing in it is interactive and nothing in it is reachable only by pointer.

Released under the MIT License