# Error Bars

> Sample point estimates with confidence deviation error range bars.

A shadcn/ui block from the [@pacekit](https://ui.pacekit.dev/?utm_source=shadcn.exchange) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/pacekit/tanstack-charts-error-bars).

- Page, with a live preview: https://shadcn.exchange/r/pacekit/tanstack-charts-error-bars
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/pacekit/tanstack-charts-error-bars-light-acd26fadf3c3830f.png
- Registry item: https://ui.pacekit.dev/r/tanstack-charts-error-bars.json
- Listed: 2026-10-21, source last changed 2026-10-21

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/pacekit/tanstack-charts-error-bars.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@tanstack/charts`, `@tanstack/react-charts`, `d3-scale`
- Registry items: `card`

## Files

### `components/tanstack-charts/bar/error-bars.tsx`

```tsx
import { type ChartPoint, defineChart, dot, link, tickY } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";

import { scaleBand, scaleLinear } from "d3-scale";
import { Sliders } from "lucide-react";

import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";

interface ErrorBarDatum {
    category: string;
    mean: number;
    low: number;
    high: number;
}

const motionRenderer = motion({
    initial: "always",
    transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});

function generateDemoData(): ErrorBarDatum[] {
    return [
        { category: "Adelie", mean: 3700, low: 3400, high: 4000 },
        { category: "Chinstrap", mean: 3733, low: 3350, high: 4110 },
        { category: "Gentoo", mean: 5076, low: 4500, high: 5650 },
    ];
}

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const d = points[0].datum as ErrorBarDatum;
    if (!d) return { title: "", rows: [] };
    return {
        title: d.category,
        rows: [
            { label: "Mean Mass", value: `${d.mean.toLocaleString("en-US")} g`, color: "var(--chart-1)" },
            {
                label: "Confidence Range",
                value: `${d.low.toLocaleString("en-US")}–${d.high.toLocaleString("en-US")} g`,
                color: "var(--foreground)",
            },
        ],
    };
}

export function ErrorBars() {
    const data = useMemo(() => generateDemoData(), []);

    const definition = useMemo(
        () =>
            defineChart(
                {
                    renderer: motionRenderer,
                    marks: [
                        link(data, {
                            id: "error-interval",
                            x1: "category",
                            y1: "low",
                            x2: "category",
                            y2: "high",
                            stroke: "var(--chart-1)",
                            strokeWidth: 2,
                        }),
                        tickY(data, {
                            id: "error-low",
                            x: "category",
                            y: "low",
                            stroke: "var(--chart-1)",
                            strokeWidth: 2,
                        }),
                        tickY(data, {
                            id: "error-high",
                            x: "category",
                            y: "high",
                            stroke: "var(--chart-1)",
                            strokeWidth: 2,
                        }),
                        dot(data, {
                            id: "error-mean",
                            x: "category",
                            y: "mean",
                            fill: "var(--chart-1)",
                            r: 4,
                        }),
                    ],
                    scales: {
                        x: {
                            scale: () => scaleBand<string>().padding(0.3),
                            axis: {
                                line: false,
                                label: "Species",
                                ticks: { size: 0, padding: 10 },
                            },
                        },
                        y: {
                            scale: scaleLinear().domain([3000, 6000]),
                            grid: true,
                            axis: {
                                line: false,
                                label: "Body Mass (g)",
                                ticks: { size: 0 },
                            },
                        },
                    },
                    margin: { top: 24, right: 24, bottom: 32, left: 50 },
                    theme: {
                        foreground: "var(--muted-foreground)",
                        grid: "var(--border)",
                        background: "transparent",
                    },
                },
                {
                    focus: "group-x",
                    tooltip: {
                        use: tooltip,
                        className:
                            "font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
                        anchor: "group-center",
                        placement: "auto",
                        content: (points) => shadcnTooltipContent(points),
                    },
                },
            ),
        [data],
    );

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Sample Mean & Error Ranges</CardTitle>
                <CardDescription>Point estimates with 95% confidence deviation error bars</CardDescription>
            </CardHeader>
            <CardContent>
                <div className="h-[250px] w-full">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={250}
                        ariaLabel="Sample Mean & Error Ranges Chart"
                    />
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Gentoo species exhibits highest mean mass dispersion <Sliders className="h-4 w-4 text-blue-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    Ticks indicate upper and lower standard error thresholds
                </div>
            </CardFooter>
        </Card>
    );
}

export default ErrorBars;
```
