{
  "title": "Radial Chart Stacked",
  "description": "A semi-circle stacked radial bar chart with centered total text built with TanStack Charts.",
  "name": "tanstack-charts-radial-stacked",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/radial/radial-stacked.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { focusGroupAngle, polar, radialBarAngle, radialText } from \"@tanstack/charts/polar\";\nimport { tooltip } from \"@tanstack/charts/tooltip\";\nimport { Chart } from \"@tanstack/react-charts/core\";\nimport { useMemo } from \"react\";\n\nimport { scaleBand, scaleLinear } from \"d3-scale\";\nimport { TrendingUp } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\nfunction generateDemoData() {\n    return [\n        {\n            id: \"mobile\",\n            ring: \"visitors\",\n            start: 0,\n            end: 570,\n            fill: \"var(--chart-2)\",\n        },\n        {\n            id: \"desktop\",\n            ring: \"visitors\",\n            start: 570,\n            end: 1830,\n            fill: \"var(--chart-1)\",\n        },\n    ];\n}\n\nconst twoSeries = [\"desktop\", \"mobile\"] as const;\nconst chartColors = [\"var(--chart-1)\", \"var(--chart-2)\"];\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction rechartsPolarAngle(degrees: number) {\n    return ((90 - degrees) * Math.PI) / 180;\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, \"id\") ??\n        Reflect.get(datum, \"ring\") ??\n        Reflect.get(datum, \"browser\") ??\n        Reflect.get(datum, \"month\");\n    const val = Reflect.get(datum, \"end\");\n    const start = Reflect.get(datum, \"start\");\n    const value = typeof val === \"number\" && typeof start === \"number\" ? val - start : val;\n    return typeof label === \"string\" && typeof value === \"number\" && Number.isFinite(value)\n        ? { label, value }\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.value.toLocaleString(\"en-US\"),\n                      color: point.color,\n                  },\n              ],\n          }\n        : { rows: [] };\n}\n\nfunction radialCenterLabels(total: string, fontSize: number, totalDy: number, labelDy: number) {\n    return polar({\n        scales: {\n            angle: { scale: scaleLinear().domain([0, 1]) },\n            radius: { scale: scaleLinear().domain([0, 1]) },\n        },\n        marks: [\n            radialText([{ id: \"total\", angle: 0, radius: 0, text: total }], {\n                id: `radial-total-${total}`,\n                angle: \"angle\",\n                radius: \"radius\",\n                key: \"id\",\n                text: \"text\",\n                dy: totalDy,\n                fill: \"var(--foreground)\",\n                fontSize,\n                fontWeight: 700,\n            }),\n            radialText([{ id: \"label\", angle: 0, radius: 0, text: \"Visitors\" }], {\n                id: `radial-label-${total}`,\n                angle: \"angle\",\n                radius: \"radius\",\n                key: \"id\",\n                text: \"text\",\n                dy: labelDy,\n                fill: \"var(--muted-foreground)\",\n                fontSize: 14,\n            }),\n        ],\n    });\n}\n\nexport function RadialStacked() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(() => {\n        return defineChart(\n            {\n                renderer: motionRenderer,\n                motion: {\n                    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n                },\n                marks: [\n                    polar({\n                        startAngle: rechartsPolarAngle(0),\n                        endAngle: rechartsPolarAngle(360),\n                        scales: {\n                            angle: { scale: scaleLinear().domain([0, 1830]) },\n                            radius: {\n                                scale: scaleBand<string>().domain([\"visitors\"]),\n                                range: [80, 110],\n                            },\n                        },\n                        marks: [\n                            radialBarAngle(data, {\n                                id: \"stacked-values\",\n                                angle1: \"start\",\n                                angle2: \"end\",\n                                angle: \"end\",\n                                radius: \"ring\",\n                                key: \"id\",\n                                fill: (row) => row.fill,\n                                cornerRadius: 5,\n                                stroke: \"transparent\",\n                                strokeWidth: 2,\n                            }),\n                        ],\n                    }),\n                    radialCenterLabels(\"1,830\", 24, -8, 14),\n                ],\n                scales: {\n                    x: null,\n                    y: null,\n                },\n                color: { domain: twoSeries, range: chartColors },\n                margin: 0,\n            },\n            {\n                focus: focusGroupAngle,\n                keyboard: false,\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]);\n\n    return (\n        <Card className=\"flex w-full max-w-md flex-col\">\n            <CardHeader className=\"items-center pb-4\">\n                <CardTitle>Multi-Region Cluster Load</CardTitle>\n                <CardDescription>Capacity utilization across US, EU, and APAC compute clusters</CardDescription>\n            </CardHeader>\n            <CardContent className=\"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=\"Multi-Region Cluster Load Chart\"\n                    />\n                </div>\n            </CardContent>\n            <CardFooter className=\"flex-col gap-2 pt-4 text-sm\">\n                <div className=\"flex items-center gap-2 leading-none font-medium\">\n                    Global cluster capacity up by 5.2% this month <TrendingUp className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground flex items-center gap-2 leading-none\">\n                    Showing total visitors for the last 6 months\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default RadialStacked;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. A semi-circle stacked radial bar chart with centered total text built with TanStack Charts. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "radial"
    ]
  }
}