<!-- generated by scripts/gen-docs.mjs from docs/system/waterfall-chart.md. Edit the source and rerun. -->
# WaterfallChart

## Description
Shows how a total moved from one amount to another, step by step, so a person reads what added to it, what took from it and where it ended.

`import { WaterfallChart } from "@/components/cobalt/ui/waterfall-chart"`, source `registry/cobalt/ui/waterfall-chart.tsx`, status `ready-with-gaps`.
Foundation: `hand-rolled`. Traps checked: `trap/loading-layout-shift`, `trap/color-only-status`, `trap/live-region-double`, `trap/reduced-motion-ignored`, `trap/motion-transition-all`, `trap/motion-layout-property`, `trap/numbers-shift`, `trap/tooltip-essential`, `trap/tooltip-interactive`, `trap/touch-hover-flash`, `trap/hover-unguarded` (n/a: no CSS hover reveals anything; the overlay follows the hover store), `trap/demo-on-timers` (n/a: every example holds fixed data), `trap/illustration-announced` (n/a: the marks are `aria-hidden` and the table carries the data), `trap/loop-offscreen` (n/a: nothing loops).

### Foundation
Not applicable: hand-rolled.

| Given by the foundation | Added by the team |
|---|---|
| `d3-scale` band and linear scales for the maths only | The chart type on the shared Chart frame (fixed box, hover store, tooltip, keyboard, states, summary and table): each step of kind `start`, `change` or `total` drawn as a bar that floats from the running total, a dashed connector between a bar and the next change, a rise in `--good` and a fall in `--bad` (swapped by `positive`) with the signed amount printed above or below the bar when every one fits, the amounts in one unit sharing their decimals through `labelFormatter`, every step named under its bar, on two lines at a space where one does not fit, and, when a column cannot carry its whole name and its amount, the same bridge turned to run across, a row per step with its name before it and its amount past the end it moved to (before a fall's bar when there is room), as long as each row has 18px, so every step keeps its name and its amount at any width, totals standing on zero in `--chart-1`, a no-change step in `--chart-previous`, the running total kept in the tooltip and the table, and one-pixel columns once a bar is narrower than 1.5px |

## Examples
How assets moved from the start of the quarter to its end, step by step.

Real uses, 0 call sites (`rg -n "<WaterfallChart\b" app docs/system/examples --glob '!docs/system/examples/waterfall-chart/*'`): no showcase page draws one outside its examples yet, and the examples folder does not count.

Uses inside the registry: none beyond its tests. The in-card.tsx example puts it in a `ChartCard` with the closing figure as the headline.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/waterfall-chart/default.tsx` | default | How assets moved from the start of the quarter to its end, step by step |
| Not applicable: up is the default, the rises in good colour in default.tsx | positive=up | A rise is good news |
| `docs/system/examples/waterfall-chart/positive-down.tsx` | positive=down | Costs, where a fall is good news: cuts are in the good colour and rises in the bad one |
| `docs/system/examples/waterfall-chart/in-card.tsx` | composition:ChartCard | The quarter's bridge in a chart card, with the closing figure as the headline |

Example: How assets moved from the start of the quarter to its end, step by step (`docs/system/examples/waterfall-chart/default.tsx`)

```tsx
import { WaterfallChart } from "@/components/cobalt/ui/waterfall-chart";

// How a fund's assets moved over the quarter, in dollars.
const steps = [
  { step: "Jun 30", amount: 4.82e9, kind: "start" as const },
  { step: "Inflows", amount: 6.1e8, kind: "change" as const },
  { step: "Outflows", amount: -3.4e8, kind: "change" as const },
  { step: "Market moves", amount: 2.2e8, kind: "change" as const },
  { step: "Fees", amount: -4.1e7, kind: "change" as const },
  { step: "Sep 30", amount: null, kind: "total" as const },
];

export default function Example() {
  return <WaterfallChart label="How assets moved" data={steps} x="step" value="amount" kind="kind" format="money" />;
}
```

Example: The quarter's bridge in a chart card, with the closing figure as the headline (`docs/system/examples/waterfall-chart/in-card.tsx`)

```tsx
import { ChartCard } from "@/components/cobalt/ui/chart-card";
import { WaterfallChart } from "@/components/cobalt/ui/waterfall-chart";
import { compactMoney } from "@/lib/cobalt/format";

// How a fund's assets moved over the quarter, in dollars.
const steps = [
  { step: "Jun 30", amount: 4.82e9, kind: "start" as const },
  { step: "Inflows", amount: 6.1e8, kind: "change" as const },
  { step: "Outflows", amount: -3.4e8, kind: "change" as const },
  { step: "Market moves", amount: 2.2e8, kind: "change" as const },
  { step: "Fees", amount: -4.1e7, kind: "change" as const },
  { step: "Sep 30", amount: null, kind: "total" as const },
];

// The headline is read from the steps: where the quarter started and where it ended.
const opening = steps[0]!.amount ?? 0;
const closing = steps.reduce((sum, step) => (step.kind === "change" ? sum + (step.amount ?? 0) : sum), opening);

export default function Example() {
  return (
    <ChartCard className="w-full max-w-xl" title="Gross assets" value={compactMoney(closing)}
      change={((closing - opening) / opening) * 100}
      note={`Jun 30 to Sep 30, from ${compactMoney(opening)}`}>
      <WaterfallChart label="How assets moved" data={steps} x="step" value="amount" kind="kind" format="money" />
    </ChartCard>
  );
}
```

## Variants

### positive
- `up`: a rise is good news, the default, for assets, revenue and flows in. Used at 0 call sites.
- `down`: a fall is good news, for costs, fees and outflows to be cut. Used at 0 call sites.

Example: Costs, where a fall is good news: cuts are in the good colour and rises in the bad one (`docs/system/examples/waterfall-chart/positive-down.tsx`)

```tsx
import { WaterfallChart } from "@/components/cobalt/ui/waterfall-chart";

// What a fund paid in costs this year against last, in dollars.
const steps = [
  { step: "Last year", amount: 1.24e7, kind: "start" as const },
  { step: "Custody", amount: -8.0e5, kind: "change" as const },
  { step: "Audit", amount: 3.5e5, kind: "change" as const },
  { step: "Data", amount: 1.1e6, kind: "change" as const },
  { step: "Legal", amount: -1.6e6, kind: "change" as const },
  { step: "This year", amount: null, kind: "total" as const },
];

export default function Example() {
  return (
    <WaterfallChart label="How costs moved" positive="down" data={steps} x="step" value="amount" kind="kind" format="money" />
  );
}
```

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| idle | data with at least one value that is not zero | read the bars, hover, tab in | the bars, the axes, the amounts printed on the bars when they all fit | screenshot |
| hover | the pointer over the box, snapped to the nearest point or group | read the tooltip, move along | the tooltip 6px above the plot and the figures printed over its steps, centred on the step, so it never covers a taller step or its figure, and the other bars dim to 40% | screenshot |
| focus | Tab reaches the box | step with the arrow keys | the system focus outline on the box, the last point shown, the live line read aloud | by hand |
| across | a column too narrow for its step's name, on two lines, or for its amount, and each row would have 18px or more | read every name and amount, hover, tab in | a row per step: the name before it, the amount past the end it moved to, dashed connectors down between rows, the value axis along the bottom; the tooltip beside the rows it would cover, or above the plot | screenshot |
| crowded | each bar would be narrower than 1.5px | read the tooltip, move along | the steps merged into one-pixel columns by direction; no printed amounts and no dimming | screenshot |
| no change | a `change` step whose amount is 0 | read it in the tooltip and the table | a grey bar one pixel high and the amount written as 0 | test |
| pending | `status="pending"` with nothing drawn yet | wait | `aria-busy`; nothing for 250ms, then a Skeleton block at the box's size | test |
| refreshing | `status="pending"` with marks on screen | read the old marks | `aria-busy`; after 250ms the marks dim to 55% and the hover stops; the new marks fade in when they arrive | by hand |
| empty | no value, or every value zero, and not pending | read why | the sentence "Nothing to show for this period." centred in the box | test |
| failure | `status="failure"` | press Retry | the title "Could not load the chart" announced once as an alert, and Retry when `onRetry` is passed | test |

### State precedence
- Failure and pending: failure wins. The box shows the failure and its Retry.
- Pending and empty: pending wins. The box stays blank, then shows the Skeleton after the slow wait; the empty sentence never flashes.
- Hover and focus: the last one wins. The pointer and the keys move the same marker, so one point shows at a time.
- Crowded and hover: crowded wins on the dimming. The crosshair and tooltip show, and the other bars no longer dim, because they are merged into columns of one pixel.
- Across and crowded: across wins while each row has 18px; past that the columns stay, with their names thinned, and crowd.
- Crowded and printed values: crowded wins. No amounts are printed on the bars; the tooltip and the table carry them.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| the crosshair, markers and tooltip appear or leave | announce | `popup-motion` at `--t-press` 140ms on `--curve`, leaving at 70% | opacity | the same fade, which the base layer keeps |
| the hovered point changes | input | none: snaps with no transition | transform | the same snap |
| a bar dims beside the hovered one | announce | `duration-press` on `--curve` | opacity | the same fade |

## Props

<!-- Props generated from registry/cobalt/ui/waterfall-chart.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `data` | `readonly T[]` |  |  |
| `label` | `string` |  |  |
| `value` | `Accessor<T, number>` |  | The change, or the amount for a "start" step. |
| `x` | `Accessor<T, string>` |  | The step's name under its bar: "Inflows", "Market moves". |
| `active?` | `number` |  |  |
| `className?` | `string` |  |  |
| `format?` | `ValueFormat` | `"number"` |  |
| `height?` | `ChartHeight` |  |  |
| `kind?` | `Accessor<T, WaterfallKind>` |  | "start" sets the amount, "change" moves it, "total" shows where it stands. |
| `labels?` | `Partial<ChartLabels>` |  |  |
| `layers?` | `((scope: ChartScope) => ReactNode)` |  | Your own SVG over the steps: `x` reads a step's name or its index, `y` reads amounts. |
| `locale?` | `string` |  |  |
| `onActiveChange?` | `((index: number) => void)` |  |  |
| `onRetry?` | `(() => void)` |  |  |
| `positive?` | `"up" \| "down"` | `"up"` | Which direction is good news: a rise in assets is, a rise in costs is not. |
| `status?` | `Status` |  |  |
| `summary?` | `string` |  | Your own sentence for screen readers; one is written from the data otherwise. |
| `tooltip?` | `boolean` |  |  |
| `words?` | `{ change: string; total: string; }` | `WORDS` | The words for the tooltip rows and the table's columns. |

`x` reads a step's name, `value` its amount, and `kind` its kind: `start` sets the amount, `change` moves it and `total` shows where it stands and ignores its `value`. Without `kind` every row is a `change` from zero. `positive` is `up` or `down`. `format` sets how amounts read. `words` replaces the tooltip rows and the table columns, `{ change: "Change", total: "Running total" }` by default. `layers` draws your own SVG over the steps: `x` reads a step's name or its index and `y` reads amounts. Everything else comes from the shared frame: `label`, `summary`, `status`, `onRetry`, `height`, `labels`, `tooltip`, `active`, `onActiveChange` and `locale`; `Chart` documents them. It has no legend and takes no `series`.

## Usage

### When to use
- A total moves from one amount to another and the parts that moved it matter, such as assets from the start of a quarter to its end through inflows, outflows and market moves.
- Costs or fees between two periods, where the question is which line grew and which was cut.

### When not to use
- Amounts per period or group are compared by length. Use BarChart instead.
- The values are a level over time. Use LineChart instead.
- Parts of one total over time. Use AreaChart instead.
- One figure and its change, with no steps. Use Stat instead.

### Rules
- `rule/waterfall-chart-positive-names-good-news`: When a rise is bad news, such as costs or fees, set `positive="down"` instead of leaving the default, because a rise draws in the good colour and a fall in the bad one by default, and a cost that grew would read as good. Evidence: person D13; principle wcag: 1.4.1 Use of color; single use registry/cobalt/ui/waterfall-chart.tsx:172. Check: review of every `WaterfallChart` whose steps are costs, for `positive="down"`.
  - Don't: `<WaterfallChart label="How costs moved" data={steps} x="step" value="amount" kind="kind" format="money" />`
  - Do: `<WaterfallChart label="How costs moved" positive="down" data={steps} x="step" value="amount" kind="kind" format="money" />`
- `rule/waterfall-chart-total-is-a-kind`: When the bridge ends at a closing figure, add a last row with `kind: "total"` and no amount instead of typing the sum yourself, because the chart sums the steps, so the end can never disagree with the steps above it. Evidence: principle heuristic: error prevention; single use docs/system/examples/waterfall-chart/positive-down.tsx:11. Check: test `waterfall-chart.test.tsx` expects a total row to show the sum, a running total of −20 below zero.
  - Don't: `{ step: "This year", amount: 1.19e7, kind: "start" }`
  - Do: `{ step: "This year", amount: null, kind: "total" }`
- `rule/waterfall-chart-steps-in-order`: When the steps are listed, pass the rows to `WaterfallChart` in the order the total moves, the start first and the total last, instead of sorting them by size, because the chart keeps the order of `data` and each running total is the sum of the rows above it. Evidence: principle heuristic: match between system and real world; single use registry/cobalt/ui/waterfall-chart.tsx:102-114. Check: test `waterfall-chart.test.tsx` expects the running totals 130 and −20 in the table in the order given.
  - Don't: `data={[...steps].sort((a, b) => b.amount - a.amount)}`
  - Do: `data={steps}`

### Content
- Follows `rule/writing-no-invented-numbers`.
- Follows `rule/chart-label-names-the-data`.
- `rule/waterfall-chart-step-names-the-flow`: When a step is named in `x`, write the flow, such as "Inflows" or "Market moves", and name the ends by their date, such as "Jun 30", instead of "Step 2", because the name sits under the bar, titles the tooltip and starts the clause "Inflows +30" in the sentence. Evidence: principle wcag: 1.1.1 Non-text content; principle heuristic: match between system and real world. Check: test `waterfall-chart.test.tsx` expects "Inflows +30, Outflows −150" in the sentence.
  - Don't: `{ step: "Step 2", amount: 30, kind: "change" }`
  - Do: `{ step: "Inflows", amount: 30, kind: "change" }`
- `rule/waterfall-chart-summary-past-twelve`: When a bridge holds more than 12 changes, pass your own `summary` that tells what the bridge shows instead of keeping the written one, because past 12 changes the sentence names only the largest rise and the largest fall. Evidence: single use registry/cobalt/ui/waterfall-chart.tsx:131-136; principle wcag: 1.1.1 Non-text content. Check: review of every `WaterfallChart` with more than 12 change rows, for a `summary`.
  - Don't: `<WaterfallChart label="How assets moved" data={fifteenSteps} x="step" value="amount" kind="kind" />`
  - Do: `<WaterfallChart label="How assets moved" summary={story} data={fifteenSteps} x="step" value="amount" kind="kind" />`

### Anti-slop
- `rule/waterfall-chart-not-a-stacked-bar`: When a bridge is needed, use `WaterfallChart` instead of a stacked `BarChart` with an invisible base series, because every series of a `BarChart` is a legend entry, a tooltip row and a table column, so the hidden base would be read out as data. Evidence: principle wcag: 1.3.1 Info and Relationships. Check: review of every `BarChart` with `layout="stacked"` for a series drawn transparent.
  - Don't: `<BarChart layout="stacked" series={[invisibleBase, rise, fall]} data={steps} x="step" label="How assets moved" />`
  - Do: `<WaterfallChart data={steps} x="step" value="amount" kind="kind" label="How assets moved" />`

### Limits
Not applicable: no measured limit. The hover-cost run measured one huge bridge, median 3.7ms and layout shift 0 in .design-system/evidence/charts/hover-cost.jsonl, but no count of steps past which a bridge stops being readable; the 12 changes the sentence names are a rule above, not a measured limit.

## Accessibility
Rests on the Chart frame: a focusable `div` with `role="group"` and `aria-roledescription="chart"`, one tab stop. The bars are `aria-hidden`; the data is a visually hidden sentence (`aria-describedby`) that says where the total started, where it ended and each change with its sign, and a visually hidden `table` with a row per step and the columns of the step name, the change and the running total, negatives written with the real minus, sampled to 200 evenly spaced rows with a note past that. Rise and fall are never colour alone: the sign sits on the bar when it fits, in the tooltip and in the table. Screen reader pass with VoiceOver: NEEDS REVIEW.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the chart | focuses the box and shows the last point | the box | by hand |
| Right, Left | the box | the next or previous point | the box | by hand |
| Home, End | the box | the first or the last point | the box | by hand |
| Page Down, Page Up | the box | a tenth of the points on or back, at least 2 | the box | by hand |
| Escape | the box, or the pointer over it | hides the crosshair and tooltip | stays | by hand |
| Tab | the box | leaves the chart; the legend buttons come next | the next control | by hand |

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| box | group | `label`, through `aria-label`; `aria-roledescription` from `labels.chart` | `aria-describedby` the summary; `aria-busy` while pending | the name, "chart" and the summary when it is reached | test |
| live line | none, `aria-live="polite"` | nothing | none | the point and each series' value as the keys move; pointer moves are silent | by hand |
| table | table | its caption, the `label` | a row header per point | read on request from the table list | test |
| legend item | button | its series name | `aria-pressed` | the name and pressed or not | test |
| failure | alert | its title | none | "Could not load the chart" once | test |

## Tokens
| Part | State | Token |
|---|---|---|
| start and total bars | all | `--chart-1` |
| rise or fall in the good direction | all | `--good` |
| rise or fall in the bad direction | all | `--bad` |
| step with no change | all | `--chart-previous` |
| connector | all | `--muted-foreground` at 50% |
| gridlines | all | `--chart-grid` |
| zero line | all | `--border` |
| axis labels, printed amounts | all | `--muted-foreground` |
| crosshair | hover | `--foreground` at 15% |
| marker ring | hover | `--card` |
| tooltip | hover | `--tip`, `--tip-foreground`, `--r-md`, `--t-press`, `--curve` |

## Related
- Chart: the frame every chart type draws in, with the shared states, keys and table.
- BarChart: amounts per period or group compared by length.
- AreaChart: parts of one total over time.
- Stat: one figure and its change, with no steps.
- ChartCard: a card with the chart's title and headline figure.
