# Sparkline Chart

A small chart without axes for KPI cards and table cells.

Page: https://sui.draco.dev/docs/charts/sparkline-chart

### Example: sparkline-chart-demo

```tsx
"use client";
import {
  chartLabelsZhCN,
  SparklineChart,
} from "@workspace/ui/charts/sparkline-chart";
import type { ExampleProps } from "../types";

const revenue = [12, 18, 15, 22, 19, 27, 31].map((value) => ({ value }));
const churn = [8, 7, 9, 6, 5, 6, 4].map((value) => ({ value }));

export default function Example({ locale }: ExampleProps) {
  const chinese = locale === "zh-CN";
  const cards = [
    {
      title: chinese ? "收入" : "Revenue",
      total: "$31K",
      data: revenue,
      color: "var(--chart-2)",
    },
    {
      title: chinese ? "流失" : "Churn",
      total: "4%",
      data: churn,
      color: "var(--chart-5)",
    },
  ];
  return (
    <div className="grid w-full gap-4 sm:grid-cols-2">
      {cards.map((card) => (
        <div key={card.title} className="grid gap-2 rounded-xl border p-4">
          <span className="text-muted-foreground text-sm">{card.title}</span>
          <span className="font-semibold text-2xl tabular-nums">
            {card.total}
          </span>
          <SparklineChart
            data={card.data}
            config={{ value: { label: card.title, color: card.color } }}
            variant="area"
            aria-label={card.title}
            labels={chinese ? chartLabelsZhCN : undefined}
          />
        </div>
      ))}
    </div>
  );
}
```

## Installation

```bash
npx shadcn@latest add @sui/sparkline-chart
```

With pnpm, Yarn or Bun, use `pnpm dlx`, `yarn dlx` or `bunx --bun` in place of `npx`, and `pnpm add`, `yarn add` or `bun add` in place of `npm install`.

Install with the shadcn CLI or use the shared `@workspace/ui` package. Follow the [installation guide](/docs/installation) to configure the registry, load styles, and choose import aliases.

## Usage

```tsx
import { SparklineChart } from "@workspace/ui/charts/sparkline-chart";

<SparklineChart data={data} config={config} variant="area" />;
```

## Customize

The wrapper covers common cases. For more control, pass Recharts elements as `children`, or compose your own chart with the [Chart](/docs/charts/chart) primitives.

## API reference

| Prop | Type | Default / behavior |
| --- | --- | --- |
| `categories` | `readonly string[]` | Every key in `config`. |
| `variant` | `"line" \| "area"` | `"line"`. |
| `data` | `readonly Record<string, unknown>[]` | Required rows. |
| `config` | `ChartConfig` | Required. Labels, colors and icons by key; series without a color use `--chart-1` to `--chart-5`. |
| `valueFormatter` | `(value: number) => string` | `toLocaleString`. Formats axes, tooltip and the screen-reader table; build one with `numberFormatter`. |
| `loading` | `boolean` | `false`. Shows a skeleton. |
| `labels` | `Partial<ChartLabels>` | English labels; pass `chartLabelsZhCN` for Chinese. |
| `aria-label` | `string` | Names the figure and captions its data table. |
| `className` | `string` | Sizes the chart; defaults to a 16:9 box. |
| `children` | `ReactNode` | Extra Recharts elements, such as `ReferenceLine`. |

The module also exports `SparklineChartProps`, `numberFormatter`, and the label presets `chartLabels` and `chartLabelsZhCN`. Screen readers get a hidden data table while the drawing itself is hidden from assistive technology, and animation is off when reduced motion is requested.
