
A semi-circle stacked radial bar chart with centered total text built with TanStack Charts.
components/tanstack-charts/radial/radial-stacked.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { focusGroupAngle, polar, radialBarAngle, radialText } 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 { TrendingUp } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
function generateDemoData() {
return [
{
id: "mobile",
ring: "visitors",
start: 0,
end: 570,
fill: "var(--chart-2)",
},
{
id: "desktop",
ring: "visitors",
start: 570,
end: 1830,
fill: "var(--chart-1)",
},
];
}
const twoSeries = ["desktop", "mobile"] as const;
const chartColors = ["var(--chart-1)", "var(--chart-2)"];
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
function rechartsPolarAngle(degrees: number) {
return ((90 - degrees) * Math.PI) / 180;
}
function titleCase(value: string) {
return value.charAt(0).toUpperCase() + value.slice(1);
}
function extractDatumMetric(datum: unknown) {
if (!datum || typeof datum !== "object") return undefined;
const label =
Reflect.get(datum, "id") ??
Reflect.get(datum, "ring") ??
Reflect.get(datum, "browser") ??
Reflect.get(datum, "month");
const val = Reflect.get(datum, "end");
const start = Reflect.get(datum, "start");
const value = typeof val === "number" && typeof start === "number" ? val - start : val;
return typeof label === "string" && typeof value === "number" && Number.isFinite(value)
? { label, value }
: undefined;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
const point = points.find((candidate) => extractDatumMetric(candidate.datum));
const metric = point && extractDatumMetric(point.datum);
return metric
? {
title: titleCase(metric.label),
rows: [
{
label: "Visitors",
value: metric.value.toLocaleString("en-US"),
color: point.color,
},
],
}
: { rows: [] };
}
function radialCenterLabels(total: string, fontSize: number, totalDy: number, labelDy: number) {
return polar({
scales: {
angle: { scale: scaleLinear().domain([0, 1]) },
radius: { scale: scaleLinear().domain([0, 1]) },
},
marks: [
radialText([{ id: "total", angle: 0, radius: 0, text: total }], {
id: `radial-total-${total}`,
angle: "angle",
radius: "radius",
key: "id",
text: "text",
dy: totalDy,
fill: "var(--foreground)",
fontSize,
fontWeight: 700,
}),
radialText([{ id: "label", angle: 0, radius: 0, text: "Visitors" }], {
id: `radial-label-${total}`,
angle: "angle",
radius: "radius",
key: "id",
text: "text",
dy: labelDy,
fill: "var(--muted-foreground)",
fontSize: 14,
}),
],
});
}
export function RadialStacked() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(() => {
return defineChart(
{
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
marks: [
polar({
startAngle: rechartsPolarAngle(0),
endAngle: rechartsPolarAngle(360),
scales: {
angle: { scale: scaleLinear().domain([0, 1830]) },
radius: {
scale: scaleBand<string>().domain(["visitors"]),
range: [80, 110],
},
},
marks: [
radialBarAngle(data, {
id: "stacked-values",
angle1: "start",
angle2: "end",
angle: "end",
radius: "ring",
key: "id",
fill: (row) => row.fill,
cornerRadius: 5,
stroke: "transparent",
strokeWidth: 2,
}),
],
}),
radialCenterLabels("1,830", 24, -8, 14),
],
scales: {
x: null,
y: null,
},
color: { domain: twoSeries, range: chartColors },
margin: 0,
},
{
focus: focusGroupAngle,
keyboard: false,
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="flex w-full max-w-md flex-col">
<CardHeader className="items-center pb-4">
<CardTitle>Multi-Region Cluster Load</CardTitle>
<CardDescription>Capacity utilization across US, EU, and APAC compute clusters</CardDescription>
</CardHeader>
<CardContent className="pb-0">
<div className="mx-auto flex aspect-square max-h-[250px] w-full items-center justify-center">
<Chart
definition={definition}
renderer={motionRenderer}
initialWidth={250}
height={250}
ariaLabel="Multi-Region Cluster Load Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col gap-2 pt-4 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Global cluster capacity up by 5.2% this month <TrendingUp className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground flex items-center gap-2 leading-none">
Showing total visitors for the last 6 months
</div>
</CardFooter>
</Card>
);
}
export default RadialStacked;