
Concentric stacked rings with rounded pill caps tracking progress.
components/tanstack-charts/pie/radial-bars.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { polar, radialBarAngle } from "@tanstack/charts/polar";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { scaleBand, scaleLinear } from "d3-scale";
import { CircleDot } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface RadialMetric {
category: string;
value: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const categories = ["Activity Goal", "Calorie Burn", "Hydration", "Sleep Quality"];
const colors = ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)", "var(--chart-4)"];
function generateDemoData(): RadialMetric[] {
return [
{ category: "Activity Goal", value: 85 },
{ category: "Calorie Burn", value: 65 },
{ category: "Hydration", value: 90 },
{ category: "Sleep Quality", value: 75 },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as RadialMetric;
if (!d) return { title: "", rows: [] };
return {
title: d.category,
rows: [{ label: "Target Completion", value: `${d.value}%`, color: "var(--chart-1)" }],
};
}
export function RadialBars() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
scales: {
x: null,
y: null,
},
marks: [
polar({
radiusRatio: 0.82,
scales: {
angle: {
scale: scaleLinear().domain([0, 100]),
},
radius: {
scale: () =>
scaleBand<string>().domain(categories).paddingInner(0.35).paddingOuter(0.15),
range: [({ radius }) => radius * 0.2, ({ radius }) => radius],
},
},
marks: [
radialBarAngle(data, {
id: "category-bars",
angle: "value",
radius: "category",
key: "category",
color: "category",
cornerRadius: "full",
}),
],
}),
],
color: { domain: categories, range: colors },
margin: { top: 20, right: 20, bottom: 20, left: 20 },
theme: {
foreground: "var(--muted-foreground)",
grid: "var(--border)",
background: "transparent",
},
},
{
keyboard: true,
tooltip: {
use: tooltip,
className:
"font-sans shadow-none rounded-md bg-popover text-popover-foreground border p-2 text-xs",
anchor: "pointer",
placement: "auto",
content: (points) => shadcnTooltipContent(points),
},
},
),
[data],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Concentric Circular Radial Metric Bars</CardTitle>
<CardDescription>
Concentric stacked rings with rounded pill caps tracking progress metrics
</CardDescription>
</CardHeader>
<CardContent>
<div className="h-[250px] w-full">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Concentric Circular Radial Metric Bars Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Apple Watch-style concentric progress rings with pill caps{" "}
<CircleDot className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
`radialBarAngle` maps values to arc angles across nested band scale radiuses
</div>
</CardFooter>
</Card>
);
}
export default RadialBars;