# Interactive Active Donut Wedge Metric

> Clickable/hoverable donut sectors with detailed metric breakdown.

A shadcn/ui block from the [@paceui](https://paceui.com/?utm_source=shadcn.exchange) registry, listed on [shadcn.exchange](https://shadcn.exchange/r/paceui/tanstack-charts-active-donut-metric).

- Page, with a live preview: https://shadcn.exchange/r/paceui/tanstack-charts-active-donut-metric
- Screenshot: https://abzjhwgwpdpz7ua5.public.blob.vercel-storage.com/shots/paceui/tanstack-charts-active-donut-metric-light-843625f32fbca66f.png
- Registry item: https://paceui.com/r/tanstack-charts-active-donut-metric.json
- Listed: 2026-10-21, source last changed 2026-10-21

## Install

```bash
npx shadcn@latest add https://shadcn.exchange/r/paceui/tanstack-charts-active-donut-metric.json
```

Run it in a project that has shadcn/ui set up. It adds the files below and installs what they depend on.

## Dependencies

- npm packages: `@tanstack/charts`, `@tanstack/react-charts`, `d3-scale`
- Registry items: `card`

## Files

### `components/tanstack-charts/pie/active-donut-metric.tsx`

```tsx
import { type ChartPoint, defineChart } from "@tanstack/charts";
import { motion } from "@tanstack/charts/motion";
import { pie, polar, radialArc, radialText } from "@tanstack/charts/polar";
import { tooltip } from "@tanstack/charts/tooltip";
import { Chart } from "@tanstack/react-charts/core";
import { useMemo, useState } from "react";

import { scaleLinear } from "d3-scale";
import { MousePointerClick } from "lucide-react";

import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card";

interface BrowserDatum {
    id: string;
    label: string;
    visitors: number;
}

const motionRenderer = motion({
    initial: "always",
    transition: { type: "spring", stiffness: 170, damping: 18, mass: 1 },
});

const tau = Math.PI * 2;
const gapAngle = (Math.PI / 180) * 3;

const browserData: BrowserDatum[] = [
    { id: "chrome", label: "Chrome", visitors: 45000 },
    { id: "safari", label: "Safari", visitors: 28000 },
    { id: "firefox", label: "Firefox", visitors: 12000 },
    { id: "edge", label: "Edge", visitors: 9000 },
    { id: "other", label: "Other", visitors: 6000 },
];

const colors = ["var(--chart-1)", "var(--chart-2)", "var(--chart-3)", "var(--chart-4)", "var(--chart-5)"];

function shadcnTooltipContent<TDatum>(points: readonly ChartPoint<TDatum>[]) {
    if (points.length === 0) return { title: "", rows: [] };
    const d = points[0].datum as { label?: string; visitors?: number };
    if (!d || !d.label) return { title: "", rows: [] };
    return {
        title: d.label,
        rows: [{ label: "Sessions", value: `${(d.visitors || 0).toLocaleString()}`, color: "var(--chart-1)" }],
    };
}

export function ActiveDonutMetric() {
    const [activeId, setActiveId] = useState<string>("chrome");

    const totalVisitors = useMemo(() => browserData.reduce((acc, curr) => acc + curr.visitors, 0), []);
    const selected = useMemo(() => browserData.find((b) => b.id === activeId) || browserData[0], [activeId]);

    const definition = useMemo(() => {
        const arcs = pie(browserData, { value: "visitors", gapAngle });
        const activeArc = arcs.filter((a) => a.id === activeId);

        const centerRows = [
            { id: `${selected.id}:val`, angle: 0, radius: 0, text: selected.visitors.toLocaleString(), dy: -8 },
            { id: `${selected.id}:lbl`, angle: 0, radius: 0, text: selected.label, dy: 14 },
        ];

        return defineChart(
            {
                renderer: motionRenderer,
                scales: {
                    x: null,
                    y: null,
                },
                marks: [
                    polar({
                        id: "browser-donut",
                        radiusRatio: 0.72,
                        scales: {
                            angle: null,
                            radius: null,
                        },
                        marks: [
                            radialArc(arcs, {
                                id: "browser-arcs",
                                key: "id",
                                color: "id",
                                innerRadius: ({ radius }) => radius * 0.6,
                                cornerRadius: 6,
                            }),
                        ],
                    }),
                    polar({
                        id: "selected-browser",
                        radiusRatio: 0.82,
                        scales: {
                            angle: null,
                            radius: null,
                        },
                        marks: [
                            radialArc(activeArc, {
                                id: "selected-browser-wedge",
                                key: "id",
                                color: "id",
                                innerRadius: ({ radius }) => radius * 0.58,
                                cornerRadius: 7,
                                stroke: "var(--background)",
                                strokeWidth: 2,
                            }),
                        ],
                    }),
                    polar({
                        id: "donut-center",
                        radiusRatio: 0.8,
                        scales: {
                            angle: { scale: scaleLinear().domain([0, tau]) },
                            radius: { scale: scaleLinear().domain([0, 1]) },
                        },
                        marks: [
                            radialText(centerRows.slice(0, 1), {
                                id: "center-val",
                                angle: "angle",
                                radius: "radius",
                                key: "id",
                                text: "text",
                                dy: (row) => row.dy,
                                fill: "var(--foreground)",
                                fontSize: 22,
                                fontWeight: 700,
                            }),
                            radialText(centerRows.slice(1), {
                                id: "center-lbl",
                                angle: "angle",
                                radius: "radius",
                                key: "id",
                                text: "text",
                                dy: (row) => row.dy,
                                fill: "var(--muted-foreground)",
                                fontSize: 11,
                                fontWeight: 600,
                            }),
                        ],
                    }),
                ],
                color: {
                    domain: browserData.map((b) => b.id),
                    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),
                },
            },
        );
    }, [activeId, selected]);

    return (
        <Card className="w-full max-w-xl">
            <CardHeader>
                <CardTitle>Interactive Active Donut Wedge Metric Selection</CardTitle>
                <CardDescription>
                    Click/hover donut sectors or legend pills to highlight active metric focus
                </CardDescription>
            </CardHeader>
            <CardContent className="flex flex-col items-center gap-4 sm:flex-row">
                <div className="h-[230px] w-full sm:w-1/2">
                    <Chart
                        definition={definition}
                        renderer={motionRenderer}
                        height={230}
                        ariaLabel="Interactive Active Donut Wedge Metric Selection Chart"
                    />
                </div>
                <div className="flex w-full flex-col gap-1.5 sm:w-1/2">
                    {browserData.map((row, idx) => {
                        const isSelected = row.id === activeId;
                        const pct = Math.round((row.visitors / totalVisitors) * 100);
                        return (
                            <button
                                key={row.id}
                                type="button"
                                className={`flex items-center justify-between rounded-lg px-3 py-2 text-xs transition-colors ${
                                    isSelected
                                        ? "bg-accent text-accent-foreground font-semibold"
                                        : "hover:bg-muted/50 text-muted-foreground"
                                }`}
                                onClick={() => setActiveId(row.id)}>
                                <span className="flex items-center gap-2">
                                    <span
                                        className="h-2.5 w-2.5 rounded-full"
                                        style={{ backgroundColor: colors[idx] }}
                                    />
                                    {row.label}
                                </span>
                                <span>{pct}%</span>
                            </button>
                        );
                    })}
                </div>
            </CardContent>
            <CardFooter className="flex-col items-start gap-2 text-sm">
                <div className="flex items-center gap-2 leading-none font-medium">
                    Active: <strong className="text-foreground">{selected.label}</strong> (
                    {selected.visitors.toLocaleString()} sessions){" "}
                    <MousePointerClick className="h-4 w-4 text-emerald-500" />
                </div>
                <div className="text-muted-foreground leading-none">
                    `radialArc` dynamic wedge scaling highlights selected active sector
                </div>
            </CardFooter>
        </Card>
    );
}

export default ActiveDonutMetric;
```
