{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-pie",
  "title": "Pie chart",
  "description": "A pie or donut whose wedges are shaded by hatching of different angles and densities, the way a plotter shades without colour, then outlined.",
  "dependencies": [
    "svg-scroll-draw@^2.29.0"
  ],
  "registryDependencies": [
    "https://scrollfx-ui.vercel.app/r/plotter.json",
    "https://scrollfx-ui.vercel.app/r/chart.json"
  ],
  "files": [
    {
      "path": "registry/ui/chart-pie.tsx",
      "content": "'use client';\n\n/**\n * chart-pie — a pie or a donut a pen shades as you scroll: each wedge hatched\n * in turn, clockwise from twelve o'clock, at its own angle and density (the\n * way a plotter shades without colour), then the outline drawn round, the\n * first wedge in the accent colour.\n *\n * Built on svg-scroll-draw (MIT): https://github.com/DhruvilChauahan0210/scrollfx\n * Needs: react, svg-scroll-draw, lib/chart and lib/plotter (installed with it).\n * Styling: Tailwind CSS with shadcn/ui tokens.\n *\n * The shares, the total, the legend and a data table for screen readers are\n * in the server HTML; the strokes are made in the browser at the chart's size\n * and finish as the chart reaches the middle of the screen. Reduced motion\n * shows the chart drawn.\n */\n\nimport { useMemo } from 'react';\nimport 'svg-scroll-draw/no-flash.css';\nimport {\n  ACCENT, DataTable, Legend, PenLayers, arc, formatter, hatchMarks, num, toPath, useSize, wedge,\n  type ChartTrigger, type ChartConfig, type Datum, type Hatch, type Mark, type Pen, type Series,\n} from '@/lib/chart';\n\nexport interface ChartPieProps {\n  /** One object per wedge. */\n  data?: Datum[];\n  /** The key in each object that names its wedge. */\n  nameKey?: string;\n  /** The key in each object that holds its value. */\n  valueKey?: string;\n  /** Labels (and colours, if you like) for the wedges, by name. */\n  config?: ChartConfig;\n  /** A full pie, or a donut with the total in the middle. */\n  variant?: 'pie' | 'donut';\n  /** Each wedge's share, written beside it. */\n  labels?: boolean;\n  /** Under the total in a donut. */\n  totalLabel?: string;\n  /** How the total is written (Intl.NumberFormat options). */\n  format?: Intl.NumberFormatOptions;\n  /** Show the key. */\n  legend?: boolean;\n  /** The first wedge's colour. */\n  accent?: string;\n  /** What the chart shows, for screen readers (it captions the data table). */\n  label?: string;\n  /** `scroll`: inked as it rises, done at mid-screen. `view`: played once as it comes into view. */\n  trigger?: ChartTrigger;\n  /** With `view`, how long the chart takes, in ms. */\n  duration?: number;\n  className?: string;\n}\n\n/** A shade per wedge: angles and densities a reader tells apart at a glance. */\nconst SHADES: Hatch[] = [\n  { angle: 45, gap: 4 },\n  { angle: 135, gap: 6 },\n  { angle: 90, gap: 5 },\n  { angle: 0, gap: 4.5 },\n  { angle: 45, gap: 8, cross: true },\n  { angle: 135, gap: 3.5 },\n  { angle: 22, gap: 6 },\n];\n\nconst SAMPLE: Datum[] = [\n  { name: 'chrome', value: 275 }, { name: 'safari', value: 200 }, { name: 'firefox', value: 187 }, { name: 'edge', value: 173 }, { name: 'other', value: 90 },\n];\nconst SAMPLE_CONFIG: ChartConfig = { chrome: { label: 'Chrome' }, safari: { label: 'Safari' }, firefox: { label: 'Firefox' }, edge: { label: 'Edge' }, other: { label: 'Other' } };\n\n// The circle's radius and where the shares sit, as fractions of its square box (the\n// shares fall in the padding round it), and the donut's hole as a fraction of the circle.\nconst R = 0.47, RL = 0.55, INNER = 0.58;\n\nexport function ChartPie({\n  data = SAMPLE,\n  nameKey = 'name',\n  valueKey = 'value',\n  config = data === SAMPLE ? SAMPLE_CONFIG : undefined,\n  variant = 'pie',\n  labels = true,\n  totalLabel = 'Total',\n  format,\n  legend = true,\n  accent = ACCENT,\n  label = 'Pie chart',\n  trigger = 'scroll',\n  duration = 1800,\n  className = '',\n}: ChartPieProps) {\n  const total = data.reduce((m, d) => m + Math.max(0, num(d[valueKey])), 0) || 1;\n  const wedges: (Series & { value: number; a0: number; a1: number })[] = [];\n  let a = 0;\n  data.forEach((d, i) => {\n    const name = String(d[nameKey]);\n    const value = Math.max(0, num(d[valueKey]));\n    const a1 = a + (value / total) * Math.PI * 2;\n    wedges.push({ key: name, label: config?.[name]?.label ?? name, color: config?.[name]?.color ?? (i === 0 ? accent : 'currentColor'), hatch: SHADES[i % SHADES.length], marker: 'circle', value, a0: a, a1 });\n    a = a1;\n  });\n  const fmt = formatter(format ?? { maximumFractionDigits: 0 });\n  const pct = (v: number) => `${Math.round((v / total) * 100)}%`;\n  const donut = variant === 'donut';\n  const [ref, size] = useSize<HTMLDivElement>();\n\n  const pens = useMemo((): Pen[] | null => {\n    if (!size) return null;\n    const s = Math.min(size.w, size.h);\n    const cx = size.w / 2, cy = size.h / 2, r = s * R, r0 = donut ? r * INNER : 0;\n    const fill: Mark[] = [];\n    const ink: Mark[] = [];\n    const first: Mark[] = [];\n    wedges.forEach((wd, i) => {\n      if (wd.a1 - wd.a0 < 1e-3) return;\n      fill.push(...hatchMarks(wedge(cx, cy, r0, r, wd.a0, wd.a1), wd.hatch, { color: i === 0 ? wd.color : undefined, opacity: i === 0 ? 0.85 : 0.7, width: 0.9, inset: 3 }));\n      // The edge each wedge starts on, from the centre (or the inner ring) out.\n      if (wedges.length > 1) ink.push({ d: toPath([[cx + Math.sin(wd.a0) * r0, cy - Math.cos(wd.a0) * r0], [cx + Math.sin(wd.a0) * r, cy - Math.cos(wd.a0) * r]]), width: 1.1 });\n    });\n    ink.push({ d: toPath(arc(cx, cy, r, 0, Math.PI * 2, 0.02)), width: 1.2 });\n    if (donut) ink.push({ d: toPath(arc(cx, cy, r0, 0, Math.PI * 2, 0.03)), width: 1.1 });\n    const w0 = wedges[0];\n    if (w0 && w0.a1 - w0.a0 > 1e-3) {\n      const outline = wedge(cx, cy, r0, r, w0.a0, w0.a1);\n      first.push({ d: toPath([...outline, outline[0]]), color: w0.color, width: 2 });\n    }\n    return [\n      { marks: fill, from: 1.12, to: 0.7, spread: 3 },\n      { marks: ink, from: 0.8, to: 0.6, spread: 0.8 },\n      { marks: first, from: 0.68, to: 0.5 },\n    ];\n  }, [size, data, nameKey, valueKey, config, donut, accent]);\n\n  return (\n    <figure className={`@container mx-auto w-full max-w-sm ${className}`}>\n      <DataTable caption={label} head={['Name', 'Value', 'Share']} rows={wedges.map((wd) => [wd.label, wd.value, pct(wd.value)])} />\n      <div aria-hidden>\n        <div className=\"px-9 py-6\">\n          <div ref={ref} data-slot=\"plot\" className=\"relative aspect-square w-full\">\n            {size && pens ? <PenLayers size={size} pens={pens} trigger={trigger} duration={duration} /> : null}\n            {labels &&\n              wedges.map((wd) => {\n                const mid = (wd.a0 + wd.a1) / 2, sx = Math.sin(mid), sy = -Math.cos(mid);\n                if (wd.a1 - wd.a0 < 0.12) return null;\n                const tx = sx > 0.3 ? '0%' : sx < -0.3 ? '-100%' : '-50%';\n                return (\n                  <span\n                    key={wd.key}\n                    className=\"absolute font-mono text-[11px] tabular-nums text-muted-foreground\"\n                    style={{ left: `${(50 + sx * RL * 100).toFixed(2)}%`, top: `${(50 + sy * RL * 100).toFixed(2)}%`, translate: `${tx} -50%` }}\n                  >\n                    {pct(wd.value)}\n                  </span>\n                );\n              })}\n            {donut && (\n              <div className=\"absolute inset-0 flex flex-col items-center justify-center\">\n                <span className=\"font-serif text-2xl @xs:text-[2.4rem] leading-none tracking-[-0.02em] tabular-nums\">{fmt(total)}</span>\n                <span className=\"mt-2 font-mono text-[10px] uppercase tracking-[0.18em] text-muted-foreground\">{totalLabel}</span>\n              </div>\n            )}\n          </div>\n        </div>\n        {legend && <Legend series={wedges} kind=\"hatch\" className=\"mt-2 justify-center\" />}\n      </div>\n    </figure>\n  );\n}\n",
      "type": "registry:ui",
      "target": "components/scrollfx/chart-pie.tsx"
    }
  ],
  "type": "registry:ui"
}