# Sparkline

A tiny line or bar trend with no axes, next to a figure.

- Family: charts
- Import: `import { Sparkline } from "@senvenli/react/charts";`
- Page: https://design.senvenli.com/docs/components/sparkline
- JSON: https://design.senvenli.com/docs/components/sparkline.json

## Example: Beside a figure

The trend behind a number, with the last point marked.

```tsx
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>
  );
}
```

## Examples

### Bars

For counts per period, where each value stands alone.

```tsx
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.

```tsx
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

- [line-chart](https://design.senvenli.com/docs/components/line-chart.md): Readers need axes, values or several series.
- [stat](https://design.senvenli.com/docs/components/stat.md): The current figure and its change are enough.

## 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 | Required | Description |
| --- | --- | --- | --- | --- |
| data | `readonly number[]` |  | yes |  |
| type | `"line" \| "bar"` | `"line"` | no |  |
| width | `number` |  | no | 120 in tables; leave out to fill the width (Stat). |
| height | `number` | `32` | no | 32 in tables, 28 in a Stat. |
| aria-label | `string` |  | no | Names the trend when the text next to it does not; a summary of the values otherwise. |
| valueFormatter | `ChartValueFormatter` | `plain` | no |  |

### Tokens it reads

`--ds-background-neutral`, `--ds-background-neutral-pressed`, `--ds-border`, `--ds-border-bold`, `--ds-border-focused`, `--ds-chart-1`, `--ds-chart-2`, `--ds-chart-3`, `--ds-chart-4`, `--ds-chart-5`, `--ds-chart-neutral`, `--ds-link`, `--ds-radius-medium`, `--ds-radius-small`, `--ds-radius-xsmall`, `--ds-shadow-overlay`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-space-400`, `--ds-surface-overlay`, `--ds-surface-raised`, `--ds-text`, `--ds-text-danger`, `--ds-text-subtle`, `--ds-text-subtlest`, `--ds-text-success`

## Related

- [stat](https://design.senvenli.com/docs/components/stat.md)
