{
  "title": "Pie Chart Interactive",
  "description": "An interactive donut pie chart with month selection built with TanStack Charts.",
  "name": "tanstack-charts-pie-interactive",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card",
    "select"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/pie/pie-interactive.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { focusGroupAngle, pie, polar, radialArc, radialText } from \"@tanstack/charts/polar\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo, useState } from \"react\";\n\nimport { scaleLinear } from \"d3-scale\";\n\nimport { Card, CardContent, CardDescription, CardHeader, CardTitle } from \"@/components/ui/card\";\nimport { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from \"@/components/ui/select\";\n\ninterface MonthDatum {\n    month: string;\n    visitors: number;\n}\n\nfunction generateDemoData(): MonthDatum[] {\n    return [\n        { month: \"january\", visitors: 186 },\n        { month: \"february\", visitors: 305 },\n        { month: \"march\", visitors: 237 },\n        { month: \"april\", visitors: 173 },\n        { month: \"may\", visitors: 209 },\n    ];\n}\n\nconst monthNames = [\"january\", \"february\", \"march\", \"april\", \"may\"] as const;\nconst chartColors = [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\", \"var(--chart-4)\", \"var(--chart-5)\"];\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction titleCase(value: string) {\n    return value.charAt(0).toUpperCase() + value.slice(1);\n}\n\nfunction extractDatumMetric(datum: unknown) {\n    if (!datum || typeof datum !== \"object\") return undefined;\n    const label =\n        Reflect.get(datum, \"month\") ??\n        Reflect.get(datum, \"browser\") ??\n        Reflect.get(datum, \"category\") ??\n        Reflect.get(datum, \"label\");\n    const visitors = Reflect.get(datum, \"visitors\") ?? Reflect.get(datum, \"value\");\n    return typeof label === \"string\" && typeof visitors === \"number\" && Number.isFinite(visitors)\n        ? { label, visitors }\n        : undefined;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    const point = points.find((candidate) => extractDatumMetric(candidate.datum));\n    const metric = point && extractDatumMetric(point.datum);\n    return metric\n        ? {\n              title: titleCase(metric.label),\n              rows: [\n                  {\n                      label: \"Visitors\",\n                      value: metric.visitors.toLocaleString(\"en-US\"),\n                      color: point.color,\n                  },\n              ],\n          }\n        : { rows: [] };\n}\n\nexport function PieInteractive() {\n    const data = useMemo(() => generateDemoData(), []);\n    const [activeMonth, setActiveMonth] = useState<string>(\"january\");\n\n    const definition = useMemo(() => {\n        const arcs = pie(data, {\n            value: \"visitors\",\n            startAngle: Math.PI / 2,\n            endAngle: (-Math.PI * 3) / 2,\n        });\n\n        const activeIndex = Math.max(\n            0,\n            data.findIndex((row) => row.month === activeMonth),\n        );\n        const activeRow = data[activeIndex]!;\n        const active = arcs.filter((row) => row.month === activeRow.month);\n        const activeColor = chartColors[activeIndex]!;\n\n        return defineChart(\n            {\n                renderer: motionRenderer,\n                motion: {\n                    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n                },\n                scales: {\n                    x: null,\n                    y: null,\n                },\n                marks: [\n                    polar({\n                        radiusRatio: 0.78,\n                        scales: {\n                            angle: { scale: scaleLinear().domain([0, Math.PI * 2]) },\n                            radius: { scale: scaleLinear().domain([0, 1]) },\n                        },\n                        marks: [\n                            radialArc(arcs, {\n                                id: \"month-slices\",\n                                key: \"month\",\n                                innerRadius: 60,\n                                color: \"month\",\n                                stroke: \"var(--background)\",\n                                strokeWidth: 1,\n                            }),\n                            radialArc(active, {\n                                id: \"active-month\",\n                                key: \"month\",\n                                innerRadius: 60,\n                                outerRadius: ({ radius }) => radius + 10,\n                                fill: activeColor,\n                                stroke: \"var(--background)\",\n                                strokeWidth: 5,\n                            }),\n                            radialArc(active, {\n                                id: \"active-month-ring\",\n                                key: \"month\",\n                                innerRadius: ({ radius }) => radius + 12,\n                                outerRadius: ({ radius }) => radius + 25,\n                                fill: activeColor,\n                                stroke: \"var(--background)\",\n                                strokeWidth: 3,\n                            }),\n                            radialText(\n                                [\n                                    {\n                                        id: \"total\",\n                                        angle: 0,\n                                        radius: 0,\n                                        text: String(activeRow.visitors),\n                                    },\n                                ],\n                                {\n                                    id: \"active-total\",\n                                    angle: \"angle\",\n                                    radius: \"radius\",\n                                    key: \"id\",\n                                    text: \"text\",\n                                    dy: -5,\n                                    fill: \"var(--foreground)\",\n                                    fontSize: 30,\n                                    fontWeight: 700,\n                                },\n                            ),\n                            radialText([{ id: \"label\", angle: 0, radius: 0, text: \"Visitors\" }], {\n                                id: \"active-label\",\n                                angle: \"angle\",\n                                radius: \"radius\",\n                                key: \"id\",\n                                text: \"text\",\n                                dy: 20,\n                                fill: \"var(--muted-foreground)\",\n                                fontSize: 12,\n                            }),\n                        ],\n                    }),\n                ],\n                color: {\n                    domain: monthNames,\n                    range: chartColors,\n                },\n                margin: 0,\n            },\n            {\n                focus: focusGroupAngle,\n                tooltip: {\n                    use: tooltip,\n                    motion: { type: \"spring\", stiffness: 170, damping: 18 },\n                    className: \"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs\",\n                    anchor: \"group-center\",\n                    placement: \"auto\",\n                    sort: \"color-domain\",\n                    content: (points) => shadcnTooltipContent(points),\n                },\n            },\n        );\n    }, [data, activeMonth]);\n\n    return (\n        <Card className=\"flex w-full max-w-md flex-col\">\n            <CardHeader className=\"flex-row items-center justify-between space-y-0 pb-2\">\n                <div className=\"grid gap-1\">\n                    <CardTitle>Traffic Source Attribution</CardTitle>\n                    <CardDescription>User acquisition distribution across marketing channels</CardDescription>\n                </div>\n                <Select value={activeMonth} onValueChange={(val) => val && setActiveMonth(val)}>\n                    <SelectTrigger size=\"sm\" className=\"w-[130px] cursor-pointer rounded-lg\" aria-label=\"Select month\">\n                        <SelectValue placeholder=\"Select month\" />\n                    </SelectTrigger>\n                    <SelectContent align=\"end\" className=\"rounded-xl\">\n                        {data.map((row, index) => (\n                            <SelectItem key={row.month} value={row.month} className=\"cursor-pointer rounded-lg\">\n                                <span className=\"flex items-center gap-2\">\n                                    <span\n                                        className=\"size-2 shrink-0 rounded-full\"\n                                        style={{ backgroundColor: chartColors[index] }}\n                                    />\n                                    <span>{titleCase(row.month)}</span>\n                                </span>\n                            </SelectItem>\n                        ))}\n                    </SelectContent>\n                </Select>\n            </CardHeader>\n            <CardContent className=\"flex-1 pb-0\">\n                <div className=\"mx-auto flex aspect-square max-h-[250px] w-full items-center justify-center\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        initialWidth={250}\n                        height={250}\n                        ariaLabel=\"Traffic Source Attribution Chart\"\n                    />\n                </div>\n            </CardContent>\n        </Card>\n    );\n}\n\nexport default PieInteractive;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. An interactive donut pie chart with month selection built with TanStack Charts. Relies on card, select. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "pie"
    ]
  }
}