
A grouped bar chart comparing desktop and mobile visitors over 6 months built with TanStack Charts.
components/tanstack-charts/bar/bar-chart-multiple.tsx
import { type ChartPoint, barY, defineChart, group } 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";
const twoSeries = ["desktop", "mobile"] as const;
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function generateDemoData() {
return [
{ month: "January", value: 186, series: "desktop" },
{ month: "January", value: 80, series: "mobile" },
{ month: "February", value: 305, series: "desktop" },
{ month: "February", value: 200, series: "mobile" },
{ month: "March", value: 237, series: "desktop" },
{ month: "March", value: 120, series: "mobile" },
{ month: "April", value: 73, series: "desktop" },
{ month: "April", value: 190, series: "mobile" },
{ month: "May", value: 209, series: "desktop" },
{ month: "May", value: 130, series: "mobile" },
{ month: "June", value: 214, series: "desktop" },
{ month: "June", value: 140, series: "mobile" },
];
}
function shadcnXAxis() {
return {
scale: () => scaleBand<string>().paddingInner(0.2).paddingOuter(0.1),
axis: {
line: false,
ticks: {
size: 0,
padding: 10,
format: (value: string) => value.slice(0, 3),
},
},
};
}
function shadcnTheme() {
return {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
};
}
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 BarChartMultiple() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(() => {
const groupScale = scaleBand<string>().domain(twoSeries).paddingInner(0.08).paddingOuter(0);
return defineChart(
{
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
marks: [
barY(data, {
id: "visitor-bars",
x: "month",
y: "value",
z: "series",
color: "series",
key: (row) => `${row.month}:${row.series}`,
layout: group({ scale: groupScale }),
radius: 4,
}),
],
scales: {
x: shadcnXAxis(),
y: { scale: scaleLinear, grid: true, axis: false },
},
color: { domain: twoSeries, range: ["var(--chart-1)", "var(--chart-2)"] },
margin: { top: 10, right: 10, bottom: 30, left: 10 },
theme: shadcnTheme(),
},
{
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-2xl">
<CardHeader>
<CardTitle>Checkout Platform Conversion</CardTitle>
<CardDescription>Desktop vs Mobile completed checkout orders</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[300px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={300}
ariaLabel="Checkout Platform Conversion Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Mobile checkout completions 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 BarChartMultiple;