{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "donut-chart",
  "title": "DonutChart",
  "description": "Parts of a whole, with each slice’s value and share in the legend.",
  "registryDependencies": [
    "https://creance.pages.dev/r/creance-base.json",
    "https://creance.pages.dev/r/creance-chart.json"
  ],
  "files": [
    {
      "path": "donut-chart/DonutChart.css",
      "content": ".cr-donut {\n  flex-direction: row;\n  flex-wrap: wrap;\n  align-items: center;\n  gap: var(--cr-space-4) var(--cr-space-8);\n}\n\n.cr-donut__ring {\n  position: relative;\n  flex: none;\n}\n\n/* Slices start at twelve o'clock. */\n.cr-donut__ring svg {\n  display: block;\n  transform: rotate(-90deg);\n}\n\n.cr-donut__center {\n  position: absolute;\n  inset: 0;\n  display: flex;\n  flex-direction: column;\n  align-items: center;\n  justify-content: center;\n}\n\n.cr-donut__total {\n  font-family: var(--cr-font-family-display);\n  font-size: var(--cr-font-size-2xl);\n  font-weight: var(--cr-font-weight-bold);\n  letter-spacing: var(--cr-font-tracking-tight);\n  line-height: 1.1;\n}\n\n.cr-donut__unit {\n  color: var(--cr-color-text-muted);\n  font-size: var(--cr-font-size-xs);\n}\n\n.cr-donut .cr-chart-legend {\n  flex: 1 1 200px;\n  flex-direction: column;\n  gap: var(--cr-space-2);\n}\n",
      "type": "registry:file",
      "target": "@components/creance/donut-chart/DonutChart.css"
    },
    {
      "path": "donut-chart/DonutChart.tsx",
      "content": "import type { ReactNode } from 'react';\nimport { ChartLegend, ChartTable, formatter, seriesColor, type ChartTone, type ValueFormat } from '@/lib/creance/chart';\nimport { cx } from '@/lib/creance/cx';\nimport './DonutChart.css';\n\nexport interface DonutSlice {\n  label: string;\n  value: number;\n  /** `danger` is red, for a failure. Other slices step down in ink in the order given. */\n  tone?: ChartTone;\n}\n\nexport interface DonutChartProps extends ValueFormat {\n  /** Up to five slices read well. Put the largest first. */\n  data: DonutSlice[];\n  /** What the chart shows, for screen readers and as the table's caption: \"Run outcomes\". */\n  label: string;\n  /** Under the total in the middle: \"runs\". */\n  centerLabel?: ReactNode;\n  /** Diameter in pixels. Default 170. */\n  size?: number;\n  /** Lists each slice with its value and share. Default true. */\n  legend?: boolean;\n  className?: string;\n}\n\n/** Parts of a whole. The legend gives each slice its value and share, so nothing depends on telling shades apart. */\nexport function DonutChart({ data, label, centerLabel, size = 170, legend = true, className, ...format }: DonutChartProps) {\n  const fmt = formatter(format);\n  const total = data.reduce((sum, d) => sum + Math.max(d.value, 0), 0);\n  const stroke = Math.round(size * 0.12);\n  const radius = (size - stroke) / 2;\n  const around = 2 * Math.PI * radius;\n  const share = (v: number) => (total ? `${Math.round((v / total) * 100)}%` : '0%');\n  const slices = data.map((d, i) => {\n    const before = data.slice(0, i);\n    // Ink steps down through the slices that are not failures; each arc starts where the last one ended.\n    const step = before.filter((b) => b.tone !== 'danger').length;\n    const start = before.reduce((sum, b) => sum + Math.max(b.value, 0), 0);\n    const arc = (v: number) => (total ? (v / total) * around : 0);\n    return { ...d, color: seriesColor(step, d.tone), length: arc(Math.max(d.value, 0)), offset: arc(start) };\n  });\n\n  return (\n    <figure className={cx('cr-chart cr-donut', className)}>\n      <div className=\"cr-donut__ring\" style={{ width: size, height: size }} aria-hidden=\"true\">\n        <svg width={size} height={size} viewBox={`0 0 ${size} ${size}`}>\n          {slices.map((s, i) => (\n            <circle\n              key={i}\n              cx={size / 2}\n              cy={size / 2}\n              r={radius}\n              fill=\"none\"\n              stroke={s.color}\n              strokeWidth={stroke}\n              // A small gap separates neighbours without relying on their shades.\n              strokeDasharray={`${Math.max(s.length - 3, 0.5)} ${around}`}\n              strokeDashoffset={-s.offset}\n            />\n          ))}\n        </svg>\n        <div className=\"cr-donut__center\">\n          <span className=\"cr-donut__total\">{fmt(total)}</span>\n          {centerLabel && <span className=\"cr-donut__unit\">{centerLabel}</span>}\n        </div>\n      </div>\n      {legend && <ChartLegend items={slices.map((s) => ({ name: s.label, color: s.color, value: fmt(s.value), share: share(s.value) }))} />}\n      <ChartTable caption={label} columns={['Slice', 'Value', 'Share']} rows={[...data.map((d) => [d.label, fmt(d.value), share(d.value)]), ['Total', fmt(total), '100%']]} />\n    </figure>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/creance/donut-chart/DonutChart.tsx"
    }
  ],
  "categories": [
    "molecule",
    "charts"
  ],
  "type": "registry:ui"
}