{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "pie-chart",
  "title": "Pie Chart",
  "description": "A donut-style pie chart with a live center total, click-to-toggle legend, percentage callouts around the ring, and a hover tooltip with ARR, account count, and average contract size.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/pie-chart/pie-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type PieChartTheme = \"light\" | \"dark\";\n\nexport interface PieChartSlice {\n  visible?: boolean;\n  label: string;\n  color: string;\n  arr: number;\n  accounts: number;\n  avgContract: number;\n}\n\nexport interface PieChartProps {\n  theme?: PieChartTheme;\n  title?: string;\n  subtitle?: string;\n  chartHeight?: number;\n  centerLabel?: string;\n  centerCaption?: string;\n  outerRadius?: number;\n  innerRadiusRatio?: number;\n  gapDeg?: number;\n  slices?: PieChartSlice[];\n  enableHover?: boolean;\n  className?: string;\n}\n\nconst VB_W = 1200;\nconst VB_H = 560;\nconst PAD_TOP = 26;\nconst PAD_BOTTOM = 44;\n\nfunction formatMoney(n: number): string {\n  if (Math.abs(n) >= 1_000_000) return `$${(n / 1_000_000).toFixed(2)}M`;\n  return `$${Math.round(n / 1000)}K`;\n}\n\nfunction formatAvgContract(n: number): string {\n  return `$${(n / 1000).toFixed(1)}K`;\n}\n\nfunction round(n: number): number {\n  return Math.round(n * 1000) / 1000;\n}\n\nfunction pointOnCircle(cx: number, cy: number, r: number, angle: number) {\n  return { x: round(cx + r * Math.cos(angle)), y: round(cy + r * Math.sin(angle)) };\n}\n\nconst DEFAULT_SLICES: PieChartSlice[] = [\n  { visible: true, label: \"Enterprise\", color: \"#C4F26B\", arr: 437500, accounts: 85, avgContract: 5147 },\n  { visible: true, label: \"Scale\", color: \"#F2A841\", arr: 325000, accounts: 210, avgContract: 1548 },\n  { visible: true, label: \"Growth\", color: \"#FF7A6B\", arr: 214000, accounts: 146, avgContract: 1466 },\n  { visible: true, label: \"Partner\", color: \"#8FB8FF\", arr: 125000, accounts: 60, avgContract: 2083 },\n  { visible: true, label: \"Starter\", color: \"#FF7DB8\", arr: 87500, accounts: 175, avgContract: 500 },\n  { visible: true, label: \"Legacy\", color: \"#4DD6E8\", arr: 61000, accounts: 40, avgContract: 1525 },\n];\n\nconst LIGHT_SLICE_COLORS = [\"#65A30D\", \"#D9822B\", \"#E5484D\", \"#3B6FD8\", \"#DB2777\", \"#0891B2\"];\nconst LIGHT_SLICES: PieChartSlice[] = DEFAULT_SLICES.map((s, i) => ({ ...s, color: LIGHT_SLICE_COLORS[i] }));\n\nexport function PieChart({\n  theme = \"dark\",\n  title = \"ARR by plan tier\",\n  subtitle = \"\",\n  chartHeight = 420,\n  centerLabel = \"ARR Mix\",\n  centerCaption = \"plans tracked\",\n  outerRadius = 150,\n  innerRadiusRatio = 0.5,\n  gapDeg = 6,\n  slices = theme === \"light\" ? LIGHT_SLICES : DEFAULT_SLICES,\n  enableHover = true,\n  className,\n}: PieChartProps) {\n  const isDark = theme === \"dark\";\n  const containerRef = React.useRef<HTMLDivElement>(null);\n  const [hoverIndex, setHoverIndex] = React.useState<number | null>(null);\n  const [hidden, setHidden] = React.useState<Set<number>>(new Set());\n  const [containerWidth, setContainerWidth] = React.useState(0);\n\n  React.useEffect(() => {\n    const el = containerRef.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) => setContainerWidth(entry.contentRect.width));\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  // preserveAspectRatio=\"none\" stretches the SVG to fill its box; on a narrow\n  // container that non-uniform scaling squishes the donut into an ellipse, so\n  // below the viewBox's natural aspect ratio we shrink the box to match it.\n  const effectiveHeight = containerWidth > 0 ? Math.min(chartHeight, containerWidth * (VB_H / VB_W)) : chartHeight;\n\n  const palette = {\n    cardBg: isDark ? \"#0E0E0E\" : \"#ffffff\",\n    cardBorder: isDark ? \"rgba(255,255,255,0.08)\" : \"rgba(0,0,0,0.06)\",\n    textPrimary: isDark ? \"#f4f5f7\" : \"#111317\",\n    textSub: isDark ? \"rgba(255,255,255,0.55)\" : \"rgba(17,19,23,0.55)\",\n    badgeBorder: isDark ? \"rgba(255,255,255,0.12)\" : \"rgba(0,0,0,0.08)\",\n  };\n\n  const headerHeight = (title ? 30 : 0) + (subtitle ? 22 : 0);\n\n  const cx = VB_W / 2;\n  const cy = PAD_TOP + headerHeight + (VB_H - PAD_TOP - headerHeight - PAD_BOTTOM) / 2;\n  const innerR = outerRadius * innerRadiusRatio;\n  const ringR = (outerRadius + innerR) / 2;\n  const ringWidth = outerRadius - innerR;\n  const gapRad = (gapDeg * Math.PI) / 180;\n\n  const active = React.useMemo(\n    () => slices.map((s, i) => ({ ...s, i })).filter((s) => s.visible !== false && !hidden.has(s.i)),\n    [slices, hidden],\n  );\n  const total = active.reduce((sum, s) => sum + s.arr, 0);\n\n  const arcs = React.useMemo(() => {\n    const result: Array<PieChartSlice & { i: number; fraction: number; start: number; end: number; mid: number }> = [];\n    let cursor = -Math.PI / 2;\n    for (const s of active) {\n      const fraction = total > 0 ? s.arr / total : 0;\n      const sweep = fraction * Math.PI * 2;\n      const start = cursor + gapRad / 2;\n      const end = cursor + sweep - gapRad / 2;\n      const mid = cursor + sweep / 2;\n      cursor += sweep;\n      result.push({ ...s, fraction, start: Math.min(start, end), end: Math.max(start, end), mid });\n    }\n    return result;\n  }, [active, total, gapRad]);\n\n  const hovered = hoverIndex !== null ? arcs.find((a) => a.i === hoverIndex) : undefined;\n\n  const toggleSlice = (i: number) => {\n    setHidden((prev) => {\n      const next = new Set(prev);\n      if (next.has(i)) next.delete(i);\n      else next.add(i);\n      return next;\n    });\n  };\n\n  return (\n    <div\n      className={cn(\"box-border flex w-full flex-col gap-1 overflow-hidden rounded-[20px]\", className)}\n      style={{ backgroundColor: palette.cardBg, border: `1px solid ${palette.cardBorder}`, padding: \"28px 32px\", fontFamily: \"Inter, -apple-system, BlinkMacSystemFont, sans-serif\" }}\n    >\n      {title && <div className=\"text-center text-[17px] font-bold\" style={{ color: palette.textPrimary }}>{title}</div>}\n      {subtitle && (\n        <div className=\"mx-auto mt-0.5 max-w-[760px] text-center text-[12.5px] leading-[1.5]\" style={{ color: palette.textSub }}>\n          {subtitle}\n        </div>\n      )}\n\n      <div ref={containerRef} className=\"relative mt-3 w-full\" style={{ height: effectiveHeight }}>\n        <svg width=\"100%\" height=\"100%\" viewBox={`0 0 ${VB_W} ${VB_H}`} preserveAspectRatio=\"none\" className=\"block overflow-visible\">\n          {arcs.map((a) => {\n            if (a.end - a.start <= 0) return null;\n            const p1 = pointOnCircle(cx, cy, ringR, a.start);\n            const p2 = pointOnCircle(cx, cy, ringR, a.end);\n            const largeArc = a.end - a.start > Math.PI ? 1 : 0;\n            const d = `M ${p1.x} ${p1.y} A ${ringR} ${ringR} 0 ${largeArc} 1 ${p2.x} ${p2.y}`;\n            return (\n              <path\n                key={a.i}\n                d={d}\n                fill=\"none\"\n                stroke={a.color}\n                strokeWidth={ringWidth}\n                strokeLinecap=\"round\"\n                opacity={hoverIndex === null || hoverIndex === a.i ? 1 : 0.45}\n                style={{ cursor: enableHover ? \"pointer\" : \"default\" }}\n                onMouseEnter={() => enableHover && setHoverIndex(a.i)}\n                onMouseLeave={() => enableHover && setHoverIndex(null)}\n              />\n            );\n          })}\n        </svg>\n\n        {arcs.map((a) => {\n          if (a.fraction <= 0) return null;\n          const pos = pointOnCircle(cx, cy, outerRadius + 34, a.mid);\n          return (\n            <div\n              key={`pct-${a.i}`}\n              className=\"pointer-events-none absolute rounded-full text-[11px] font-extrabold whitespace-nowrap\"\n              style={{\n                left: `${(pos.x / VB_W) * 100}%`,\n                top: `${(pos.y / VB_H) * 100}%`,\n                transform: \"translate(-50%, -50%)\",\n                padding: \"2px 8px\",\n                backgroundColor: `${a.color}33`,\n                border: `1px solid ${a.color}77`,\n                color: palette.textPrimary,\n              }}\n            >\n              {Math.round(a.fraction * 100)}%\n            </div>\n          );\n        })}\n\n        <div\n          className=\"absolute pointer-events-none text-center\"\n          style={{\n            left: `${(cx / VB_W) * 100}%`,\n            top: `${(cy / VB_H) * 100}%`,\n            transform: \"translate(-50%, -50%)\",\n            width: innerR * 1.7,\n          }}\n        >\n          <div className=\"text-[11px] font-bold uppercase tracking-[0.8px]\" style={{ color: palette.textSub }}>\n            {hovered ? hovered.label : centerLabel}\n          </div>\n          <div className=\"mt-1 text-[24px] font-extrabold\" style={{ color: palette.textPrimary }}>\n            {formatMoney(hovered ? hovered.arr : total)}\n          </div>\n          <div className=\"mt-0.5 text-[11px]\" style={{ color: palette.textSub }}>\n            {hovered ? `${Math.round(hovered.fraction * 100)}% of total` : `${active.length} plans tracked`}\n          </div>\n          <div className=\"sr-only\">{centerCaption}</div>\n        </div>\n\n        {enableHover && hoverIndex !== null && hovered && (\n          <div\n            className=\"absolute min-w-[170px] rounded-[10px] px-3.5 py-2.5 pointer-events-none\"\n            style={{\n              left: `${(pointOnCircle(cx, cy, outerRadius + 40, hovered.mid).x / VB_W) * 100}%`,\n              top: `${(pointOnCircle(cx, cy, outerRadius + 40, hovered.mid).y / VB_H) * 100}%`,\n              transform: `translate(${Math.cos(hovered.mid) >= 0 ? \"0%\" : \"-100%\"}, -50%)`,\n              backgroundColor: isDark ? \"rgba(14,14,14,0.96)\" : \"rgba(255,255,255,0.97)\",\n              border: `1px solid ${palette.badgeBorder}`,\n              boxShadow: isDark ? \"0 10px 28px rgba(0,0,0,0.5)\" : \"0 6px 20px rgba(0,0,0,0.12)\",\n            }}\n          >\n            <div className=\"mb-1.5 text-[12px] font-bold\" style={{ color: palette.textPrimary }}>\n              {hovered.label}\n            </div>\n            <div className=\"flex items-center justify-between gap-3.5\">\n              <span className=\"text-[11.5px]\" style={{ color: palette.textSub }}>ARR</span>\n              <span className=\"text-[11.5px] font-bold\" style={{ color: palette.textPrimary }}>{formatMoney(hovered.arr)}</span>\n            </div>\n            <div className=\"mt-1 flex items-center justify-between gap-3.5\">\n              <span className=\"text-[11.5px]\" style={{ color: palette.textSub }}>Accounts</span>\n              <span className=\"text-[11.5px] font-bold\" style={{ color: palette.textPrimary }}>{hovered.accounts}</span>\n            </div>\n            <div className=\"mt-1 flex items-center justify-between gap-3.5\">\n              <span className=\"text-[11.5px]\" style={{ color: palette.textSub }}>Avg contract</span>\n              <span className=\"text-[11.5px] font-bold\" style={{ color: palette.textPrimary }}>{formatAvgContract(hovered.avgContract)}</span>\n            </div>\n          </div>\n        )}\n      </div>\n\n      <div className=\"mt-4 flex flex-wrap justify-center gap-4.5\">\n        {slices.map((s, i) => {\n          const isHidden = hidden.has(i);\n          return (\n            <div\n              key={i}\n              onClick={() => toggleSlice(i)}\n              className=\"flex cursor-pointer items-center gap-[7px]\"\n              style={{ opacity: isHidden ? 0.4 : 1 }}\n            >\n              <span className=\"inline-block h-[9px] w-[9px] rounded-full\" style={{ backgroundColor: s.color }} />\n              <span\n                className=\"text-[12.5px] font-semibold\"\n                style={{ color: palette.textPrimary, textDecoration: isHidden ? \"line-through\" : \"none\" }}\n              >\n                {s.label}\n              </span>\n            </div>\n          );\n        })}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}