# Sparkline

> A word-sized trend chart for metric cards, tables, and dense dashboards.

Source: https://www.honestui.com/docs/charts/sparkline/static

### Basic Sparkline

```tsx
"use client";

import { Sparkline, type ChartConfig } from "honestui/charts";

// Scenario: Weekly revenue card
const data = [
  { week: "Week 1", revenue: 31.2 },
  { week: "Week 2", revenue: 33.8 },
  { week: "Week 3", revenue: 32.1 },
  { week: "Week 4", revenue: 35.6 },
  { week: "Week 5", revenue: 34.9 },
  { week: "Week 6", revenue: 38.4 },
  { week: "Week 7", revenue: 37.2 },
  { week: "Week 8", revenue: 40.9 },
  { week: "Week 9", revenue: 39.8 },
  { week: "Week 10", revenue: 43.5 },
  { week: "Week 11", revenue: 45.1 },
  { week: "Week 12", revenue: 48.2 },
];

const chartConfig = {
  revenue: {
    label: "Revenue",
    colors: {
      light: ["#2563eb"],
      dark: ["#60a5fa"],
    },
  },
} satisfies ChartConfig;

export function ExampleSparkline() {
  return (
    <div className="bg-background flex w-full max-w-72 items-end justify-between gap-6 rounded-lg border p-4 text-xs">
      <div className="grid gap-1">
        <span className="text-muted-foreground">Revenue</span>
        <span className="text-foreground font-mono text-xl font-medium tabular-nums">$48.2k</span>
        <span className="text-muted-foreground">
          <span className="text-emerald-600 dark:text-emerald-400">+54%</span> in 12 weeks
        </span>
      </div>
      <Sparkline
        data={data}
        config={chartConfig}
        dataKey="revenue"
        xDataKey="week"
        variant="area"
        className="h-12 w-28"
      >
        <Sparkline.Dot points="last" />
        <Sparkline.Tooltip valueFormatter={(value) => `$${value.toFixed(1)}k`} />
      </Sparkline>
    </div>
  );
}

```

## Overview [#overview]

A sparkline shows the shape of a series without axes, gridlines, or labels, so it fits beside the number it explains: a metric card, a table row, or a list of services. Use it to answer "which way is this going and how bumpy was it?" at a glance. When readers need exact values, dates, or comparisons between series, use a [Line Chart](/docs/charts/line-chart/static) instead.

## Anatomy [#anatomy]

`Sparkline` owns the data, the plotted `dataKey`, its colors, and the value scale. Choose a `variant` of `line`, `area`, or `bar`. Optional parts add context without adding chrome: `Dot` marks the latest point or the extremes, `ReferenceLine` draws a dashed baseline at a value or a statistic such as the average, and `Tooltip` reveals one point at a time on hover.

## Accessibility [#accessibility]

The canvas carries a generated accessible name that states the trend direction, the first and last values, and the range, for example "Revenue sparkline, up from $31.2k to $48.2k". Pass `ariaLabel` when the surrounding text already says this, or when the sparkline needs more specific wording. Keep the headline number in real text next to the chart; the tooltip is pointer-operated and must not be the only way to read a value.

## Installation [#installation]

<CommandBlock commands="[&#x22;honestui&#x22;]" />

## Usage [#usage]

```tsx
import { Sparkline, type ChartConfig } from "honestui/charts";
```

```tsx
<Sparkline
  data={data}
  config={chartConfig}
  dataKey="revenue"
  xDataKey="week"
  variant="area"
  className="h-12 w-28"
>
  <Sparkline.Dot points="last" />
  <Sparkline.Tooltip valueFormatter={(value) => `$${value}k`} />
</Sparkline>
```

Give the sparkline an explicit size. Its plot is inset only as far as the stroke and dots need, so the line reaches the edges of its box. The tooltip is attached to the page rather than the chart, so it can rise above a chart only a few pixels tall.

## Examples [#examples]

### Variants [#variants]

### variant='line' | 'area' | 'bar'

```tsx
"use client";

import { Sparkline, type ChartConfig } from "honestui/charts";

// Scenario: Daily signups, three ways
const data = [42, 48, 45, 53, 61, 58, 66, 63, 71, 69, 78, 74, 82, 88].map((signups, index) => ({
  day: `Day ${index + 1}`,
  signups,
}));

const chartConfig = {
  signups: {
    label: "Signups",
    colors: {
      light: ["#059669"],
      dark: ["#34d399"],
    },
  },
} satisfies ChartConfig;

const variants = ["line", "area", "bar"] as const;

export function ExampleSparklineVariants() {
  return (
    <div className="grid w-full max-w-md gap-3 text-xs">
      {variants.map((variant) => (
        <div
          key={variant}
          className="flex items-center justify-between gap-6 border-b pb-3 last:border-0 last:pb-0"
        >
          <span className="text-muted-foreground w-10 font-mono">{variant}</span>
          <Sparkline
            data={data}
            config={chartConfig}
            dataKey="signups"
            xDataKey="day"
            variant={variant}
            curve="smooth"
            className="h-9 flex-1"
          >
            <Sparkline.Tooltip />
          </Sparkline>
          <span className="text-foreground w-8 text-right font-mono font-medium tabular-nums">88</span>
        </div>
      ))}
    </div>
  );
}

```

