{
  "title": "Bar Chart Label",
  "description": "A bar chart with labels displayed above bars built with TanStack Charts.",
  "name": "tanstack-charts-bar-label",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/bar/bar-chart-label.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, barY, defineChart, text } 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 { TrendingUp } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface MonthDatum {\n    month: string;\n    desktop: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): MonthDatum[] {\n    return [\n        { month: \"January\", desktop: 186 },\n        { month: \"February\", desktop: 305 },\n        { month: \"March\", desktop: 237 },\n        { month: \"April\", desktop: 73 },\n        { month: \"May\", desktop: 209 },\n        { month: \"June\", desktop: 214 },\n    ];\n}\n\nfunction titleCase(value: string) {\n    return value.charAt(0).toUpperCase() + value.slice(1);\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    return {\n        title: String(points[0]?.xValue ?? \"\"),\n        rows: points.map((point) => ({\n            label: titleCase(\n                String(point.group ?? point.markId.replace(/-?(bars|lines|areas|slices|values|radar)$/u, \"\")),\n            ),\n            value: Number(point.yValue ?? point.xValue ?? 0).toLocaleString(\"en-US\"),\n            color: point.color,\n        })),\n    };\n}\n\nexport function BarChartLabel() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    motion: {\n                        transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n                    },\n                    marks: [\n                        barY(data, {\n                            id: \"visitor-bars\",\n                            x: \"month\",\n                            y: \"desktop\",\n                            fill: \"var(--chart-1)\",\n                            radius: 8,\n                        }),\n                        text(data, {\n                            id: \"visitor-labels\",\n                            x: \"month\",\n                            y: \"desktop\",\n                            text: (row: MonthDatum) => row.desktop.toLocaleString(\"en-US\"),\n                            dy: -12,\n                            fill: \"var(--foreground)\",\n                            fontSize: 12,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: () => scaleBand<string>().paddingInner(0.2).paddingOuter(0.1),\n                            axis: {\n                                line: false,\n                                ticks: {\n                                    size: 0,\n                                    padding: 10,\n                                    format: (value: string) => value.slice(0, 3),\n                                },\n                            },\n                        },\n                        y: { scale: scaleLinear, grid: true, axis: false },\n                    },\n                    margin: { top: 24, right: 5, bottom: 35, left: 5 },\n                    theme: {\n                        foreground: \"var(--muted-foreground)\",\n                        grid: \"var(--border)\",\n                        background: \"transparent\",\n                    },\n                },\n                {\n                    focus: \"group-x\",\n                    tooltip: {\n                        use: tooltip,\n                        motion: { type: \"spring\", stiffness: 170, damping: 18 },\n                        className:\n                            \"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\n    return (\n        <Card className=\"w-full max-w-xl\">\n            <CardHeader>\n                <CardTitle>Sprint Velocity Points</CardTitle>\n                <CardDescription>Engineering story points delivered per sprint cycle</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=\"Sprint Velocity Points 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                    Sprint delivery rate up by 5.2% this month <TrendingUp className=\"h-4 w-4 text-emerald-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">Showing total visitors for the last 6 months</div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default BarChartLabel;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. A bar chart with labels displayed above bars built with TanStack Charts. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "bar"
    ]
  }
}