
Concentric multi-ring sunburst displaying nested file system hierarchy.
components/tanstack-charts/pie/sunburst.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { sunburst } from "@tanstack/charts/hierarchy/sunburst";
import { motion } from "@tanstack/charts/motion";
import { polar } from "@tanstack/charts/polar";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { Sun } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface SunburstNode {
name: string;
size?: number;
branchId?: string;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): SunburstNode[] {
return [
{ name: "Analytics", size: 100, branchId: "b1" },
{ name: "Analytics.Cluster", size: 40, branchId: "b1" },
{ name: "Analytics.Cluster.Agglomerative", size: 20, branchId: "b1" },
{ name: "Analytics.Cluster.KMeans", size: 20, branchId: "b1" },
{ name: "Analytics.Graph", size: 60, branchId: "b1" },
{ name: "Analytics.Graph.Betweenness", size: 30, branchId: "b1" },
{ name: "Analytics.Graph.PageRank", size: 30, branchId: "b1" },
{ name: "Display", size: 80, branchId: "b2" },
{ name: "Display.Text", size: 30, branchId: "b2" },
{ name: "Display.Sprite", size: 50, branchId: "b2" },
{ name: "Physics", size: 70, branchId: "b3" },
{ name: "Physics.Gravity", size: 35, branchId: "b3" },
{ name: "Physics.Collision", size: 35, branchId: "b3" },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as { data?: SunburstNode; value?: number; id?: string };
if (!d) return { title: "", rows: [] };
const name = (d.data?.name || d.id || "").replaceAll(".", " › ");
return {
title: name,
rows: [{ label: "Proportional Node Size", value: `${d.value ?? 0} units`, color: "var(--chart-1)" }],
};
}
export function SunburstChart() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
scales: {
x: null,
y: null,
},
marks: [
polar({
radiusRatio: 0.86,
startAngle: Math.PI / 2,
endAngle: Math.PI / 2 - Math.PI * 2,
scales: {
angle: null,
radius: null,
},
marks: [
sunburst(data, {
id: "sunburst-arcs",
path: "name",
delimiter: ".",
value: "size",
innerRadius: ({ radius }) => radius * 0.15,
outerRadius: ({ radius }) => {
const inner = radius * 0.15;
return inner + ((radius - inner) * 2) / 3;
},
ringPadding: 2,
color: "branchId",
stroke: "var(--background)",
strokeWidth: 1.5,
}),
],
}),
],
color: {
range: ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)"],
},
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>Hierarchical Partition Sunburst Radial Ring</CardTitle>
<CardDescription>
Concentric multi-ring sunburst displaying dot-delimited file path hierarchies
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[270px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={270}
ariaLabel="Hierarchical Partition Sunburst Radial Ring Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Multi-level partition tree with proportional arc spans <Sun className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
`sunburst` mark transforms dot-delimited path strings into radial rings
</div>
</CardFooter>
</Card>
);
}
export default SunburstChart;