{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "donut-chart",
  "type": "registry:component",
  "title": "Donut Chart",
  "description": "Gapped arc segments that sweep in, a centered total, a percentage legend and hover that expands a segment.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "charts",
    "chart",
    "donut",
    "pie",
    "svg",
    "legend",
    "analytics"
  ],
  "docs": "https://hyperblur.dev/effects/donut-chart",
  "files": [
    {
      "path": "registry/hyperblur/donut-chart.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/donut-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface DonutDatum {\n  label: string;\n  value: number;\n  color?: string;\n}\n\nexport interface DonutChartProps extends React.HTMLAttributes<HTMLDivElement> {\n  data: DonutDatum[];\n  size?: number;\n  thickness?: number;\n  centerLabel?: string;\n  showLegend?: boolean;\n  locale?: string;\n  animate?: boolean;\n}\n\nconst PALETTE = [\"#2f7bff\", \"#22c55e\", \"#f5b301\", \"#a78bfa\", \"#ef4444\", \"#14b8a6\", \"#ec4899\"];\n\nconst css = `\n.hb-dn { display: flex; align-items: center; gap: 20px; min-width: 0; color: #ededed; }\n.hb-dn-ring { position: relative; flex: none; }\n.hb-dn-svg { display: block; overflow: visible; }\n.hb-dn-track { fill: none; stroke: #1c1c1c; }\n.hb-dn-seg {\n  fill: none;\n  transition: stroke-dasharray var(--hb-dn-t, .6s) cubic-bezier(.3, .6, .3, 1) var(--hb-dn-d, 0s), stroke-width .2s ease, opacity .2s ease;\n}\n.hb-dn-seg[data-dim=\"true\"] { opacity: .35; }\n.hb-dn-center {\n  position: absolute;\n  inset: 0;\n  display: flex;\n  flex-direction: column;\n  align-items: center;\n  justify-content: center;\n  gap: 2px;\n  padding: 0 14%;\n  text-align: center;\n  pointer-events: none;\n}\n.hb-dn-big {\n  max-width: 100%;\n  overflow: hidden;\n  text-overflow: ellipsis;\n  white-space: nowrap;\n  font-family: var(--font-grotesk, ui-sans-serif, system-ui, sans-serif);\n  font-weight: 600;\n  letter-spacing: -.02em;\n  line-height: 1.1;\n  font-variant-numeric: tabular-nums;\n}\n.hb-dn-small {\n  max-width: 100%;\n  overflow: hidden;\n  text-overflow: ellipsis;\n  white-space: nowrap;\n  font-size: 11px;\n  line-height: 1.2;\n  color: #8f8f8f;\n}\n.hb-dn-legend { display: flex; flex-direction: column; gap: 2px; min-width: 0; margin: 0; padding: 0; list-style: none; }\n.hb-dn-item {\n  display: flex;\n  align-items: center;\n  gap: 8px;\n  padding: 3px 6px;\n  margin: 0 -6px;\n  border-radius: 6px;\n  font-size: 12px;\n  line-height: 1.3;\n  transition: background-color .15s ease, opacity .2s ease;\n}\n.hb-dn-item[data-on=\"true\"] { background: #1c1c1c; }\n.hb-dn-item[data-dim=\"true\"] { opacity: .5; }\n.hb-dn-dot { width: 8px; height: 8px; border-radius: 3px; flex: none; }\n.hb-dn-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: #ededed; }\n.hb-dn-pct {\n  margin-left: auto;\n  padding-left: 12px;\n  color: #8f8f8f;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-dn-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  margin: -1px;\n  padding: 0;\n  overflow: hidden;\n  clip: rect(0 0 0 0);\n  white-space: nowrap;\n  border: 0;\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-dn-seg, .hb-dn-item { transition: none !important; }\n}\n`;\n\nfunction useReady(animate: boolean) {\n  const [ready, setReady] = React.useState(!animate);\n  React.useEffect(() => {\n    if (!animate) return;\n    let a = 0;\n    let b = 0;\n    a = requestAnimationFrame(() => {\n      b = requestAnimationFrame(() => setReady(true));\n    });\n    return () => {\n      cancelAnimationFrame(a);\n      cancelAnimationFrame(b);\n    };\n  }, [animate]);\n  return animate ? ready : true;\n}\n\nexport function DonutChart({\n  data,\n  size = 180,\n  thickness = 22,\n  centerLabel,\n  showLegend = true,\n  locale = \"en-US\",\n  animate = true,\n  className = \"\",\n  ...rest\n}: DonutChartProps) {\n  const ready = useReady(animate);\n  const [active, setActive] = React.useState<number | null>(null);\n\n  const items = data\n    .map((d, i) => ({ ...d, color: d.color ?? PALETTE[i % PALETTE.length], idx: i }))\n    .filter((d) => Number.isFinite(d.value) && d.value > 0);\n  const total = items.reduce((s, d) => s + d.value, 0);\n\n  const nf = React.useMemo(\n    () => new Intl.NumberFormat(locale, { notation: \"compact\", maximumFractionDigits: 1 }),\n    [locale]\n  );\n  const full = React.useMemo(\n    () => new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }),\n    [locale]\n  );\n  const pf = React.useMemo(\n    () => new Intl.NumberFormat(locale, { style: \"percent\", maximumFractionDigits: 0 }),\n    [locale]\n  );\n\n  const grow = 4;\n  const stroke = Math.max(2, Math.min(thickness, size / 2 - 4));\n  const radius = Math.max(1, (size - stroke) / 2 - grow / 2);\n  const C = 2 * Math.PI * radius;\n  const gap = items.length > 1 ? Math.min(4, stroke * 0.3) : 0;\n  const cx = size / 2;\n\n  const segs = items.map((d, i) => ({\n    ...d,\n    frac: d.value / total,\n    start: items.slice(0, i).reduce((s, x) => s + x.value, 0) / total,\n  }));\n\n  const act = active === null ? null : (segs.find((s) => s.idx === active) ?? null);\n  const bigText = act ? pf.format(act.frac) : (centerLabel ?? nf.format(total));\n  const smallText = act ? act.label : centerLabel ? \"\" : \"Total\";\n  const bigSize = Math.max(12, Math.round(size * 0.17));\n\n  const label = segs.length\n    ? `Donut chart of ${segs.length} segments, total ${full.format(total)}. ${segs\n        .map((s) => `${s.label} ${pf.format(s.frac)}`)\n        .join(\", \")}.`\n    : \"Donut chart with no data\";\n\n  return (\n    <div className={`hb-dn ${className}`} {...rest}>\n      <style href=\"hb-donut-chart\" precedence=\"default\">{css}</style>\n      <div className=\"hb-dn-ring\" style={{ width: size, height: size }}>\n        <svg\n          className=\"hb-dn-svg\"\n          width={size}\n          height={size}\n          viewBox={`0 0 ${size} ${size}`}\n          role=\"img\"\n          aria-label={label}\n          onMouseLeave={() => setActive(null)}\n        >\n          <circle className=\"hb-dn-track\" cx={cx} cy={cx} r={radius} strokeWidth={stroke} />\n          <g transform={`rotate(-90 ${cx} ${cx})`}>\n            {segs.map((s) => {\n              const len = Math.max(0.001, s.frac * C - gap);\n              const on = active === s.idx;\n              return (\n                <circle\n                  key={s.idx}\n                  className=\"hb-dn-seg\"\n                  data-dim={active !== null && !on ? \"true\" : \"false\"}\n                  cx={cx}\n                  cy={cx}\n                  r={radius}\n                  stroke={s.color}\n                  strokeWidth={on ? stroke + grow : stroke}\n                  strokeDasharray={ready ? `${r2(len)} ${r2(C)}` : `0 ${r2(C)}`}\n                  strokeDashoffset={-r2(s.start * C + gap / 2)}\n                  style={{\n                    [\"--hb-dn-t\" as string]: `${Math.max(0.15, s.frac * 1.1)}s`,\n                    [\"--hb-dn-d\" as string]: `${r2(s.start * 1.1)}s`,\n                  }}\n                  onMouseEnter={() => setActive(s.idx)}\n                />\n              );\n            })}\n          </g>\n        </svg>\n        <div className=\"hb-dn-center\" aria-hidden=\"true\">\n          <div className=\"hb-dn-big\" style={{ fontSize: centerLabel && !act ? Math.max(11, bigSize - 2) : bigSize }}>\n            {bigText}\n          </div>\n          {smallText ? <div className=\"hb-dn-small\">{smallText}</div> : null}\n        </div>\n      </div>\n      {showLegend ? (\n        <ul className=\"hb-dn-legend\">\n          {segs.map((s) => (\n            <li\n              key={s.idx}\n              className=\"hb-dn-item\"\n              data-on={active === s.idx ? \"true\" : \"false\"}\n              data-dim={active !== null && active !== s.idx ? \"true\" : \"false\"}\n              onMouseEnter={() => setActive(s.idx)}\n              onMouseLeave={() => setActive(null)}\n            >\n              <span className=\"hb-dn-dot\" style={{ background: s.color }} />\n              <span className=\"hb-dn-name\">{s.label}</span>\n              <span className=\"hb-dn-pct\">{pf.format(s.frac)}</span>\n            </li>\n          ))}\n        </ul>\n      ) : (\n        <ul className=\"hb-dn-sr\">\n          {segs.map((s) => (\n            <li key={s.idx}>\n              {s.label}: {full.format(s.value)} ({pf.format(s.frac)})\n            </li>\n          ))}\n        </ul>\n      )}\n    </div>\n  );\n}\n\nfunction r2(n: number) {\n  return Math.round(n * 100) / 100;\n}\n"
    }
  ]
}