components/tanstack-charts/line/change-arrows.tsx
import { type ChartPoint, arrow, defineChart } 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 { scaleLinear } from "d3-scale";
import { MoveUpRight } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface ArrowDatum {
metro: string;
pop1980: number;
ratio1980: number;
pop2015: number;
ratio2015: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): ArrowDatum[] {
return [
{ metro: "New York", pop1980: 6.9, ratio1980: 4.5, pop2015: 7.3, ratio2015: 7.8 },
{ metro: "Los Angeles", pop1980: 6.8, ratio1980: 4.2, pop2015: 7.1, ratio2015: 8.2 },
{ metro: "Chicago", pop1980: 6.8, ratio1980: 4.0, pop2015: 6.9, ratio2015: 6.1 },
{ metro: "Houston", pop1980: 6.4, ratio1980: 3.8, pop2015: 6.8, ratio2015: 5.9 },
{ metro: "Phoenix", pop1980: 6.1, ratio1980: 3.6, pop2015: 6.6, ratio2015: 5.2 },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as ArrowDatum;
if (!d) return { title: "", rows: [] };
const diff = d.ratio2015 - d.ratio1980;
return {
title: d.metro,
rows: [
{ label: "1980 Ratio", value: d.ratio1980.toFixed(1), color: "var(--muted-foreground)" },
{ label: "2015 Ratio", value: d.ratio2015.toFixed(1), color: points[0].color || "var(--chart-1)" },
{ label: "Shift", value: `${diff > 0 ? "+" : ""}${diff.toFixed(1)}`, color: "var(--foreground)" },
],
};
}
export function ChangeArrows() {
const data = useMemo(() => generateDemoData(), []);
const { increases, decreases } = useMemo(() => {
return {
increases: data.filter((d) => d.ratio2015 >= d.ratio1980),
decreases: data.filter((d) => d.ratio2015 < d.ratio1980),
};
}, [data]);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
arrow(increases, {
id: "arrow-increases",
x1: "pop1980",
y1: "ratio1980",
x2: "pop2015",
y2: "ratio2015",
stroke: "var(--chart-1)",
headLength: 7,
}),
arrow(decreases, {
id: "arrow-decreases",
x1: "pop1980",
y1: "ratio1980",
x2: "pop2015",
y2: "ratio2015",
stroke: "var(--chart-2)",
headLength: 7,
}),
],
scales: {
x: {
scale: scaleLinear().domain([5.8, 7.6]),
grid: true,
axis: {
line: false,
label: "Log10 Population",
ticks: { size: 0 },
},
},
y: {
scale: scaleLinear().domain([3, 9]),
grid: true,
axis: {
line: false,
label: "Wage Inequality Ratio",
ticks: { size: 0 },
},
},
},
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),
},
},
),
[increases, decreases],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Population & Wage Vector Shifts</CardTitle>
<CardDescription>
2D vector arrows depicting metro population growth versus wage inequality shift
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Population & Wage Vector Shifts Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Vector direction shows simultaneous population expansion and inequality ratio{" "}
<MoveUpRight className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Green vectors denote increasing inequality; Red vectors denote decreasing inequality
</div>
</CardFooter>
</Card>
);
}
export default ChangeArrows;