components/tanstack-charts/pie/pie-stacked.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { focusGroupAngle, 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 { TrendingUp } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
function generateDemoData() {
const desktopRows = [186, 305, 237, 173, 209].map((visitors, index) => ({
browser: ["january", "february", "march", "april", "may"][index]!,
visitors,
}));
const mobileRows = [80, 200, 120, 190, 130].map((visitors, index) => ({
browser: ["january", "february", "march", "april", "may"][index]!,
visitors,
}));
return { desktopRows, mobileRows };
}
const monthNames = ["january", "february", "march", "april", "may"] as const;
const chartColors = ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)", "var(--chart-4)", "var(--chart-5)"];
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function titleCase(value: string) {
return value.charAt(0).toUpperCase() + value.slice(1);
}
function browserMetric(datum: unknown) {
if (!datum || typeof datum !== "object") return undefined;
const browser = Reflect.get(datum, "browser");
const visitors = Reflect.get(datum, "visitors");
return typeof browser === "string" && typeof visitors === "number" && Number.isFinite(visitors)
? { browser, visitors }
: undefined;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
const point = points.find((candidate) => browserMetric(candidate.datum));
const metric = point && browserMetric(point.datum);
return metric
? {
title: titleCase(metric.browser),
rows: [
{
label: "Visitors",
value: metric.visitors.toLocaleString("en-US"),
color: point.color,
},
],
}
: { rows: [] };
}
export function PieStacked() {
const { desktopRows, mobileRows } = useMemo(() => generateDemoData(), []);
const definition = useMemo(() => {
const desktopArcs = pie(desktopRows, {
value: "visitors",
startAngle: Math.PI / 2,
endAngle: (-Math.PI * 3) / 2,
});
const mobileArcs = pie(mobileRows, {
value: "visitors",
startAngle: Math.PI / 2,
endAngle: (-Math.PI * 3) / 2,
});
return defineChart(
{
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
scales: {
x: null,
y: null,
},
marks: [
polar({
radiusRatio: 1,
scales: {
angle: null,
radius: null,
},
marks: [
radialArc(desktopArcs, {
id: "desktop-slices",
key: "browser",
outerRadius: 60,
color: "browser",
stroke: "var(--background)",
strokeWidth: 1,
}),
radialArc(mobileArcs, {
id: "mobile-slices",
key: "browser",
innerRadius: 70,
outerRadius: 90,
color: "browser",
stroke: "var(--background)",
strokeWidth: 1,
}),
],
}),
],
color: { domain: monthNames, range: chartColors },
margin: 0,
},
{
focus: focusGroupAngle,
tooltip: {
use: tooltip,
motion: { type: "spring", stiffness: 170, damping: 18 },
className: "font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "group-center",
placement: "auto",
sort: "color-domain",
content: (points) => shadcnTooltipContent(points),
},
},
);
}, [desktopRows, mobileRows]);
return (
<Card className="flex w-full max-w-md flex-col">
<CardHeader className="items-center pb-0">
<CardTitle>Subscription Tier Composition</CardTitle>
<CardDescription>Active user base breakdown across Free, Pro, and Enterprise</CardDescription>
</CardHeader>
<CardContent className="flex-1 pb-0">
<div className="mx-auto flex aspect-square max-h-[250px] w-full items-center justify-center">
<Chart
definition={definition}
renderer={motionRenderer}
initialWidth={250}
height={250}
ariaLabel="Subscription Tier Composition Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Pro tier adoption up by 5.2% this month <TrendingUp className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">Showing total visitors for the last 6 months</div>
</CardFooter>
</Card>
);
}
export default PieStacked;