{
  "title": "Radial Chart Shape",
  "description": "A single metric gauge chart with custom shape and centered total text built with TanStack Charts.",
  "name": "tanstack-charts-radial-shape",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/radial/radial-shape.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\nimport { focusGroupAngle, pie, polar, radialArc, 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 [{ browser: \"safari\", visitors: 1260, ring: \"visitors\", fill: \"var(--chart-2)\" }];\n}\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 browserMetric(datum: unknown) {\n    if (!datum || typeof datum !== \"object\") return undefined;\n    const browser = Reflect.get(datum, \"browser\");\n    const visitors = Reflect.get(datum, \"visitors\");\n    return typeof browser === \"string\" && typeof visitors === \"number\" && Number.isFinite(visitors)\n        ? { browser, visitors }\n        : undefined;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    const point = points.find((candidate) => browserMetric(candidate.datum));\n    const metric = point && browserMetric(point.datum);\n    return metric\n        ? {\n              title: titleCase(metric.browser),\n              rows: [\n                  {\n                      label: \"Visitors\",\n                      value: metric.visitors.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 RadialShape() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(() => {\n        const background = pie([{ id: \"background\", value: 1 }], { value: \"value\" });\n\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                        scales: {\n                            angle: null,\n                            radius: null,\n                        },\n                        marks: [\n                            radialArc(background, {\n                                id: \"shape-background\",\n                                key: \"id\",\n                                innerRadius: 74,\n                                outerRadius: 86,\n                                fill: \"var(--muted)\",\n                            }),\n                        ],\n                    }),\n                    polar({\n                        startAngle: rechartsPolarAngle(0),\n                        endAngle: rechartsPolarAngle(100),\n                        scales: {\n                            angle: { scale: scaleLinear().domain([0, 1260]) },\n                            radius: {\n                                scale: scaleBand<string>().domain([\"visitors\"]),\n                                range: [65, 95],\n                            },\n                        },\n                        marks: [\n                            radialBarAngle(data, {\n                                id: \"shape-value\",\n                                angle: \"visitors\",\n                                radius: \"ring\",\n                                key: \"browser\",\n                                fill: (row) => row.fill,\n                                cornerRadius: 0,\n                            }),\n                        ],\n                    }),\n                    radialCenterLabels(\"1,260\", 36, 0, 24),\n                ],\n                scales: {\n                    x: null,\n                    y: null,\n                },\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>Service SLA Uptime Level</CardTitle>\n                <CardDescription>System operational uptime tracking against 99.98% SLA target</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=\"Service SLA Uptime Level 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                    SLA uptime performance 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 RadialShape;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. A single metric gauge chart with custom shape and centered total text built with TanStack Charts. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "radial"
    ]
  }
}