{
  "title": "Stacked Bar Chart with Dynamic Focus Band",
  "description": "Hover crosshairs highlight full column band with breakdown tooltip.",
  "name": "tanstack-charts-stacked-bar-band-cursor",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/bar/stacked-bar-band-cursor.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, barY, colorLegend, crosshair, defineChart, stack } from \"@tanstack/charts\";\nimport { motion } from \"@tanstack/charts/motion\";\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 { Layers } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface StackedCauseDatum {\n    id: string;\n    period: string;\n    cause: string;\n    deaths: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nconst periods = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\"];\nconst causes = [\"Zymotic\", \"Wounds\", \"Other\"];\nconst colors = [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\"];\n\nfunction generateDemoData(): StackedCauseDatum[] {\n    const results: StackedCauseDatum[] = [];\n    periods.forEach((p) => {\n        causes.forEach((c) => {\n            const val = Math.floor(20 + Math.random() * 80);\n            results.push({\n                id: `${p}-${c}`,\n                period: p,\n                cause: c,\n                deaths: val,\n            });\n        });\n    });\n    return results;\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as StackedCauseDatum;\n    if (!d) return { title: \"\", rows: [] };\n    return {\n        title: `${d.period} - ${d.cause}`,\n        rows: [{ label: \"Count\", value: `${d.deaths} cases`, color: \"var(--chart-1)\" }],\n    };\n}\n\nexport function StackedBarBandCursor() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        crosshair<string, number>({\n                            id: \"stacked-cursor-band\",\n                            x: {\n                                band: {\n                                    fill: \"var(--muted-foreground)\",\n                                    fillOpacity: 0.15,\n                                    radius: 3,\n                                },\n                            },\n                            y: false,\n                        }),\n                        barY(data, {\n                            id: \"stacked-cursor-bars\",\n                            x: \"period\",\n                            y: \"deaths\",\n                            z: \"cause\",\n                            color: \"cause\",\n                            key: \"id\",\n                            layout: stack({ order: causes }),\n                            radius: 2,\n                        }),\n                        crosshair<string, number>({\n                            id: \"stacked-cursor-rule\",\n                            x: false,\n                            y: {\n                                stroke: \"var(--foreground)\",\n                                strokeOpacity: 0.7,\n                                strokeWidth: 1,\n                                strokeDasharray: \"4 4\",\n                            },\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: scaleBand<string>().domain(periods).padding(0.2),\n                        },\n                        y: {\n                            scale: scaleLinear().domain([0, 300]),\n                            grid: true,\n                            axis: { label: \"Total Cases\" },\n                        },\n                    },\n                    color: {\n                        domain: causes,\n                        range: colors,\n                        legend: colorLegend({ label: \"Cause Category\" }),\n                    },\n                    focus: \"group-x\",\n                    maxFocusDistance: Number.POSITIVE_INFINITY,\n                    margin: { top: 24, right: 24, bottom: 32, left: 45 },\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:\n                            \"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\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Stacked Bar Chart with Dynamic Focus X-Band Cursor</CardTitle>\n                <CardDescription>\n                    Hover crosshairs highlight full column bands and project horizontal segment rules\n                </CardDescription>\n            </CardHeader>\n            <CardContent>\n                <div className=\"h-[250px] w-full\">\n                    <Chart\n                        definition={definition}\n                        renderer={motionRenderer}\n                        height={250}\n                        ariaLabel=\"Stacked Bar Chart with Dynamic Focus X-Band Cursor 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                    Crosshair band highlights active period column <Layers className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    `crosshair` with `band` fill parameter tracks discrete band scale columns\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default StackedBarBandCursor;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Hover crosshairs highlight full column band with breakdown tooltip. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "bar"
    ]
  }
}