# Area chart

> Several series stacked, overlapping or as shares: every band morphs between layouts, and the tiles are the legend, the switches and the readout.

A multi-series area chart is usually three charts behind a dropdown. Here it is one surface that changes shape: switch from Stacked to Overlap or 100% and every band morphs to its new place while the scale rolls between counts and percentages. The tiles under the chart are the legend, and they’re buttons: press one and its band shrinks away while the others settle into the room, press it again and it grows back, and every colour stays with its series. Point at the chart and a hairline follows you with a dot on each band while the tiles turn into the readout, the period morphing to that week and every figure and share rolling to it. The first time the card scrolls into view the chart draws itself from left to right.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/area-chart.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `number-roll`, `text-morph`
- Docs and live preview: https://hairlineui.com/ui/area-chart
- Registry item (source): https://hairlineui.com/r/area-chart.json

## Usage

```tsx
import { AreaChart } from "@/components/area-chart";

<AreaChart
  title="Orders"
  caption="in the last 12 weeks"
  labels={weeks}
  series={[
    { id: "counter", label: "Counter", values: counter },
    { id: "app", label: "App", values: app },
    { id: "delivery", label: "Delivery", values: delivery },
  ]}
/>
```

## From your analytics

Group events by week and channel on the server. Keep the series in a fixed order so each keeps its colour.

```tsx
import { AreaChart } from "@/components/area-chart";
import { db } from "@/lib/db";

const CHANNELS = [
  { id: "counter", label: "Counter" },
  { id: "app", label: "App" },
  { id: "delivery", label: "Delivery" },
];

export async function OrdersByChannel() {
  const rows = await db.ordersByWeekAndChannel({ weeks: 12 }); // [{ week: "2026-10-05", channel: "app", count: 478 }, …]
  const weeks = [...new Set(rows.map((r) => r.week))];
  return (
    <AreaChart
      title="Orders"
      caption="in the last 12 weeks"
      labels={weeks.map((w) => new Date(w).toLocaleDateString("en-GB", { day: "numeric", month: "short" }))}
      series={CHANNELS.map((c) => ({ ...c, values: weeks.map((w) => rows.find((r) => r.week === w && r.channel === c.id)?.count ?? 0) }))}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `series` | `AreaSeries[]` | – | Up to five, each { id, label, values }, coloured --chart-1 to --chart-5 in this order. |
| `labels` | `string[]` | – | Along the bottom, one per point; they thin out on narrow screens. |
| `titles` | `string[]` | – | Longer names for the readout, e.g. "Week of 28 September". |
| `layout / defaultLayout` | `"stacked" \| "overlap" \| "percent"` | `"stacked"` | Controlled or not. |
| `onLayoutChange` | `(layout) => void` | – | Called when the layout changes. |
| `title` | `string` | `"Orders"` | Above the total. |
| `caption` | `string` | – | Under the total, e.g. "in the last 12 weeks". |
| `format` | `Intl.NumberFormatOptions` | – | How the figures read. |
| `locales` | `string \| string[]` | – | For number formatting. |

## States

- **arrive**: In view for the first time, the bands are revealed from left to right (1200ms) and every figure rolls up from zero.
- **layout**: The pill is thrown to Stacked, Overlap or 100% (460ms); every band’s top and bottom tween to the new layout (620ms) and the scale rolls to counts or percentages.
- **toggle**: A tile is a toggle button: its band fades and collapses to nothing while the others restack, its swatch empties and its figures roll to zero; pressing again reverses it. The last series showing can’t be hidden.
- **hover**: A hairline glides to the nearest point (260ms, slight overshoot) with a dot on each band; the caption morphs to that point’s name and the total, the tiles’ figures and shares roll to it.

## What it handles

A multi-series area chart is usually a chart library config plus a custom legend, a layout dropdown and a tooltip. This is one card that does all four, without a chart library.

- **Three layouts, one chart**: Stacked, Overlap or 100%: the bands morph to their new places and the scale rolls between counts and percentages.
- **A legend you can press**: Each tile hides or shows its series while the others settle into the room. The last one showing can’t be hidden.
- **A readout on hover**: A hairline follows the pointer and the tiles turn into that point’s figures and shares.
- **It draws itself in**: The first time the card is in view the bands are revealed from left to right.
- **Keyboard and table**: The plot is one tab stop with arrow keys, and every value is also in a visually hidden table.
- **Colours from your theme**: Up to five series take --chart-1 to --chart-5 in order and keep them when another is hidden.

## Replaces

a chart library, a custom legend, a layout dropdown, a hover tooltip, axis and scale maths

## Where it fits

- **Analytics dashboards**: Traffic, signups or orders by channel over the last twelve weeks.
- **Revenue by plan**: See how the mix changes over time, or switch to 100% to see shares.
- **Usage by feature or model**: Show which part of the product or which model drives the growth.
- **Weekly reports**: A chart, legend and readout in one card inside an email-style summary page.

## Notes

- Grey in a new shadcn project? Its default theme sets --chart-1 to --chart-5 to greys. Set five colours, light and dark, as in Theming › Chart colours (/ui/theming#charts).
- The layout switch is a radio group; the tiles are toggle buttons (aria-pressed) and the last visible series can’t be hidden.
- The plot is one tab stop: arrow keys, Home and End move between points and every visible series is announced.
- Identity never rests on colour alone: each tile pairs its swatch with the series name, and every value is in a visually hidden table.
- Colours come from the theme’s --chart-1 to --chart-5 and follow the series, not its rank, so hiding one never repaints the others.
- Installs Number roll and Text morph. With reduced motion every change happens in place.

License: MIT (https://hairlineui.com/license#components)
