{
  "title": "Interactive Active Donut Wedge Metric",
  "description": "Clickable/hoverable donut sectors with detailed metric breakdown.",
  "name": "tanstack-charts-active-donut-metric",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/pie/active-donut-metric.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, useState } from \"react\";\n\nimport { scaleLinear } from \"d3-scale\";\nimport { MousePointerClick } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface BrowserDatum {\n    id: string;\n    label: string;\n    visitors: 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 gapAngle = (Math.PI / 180) * 3;\n\nconst browserData: BrowserDatum[] = [\n    { id: \"chrome\", label: \"Chrome\", visitors: 45000 },\n    { id: \"safari\", label: \"Safari\", visitors: 28000 },\n    { id: \"firefox\", label: \"Firefox\", visitors: 12000 },\n    { id: \"edge\", label: \"Edge\", visitors: 9000 },\n    { id: \"other\", label: \"Other\", visitors: 6000 },\n];\n\nconst colors = [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\", \"var(--chart-4)\", \"var(--chart-5)\"];\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as { label?: string; visitors?: number };\n    if (!d || !d.label) return { title: \"\", rows: [] };\n    return {\n        title: d.label,\n        rows: [{ label: \"Sessions\", value: `${(d.visitors || 0).toLocaleString()}`, color: \"var(--chart-1)\" }],\n    };\n}\n\nexport function ActiveDonutMetric() {\n    const [activeId, setActiveId] = useState<string>(\"chrome\");\n\n    const totalVisitors = useMemo(() => browserData.reduce((acc, curr) => acc + curr.visitors, 0), []);\n    const selected = useMemo(() => browserData.find((b) => b.id === activeId) || browserData[0], [activeId]);\n\n    const definition = useMemo(() => {\n        const arcs = pie(browserData, { value: \"visitors\", gapAngle });\n        const activeArc = arcs.filter((a) => a.id === activeId);\n\n        const centerRows = [\n            { id: `${selected.id}:val`, angle: 0, radius: 0, text: selected.visitors.toLocaleString(), dy: -8 },\n            { id: `${selected.id}:lbl`, angle: 0, radius: 0, text: selected.label, dy: 14 },\n        ];\n\n        return defineChart(\n            {\n                renderer: motionRenderer,\n                scales: {\n                    x: null,\n                    y: null,\n                },\n                marks: [\n                    polar({\n                        id: \"browser-donut\",\n                        radiusRatio: 0.72,\n                        scales: {\n                            angle: null,\n                            radius: null,\n                        },\n                        marks: [\n                            radialArc(arcs, {\n                                id: \"browser-arcs\",\n                                key: \"id\",\n                                color: \"id\",\n                                innerRadius: ({ radius }) => radius * 0.6,\n                                cornerRadius: 6,\n                            }),\n                        ],\n                    }),\n                    polar({\n                        id: \"selected-browser\",\n                        radiusRatio: 0.82,\n                        scales: {\n                            angle: null,\n                            radius: null,\n                        },\n                        marks: [\n                            radialArc(activeArc, {\n                                id: \"selected-browser-wedge\",\n                                key: \"id\",\n                                color: \"id\",\n                                innerRadius: ({ radius }) => radius * 0.58,\n                                cornerRadius: 7,\n                                stroke: \"var(--background)\",\n                                strokeWidth: 2,\n                            }),\n                        ],\n                    }),\n                    polar({\n                        id: \"donut-center\",\n                        radiusRatio: 0.8,\n                        scales: {\n                            angle: { scale: scaleLinear().domain([0, tau]) },\n                            radius: { scale: scaleLinear().domain([0, 1]) },\n                        },\n                        marks: [\n                            radialText(centerRows.slice(0, 1), {\n                                id: \"center-val\",\n                                angle: \"angle\",\n                                radius: \"radius\",\n                                key: \"id\",\n                                text: \"text\",\n                                dy: (row) => row.dy,\n                                fill: \"var(--foreground)\",\n                                fontSize: 22,\n                                fontWeight: 700,\n                            }),\n                            radialText(centerRows.slice(1), {\n                                id: \"center-lbl\",\n                                angle: \"angle\",\n                                radius: \"radius\",\n                                key: \"id\",\n                                text: \"text\",\n                                dy: (row) => row.dy,\n                                fill: \"var(--muted-foreground)\",\n                                fontSize: 11,\n                                fontWeight: 600,\n                            }),\n                        ],\n                    }),\n                ],\n                color: {\n                    domain: browserData.map((b) => b.id),\n                    range: colors,\n                },\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    }, [activeId, selected]);\n\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Interactive Active Donut Wedge Metric Selection</CardTitle>\n                <CardDescription>\n                    Click/hover donut sectors or legend pills to highlight active metric focus\n                </CardDescription>\n            </CardHeader>\n            <CardContent className=\"flex flex-col items-center gap-4 sm:flex-row\">\n                <div className=\"h-[230px] w-full sm:w-1/2\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={230}\n                        ariaLabel=\"Interactive Active Donut Wedge Metric Selection Chart\"\n                    />\n                </div>\n                <div className=\"flex w-full flex-col gap-1.5 sm:w-1/2\">\n                    {browserData.map((row, idx) => {\n                        const isSelected = row.id === activeId;\n                        const pct = Math.round((row.visitors / totalVisitors) * 100);\n                        return (\n                            <button\n                                key={row.id}\n                                type=\"button\"\n                                className={`flex items-center justify-between rounded-lg px-3 py-2 text-xs transition-colors ${\n                                    isSelected\n                                        ? \"bg-accent text-accent-foreground font-semibold\"\n                                        : \"hover:bg-muted/50 text-muted-foreground\"\n                                }`}\n                                onClick={() => setActiveId(row.id)}>\n                                <span className=\"flex items-center gap-2\">\n                                    <span\n                                        className=\"h-2.5 w-2.5 rounded-full\"\n                                        style={{ backgroundColor: colors[idx] }}\n                                    />\n                                    {row.label}\n                                </span>\n                                <span>{pct}%</span>\n                            </button>\n                        );\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                    Active: <strong className=\"text-foreground\">{selected.label}</strong> (\n                    {selected.visitors.toLocaleString()} sessions){\" \"}\n                    <MousePointerClick className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    `radialArc` dynamic wedge scaling highlights selected active sector\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default ActiveDonutMetric;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Clickable/hoverable donut sectors with detailed metric breakdown. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "pie"
    ]
  }
}