Recharts Waterfall Chart: Why the Transparent-Bar Trick Breaks Below Zero
Recharts has no waterfall chart. The request was closed without one (recharts#7010), and the person asking summed up the usual workaround in one line: it "breaks with negative values". This article shows exactly when it breaks, with a test you can rerun, and how to build a waterfall that doesn't.
The workaround everyone uses
A waterfall (or bridge) chart walks from one total to another: start, then each driver up or down, then the end total. Recharts can't float a bar, so the common recipe stacks two bars:
- An invisible base bar that lifts the visible bar to the right height.
- A visible delta bar on top of it with the size of the step.
<BarChart data={rows}>
<XAxis dataKey="label" />
<YAxis />
<Bar dataKey="base" stackId="w" fill="transparent" />
<Bar dataKey="delta" stackId="w" fill="#111" />
</BarChart>There are two ways to fill base and delta for each step that goes from running total a to b:
- min base:
base = Math.min(a, b),delta = Math.abs(b - a) - signed:
base = a,delta = b - a
What we tested
We rendered both recipes with Recharts 3.11.2 in Chromium, with a bridge that crosses zero: Start 50, Refunds −80 (to −30), Upsell +20 (to −10). The correct drawing is Start from 0 to 50, Refunds from 50 down to −30, Upsell from −30 up to −10.
| Recipe | stackOffset |
Result |
|---|---|---|
| min base | default ("none") |
Correct |
| signed | default ("none") |
Correct |
| min base | "sign" |
Wrong. Refunds is drawn from 0 up to 80; Upsell from 0 up to 20 |
| signed | "sign" |
Wrong. Refunds hangs from 0 down to −80; Upsell from 0 up to 20 |
So the trick doesn't break below zero by itself. It breaks as soon as stackOffset="sign" is set. That's the setting you add to make negative values in a normal stacked bar chart hang below the axis, so it's often already there when someone reuses a stacked bar component. With "sign", Recharts stacks positive and negative values separately from zero, which is right for a stacked bar and wrong for a waterfall: the invisible base and the visible step end up in different stacks.
The problems that remain even when it renders
Getting the bars in the right place is only the first issue:
- The tooltip shows the invisible bar. Hovering a step lists
baseas well asdelta, unless you write a custom tooltip that filters it out. - Colors don't know the direction. With the min-base recipe every
deltais positive, so you need a<Cell>per bar to color increases and decreases, and another rule if decreases are good (a cost bridge). - Totals are just more bars. Subtotals ("Gross profit", "Actual") have to be computed by hand. If one is typed in and doesn't match the steps, the chart draws it anyway.
- No connectors. The thin lines from the end of one bar to the start of the next, which make a bridge readable, need a custom shape.
- Labels sit in the wrong place. Value labels attach to the stack, so negative steps put their label at the wrong end.
How to build one that doesn't break
Stop stacking. A waterfall bar is a range: it starts at the running total before the step and ends at the running total after it. Compute the ranges first, then draw each bar from min(start, end) to max(start, end) on one linear scale:
type Step = { label: string; value?: number; kind?: "total" | "step" | "subtotal" };
function bridge(steps: Step[]) {
let run = 0;
return steps.map((s) => {
if (s.kind === "total") { run = s.value ?? 0; return { ...s, from: 0, to: run }; }
if (s.kind === "subtotal") return { ...s, from: 0, to: run }; // computed, so it always closes
const from = run;
run += s.value ?? 0;
return { ...s, from, to: run };
});
}Each bar's direction is to - from, so colors, labels and connectors all come from the same numbers. Subtotals are computed rather than typed, so the arithmetic can't drift. In Recharts you can approximate this with a range bar (dataKey returning [low, high]), which avoids the stacking problem entirely.
The ChartslibraryUI waterfall
The waterfall chart is built on ranges from the start:
- Steps, totals and computed subtotals from one list:
{ label, value, kind }. - Bars cross zero correctly, with the zero line drawn.
- Colors follow business meaning: set
favorable="down"for a cost bridge and decreases turn green. - Connector lines between bars, a table view and CSV export.
- A warning on the chart if a subtotal you pass doesn't match the steps.
It's part of Pro. If you're comparing approaches for a finance dashboard, the variance chart guide and budget vs actual guide cover the charts that usually sit next to a bridge.
Rerun the test
The test is small: render both recipes with both stackOffset values and the data above, then compare each bar's top and bottom with the expected running totals. Recharts 3 doesn't draw bars in a server render, so run it in a browser (we used Playwright with Chromium).