Lines suit continuous measures, areas add weight to a volume, and bars suit discrete counts such as daily signups. Bars start from zero unless the data has negative values; lines scale to the data range so small movements stay visible.

### Dots and reference lines [#dots-and-reference-lines]

### ReferenceLine and Dot

```tsx
"use client";

import { Sparkline, type ChartConfig } from "honestui/charts";

// Scenario: p95 latency against its average
const latency = [182, 176, 190, 214, 168, 172, 251, 198, 186, 164, 171, 179, 206, 188, 175, 169];
const data = latency.map((ms, index) => ({ hour: `${String(index + 6).padStart(2, "0")}:00`, ms }));

const chartConfig = {
  ms: {
    label: "p95 latency",
    colors: {
      light: ["#ea580c"],
      dark: ["#fb923c"],
    },
  },
} satisfies ChartConfig;

export function ExampleSparklineReference() {
  return (
    <div className="grid w-full max-w-sm gap-2 text-xs">
      <div className="flex items-baseline justify-between">
        <span className="text-foreground font-medium">p95 latency · today</span>
        <span className="text-muted-foreground">Dashed line is the average</span>
      </div>
      <Sparkline data={data} config={chartConfig} dataKey="ms" xDataKey="hour" className="h-16 w-full">
        <Sparkline.ReferenceLine y="average" />
        <Sparkline.Dot points="last-and-extremes" variant="colored-border" />
        <Sparkline.Tooltip valueFormatter={(value) => `${value} ms`} />
      </Sparkline>
      <div className="text-muted-foreground flex justify-between font-mono tabular-nums">
        <span>Low 164 ms</span>
        <span>High 251 ms</span>
      </div>
    </div>
  );
}

```

`ReferenceLine` accepts a number or one of `average`, `first`, `min`, or `max`. `Dot` marks the `last` point, the `extremes`, both, or `all` points, using the same dot variants as the Line Chart.

### In a table [#in-a-table]

### Sparklines in table rows

```tsx
"use client";

import { Sparkline, type ChartConfig } from "honestui/charts";

// Scenario: Watchlist with a 30-day trend per row
function series(start: number, drift: number, swing: number) {
  return Array.from({ length: 30 }, (_, day) => ({
    day: `Day ${day + 1}`,
    price: Number((start + drift * day + Math.sin(day * 0.9 + start) * swing).toFixed(2)),
  }));
}

const trendConfig = {
  up: { label: "Price", colors: { light: ["#059669"], dark: ["#34d399"] } },
  down: { label: "Price", colors: { light: ["#dc2626"], dark: ["#f87171"] } },
} satisfies ChartConfig;

const rows = [
  { ticker: "ACME", name: "Acme Corp", data: series(124, 0.42, 2.4) },
  { ticker: "GLBX", name: "Globex", data: series(88, -0.36, 1.8) },
  { ticker: "INIT", name: "Initech", data: series(46, 0.12, 1.1) },
  { ticker: "UMBR", name: "Umbrella", data: series(212, -0.6, 3.6) },
].map((row) => {
  const first = row.data[0].price;
  const last = row.data[row.data.length - 1].price;
  const change = ((last - first) / first) * 100;
  // Each row's config points the one data key at a rising or falling color.
  const config: ChartConfig = { price: change >= 0 ? trendConfig.up : trendConfig.down };
  return { ...row, last, change, config };
});

export function ExampleSparklineTable() {
  return (
    <table className="w-full max-w-lg text-xs">
      <thead>
        <tr className="text-muted-foreground border-b text-left">
          <th className="pb-2 font-medium">Ticker</th>
          <th className="pb-2 font-medium">30 days</th>
          <th className="pb-2 text-right font-medium">Price</th>
          <th className="pb-2 text-right font-medium">Change</th>
        </tr>
      </thead>
      <tbody>
        {rows.map((row) => (
          <tr key={row.ticker} className="border-b last:border-0">
            <td className="py-2.5">
              <div className="text-foreground font-mono font-medium">{row.ticker}</div>
              <div className="text-muted-foreground">{row.name}</div>
            </td>
            <td className="py-2.5">
              <Sparkline
                data={row.data}
                config={row.config}
                dataKey="price"
                xDataKey="day"
                className="h-8 w-32"
                ariaLabel={`${row.ticker} price over 30 days`}
              >
                <Sparkline.ReferenceLine y="first" />
                <Sparkline.Tooltip valueFormatter={(value) => `$${value.toFixed(2)}`} />
              </Sparkline>
            </td>
            <td className="text-foreground py-2.5 text-right font-mono tabular-nums">
              ${row.last.toFixed(2)}
            </td>
            <td
              className={`py-2.5 text-right font-mono tabular-nums ${
                row.change >= 0 ? "text-emerald-600 dark:text-emerald-400" : "text-red-600 dark:text-red-400"
              }`}
            >
              {row.change >= 0 ? "+" : ""}
              {row.change.toFixed(1)}%
            </td>
          </tr>
        ))}
      </tbody>
    </table>
  );
}

```

