components/tanstack-charts/bar/waterfall.tsx
import { type ChartPoint, barY, defineChart, ruleY } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { scaleBand, scaleLinear } from "d3-scale";
import { TrendingDown } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface WaterfallItemDatum {
step: string;
start: number;
end: number;
kind: "increase" | "decrease" | "total";
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): WaterfallItemDatum[] {
return [
{ step: "Start Revenue", start: 0, end: 100, kind: "increase" },
{ step: "New Sales", start: 100, end: 145, kind: "increase" },
{ step: "Churn", start: 145, end: 125, kind: "decrease" },
{ step: "Discounts", start: 125, end: 110, kind: "decrease" },
{ step: "Upsells", start: 110, end: 140, kind: "increase" },
{ step: "Net Total", start: 0, end: 140, kind: "total" },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as WaterfallItemDatum;
if (!d) return { title: "", rows: [] };
const diff = d.end - d.start;
const isTotal = d.kind === "total";
return {
title: d.step,
rows: [
{
label: isTotal ? "Total Value" : "Variance",
value: isTotal ? `$${d.end}k` : `${diff > 0 ? "+" : ""}$${diff}k`,
color: points[0].color || "var(--chart-1)",
},
],
};
}
export function WaterfallChart() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
barY(data, {
id: "waterfall-bars",
x: "step",
y1: "start",
y2: "end",
color: "kind",
inset: 1.5,
radius: 3,
}),
ruleY([0], { stroke: "var(--border)" }),
],
scales: {
x: {
scale: () => scaleBand<string>().padding(0.2),
axis: {
line: false,
ticks: { size: 0, padding: 10 },
},
},
y: {
scale: scaleLinear().domain([0, 170]),
grid: true,
axis: {
line: false,
label: "Revenue ($k)",
ticks: { size: 0 },
},
},
},
color: {
domain: ["increase", "decrease", "total"],
range: ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)"],
},
margin: { top: 24, right: 24, bottom: 32, left: 45 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
focus: "group-x",
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "group-center",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Revenue Waterfall Bridge</CardTitle>
<CardDescription>Sequential waterfall bridge analyzing net revenue variance drivers</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Revenue Waterfall Bridge Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Net ending revenue expanded +$40k over fiscal period{" "}
<TrendingDown className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Green bars reflect gains; Red bars depict losses; Blue bar shows net ending total
</div>
</CardFooter>
</Card>
);
}
export default WaterfallChart;