
Categorical breakdown pie chart with pointer callout labels.
components/tanstack-charts/pie/labeled-pie.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { pie, polar, radialArc, radialRule, radialText } from "@tanstack/charts/polar";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { scaleLinear } from "d3-scale";
import { PieChart as PieIcon } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface CategoricalShare {
category: string;
value: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const tau = Math.PI * 2;
const categories = ["Direct", "Organic Search", "Social Media", "Referral Traffic"];
const colors = ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)", "var(--chart-4)"];
function generateDemoData(): CategoricalShare[] {
return [
{ category: "Direct", value: 35 },
{ category: "Organic Search", value: 40 },
{ category: "Social Media", value: 15 },
{ category: "Referral Traffic", value: 10 },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as CategoricalShare;
if (!d) return { title: "", rows: [] };
return {
title: d.category,
rows: [{ label: "Share Percentage", value: `${d.value}%`, color: "var(--chart-1)" }],
};
}
export function LabeledPie() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(() => {
const arcs = pie(data, { value: "value" });
return defineChart(
{
renderer: motionRenderer,
scales: {
x: null,
y: null,
},
marks: [
polar({
radiusRatio: 0.5,
scales: {
angle: { scale: scaleLinear().domain([0, tau]) },
radius: { scale: scaleLinear().domain([0, 1]) },
},
marks: [
radialArc(arcs, {
id: "pie-slices",
key: "category",
color: "category",
}),
radialRule(arcs, {
id: "pie-leaders",
angle: "angle",
radius1: 1,
radius2: 1,
radius2Offset: 16,
key: "category",
stroke: "var(--muted-foreground)",
strokeWidth: 1,
}),
radialText(arcs, {
id: "pie-labels",
angle: "angle",
radius: 1,
radiusOffset: 18,
text: "category",
key: "category",
color: "category",
fontSize: 11,
fontWeight: 600,
anchor: "outside",
}),
],
}),
],
color: { domain: categories, range: colors },
margin: { top: 30, right: 40, bottom: 30, left: 40 },
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>Pie Chart with Radial Callout Leader Labels</CardTitle>
<CardDescription>
Categorical breakdown pie chart with pointer leader lines and radial texts
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[270px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={270}
ariaLabel="Pie Chart with Radial Callout Leader Labels Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Leader callouts ensure clear label visibility for tight sectors{" "}
<PieIcon className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
`radialRule` and `radialText` project external leader labels around polar pie geometry
</div>
</CardFooter>
</Card>
);
}
export default LabeledPie;