# Earnings chart

> A bar chart that changes instead of redrawing: bars glide between periods, the scale and the total roll, a readout follows the pointer.

Most chart cards throw the whole chart away when you switch from six months to a year and draw a new one. Here the bars keep who they are: the months both periods share glide to their new place and height, the ones that leave fold away on their side and the new ones grow in beside them, while the scale’s labels and the total roll to their new figures and the change chip turns emerald or red with its arrow. The first time it scrolls into view the bars grow out of the baseline one after another. Point at the chart and a hairline column follows you from bar to bar, with a readout riding its top whose month morphs and whose amount rolls. No chart library: one file of plain elements that takes on your theme.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/earnings-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/earnings-chart
- Registry item (source): https://hairlineui.com/r/earnings-chart.json

## Usage

```tsx
import { EarningsChart } from "@/components/earnings-chart";

<EarningsChart
  title="Earned so far"
  periods={[
    { id: "6m", label: "6M", bars: lastSixMonths, previous: 46120, comparison: "vs May to Oct last year" },
    { id: "1y", label: "1Y", bars: lastTwelveMonths, previous: 61940, comparison: "vs the year before" },
  ]}
  defaultValue="1y"
/>
```

## From your database

Group revenue by month on the server and give every month the same key in every period, so the months two periods share glide between them.

```tsx
import { EarningsChart, type EarningsPeriod } from "@/components/earnings-chart";
import { db } from "@/lib/db";

export async function EarningsCard() {
  const months = await db.revenueByMonth({ months: 12 }); // [{ month: "2026-08", total: 11210 }, …]
  const bars = months.map((m) => ({
    key: m.month,
    label: new Date(m.month).toLocaleDateString("en-GB", { month: "short" }),
    title: new Date(m.month).toLocaleDateString("en-GB", { month: "long", year: "numeric" }),
    value: m.total,
  }));
  const periods: EarningsPeriod[] = [
    { id: "6m", label: "6M", bars: bars.slice(-6), previous: await db.revenueBetween("-18m", "-12m") },
    { id: "1y", label: "1Y", bars, previous: await db.revenueBetween("-24m", "-12m") },
  ];
  return <EarningsChart title="Earned so far" periods={periods} defaultValue="1y" />;
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `periods` | `EarningsPeriod[]` | – | Each with id, label (for the switch), bars ({ key, label, value, title? }) and optionally previous (the total before, for the chip) and comparison. |
| `value / defaultValue` | `string` | `the last period` | The period on show, by id; controlled or not. |
| `onValueChange` | `(id: string) => void` | – | Called when the period changes. |
| `title` | `string` | `"Earnings"` | Above the total. |
| `format` | `Intl.NumberFormatOptions` | `USD, no decimals` | How amounts read; the scale uses the same with compact notation. |
| `highlight` | `string` | `the last bar` | The bar drawn in your primary colour, by key. |
| `locales` | `string \| string[]` | – | For number formatting. |

## States

- **arrive**: In view for the first time, each bar grows from the baseline (640ms, 28ms apart) and the total rolls up from zero (1000ms).
- **period**: The pill is thrown to the choice (460ms). Bars with the same key in both periods glide to their new place and width (560ms) and ease to their new height; bars that leave fold to nothing on the side they were on; new bars grow in. The scale labels, total and change roll.
- **hover**: A hairline column glides to the bar under the pointer (380ms, slight overshoot), the other bars dim, and the readout at its top slides along with the month morphing and the amount rolling. On touch, tap or drag across the bars.
- **change**: The change against the period before is a chip, emerald with an up arrow or red with the arrow turned down. A period with nothing to compare folds the chip away.

## What it handles

Most chart cards throw the whole chart away when you switch periods and draw a new one. This keeps each bar as itself, so people can follow what changed instead of starting over.

- **Switch period, keep the bars**: Months both periods share glide to their new place and height. Months that leave fold away and new ones grow in.
- **Totals that roll**: The total and the scale labels roll to their new figures instead of changing at once.
- **A change chip**: The change against the period before is emerald with an up arrow, or red with a down arrow. It folds away when there is nothing to compare.
- **A readout that follows the pointer**: A hairline column and a readout ride from bar to bar. On touch, tap or drag across the bars.
- **Highlight the bar that matters**: One bar is drawn in your primary colour, the last by default, or any bar by key.
- **Accessible by default**: The period switch is a radio group, arrow keys move the readout, and every figure is in a visually hidden table.

## Replaces

a chart library, a period switch, axis scale labels, a hover readout, a change-since-last-period chip

## Where it fits

- **Revenue dashboards**: Monthly revenue that switches between six months and a year.
- **Creator and seller earnings**: The earnings page of a marketplace, a course platform or a payout tool.
- **Affiliate and partner portals**: Show partners what they have earned this period and the one before.
- **Customer reports**: A results chart in a monthly summary for clients.

## Notes

- The period switch is a radio group (arrow keys move). The plot is one tab stop: arrow keys, Home and End move the readout between bars and it is announced politely; Escape hides it.
- Every bar’s figure is in a visually hidden table, with the total in its caption.
- Green and red are only used for the change, and always with an arrow that points the same way.
- Bars, lane and readout are divs moved by left, width and height on a dozen elements; nothing animates while it’s off screen.
- Installs Number roll and Text morph. With reduced motion every change happens in place.

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