{
  "title": "Pie Chart with Radial Callout Leader Lines",
  "description": "Categorical breakdown pie chart with pointer callout labels.",
  "name": "tanstack-charts-labeled-pie",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/pie/labeled-pie.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { pie, polar, radialArc, radialRule, 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 { PieChart as PieIcon } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface CategoricalShare {\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 = [\"Direct\", \"Organic Search\", \"Social Media\", \"Referral Traffic\"];\nconst colors = [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\", \"var(--chart-4)\"];\n\nfunction generateDemoData(): CategoricalShare[] {\n    return [\n        { category: \"Direct\", value: 35 },\n        { category: \"Organic Search\", value: 40 },\n        { category: \"Social Media\", value: 15 },\n        { category: \"Referral Traffic\", 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 CategoricalShare;\n    if (!d) return { title: \"\", rows: [] };\n    return {\n        title: d.category,\n        rows: [{ label: \"Share Percentage\", value: `${d.value}%`, color: \"var(--chart-1)\" }],\n    };\n}\n\nexport function LabeledPie() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(() => {\n        const arcs = pie(data, { value: \"value\" });\n\n        return defineChart(\n            {\n                renderer: motionRenderer,\n                scales: {\n                    x: null,\n                    y: null,\n                },\n                marks: [\n                    polar({\n                        radiusRatio: 0.5,\n                        scales: {\n                            angle: { scale: scaleLinear().domain([0, tau]) },\n                            radius: { scale: scaleLinear().domain([0, 1]) },\n                        },\n                        marks: [\n                            radialArc(arcs, {\n                                id: \"pie-slices\",\n                                key: \"category\",\n                                color: \"category\",\n                            }),\n                            radialRule(arcs, {\n                                id: \"pie-leaders\",\n                                angle: \"angle\",\n                                radius1: 1,\n                                radius2: 1,\n                                radius2Offset: 16,\n                                key: \"category\",\n                                stroke: \"var(--muted-foreground)\",\n                                strokeWidth: 1,\n                            }),\n                            radialText(arcs, {\n                                id: \"pie-labels\",\n                                angle: \"angle\",\n                                radius: 1,\n                                radiusOffset: 18,\n                                text: \"category\",\n                                key: \"category\",\n                                color: \"category\",\n                                fontSize: 11,\n                                fontWeight: 600,\n                                anchor: \"outside\",\n                            }),\n                        ],\n                    }),\n                ],\n                color: { domain: categories, range: colors },\n                margin: { top: 30, right: 40, bottom: 30, left: 40 },\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]);\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Pie Chart with Radial Callout Leader Labels</CardTitle>\n                <CardDescription>\n                    Categorical breakdown pie chart with pointer leader lines and radial texts\n                </CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[270px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={270}\n                        ariaLabel=\"Pie Chart with Radial Callout Leader Labels 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                    Leader callouts ensure clear label visibility for tight sectors{\" \"}\n                    <PieIcon className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    `radialRule` and `radialText` project external leader labels around polar pie geometry\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default LabeledPie;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Categorical breakdown pie chart with pointer callout labels. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "pie"
    ]
  }
}