
Concentric multi-level donut rings representing hierarchical category breakdown.
components/tanstack-charts/pie/nested-donut.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { pie, polar, radialArc } from "@tanstack/charts/polar";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { Layers } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface HierarchicalItem {
name: string;
size: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const names = ["Frontend", "Backend", "Frontend.React", "Frontend.Vue", "Backend.Node", "Backend.Go"];
const colors = [
"var(--chart-1)",
"var(--chart-2)",
"var(--chart-1)",
"var(--chart-3)",
"var(--chart-2)",
"var(--chart-4)",
];
function generateDemoData() {
const inner: HierarchicalItem[] = [
{ name: "Frontend", size: 60 },
{ name: "Backend", size: 40 },
];
const outer: HierarchicalItem[] = [
{ name: "Frontend.React", size: 40 },
{ name: "Frontend.Vue", size: 20 },
{ name: "Backend.Node", size: 25 },
{ name: "Backend.Go", size: 15 },
];
return { inner, outer };
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as HierarchicalItem;
if (!d) return { title: "", rows: [] };
return {
title: d.name,
rows: [{ label: "Proportional Size", value: `${d.size} units`, color: "var(--chart-1)" }],
};
}
export function NestedDonut() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(() => {
const innerArcs = pie(data.inner, { value: "size" });
const outerArcs = pie(data.outer, { value: "size" });
return defineChart(
{
renderer: motionRenderer,
scales: {
x: null,
y: null,
},
marks: [
polar({
radiusRatio: 0.8,
scales: {
angle: null,
radius: null,
},
marks: [
radialArc(innerArcs, {
id: "family-slices",
key: "name",
innerRadius: ({ radius }) => radius * 0.15,
outerRadius: ({ radius }) => radius * 0.48,
color: "name",
}),
radialArc(outerArcs, {
id: "detail-slices",
key: "name",
innerRadius: ({ radius }) => radius * 0.55,
color: "name",
}),
],
}),
],
color: { domain: names, range: colors },
margin: { top: 20, right: 20, bottom: 20, left: 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]);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Nested Multi-Ring Sunburst Donut Chart</CardTitle>
<CardDescription>
Concentric multi-level donut rings representing parent category and sub-item proportions
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Nested Multi-Ring Sunburst Donut Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Inner ring maps major categories, outer ring breaks down sub-components{" "}
<Layers className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Multiple `radialArc` layers with distinct inner/outer radii compose sunburst rings
</div>
</CardFooter>
</Card>
);
}
export default NestedDonut;