components/tanstack-charts/bar/error-bars.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;