Each row passes its own `config`, so one data key can take a rising or falling color. A reference line at the `first` value shows where the period started.

### Loading State [#loading-state]

### isLoading='true'

```tsx
"use client";

import { Sparkline, type ChartConfig } from "honestui/charts";

const chartConfig = {
  sessions: {
    label: "Sessions",
    colors: {
      light: ["#2563eb"],
      dark: ["#60a5fa"],
    },
  },
} satisfies ChartConfig;

export function ExampleSparklineLoading() {
  return (
    <div className="grid w-full max-w-sm gap-4">
      <Sparkline
        data={[]}
        config={chartConfig}
        dataKey="sessions"
        variant="area"
        isLoading
        className="h-12 w-full"
      />
      <Sparkline
        data={[]}
        config={chartConfig}
        dataKey="sessions"
        variant="bar"
        isLoading
        className="h-12 w-full"
      />
    </div>
  );
}

```

A loading sparkline draws a neutral placeholder shape. A loading pill would not fit a chart this small, so the state is announced to assistive technology only.

## API Reference [#api-reference]

<ApiHeading>
  Sparkline
</ApiHeading>


  ### `data`

type: `TData[]`

The ordered points to plot.

  ### `config`

type: `ChartConfig`

The label and theme colors for `dataKey`. Several colors draw a horizontal gradient.

  ### `dataKey`

type: `keyof TData & string`

The numeric value to plot. Empty or non-numeric values leave a gap.

  ### `xDataKey`

type: `keyof TData & string`

Labels each point in the tooltip, such as a date.

  ### `variant`

type: `&#x22;line&#x22; | &#x22;area&#x22; | &#x22;bar&#x22;` · default: `&#x22;line&#x22;`

How the series is drawn.

  ### `curve`

type: `&#x22;linear&#x22; | &#x22;smooth&#x22; | &#x22;step&#x22;` · default: `&#x22;linear&#x22;`

The line interpolation. Ignored by bars.

  ### `strokeWidth`

type: `number` · default: `1`

Line width in pixels.

  ### `min`

type: `number`

Lower bound of the value scale. Lines default to the smallest value; bars to zero.

  ### `max`

type: `number`

Upper bound of the value scale. Defaults to the largest value.

  ### `connectNulls`

type: `boolean` · default: `false`

Draws the line across missing values instead of leaving a gap.

  ### `className`

type: `string`

Classes for the container. Give the chart an explicit width and height.

  ### `animation`

type: `boolean` · default: `true`

Enables the intro animation. Reduced-motion preferences disable it.

  ### `isLoading`

type: `boolean` · default: `false`

Draws a neutral placeholder and announces the loading state.

  ### `ariaLabel`

type: `string`

Accessible name for the chart. A trend summary is generated when omitted.

  ### `chartOptions`



">
    ECharts options merged over the generated option.


<ApiHeading>
  Dot
</ApiHeading>


  ### `points`

type: `&#x22;last&#x22; | &#x22;extremes&#x22; | &#x22;last-and-extremes&#x22; | &#x22;all&#x22;` · default: `&#x22;last&#x22;`

Which points carry a dot. Ignored by bars.

  ### `variant`

type: `&#x22;default&#x22; | &#x22;border&#x22; | &#x22;colored-border&#x22; | &#x22;ping&#x22;` · default: `&#x22;border&#x22;`

The dot style, shared with the Line Chart.


<ApiHeading>
  ReferenceLine
</ApiHeading>


  ### `y`

type: `number | &#x22;average&#x22; | &#x22;first&#x22; | &#x22;min&#x22; | &#x22;max&#x22;` · default: `&#x22;average&#x22;`

A fixed value, or a statistic computed from the data. Add several to draw several lines.


<ApiHeading>
  Tooltip
</ApiHeading>


  ### `variant`

type: `&#x22;default&#x22; | &#x22;frosted-glass&#x22;` · default: `&#x22;default&#x22;`

The tooltip surface.

  ### `roundness`

type: `&#x22;sm&#x22; | &#x22;md&#x22; | &#x22;lg&#x22; | &#x22;xl&#x22;` · default: `&#x22;lg&#x22;`

The tooltip corner radius.

  ### `valueFormatter`



string">
    Formats the value, and the values in the generated accessible name.

  ### `labelFormatter`



string">
    Formats the `xDataKey` heading.
