components/tanstack-charts/line/parallel-coordinates.tsx
import { type ChartPoint, defineChart, dot, lineY } 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 { GitCommit } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface ParallelDatum {
Country: string;
event: string;
relativePerformance: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const eventsList = ["100m", "LongJump", "ShotPut", "HighJump", "400m"];
function generateDemoData(): ParallelDatum[] {
const countries = ["USA", "GER", "FRA", "GBR"];
const results: ParallelDatum[] = [];
countries.forEach((country, cIdx) => {
eventsList.forEach((ev, eIdx) => {
const score = 40 + Math.sin(cIdx * 1.5 + eIdx) * 35 + cIdx * 5;
results.push({ Country: country, event: ev, relativePerformance: Math.min(100, Math.max(10, score)) });
});
});
return results;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as ParallelDatum;
if (!d) return { title: "", rows: [] };
return {
title: `${d.Country} - ${d.event}`,
rows: [
{
label: "Relative Score",
value: `${d.relativePerformance.toFixed(1)}/100`,
color: points[0].color || "var(--chart-1)",
},
],
};
}
export function ParallelCoordinates() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
marks: [
lineY(data, {
id: "country-lines",
x: "event",
y: "relativePerformance",
color: "Country",
key: (d: ParallelDatum) => `${d.Country}:${d.event}`,
strokeWidth: 2,
}),
dot(data, {
id: "country-points",
x: "event",
y: "relativePerformance",
color: "Country",
key: (d: ParallelDatum) => `${d.Country}:${d.event}`,
r: 3.5,
}),
],
scales: {
x: {
scale: () => scaleBand<string>().domain(eventsList).padding(0.1),
axis: {
line: false,
label: "Decathlon Event",
ticks: { size: 0, padding: 10 },
},
},
y: {
scale: scaleLinear().domain([0, 100]),
grid: true,
axis: {
line: false,
label: "Relative Score (%)",
ticks: { size: 0 },
},
},
},
color: {
domain: ["USA", "GER", "FRA", "GBR"],
range: ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)", "var(--chart-4)"],
},
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>Multivariate Performance Profile</CardTitle>
<CardDescription>
Parallel coordinates comparing multi-attribute athlete scores across events
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Multivariate Performance Profile Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
USA leads overall performance profile across speed events{" "}
<GitCommit className="h-4 w-4 text-purple-500" />
</div>
<div className="text-muted-foreground leading-none">
Connected series lines display high-dimensional metric tradeoffs across vertical axes
</div>
</CardFooter>
</Card>
);
}
export default ParallelCoordinates;