{
  "title": "Donut Chart with Central KPI Radial Stat",
  "description": "Hollow inner radius donut chart displaying center KPI metric.",
  "name": "tanstack-charts-center-donut",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/pie/center-donut.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { pie, polar, radialArc, radialText } from \"@tanstack/charts/polar\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleLinear } from \"d3-scale\";\nimport { Donut } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface DonutSegment {\n    category: string;\n    value: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nconst tau = Math.PI * 2;\nconst categories = [\"Desktop\", \"Mobile\", \"Tablet\"];\nconst colors = [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\"];\n\nfunction generateDemoData(): DonutSegment[] {\n    return [\n        { category: \"Desktop\", value: 55 },\n        { category: \"Mobile\", value: 35 },\n        { category: \"Tablet\", value: 10 },\n    ];\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as DonutSegment;\n    if (!d || !d.category) return { title: \"\", rows: [] };\n    return {\n        title: d.category,\n        rows: [{ label: \"Device Share\", value: `${d.value}%`, color: \"var(--chart-1)\" }],\n    };\n}\n\nexport function CenterDonut() {\n    const data = useMemo(() => generateDemoData(), []);\n    const totalValue = useMemo(() => data.reduce((acc, curr) => acc + curr.value, 0), [data]);\n\n    const definition = useMemo(() => {\n        const arcs = pie(data, { value: \"value\" });\n        const center = [{ id: \"total\", angle: 0, radius: 0, text: `${totalValue}%` }];\n\n        return defineChart(\n            {\n                renderer: motionRenderer,\n                scales: {\n                    x: null,\n                    y: null,\n                },\n                marks: [\n                    polar({\n                        radiusRatio: 0.75,\n                        scales: {\n                            angle: { scale: scaleLinear().domain([0, tau]) },\n                            radius: { scale: scaleLinear().domain([0, 1]) },\n                        },\n                        marks: [\n                            radialArc(arcs, {\n                                id: \"segment-slices\",\n                                key: \"category\",\n                                innerRadius: ({ radius }) => radius * 0.65,\n                                color: \"category\",\n                            }),\n                            radialText(center, {\n                                id: \"center-total\",\n                                angle: \"angle\",\n                                radius: \"radius\",\n                                key: \"id\",\n                                text: \"text\",\n                                fill: \"var(--foreground)\",\n                                fontSize: 24,\n                                fontWeight: 700,\n                            }),\n                        ],\n                    }),\n                ],\n                color: { domain: categories, range: colors },\n                margin: { top: 20, right: 20, bottom: 20, left: 20 },\n                theme: {\n                    foreground: \"var(--muted-foreground)\",\n                    grid: \"var(--border)\",\n                    background: \"transparent\",\n                },\n            },\n            {\n                keyboard: true,\n                tooltip: {\n                    use: tooltip,\n                    className: \"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs\",\n                    anchor: \"pointer\",\n                    placement: \"auto\",\n                    content: (points) => shadcnTooltipContent(points),\n                },\n            },\n        );\n    }, [data, totalValue]);\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Donut Chart with Central KPI Radial Text Metric</CardTitle>\n                <CardDescription>\n                    Hollow inner radius donut chart displaying total aggregate KPI metric in center origin\n                </CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[250px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={250}\n                        ariaLabel=\"Donut Chart with Central KPI Radial Text Metric Chart\"\n                    />\n                </div>\n            </CardContent>\n            <CardFooter className=\"flex-col items-start gap-2 text-sm\">\n                <div className=\"flex items-center gap-2 leading-none font-medium\">\n                    Total aggregated metric volume centered in hole <Donut className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    `radialText` positioned at origin (radius: 0) anchors central KPI typography\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default CenterDonut;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Hollow inner radius donut chart displaying center KPI metric. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "pie"
    ]
  }
}