{
  "title": "Error Bars",
  "description": "Sample point estimates with confidence deviation error range bars.",
  "name": "tanstack-charts-error-bars",
  "dependencies": [
    "@tanstack/charts",
    "@tanstack/react-charts",
    "d3-scale"
  ],
  "registryDependencies": [
    "card"
  ],
  "type": "registry:block",
  "files": [
    {
      "path": "components/blocks/tanstack-charts/bar/error-bars.tsx",
      "type": "registry:block",
      "content": "import { type ChartPoint, defineChart, dot, link, tickY } 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 { Sliders } from \"lucide-react\";\n\nimport { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from \"@/components/ui/card\";\n\ninterface ErrorBarDatum {\n    category: string;\n    mean: number;\n    low: number;\n    high: number;\n}\n\nconst motionRenderer = motion({\n    initial: \"always\",\n    transition: { type: \"spring\", stiffness: 170, damping: 18, mass: 1 },\n});\n\nfunction generateDemoData(): ErrorBarDatum[] {\n    return [\n        { category: \"Adelie\", mean: 3700, low: 3400, high: 4000 },\n        { category: \"Chinstrap\", mean: 3733, low: 3350, high: 4110 },\n        { category: \"Gentoo\", mean: 5076, low: 4500, high: 5650 },\n    ];\n}\n\nfunction shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {\n    if (points.length === 0) return { title: \"\", rows: [] };\n    const d = points[0].datum as ErrorBarDatum;\n    if (!d) return { title: \"\", rows: [] };\n    return {\n        title: d.category,\n        rows: [\n            { label: \"Mean Mass\", value: `${d.mean.toLocaleString(\"en-US\")} g`, color: \"var(--chart-1)\" },\n            {\n                label: \"Confidence Range\",\n                value: `${d.low.toLocaleString(\"en-US\")}–${d.high.toLocaleString(\"en-US\")} g`,\n                color: \"var(--foreground)\",\n            },\n        ],\n    };\n}\n\nexport function ErrorBars() {\n    const data = useMemo(() => generateDemoData(), []);\n\n    const definition = useMemo(\n        () =>\n            defineChart(\n                {\n                    renderer: motionRenderer,\n                    marks: [\n                        link(data, {\n                            id: \"error-interval\",\n                            x1: \"category\",\n                            y1: \"low\",\n                            x2: \"category\",\n                            y2: \"high\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2,\n                        }),\n                        tickY(data, {\n                            id: \"error-low\",\n                            x: \"category\",\n                            y: \"low\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2,\n                        }),\n                        tickY(data, {\n                            id: \"error-high\",\n                            x: \"category\",\n                            y: \"high\",\n                            stroke: \"var(--chart-1)\",\n                            strokeWidth: 2,\n                        }),\n                        dot(data, {\n                            id: \"error-mean\",\n                            x: \"category\",\n                            y: \"mean\",\n                            fill: \"var(--chart-1)\",\n                            r: 4,\n                        }),\n                    ],\n                    scales: {\n                        x: {\n                            scale: () => scaleBand<string>().padding(0.3),\n                            axis: {\n                                line: false,\n                                label: \"Species\",\n                                ticks: { size: 0, padding: 10 },\n                            },\n                        },\n                        y: {\n                            scale: scaleLinear().domain([3000, 6000]),\n                            grid: true,\n                            axis: {\n                                line: false,\n                                label: \"Body Mass (g)\",\n                                ticks: { size: 0 },\n                            },\n                        },\n                    },\n                    margin: { top: 24, right: 24, bottom: 32, left: 50 },\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                        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                        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>Sample Mean & Error Ranges</CardTitle>\n                <CardDescription>Point estimates with 95% confidence deviation error bars</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=\"Sample Mean & Error Ranges 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                    Gentoo species exhibits highest mean mass dispersion <Sliders className=\"h-4 w-4 text-blue-500\" />\n                </div>\n                <div className=\"text-muted-foreground leading-none\">\n                    Ticks indicate upper and lower standard error thresholds\n                </div>\n            </CardFooter>\n        </Card>\n    );\n}\n\nexport default ErrorBars;\n"
    }
  ],
  "mcp": {
    "description": "Renders responsive data visualization with dark mode styling and customizable tooltips. Sample point estimates with confidence deviation error range bars. Relies on card. Use when presenting data insights or analytics dashboards.",
    "tags": [
      "chart",
      "tanstack",
      "bar"
    ]
  }
}