{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-radial",
  "title": "Chart radial",
  "description": "What the charts drawn round a centre share, on the chart core: places as turns from the top, an arc’s sweep and the radius that fits it in its plot, a ring’s slices and bands, the slice, ring or axis under the pointer, the tooltip’s side, and the labels round the edge that stand upright and travel round with what they name, with the room they need. The rings of a tree too: each arc’s path by d3’s arc, the arc under the pointer, and how its name lies on it. The Donut, Radar, Radial and Sunburst charts draw on it.",
  "dependencies": [
    "d3-shape"
  ],
  "devDependencies": [
    "@types/d3-shape"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/chart-root.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/lib/chart-radial.tsx",
      "content": "'use client'\n\nimport type { ChartPoint } from '@/registry/lib/chart-root'\nimport { arc as d3Arc } from 'd3-shape'\nimport { type ComponentProps, type CSSProperties, memo, type Ref } from 'react'\n\nimport { cn } from '@/lib/utils'\n\n/*\n * What the charts drawn round a centre share, on the chart core: the Donut\n * chart's rings and the Radar chart's axes. Places are turns, fractions of a\n * whole turn clockwise from the top, so a part moves by turning, and every\n * label round the edge stands upright on the same transform.\n */\n\n/** A slice's place on the ring, as fractions of a turn clockwise from the top. */\ninterface Slice {\n  start: number\n  size: number\n}\n\n/** A ring's edges, in px from the centre. */\ninterface RingBand {\n  inner: number\n  outer: number\n}\n\n/** How far past the ring, in px, the pointer still finds a slice. */\nconst PICK_SLACK = 8\n\n/** A label's line past the ring: the gap before it and its length, then the gap before the text, in px. */\nconst LEADER = { gap: 3, length: 10, text: 4 } as const\n\n/** A line of the chart's text, in px. */\nconst TEXT_HEIGHT = 16\n\n/**\n * Each value's slice of the ring, in order clockwise from the top, as\n * fractions of a turn. A value above zero takes at least `least` of the turn,\n * taken from the others in proportion, so it shows and can be pointed at.\n * With nothing to share, every slice is empty at the top.\n */\nfunction ringSlices(values: readonly number[], least = 0): Slice[] {\n  const positive = values.map((value) => Math.max(0, value))\n  const count = positive.filter((value) => value > 0).length\n  const floor = count === 0 ? 0 : Math.min(least, 1 / count)\n  // The values held at the floor: each pass adds those the rest's shares leave below it.\n  const held = new Set<number>()\n  const sizesWith = () => {\n    const free = 1 - held.size * floor\n    const rest = positive.reduce(\n      (sum, value, at) => (held.has(at) ? sum : sum + value),\n      0,\n    )\n    return positive.map((value, at) =>\n      value === 0 ? 0 : held.has(at) ? floor : rest === 0 ? 0 : (value / rest) * free,\n    )\n  }\n  let sizes = sizesWith()\n  for (;;) {\n    const below = sizes.flatMap((size, at) =>\n      positive[at]! > 0 && !held.has(at) && size < floor ? [at] : [],\n    )\n    if (below.length === 0) break\n    for (const at of below) held.add(at)\n    sizes = sizesWith()\n  }\n  let start = 0\n  return sizes.map((size) => {\n    const slice = { start, size }\n    start += size\n    return slice\n  })\n}\n\n/**\n * The rings' bands, outermost first: the room from `inner` to `outer` px\n * shared evenly by `count` rings, `gap` px apart. A gap never takes more\n * than a fifth of a ring's room, so thin rings stay rings.\n */\nfunction ringBands(outer: number, inner: number, count: number, gap: number): RingBand[] {\n  const rings = Math.max(1, count)\n  const room = Math.max(0, outer - inner)\n  const between = rings === 1 ? 0 : Math.min(gap, room / rings / 5)\n  const thickness = (room - between * (rings - 1)) / rings\n  return Array.from({ length: rings }, (_, at) => {\n    const edge = outer - at * (thickness + between)\n    return { outer: edge, inner: Math.max(0, edge - thickness) }\n  })\n}\n\n/** Where a slice that leaves from place `at` folds to: the start of the slice now there, or the end. */\nfunction foldPoint(slices: readonly Slice[], at: number) {\n  const next = slices[at]\n  if (next) return next.start\n  const last = slices.at(-1)\n  return last ? last.start + last.size : 0\n}\n\n/**\n * The ring a point `distance` px from the centre is on, or nearest to while\n * it is close, by its place among `rings`, outermost first; null past them\n * or in the hole.\n */\nfunction nearestRing(distance: number, rings: readonly RingBand[]): number | null {\n  const outermost = rings[0]\n  const innermost = rings.at(-1)\n  if (!outermost || !innermost || outermost.outer <= 0) return null\n  if (distance > outermost.outer + PICK_SLACK) return null\n  if (innermost.inner > 0 && distance < innermost.inner - PICK_SLACK) return null\n  const away = (band: RingBand) =>\n    Math.max(0, band.inner - distance, distance - band.outer)\n  // A ring with no room, such as one leaving, is never found.\n  let near: number | null = null\n  rings.forEach((band, at) => {\n    if (band.outer <= band.inner) return\n    if (near === null || away(band) < away(rings[near]!)) near = at\n  })\n  return near\n}\n\n/**\n * The slice under a point `x` across and `y` down from the centre, in px, by\n * its place in the data: the ring the point is on, or nearest to while it is\n * close, then the slice its angle falls in.\n */\nfunction sliceAt(\n  x: number,\n  y: number,\n  rings: readonly (RingBand & { slices: readonly Slice[] })[],\n): number | null {\n  const near = nearestRing(Math.hypot(x, y), rings)\n  if (near === null) return null\n  const turn = turnOf(x, y)\n  const at = rings[near]!.slices.findIndex(\n    (slice) => slice.size > 0 && turn >= slice.start && turn < slice.start + slice.size,\n  )\n  return at === -1 ? null : at\n}\n\n/**\n * Where an arc runs: from `start`, `size` round, clockwise, as fractions of\n * a turn from the top. A whole circle has a size of 1.\n */\ninterface Sweep {\n  start: number\n  size: number\n}\n\n/**\n * The arc from `startAngle` to `endAngle`, in degrees clockwise from the\n * top. One that runs backwards, or further than a turn, is held to none or\n * one.\n */\nfunction sweepOf(startAngle: number, endAngle: number): Sweep {\n  const size = Math.min(1, Math.max(0, (endAngle - startAngle) / 360))\n  return { start: (((startAngle / 360) % 1) + 1) % 1, size }\n}\n\n/** How far round `turn` is from the sweep's start, clockwise, from 0 to under 1. */\nfunction turnIntoSweep(turn: number, sweep: Sweep) {\n  return (((turn - sweep.start) % 1) + 1) % 1\n}\n\n/** Whether `turn` lies on the arc. */\nfunction withinSweep(turn: number, sweep: Sweep) {\n  return sweep.size >= 1 || turnIntoSweep(turn, sweep) <= sweep.size + 1e-9\n}\n\n/**\n * The ring under a point `x` across and `y` down from the centre, in px, by\n * its place among `rings`: the one it is on or nearest, wherever along the\n * arc, value or track, so a ring with little value is as easy to point at.\n */\nfunction ringAt(\n  x: number,\n  y: number,\n  rings: readonly RingBand[],\n  sweep: Sweep,\n): number | null {\n  if (!withinSweep(turnOf(x, y), sweep)) return null\n  return nearestRing(Math.hypot(x, y), rings)\n}\n\n/**\n * What a round chart has to fit in its plot: a point at `x` across and `y`\n * down from the centre, as fractions of the radius, and the px it needs\n * round it, such as a line of text below.\n */\ninterface RoundMark {\n  x: number\n  y: number\n  left?: number\n  right?: number\n  up?: number\n  down?: number\n}\n\n/**\n * The marks that hold an arc `sweep` with a hole of `inner`, as a share of\n * the radius: its ends, inside and out, each side of the circle it passes,\n * and the centre, where its centre's words sit.\n */\nfunction sweepMarks(sweep: Sweep, inner: number): RoundMark[] {\n  const at = (turn: number, radius: number): RoundMark => ({\n    x: radius * Math.sin(turn * Math.PI * 2),\n    y: -radius * Math.cos(turn * Math.PI * 2),\n  })\n  const marks: RoundMark[] = [{ x: 0, y: 0 }]\n  for (const side of [0, 0.25, 0.5, 0.75]) {\n    if (withinSweep(side, sweep)) marks.push(at(side, 1))\n  }\n  const end = sweep.start + sweep.size\n  for (const turn of [sweep.start, end]) marks.push(at(turn, 1), at(turn, inner))\n  return marks\n}\n\n/** How far a set of marks reaches along one axis, in px, at a radius of `radius` px. */\nfunction reachOf(\n  marks: readonly RoundMark[],\n  radius: number,\n  axis: 'x' | 'y',\n): { low: number; high: number } {\n  let low = Infinity\n  let high = -Infinity\n  for (const mark of marks) {\n    const place = mark[axis] * radius\n    low = Math.min(low, place - ((axis === 'x' ? mark.left : mark.up) ?? 0))\n    high = Math.max(high, place + ((axis === 'x' ? mark.right : mark.down) ?? 0))\n  }\n  return { low, high }\n}\n\n/**\n * The largest radius at which `marks` fit a plot `width` by `height` px, in\n * whole px, and where the centre sits, in px from the plot's top left, so\n * that what they reach is centred in the plot both ways.\n */\nfunction fitRound(\n  width: number,\n  height: number,\n  marks: readonly RoundMark[],\n): { radius: number; x: number; y: number } {\n  const fits = (radius: number) => {\n    const across = reachOf(marks, radius, 'x')\n    const down = reachOf(marks, radius, 'y')\n    return across.high - across.low <= width && down.high - down.low <= height\n  }\n  let low = 0\n  let high = Math.max(width, height)\n  if (marks.length === 0 || !fits(0)) high = 0\n  // What the marks reach grows steadily with the radius, so halving finds the largest that fits.\n  for (let step = 0; step < 40 && high - low > 0.01; step++) {\n    const middle = (low + high) / 2\n    if (fits(middle)) low = middle\n    else high = middle\n  }\n  const radius = Math.max(0, Math.floor(low * 2) / 2 - 0.5)\n  const across = reachOf(marks, radius, 'x')\n  const down = reachOf(marks, radius, 'y')\n  return {\n    radius,\n    x: (width - (across.high - across.low)) / 2 - across.low,\n    y: (height - (down.high - down.low)) / 2 - down.low,\n  }\n}\n\n/**\n * The rings' bands, outermost first, as `ringBands` shares them among the\n * rings `present`; a ring that isn't present, such as one leaving, takes no\n * room where it sits, at the edge between its neighbours, so it thins to\n * nothing there while the rest take its room.\n */\nfunction ringBandsOf(\n  outer: number,\n  inner: number,\n  present: readonly boolean[],\n  gap: number,\n): RingBand[] {\n  const shared = ringBands(outer, inner, present.filter(Boolean).length, gap)\n  let next = 0\n  return present.map((here) => {\n    if (here) return shared[next++]!\n    const edge = shared[next - 1]?.inner ?? shared[next]?.outer ?? outer\n    return { inner: edge, outer: edge }\n  })\n}\n\n/** How far round a point is from the top, clockwise, as a fraction of a turn. */\nfunction turnOf(x: number, y: number) {\n  const turn = Math.atan2(x, -y) / (Math.PI * 2)\n  return turn < 0 ? turn + 1 : turn\n}\n\n/**\n * The point `radius` px out from the centre, `turn` round from the top, as\n * fractions of a plot `width` by `height` px, and the side a tooltip there\n * opens on. The centre is the plot's, unless it is given in px.\n */\nfunction outerPoint(\n  turn: number,\n  width: number,\n  height: number,\n  radius = Math.min(width, height) / 2,\n  centre = { x: width / 2, y: height / 2 },\n): ChartPoint {\n  const angle = turn * Math.PI * 2\n  const x = (centre.x + radius * Math.sin(angle)) / width\n  const y = (centre.y - radius * Math.cos(angle)) / height\n  return { x, y, side: tooltipSide(turn) }\n}\n\n/**\n * The side a tooltip opens on at `turn`: above the top half and below the\n * bottom half, never beside the centre, where a tooltip that doesn't fit\n * would flip across it.\n */\nfunction tooltipSide(turn: number): 'top' | 'bottom' {\n  return turn >= 0.25 && turn < 0.75 ? 'bottom' : 'top'\n}\n\n/** A dash along a circle, in px: its length and the offset that starts it where it belongs. */\ninterface SliceDash {\n  dash: string\n  offset: number\n  /** The slice has nothing to draw, so it shows nothing rather than a hairline. */\n  empty: boolean\n}\n\n/**\n * A slice as a dash along a circle `circumference` px round that starts at\n * the top: its length, and an offset that is never negative, as Safari\n * misplaces a negative one. Slices meet end to end; the gaps between them are\n * cut out by the ring's mask. The dash repeats a px later than the circle\n * ends, so a browser that measures the circle a little longer never draws a\n * slice a second time.\n */\nfunction sliceDash(slice: Slice, circumference: number, alone: boolean): SliceDash {\n  const period = circumference + 1\n  const length = slice.size * circumference\n  // Never a dash of nothing, which Safari draws as a hairline; a whole ring, the whole period,\n  // so it has no seam.\n  const drawn =\n    alone && slice.size > 0 ? period : Math.max(0.01, Math.min(length, circumference))\n  return {\n    dash: `${round(drawn)} ${round(period - drawn)}`,\n    offset: round(period - slice.start * circumference),\n    empty: length === 0,\n  }\n}\n\nfunction round(value: number) {\n  return Math.round(value * 100) / 100\n}\n\n/** A box in px from the centre. */\ninterface RadialBox {\n  left: number\n  right: number\n  top: number\n  bottom: number\n}\n\n/**\n * A label's box round the edge, in px from the centre: its point sits\n * `radius` out, `turn` round, and the box `width` by `height` px sits\n * against that point, away from the centre, as `RadialLabel` places it.\n */\nfunction labelBoxAt(\n  turn: number,\n  radius: number,\n  width: number,\n  height = TEXT_HEIGHT,\n): RadialBox {\n  const angle = turn * Math.PI * 2\n  const sin = Math.sin(angle)\n  const cos = Math.cos(angle)\n  const x = radius * sin + (sin * width) / 2\n  const y = -radius * cos - (cos * height) / 2\n  return {\n    left: x - width / 2,\n    right: x + width / 2,\n    top: y - height / 2,\n    bottom: y + height / 2,\n  }\n}\n\n/**\n * The room outside labels take past the ring, in px, in a plot `width` by\n * `height` px whose widest label is `widest` px: a line of text above and\n * below, and the widest beside, each past its line.\n */\nfunction outsideRoom(width: number, height: number, widest: number) {\n  if (width === 0 || height === 0) return 0\n  const past = LEADER.gap + LEADER.length + LEADER.text\n  const half = Math.min(width, height) / 2 - 0.5\n  const edge = Math.min(width / 2 - past - widest, height / 2 - past - TEXT_HEIGHT)\n  return Math.ceil(Math.max(0, half - Math.max(0, edge)))\n}\n\n/** A label to place round the edge: where it is, and its size in px. */\ninterface EdgeLabel {\n  turn: number\n  width: number\n  height: number\n}\n\n/**\n * The room labels round the edge take, in px inward from the largest circle\n * a plot `width` by `height` px holds, so that each label, `gap` px past the\n * edge at its own turn, stays inside the plot: the labels at the sides hold\n * the edge in by their widths, those at the top and bottom by their heights.\n */\nfunction edgeRoom(\n  width: number,\n  height: number,\n  labels: readonly EdgeLabel[],\n  gap: number,\n) {\n  if (width === 0 || height === 0) return 0\n  const half = Math.min(width, height) / 2 - 0.5\n  let radius = half\n  for (const label of labels) {\n    const angle = label.turn * Math.PI * 2\n    const sin = Math.abs(Math.sin(angle))\n    const cos = Math.abs(Math.cos(angle))\n    // How far out its point may sit before the box, against it, crosses a side.\n    if (sin > 1e-9) {\n      radius = Math.min(radius, (width / 2 - (label.width * (1 + sin)) / 2) / sin - gap)\n    }\n    if (cos > 1e-9) {\n      radius = Math.min(radius, (height / 2 - (label.height * (1 + cos)) / 2) / cos - gap)\n    }\n  }\n  return Math.ceil(Math.max(0, half - Math.max(0, radius)))\n}\n\n/** Whether a label's box stands clear of every box in `placed`, with a little room across. */\nfunction standsClear(box: RadialBox, placed: readonly RadialBox[]) {\n  return placed.every(\n    (other) =>\n      box.right + 2 <= other.left ||\n      box.left - 2 >= other.right ||\n      box.bottom <= other.top ||\n      box.top >= other.bottom,\n  )\n}\n\n/**\n * Which outside labels show, by place: each slice with a share, unless its\n * box `widths` wide would overlap that of a bigger slice that shows. Labels\n * end `radius` px from the centre.\n */\nfunction outsideLabelsShown(\n  slices: readonly Slice[],\n  widths: readonly number[],\n  radius: number,\n): boolean[] {\n  const shown = slices.map(() => false)\n  const placed: RadialBox[] = []\n  const order = slices\n    .map((slice, at) => ({ slice, at }))\n    .filter(({ slice }) => slice.size > 0)\n    .sort((a, b) => b.slice.size - a.slice.size)\n  for (const { slice, at } of order) {\n    const box = labelBoxAt(slice.start + slice.size / 2, radius, widths[at] ?? 0)\n    if (!standsClear(box, placed)) continue\n    placed.push(box)\n    shown[at] = true\n  }\n  return shown\n}\n\n/**\n * Whether a label `width` px wide fits on its slice, centred `radius` px\n * out on a ring `thickness` px thick: the upright box, measured across the\n * ring and along it, with a little room to spare.\n */\nfunction insideLabelFits(\n  slice: Slice,\n  width: number,\n  radius: number,\n  thickness: number,\n  gap: number,\n) {\n  if (slice.size <= 0) return false\n  const angle = (slice.start + slice.size / 2) * Math.PI * 2\n  const sin = Math.abs(Math.sin(angle))\n  const cos = Math.abs(Math.cos(angle))\n  const across = width * sin + TEXT_HEIGHT * cos\n  const along = width * cos + TEXT_HEIGHT * sin\n  const chord = 2 * radius * Math.sin(Math.min(Math.PI / 2, slice.size * Math.PI)) - gap\n  // A slice over half the ring is wide enough wherever the label sits along it.\n  const room = slice.size >= 0.5 ? Infinity : chord\n  return across + 4 <= thickness && along + 6 <= room\n}\n\n/**\n * A label round the centre, turned to `turn` and stood back upright, so it\n * travels round with what it names: against its point, away from the\n * centre, or with `centred`, centred on it. With `edge`, a short line leads\n * to it from that far out. Memoised, as what it names is.\n */\nconst RadialLabel = memo(function RadialLabel({\n  turn,\n  radius,\n  stretch,\n  edge = null,\n  centred = false,\n  shown,\n  className,\n  style,\n  ref,\n  ...props\n}: Omit<ComponentProps<'span'>, 'ref'> & {\n  turn: number\n  radius: number\n  /** Before the plot is measured, one over the stand-in's shorter side, which `radius` is in; else null. */\n  stretch: number | null\n  /** How far out the line before it starts, in px; null for no line. */\n  edge?: number | null\n  /** Centred on its point, as a label on its slice is, rather than against it. */\n  centred?: boolean\n  shown: boolean\n  ref?: Ref<HTMLSpanElement>\n}) {\n  const angle = turn * Math.PI * 2\n  return (\n    <div\n      className={cn(\n        'absolute top-1/2 left-1/2 size-0 transition-none duration-(--motion-layout-duration) ease-(--motion-layout-fade-easing) group-data-mounted/chart:transition-[opacity]',\n        !shown && 'opacity-0',\n      )}\n      style={\n        {\n          '--label-turn': `${round(turn * 360)}deg`,\n          '--label-out':\n            stretch === null\n              ? `${-round(radius)}px`\n              : `calc(min(100cqw, 100cqh) * ${-round(radius * stretch * 100) / 100})`,\n        } as CSSProperties\n      }\n    >\n      {edge !== null && (\n        <div className='absolute size-0 [rotate:var(--label-turn)] transition-none duration-(--motion-layout-duration) ease-(--motion-layout-easing) motion-reduce:group-data-mounted/chart:transition-none group-data-mounted/chart:transition-[rotate]'>\n          <span\n            className='bg-muted-foreground/60 absolute left-0 w-px -translate-x-1/2 forced-colors:bg-[CanvasText]'\n            style={{ bottom: round(edge + LEADER.gap), height: LEADER.length }}\n          />\n        </div>\n      )}\n      {/*\n       * Out along its turn and turned back as far, in one transform, so both turns move in\n       * step and the text stays upright all the way round.\n       */}\n      <div className='absolute size-0 [transform:rotate(var(--label-turn))_translateY(var(--label-out))_rotate(calc(var(--label-turn)*-1))] transition-none duration-(--motion-layout-duration) ease-(--motion-layout-easing) motion-reduce:group-data-mounted/chart:transition-none group-data-mounted/chart:transition-[transform]'>\n        <span\n          ref={ref}\n          className={cn(\n            'absolute top-0 left-0 transition-none duration-(--motion-layout-duration) ease-[var(--motion-layout-fade-easing),var(--motion-layout-easing)] group-data-mounted/chart:transition-[color,translate] motion-reduce:group-data-mounted/chart:transition-[color]',\n            className,\n          )}\n          style={{\n            // Against its point and away from the centre, or centred on it.\n            translate: centred\n              ? '-50% -50%'\n              : `calc(-50% + ${round(Math.sin(angle) * 50)}%) calc(-50% - ${round(Math.cos(angle) * 50)}%)`,\n            ...style,\n          }}\n          {...props}\n        />\n      </div>\n    </div>\n  )\n})\n\n/*\n * Arcs: the rings of a chart of a tree round a centre, such as a sunburst's,\n * each arc a place as fractions (`ArcPlace` in `chart-tiles`) drawn in px\n * between the hole and the edge.\n */\n\n/** Where an arc sits, as fractions: of a turn clockwise from the top, and of the room from the hole to the edge. */\ninterface ArcFractions {\n  start: number\n  end: number\n  inner: number\n  outer: number\n}\n\n/** The rings' room in px: the hole's radius and the edge's. */\ninterface RingRoom {\n  hole: number\n  edge: number\n}\n\n/** An arc's edges in px from the centre, and its turns. */\ninterface ArcBox {\n  start: number\n  end: number\n  inner: number\n  outer: number\n}\n\n/** The room between two arcs, and between two rings, in px, as between a donut's slices. */\nconst ARC_GAP = 2\n\n/**\n * An arc's edges in px: its fractions of the room from the hole to the edge,\n * each side that meets another ring half a gap in, so rings sit `ARC_GAP`\n * apart while the edge and a centre with no hole stay flush.\n */\nfunction arcBox(place: ArcFractions, room: RingRoom): ArcBox {\n  const across = Math.max(0, room.edge - room.hole)\n  const half = ARC_GAP / 2\n  const inner =\n    room.hole + place.inner * across + (place.inner > 0 || room.hole > 0 ? half : 0)\n  const outer = room.hole + place.outer * across - (place.outer < 1 ? half : 0)\n  return { start: place.start, end: place.end, inner, outer: Math.max(inner, outer) }\n}\n\n/**\n * An arc's path round a centre at 0, 0, by d3's arc: `ARC_GAP` px between\n * it and the arcs beside it, as wide at the hole as at the edge, and a\n * whole turn drawn as a ring with no seam. Empty for an arc with no room.\n */\nfunction arcPath(place: ArcFractions, room: RingRoom): string {\n  const box = arcBox(place, room)\n  if (box.end - box.start <= 1e-6 || box.outer - box.inner <= 1e-3) return ''\n  const turn = Math.PI * 2\n  const pad = room.edge > 0 ? ARC_GAP / room.edge : 0\n  return (\n    d3Arc<ArcBox>()\n      .innerRadius(box.inner)\n      .outerRadius(box.outer)\n      .startAngle(box.start * turn)\n      .endAngle(box.end * turn)\n      // A gap as wide everywhere: the pad's angle at the edge, held to its px at every radius.\n      .padRadius(room.edge)\n      .padAngle(pad)\n      .digits(2)(box) ?? ''\n  )\n}\n\n/** What a point is on: an arc, by its place among `arcs`, the hole, or nothing. */\ntype ArcHit = { kind: 'arc'; index: number } | { kind: 'hole' } | null\n\n/**\n * What sits under a point `x` across and `y` down from the centre, in px:\n * the hole, or the arc its angle falls in on the ring it is on, or nearest\n * to within `PICK_SLACK` px, so the gaps between rings and just past the\n * edge still find one.\n */\nfunction arcAt(\n  x: number,\n  y: number,\n  arcs: readonly ArcFractions[],\n  room: RingRoom,\n): ArcHit {\n  const distance = Math.hypot(x, y)\n  if (room.edge <= 0 || distance > room.edge + PICK_SLACK) return null\n  if (room.hole > 0 && distance < room.hole) return { kind: 'hole' }\n  const turn = turnOf(x, y)\n  let best: number | null = null\n  let nearest = PICK_SLACK\n  for (const [at, place] of arcs.entries()) {\n    if (place.end - place.start <= 0 || turn < place.start || turn >= place.end) continue\n    const box = arcBox(place, room)\n    if (box.outer <= box.inner) continue\n    const away = Math.max(0, box.inner - distance, distance - box.outer)\n    if (away <= nearest) {\n      best = at\n      nearest = away\n    }\n  }\n  return best === null ? null : { kind: 'arc', index: best }\n}\n\n/** How a label lies on its arc: along it, across the ring, or not at all. */\ntype ArcLabelFit = 'along' | 'across' | 'none'\n\n/** The room a label keeps from its arc's edges, in px. */\nconst ARC_LABEL_PADDING = 4\n\n/** A label's height on an arc, in px: a line of the chart's small text. */\nconst ARC_LABEL_HEIGHT = 12\n\n/**\n * How a label `width` px wide lies on an arc, centred on its middle: along\n * the arc when the arc is at least as long there as the ring is thick, else\n * across the ring, and the other way when that one doesn't fit; or not at\n * all, never cut short. Along the arc, it stays inside the ring at its ends\n * and its middle; across, inside the ring's thickness and the arc's width\n * where it starts.\n */\nfunction arcLabelFit(box: ArcBox, width: number): ArcLabelFit {\n  const thickness = box.outer - box.inner\n  const middle = (box.inner + box.outer) / 2\n  const half = (box.end - box.start) * Math.PI\n  // The angle each side of the middle the arc has, less the gap beside it.\n  const room = Math.min(Math.PI, half) - (middle > 0 ? ARC_GAP / 2 / middle : 0)\n  if (width <= 0 || thickness <= 0 || room <= 0) return 'none'\n  const pad = ARC_LABEL_PADDING\n  const line = ARC_LABEL_HEIGHT\n  const along = () => {\n    const low = middle - line / 2\n    if (low < box.inner + pad) return false\n    if (Math.hypot(width / 2, middle + line / 2) > box.outer - pad) return false\n    return Math.atan2(width / 2 + pad, low) <= room\n  }\n  const across = () => {\n    if (width + pad * 2 > thickness) return false\n    const foot = box.inner + pad\n    return foot * Math.sin(Math.min(room, Math.PI / 2)) >= line / 2 + 1\n  }\n  const order: ArcLabelFit[] =\n    room * 2 * middle >= thickness ? ['along', 'across'] : ['across', 'along']\n  for (const way of order) if (way === 'along' ? along() : across()) return way\n  return 'none'\n}\n\n/**\n * How far a label on an arc whose middle is `turn` round turns past the\n * arc's own direction, in degrees, so it reads upright: along the arc it\n * flips on the bottom half, and across the ring a quarter back, flipping on\n * the left half.\n */\nfunction arcLabelSpin(fit: Exclude<ArcLabelFit, 'none'>, turn: number): number {\n  const at = ((turn % 1) + 1) % 1\n  if (fit === 'along') return at > 0.25 && at < 0.75 ? 180 : 0\n  return at > 0.5 ? 90 : -90\n}\n\n/**\n * The SVG transform that puts a label on an arc's middle, `turn` round and\n * `radius` px out from the centre at 0, 0, turned `spin` degrees past the\n * arc's own direction.\n */\nfunction arcLabelTransform(turn: number, radius: number, spin: number): string {\n  return `rotate(${round(turn * 360)}) translate(0 ${-round(radius)}) rotate(${round(spin)})`\n}\n\nexport {\n  ARC_GAP,\n  type ArcBox,\n  arcAt,\n  arcBox,\n  type ArcFractions,\n  type ArcHit,\n  type ArcLabelFit,\n  arcLabelFit,\n  arcLabelSpin,\n  arcLabelTransform,\n  arcPath,\n  type RingRoom,\n  edgeRoom,\n  fitRound,\n  ringAt,\n  ringBandsOf,\n  type RoundMark,\n  type Sweep,\n  sweepMarks,\n  sweepOf,\n  turnIntoSweep,\n  withinSweep,\n  type EdgeLabel,\n  foldPoint,\n  insideLabelFits,\n  labelBoxAt,\n  LEADER,\n  outerPoint,\n  outsideLabelsShown,\n  outsideRoom,\n  type RadialBox,\n  RadialLabel,\n  ringBands,\n  type RingBand,\n  ringSlices,\n  round,\n  type Slice,\n  sliceAt,\n  sliceDash,\n  type SliceDash,\n  standsClear,\n  TEXT_HEIGHT,\n  tooltipSide,\n  turnOf,\n}\n",
      "type": "registry:lib",
      "target": "@lib/chart-radial.tsx"
    }
  ],
  "type": "registry:lib"
}