
A radial gauge chart displaying visitor metrics with centered text built with TanStack Charts.
components/tanstack-charts/radial/radial-chart-text.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { focusGroupAngle, pie, polar, radialArc, 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 [{ browser: "safari", visitors: 200, ring: "visitors" }];
}
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 browserMetric(datum: unknown) {
if (!datum || typeof datum !== "object") return undefined;
const browser = Reflect.get(datum, "browser");
const visitors = Reflect.get(datum, "visitors");
return typeof browser === "string" && typeof visitors === "number" && Number.isFinite(visitors)
? { browser, visitors }
: undefined;
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
const point = points.find((candidate) => browserMetric(candidate.datum));
const metric = point && browserMetric(point.datum);
return metric
? {
title: titleCase(metric.browser),
rows: [
{
label: "Visitors",
value: metric.visitors.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 RadialChartText() {
const data = useMemo(() => generateDemoData(), []);
const definition = useMemo(() => {
const background = pie([{ id: "background", value: 1 }], { value: "value" });
return defineChart(
{
renderer: motionRenderer,
motion: {
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
},
marks: [
polar({
scales: {
angle: null,
radius: null,
},
marks: [
radialArc(background, {
id: "text-background",
key: "id",
innerRadius: 80,
outerRadius: 90,
fill: "var(--muted)",
}),
],
}),
polar({
startAngle: rechartsPolarAngle(0),
endAngle: rechartsPolarAngle(250),
scales: {
angle: { scale: scaleLinear().domain([0, 200]) },
radius: {
scale: scaleBand<string>().domain(["visitors"]),
range: [80, 90],
},
},
marks: [
radialBarAngle(data, {
id: "text-value",
angle: "visitors",
radius: "ring",
key: "browser",
fill: "var(--chart-2)",
cornerRadius: 10,
}),
],
}),
radialCenterLabels("200", 36, 0, 24),
],
scales: {
x: null,
y: null,
},
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-0">
<CardTitle>Cloud Storage Allocation</CardTitle>
<CardDescription>Used disk space relative to workspace storage quota</CardDescription>
</CardHeader>
<CardContent className="flex-1 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="Cloud Storage Allocation Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
Storage capacity utilization 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 RadialChartText;