components/tanstack-charts/pie/pie-chart.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 { PieChartIcon } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface BrowserDatum {
browser: string;
share: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): BrowserDatum[] {
return [
{ browser: "Chrome", share: 0.64 },
{ browser: "Safari", share: 0.19 },
{ browser: "Edge", share: 0.08 },
{ browser: "Firefox", share: 0.06 },
{ browser: "Others", share: 0.03 },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as BrowserDatum;
if (!d) return { title: "", rows: [] };
return {
title: d.browser,
rows: [{ label: "Market Share", value: `${(d.share * 100).toFixed(1)}%`, color: "var(--chart-1)" }],
};
}
export function PieChart() {
const data = useMemo(() => generateDemoData(), []);
const arcs = useMemo(() => {
return pie(data, {
value: "share",
});
}, [data]);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
scales: {
x: null,
y: null,
},
marks: [
polar({
inset: 0,
radiusRatio: 0.8,
scales: {
angle: null,
radius: null,
},
marks: [
radialArc(arcs, {
id: "browser-slices",
key: "browser",
color: "browser",
stroke: "var(--background)",
strokeWidth: 2,
}),
],
}),
],
color: {
range: [
"var(--chart-1)",
"var(--chart-2)",
"var(--chart-3)",
"var(--chart-4)",
"var(--chart-5)",
],
},
margin: 10,
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),
},
},
),
[arcs],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Solid Sector Pie Proportions Chart</CardTitle>
<CardDescription>
Full circular pie slices representing relative categorical percentage shares
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex h-[250px] w-full items-center justify-center">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Solid Sector Pie Proportions Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Chrome retains majority 64% global web market share{" "}
<PieChartIcon className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Radial arc transformation converts proportional scalar values into 360-degree pie segments
</div>
</CardFooter>
</Card>
);
}
export default PieChart;