
A horizontal bar chart with custom month and visitor labels built with TanStack Charts.
components/tanstack-charts/bar/bar-chart-label-custom.tsx
import { type ChartPoint, barX, defineChart, text } 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 { TrendingUp } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface MonthDatum {
month: string;
desktop: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData(): MonthDatum[] {
return [
{ month: "January", desktop: 186 },
{ month: "February", desktop: 305 },
{ month: "March", desktop: 237 },
{ month: "April", desktop: 73 },
{ month: "May", desktop: 209 },
{ month: "June", desktop: 214 },
];
}
function titleCase(value: string) {
return value.charAt(0).toUpperCase() + value.slice(1);
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
return {
title: String(points[0]?.xValue ?? ""),
rows: points.map((point) => ({
label: titleCase(
String(point.group ?? point.markId.replace(/-?(bars|lines|areas|slices|values|radar)$/u, "")),
),
value: Number(point.yValue ?? point.xValue ?? 0).toLocaleString("en-US"),
color: point.color,
})),
};
}
export function BarChartLabelCustom() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
marks: [
barX(data, {
id: "visitor-bars",
x: "desktop",
y: "month",
fill: "var(--chart-1)",
radius: 4,
}),
text(data, {
id: "month-labels",
x: () => 0,
y: "month",
text: "month",
dx: 8,
anchor: "start",
fill: "var(--background)",
fontSize: 12,
}),
text(data, {
id: "visitor-labels",
x: "desktop",
y: "month",
text: (row: MonthDatum) => row.desktop.toLocaleString("en-US"),
dx: 8,
anchor: "start",
fill: "var(--foreground)",
fontSize: 12,
}),
],
scales: {
x: { scale: scaleLinear, grid: true, axis: false },
y: {
scale: () => scaleBand<string>().paddingInner(0.18).paddingOuter(0.08),
axis: false,
},
},
margin: { top: 5, right: 36, bottom: 5, left: 0 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
focus: "group-x",
tooltip: {
use: tooltip,
motion: { type: "spring", stiffness: 170, damping: 18 },
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "group-center",
placement: "auto",
sort: "color-domain",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Customer Support Workload</CardTitle>
<CardDescription>Resolved support ticket volume by queue</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Customer Support Workload Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Resolution velocity up by 5.2% this month <TrendingUp className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">Showing total visitors for the last 6 months</div>
</CardFooter>
</Card>
);
}
export default BarChartLabelCustom;