Beside a figure
import { Text } from "@senvenli/react/display";
import { Sparkline } from "@senvenli/react/charts";
import { Stack } from "@senvenli/react/layout";
const WEEKLY_REVENUE = [38200, 36900, 41500, 40100, 44800, 43600, 47900, 46200, 48920];
const usd = new Intl.NumberFormat("en", { style: "currency", currency: "USD", maximumFractionDigits: 0 });
export default function Example() {
// With no aria-label, the formatter writes the summary screen readers hear (first, last, low, high).
return (
<Stack direction="horizontal" space="space-150">
<Text size="body-large" weight="medium" isNumeric>{usd.format(48920)}</Text>
<Sparkline data={WEEKLY_REVENUE} width={220} height={48} valueFormatter={usd.format} />
</Stack>
);
}import { Sparkline } from "@senvenli/react/charts";Examples
Bars
For counts per period, where each value stands alone.import { Sparkline } from "@senvenli/react/charts";
const DAILY_ORDERS = [42, 51, 38, 64, 58, 71, 33, 47, 55, 62, 69, 74, 36, 49];
export default function Example() {
return <Sparkline type="bar" data={DAILY_ORDERS} width={280} height={64} aria-label="Orders per day, last 14 days" />;
}In a table cell
One trend per row, all at the same size.import { Sparkline } from "@senvenli/react/charts";
import { Table } from "@senvenli/react/collection";
const REGIONS = [
{ id: "north", region: "North", orders: 455, trend: [320, 345, 390, 372, 418, 455] },
{ id: "south", region: "South", orders: 276, trend: [210, 232, 228, 260, 281, 276] },
{ id: "west", region: "West", orders: 239, trend: [150, 148, 171, 196, 204, 239] },
];
export default function Example() {
return (
<Table
aria-label="Orders by region"
rows={REGIONS}
columns={[
{ key: "region", header: "Region" },
{ key: "orders", header: "Orders in June", align: "end" },
{ key: "trend", header: "Last six months", width: "160px", render: (row) => <Sparkline data={row.trend} width={120} /> },
]}
/>
);
}Guidelines
When to use it
- A trend beside a stat or in a table cell.
Use something else when
- Readers need axes, values or several series. Line chart
- The current figure and its change are enough. Stat
Accessibility
Built in
- A "Show as table" button shows the same data as a table, which screen readers get by default.
You provide
- An aria-label that summarizes the trend.
API reference
Sparkline
| Prop | Type | Default | Description |
|---|---|---|---|
dataRequired | readonly number[] | – | |
type | "line" | "bar" | "line" | |
width | number | – | 120 in tables; leave out to fill the width (Stat). |
height | number | 32 | 32 in tables, 28 in a Stat. |
aria-label | string | – | Names the trend when the text next to it does not; a summary of the values otherwise. |
valueFormatter | ChartValueFormatter | plain |
dataRequiredreadonly number[]type"line" | "bar"Default"line"widthnumber120 in tables; leave out to fill the width (Stat).
heightnumberDefault3232 in tables, 28 in a Stat.
aria-labelstringNames the trend when the text next to it does not; a summary of the values otherwise.
valueFormatterChartValueFormatterDefaultplain