components/tanstack-charts/line/dumbbell.tsx
import { type ChartPoint, defineChart, dot, link } 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 { ArrowLeftRight } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface DumbbellDatum {
city: string;
v1980: number;
v2015: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): DumbbellDatum[] {
return [
{ city: "New York", v1980: 4.2, v2015: 7.8 },
{ city: "San Francisco", v1980: 3.8, v2015: 8.4 },
{ city: "Chicago", v1980: 4.0, v2015: 6.2 },
{ city: "Houston", v1980: 3.5, v2015: 5.9 },
{ city: "Boston", v1980: 4.1, v2015: 7.1 },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as DumbbellDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.city,
rows: [
{ label: "1980 Ratio", value: d.v1980.toFixed(1), color: "var(--chart-1)" },
{ label: "2015 Ratio", value: d.v2015.toFixed(1), color: "var(--chart-2)" },
{ label: "Net Shift", value: `+${(d.v2015 - d.v1980).toFixed(1)}`, color: "var(--foreground)" },
],
};
}
export function DumbbellChart() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
link(data, {
id: "dumbbell-connector",
x1: "v1980",
y1: "city",
x2: "v2015",
y2: "city",
stroke: "var(--border)",
strokeWidth: 2,
}),
dot(data, {
id: "dumbbell-1980",
x: "v1980",
y: "city",
fill: "var(--chart-1)",
r: 4.5,
}),
dot(data, {
id: "dumbbell-2015",
x: "v2015",
y: "city",
fill: "var(--chart-2)",
r: 4.5,
}),
],
scales: {
x: {
scale: scaleLinear().domain([2, 10]),
grid: true,
axis: {
line: false,
label: "90th/10th Percentile Wage Ratio",
ticks: { size: 0 },
},
},
y: {
scale: () => scaleBand<string>().padding(0.3),
axis: {
line: false,
ticks: { size: 0 },
},
},
},
color: {
domain: ["1980", "2015"],
range: ["var(--chart-1)", "var(--chart-2)"],
},
margin: { top: 24, right: 24, bottom: 32, left: 100 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
focus: "group-y",
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>Metro Inequality Shift (1980 vs 2015)</CardTitle>
<CardDescription>Dumbbell plot comparing historical baseline and current wage ratios</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Metro Inequality Shift Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
San Francisco experienced largest ratio expansion (+4.6){" "}
<ArrowLeftRight className="h-4 w-4 text-orange-500" />
</div>
<div className="text-muted-foreground leading-none">
Blue dots mark 1980 baseline; Orange dots denote 2015 measurement
</div>
</CardFooter>
</Card>
);
}
export default DumbbellChart;