# Revenue chart

> This year as an area, last year behind it; point at a month and the header becomes the readout, switch the measure and the lines reshape.

A year against the year before is the chart every business dashboard wants, and most bolt a tooltip box onto it. Here the header is the readout: point at a month and a hairline follows you with a dot on each line while the title morphs to "Revenue in August", the total rolls to that month’s figure, the change chip follows and the last-year line beneath it says what August was a year ago; move away and everything rolls back to the year. Switch from Revenue to Orders or Average order and both lines morph into their new shapes while the scale and the figures roll. The first time the card scrolls into view the chart draws itself from left to right, and a year still under way ends its line with a dot. Hand-drawn SVG with a monotone curve, no chart library.

## Install

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

## Usage

```tsx
import { RevenueChart } from "@/components/revenue-chart";

<RevenueChart
  labels={["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}
  metrics={[{ id: "revenue", label: "Revenue", current: thisYear, previous: lastYear, format: { style: "currency", currency: "USD" } }]}
/>
```

## From Stripe

Sum paid invoices by month for this year and last on the server, then pass both series; add orders or average order as extra measures.

```tsx
import { RevenueChart } from "@/components/revenue-chart";
import { stripe } from "@/lib/stripe";

async function monthly(year: number) {
  const totals = Array(12).fill(0);
  for await (const invoice of stripe.invoices.list({ status: "paid", created: { gte: Date.UTC(year, 0) / 1000, lt: Date.UTC(year + 1, 0) / 1000 } })) {
    totals[new Date(invoice.created * 1000).getUTCMonth()] += invoice.amount_paid / 100;
  }
  return totals;
}

export async function RevenueCard() {
  const year = new Date().getUTCFullYear();
  const [current, previous] = await Promise.all([monthly(year), monthly(year - 1)]);
  return (
    <RevenueChart
      labels={["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]}
      series={[String(year), String(year - 1)]}
      metrics={[{ id: "revenue", label: "Revenue", current: current.slice(0, new Date().getUTCMonth() + 1), previous, format: { style: "currency", currency: "USD", maximumFractionDigits: 0 } }]}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `labels` | `string[]` | – | Along the bottom, one per point; they thin out on narrow screens. |
| `titles` | `string[]` | – | Longer names for the readout, e.g. "August". |
| `metrics` | `RevenueMetric[]` | – | { id, label, current, previous?, format?, total? ("sum" or "average") }. More than one shows a switch. |
| `value / defaultValue` | `string` | `the first measure` | The measure on show, by id; controlled or not. |
| `onValueChange` | `(id: string) => void` | – | Called when the measure changes. |
| `series` | `[string, string]` | `["This year", "Last year"]` | The two lines’ names, in the legend and the readout. |
| `locales` | `string \| string[]` | – | For number formatting. |

## States

- **arrive**: In view for the first time, everything drawn is revealed from left to right (1200ms) and the total rolls up from zero.
- **hover**: A hairline glides to the nearest month (260ms, slight overshoot) with a dot on each line. The title morphs to "<measure> in <month>", the total rolls to the month, the chip shows its change on last year and the line below morphs to last year’s figure for it. Leaving rolls it all back.
- **measure**: With more than one measure, the pill is thrown to the choice (460ms); both lines and the area tween to their new values (640ms) while the scale labels and figures roll.
- **partial**: When this year has fewer values than labels, its line ends with a dot at the latest month and the comparison uses last year’s same months.

## What it handles

A year against the year before is the chart every business dashboard wants, and most bolt a tooltip box onto it. Here the header itself is the readout.

- **This year against last**: This year is a solid line over a soft area, last year a dashed grey line behind it.
- **The header is the readout**: Point at a month and the title, the total, the change chip and last year’s figure all roll to that month. Leaving rolls them back.
- **Switch the measure**: Revenue, Orders or Average order: both lines morph to their new shapes while the scale and figures roll.
- **A year still in progress**: This year’s line ends with a dot at the latest month, and the comparison uses last year’s same months.
- **It draws itself in**: The first time the card is in view the chart is revealed from left to right.
- **Not colour alone**: Solid and dashed lines tell the years apart, and every month is in a hidden table with both years.

## Replaces

a chart library, a year-on-year comparison, a tooltip, a measure switch, change-against-last-year maths

## Where it fits

- **SaaS and finance dashboards**: This year against the year before, month by month.
- **Stripe revenue reports**: Paid invoices by month, with last year behind them.
- **Shop dashboards**: Switch between revenue, orders and average order value.
- **Board and investor reports**: A year-on-year chart that answers questions when someone points at a month.

## 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 plot is one tab stop: arrow keys, Home and End move between months, the header follows, and the month with both figures is announced politely; Escape returns to the year.
- The two lines differ by more than colour: this year is solid with a wash beneath, last year is a dashed grey line, and the legend draws both keys.
- Every month is in a visually hidden table with both years, and the totals in its caption.
- The line uses --chart-1 (your primary if the theme has no chart colours); gridlines are solid hairlines.
- Installs Number roll and Text morph. With reduced motion the chart is simply there and changes happen in place.

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