
Multivariate polar radar spider chart evaluating multi-axis performance dimensions.
components/tanstack-charts/pie/radar-chart.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { angleGrid, polar, radialArea, radialGrid } from "@tanstack/charts/polar";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { scaleLinear, scalePoint } from "d3-scale";
import { curveLinearClosed } from "d3-shape";
import { Compass } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface SkillDatum {
skill: string;
score: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const radarSkills = ["Speed", "Endurance", "Strength", "Agility", "Flexibility", "Precision"];
function generateDemoData(): SkillDatum[] {
const scores = [85, 92, 78, 88, 70, 95];
return radarSkills.map((skill, idx) => ({
skill,
score: scores[idx] / 100,
}));
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as SkillDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.skill,
rows: [{ label: "Performance Score", value: `${(d.score * 100).toFixed(0)}%`, color: "var(--chart-1)" }],
};
}
export function RadarChart() {
const data = useMemo(() => generateDemoData(), []);
const angleScale = useMemo(() => scalePoint<string>().domain(radarSkills), []);
const radiusScale = useMemo(() => scaleLinear().domain([0, 1]), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
scales: {
x: null,
y: null,
},
marks: [
polar({
scales: {
angle: { scale: angleScale, wrap: true },
radius: { scale: radiusScale },
},
inset: 0,
radiusRatio: 0.8,
guides: [
radialGrid({
values: [0.2, 0.4, 0.6, 0.8, 1],
shape: "polygon",
labels: true,
labelFontSize: 10,
format: (v) => `${Math.round(Number(v) * 100)}%`,
labelFill: "var(--muted-foreground)",
stroke: "var(--border)",
}),
angleGrid({
values: radarSkills,
labels: true,
labelOffset: 10,
labelFill: "var(--foreground)",
labelFontSize: 11,
format: String,
stroke: "var(--border)",
}),
],
marks: [
radialArea(data, {
id: "athlete-profile",
angle: "skill",
radius: "score",
key: "skill",
curve: curveLinearClosed,
fill: "var(--chart-1)",
fillOpacity: 0.45,
stroke: "var(--chart-1)",
strokeWidth: 2,
}),
],
}),
],
margin: 20,
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
keyboard: true,
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "pointer",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data, angleScale, radiusScale],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Multivariate Polar Radar Spider Chart</CardTitle>
<CardDescription>Closed radial polygon plot evaluating multi-axis performance ratings</CardDescription>
</CardHeader>
<CardContent>
<div className="flex h-[280px] w-full items-center justify-center">
<Chart
definition={definition}
renderer={motionRenderer}
height={280}
ariaLabel="Multivariate Polar Radar Spider Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Precision and Endurance score highest at 95% and 92%{" "}
<Compass className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Polygon radial grid guides partition equal angular spans for each attribute dimension
</div>
</CardFooter>
</Card>
);
}
export default RadarChart;