components/tanstack-charts/pie/gauge-chart.tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { pie, polar, radialArc } from "@tanstack/charts/polar";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo } from "react";
import { Gauge } from "lucide-react";
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";
interface GaugeSegment {
id: "value" | "remainder";
label: string;
value: number;
}
const motionRenderer = motion({
initial: "always",
transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});
const startAngle = (-Math.PI * 3) / 4;
const endAngle = (Math.PI * 3) / 4;
function generateDemoData(): GaugeSegment[] {
const value = 78;
return [
{ id: "value", label: "System Health Score", value },
{ id: "remainder", label: "Remaining Gap", value: 100 - value },
];
}
function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
if (points.length === 0) return { title: "", rows: [] };
const d = points[0].datum as GaugeSegment;
if (!d) return { title: "", rows: [] };
return {
title: d.label,
rows: [
{ label: "Percentage", value: `${d.value}%`, color: d.id === "value" ? "var(--chart-1)" : "var(--muted)" },
],
};
}
export function GaugeChart() {
const data = useMemo(() => generateDemoData(), []);
const arcs = useMemo(() => {
return pie(data, {
value: "value",
startAngle,
endAngle,
});
}, [data]);
const definition = useMemo(
() =>
defineChart(
{
renderer: motionRenderer,
scales: {
x: null,
y: null,
},
marks: [
polar({
inset: 0,
radiusRatio: 0.8,
scales: {
angle: null,
radius: null,
},
marks: [
radialArc(arcs, {
id: "gauge-segments",
key: "id",
innerRadius: ({ radius }: { radius: number }) => radius * 0.72,
color: "id",
stroke: "var(--background)",
strokeWidth: 2,
}),
],
}),
],
color: {
domain: ["value", "remainder"],
range: ["var(--chart-1)", "var(--muted)"],
},
margin: 10,
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),
},
},
),
[arcs],
);
return (
<Card className="w-full max-w-xl">
<CardHeader>
<CardTitle>Radial Gauge Metric Dial</CardTitle>
<CardDescription>
Arc meter dial indicating percentage completion relative to maximum target
</CardDescription>
</CardHeader>
<CardContent>
<div className="flex h-[250px] w-full items-center justify-center">
<Chart
definition={definition}
renderer={motionRenderer}
height={250}
ariaLabel="Radial Gauge Metric Dial Chart"
/>
</div>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="flex items-center gap-2 leading-none font-medium">
System health operating at 78% optimal target <Gauge className="h-4 w-4 text-emerald-500" />
</div>
<div className="text-muted-foreground leading-none">
Sweeping 270-degree radial arc provides clean speedometer gauge visualization
</div>
</CardFooter>
</Card>
);
}
export default GaugeChart;