{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-bar-marks",
  "title": "Chart bar marks",
  "description": "Bars in the cartesian frame: their spans, side by side or stacked, the slot each takes in its band, and how far they reach.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/chart-frame.json",
    "https://kinoui.com/r/chart-math.json"
  ],
  "files": [
    {
      "path": "registry/lib/chart-bar-marks.ts",
      "content": "import type { MarkKind, SeriesSpans } from '@/registry/lib/chart-frame'\nimport {\n  type ChartStack,\n  hiddenSpans,\n  numberOf,\n  rangeOf,\n  reachOf,\n  seriesSpans,\n  type Span,\n  spanExtent,\n  stacksOf,\n} from '@/registry/lib/chart-math'\nimport { type Band, bandAt } from '@/registry/lib/chart-scale'\n\n/*\n * Bars: each series' bar runs from zero to its value, side by side or\n * stacked by `stack` group, and a value given as `[from, to]` spans exactly\n * that. A hidden series collapses where it would stack, so it leaves from\n * its own place and comes back to it.\n */\n\n/** What a bar's label reads: a value or a range, `signed` for a change written with its sign; null for none. */\ntype BarReading = { value: number | Span; signed: boolean } | null\n\n/** Bars, which also say what each bar's label reads. */\ninterface BarMarkKind extends MarkKind {\n  /** What each of a series' bars reads, from its values in the data's order. */\n  reads: (values: readonly unknown[]) => readonly BarReading[]\n}\n\n/** The chart's kind of bar: the one that says what its bars read. */\nfunction barKind(kinds: readonly MarkKind[]): BarMarkKind {\n  const kind = kinds.find((kind): kind is BarMarkKind => 'reads' in kind)\n  if (!kind) throw new Error('A `Bar` sits in a chart that draws bars.')\n  return kind\n}\n\nconst barMarks: BarMarkKind = {\n  mark: 'bar',\n  stacks: true,\n  // Each bar reads its own value, or its range.\n  reads: (values) =>\n    values.map((raw) => {\n      const range = rangeOf(raw)\n      if (range) return { value: range, signed: false }\n      const value = numberOf(raw)\n      return value === null ? null : { value, signed: false }\n    }),\n  spans: ({ count, valueOf, series, shown, stack, stackOf }) => {\n    const spans = seriesSpans(count, valueOf, shown, stack, stackOf)\n    return {\n      spans: {\n        ...spans,\n        ...hiddenSpans(count, valueOf, series, shown, stack, stackOf),\n      },\n      extent: shown.length === 0 ? null : spanExtent(spans),\n    }\n  },\n  // The far end of the row's bars: the tallest, or the stack's top.\n  reach: (spans, shown, index, scale) => scale.place(farEnd(spans, shown, index)),\n}\n\n/**\n * A bridge's bars: each change runs from the running total before it to the\n * total after, and each row `isTotal` marks stands on the axis\n * (`bridgeSpans`). Bridges never stack. A hidden series sinks into where\n * each of its bars starts, so it leaves from there and comes back to it.\n */\nfunction bridgeMarks(isTotal: readonly boolean[]): BarMarkKind {\n  return {\n    mark: 'bar',\n    stacks: false,\n    // A change reads with its sign, and a total reads the running total, given or worked out.\n    reads: (values) => {\n      const changes = values.map(numberOf)\n      const runs = runningTotals(bridgeSpans(changes, isTotal))\n      return changes.map((value, at) => {\n        if (isTotal[at]) return { value: runs[at] ?? 0, signed: false }\n        return value === null ? null : { value, signed: true }\n      })\n    },\n    spans: ({ count, valueOf, series, shown }) => {\n      const spans: Record<string, (Span | null)[]> = {}\n      for (const key of series) {\n        const own = bridgeSpans(\n          Array.from({ length: count }, (_, at) => numberOf(valueOf(key, at))),\n          isTotal,\n        )\n        spans[key] = shown.includes(key)\n          ? own\n          : own.map((span) => span && [span[0], span[0]])\n      }\n      const reached = Object.fromEntries(shown.map((key) => [key, spans[key] ?? []]))\n      return { spans, extent: shown.length === 0 ? null : spanExtent(reached) }\n    },\n    // The far end of the row's bar: its top, or below zero, its foot.\n    reach: (spans, shown, index, scale) => scale.place(farEnd(spans, shown, index)),\n  }\n}\n\n/**\n * The `[from, to]` of each bar of a bridge, in the data's order: a change\n * runs from the running total before it to the total after it, and a row\n * `isTotal` marks stands on zero, at its own value when it has one, which\n * the run carries on from, or else at the running total. A missing change\n * has no bar, and the run carries on past it.\n */\nfunction bridgeSpans(\n  values: readonly (number | null)[],\n  isTotal: readonly boolean[],\n): (Span | null)[] {\n  let run = 0\n  return values.map((value, at) => {\n    if (isTotal[at]) {\n      run = value ?? run\n      return [0, run]\n    }\n    if (value === null) return null\n    const span: Span = [run, run + value]\n    run += value\n    return span\n  })\n}\n\n/** The running total after each bar of a bridge's `spans`: where its bar ends, or, for a missing change, where the one before ended. */\nfunction runningTotals(spans: readonly (Span | null)[]): number[] {\n  let run = 0\n  return spans.map((span) => {\n    if (span) run = span[1]\n    return run\n  })\n}\n\n/** How far the bars of row `index` reach from zero: the highest above it, or, with none above, the lowest below. */\nfunction farEnd(spans: SeriesSpans, keys: readonly string[], index: number) {\n  const up = reachOf(spans, keys, index)\n  return up > 0 ? up : reachOf(spans, keys, index, true)\n}\n\n/**\n * The part of a category's `band` the bar of `key` stands in: its slot\n * among the bars side by side, or its stack's among the stacks. `bars` are\n * the chart's bar series in order, and only the shown ones and `key` take a\n * slot, so the rest close up when one hides. Every slot is as wide at every\n * stop, and at a stop where only some of them have a row (`here`), such as\n * a series' own dated rows beside the chart's, those sit together in the\n * band's middle.\n */\nfunction barBand(\n  band: Band,\n  key: string,\n  bars: readonly string[],\n  hidden: readonly string[],\n  stack: ChartStack,\n  stackOf: (key: string) => string,\n  gap: number,\n  here: (key: string) => boolean,\n): Band {\n  const order = bars.filter((k) => k === key || !hidden.includes(k))\n  const slotOf = stack === 'none' ? (k: string) => k : stackOf\n  const slots = stacksOf(order, slotOf)\n  const filled = slots.filter(\n    (slot) => slot === slotOf(key) || order.some((k) => slotOf(k) === slot && here(k)),\n  )\n  return slotBand(band, filled.indexOf(slotOf(key)), filled.length, gap, slots.length)\n}\n\n/**\n * The part of `band` the series in `slot` takes when `slots` series sit\n * side by side, `gap` apart as a share of one slot, each as wide as one of\n * `room` slots: with fewer than `room`, the `slots` sit in the band's middle.\n * A gap below zero overlaps each slot with the one before by that share of\n * a slot, so at -0.5 each starts halfway along the last, and at -1 they all\n * stand in one place.\n */\nfunction slotBand(\n  band: Band,\n  slot: number,\n  slots: number,\n  gap = 0.12,\n  room = slots,\n): Band {\n  if (room <= 1 || slot < 0) return band\n  const at =\n    gap < 0 ? overlapAt : (index: number) => bandAt(index, room, { gap, outer: 0 })\n  function overlapAt(index: number): Band {\n    const size = 1 / (1 + (room - 1) * (1 + gap))\n    return { start: index * (1 + gap) * size, size }\n  }\n  const inner = at(slot)\n  const last = at(slots - 1)\n  const shift = (1 - (last.start + last.size)) / 2\n  return {\n    start: band.start + (inner.start + shift) * band.size,\n    size: inner.size * band.size,\n  }\n}\n\n/**\n * Throws on a `barGap` bars can't stand with: one of 1 or more leaves them\n * no width, and one of -1 or less stands them in one place or past it.\n */\nfunction checkBarGap(gap: number) {\n  if (!(gap > -1 && gap < 1)) {\n    throw new Error(\n      `\\`barGap\\` is a share of one bar's slot, more than -1 and less than 1, not ${gap}: at 1 or more the bars have no width, and at -1 or less they stand in one place, as \\`stack='overlap'\\` sets them. Below 0, each bar overlaps the one before by that share.`,\n    )\n  }\n}\n\nexport type { BarMarkKind, BarReading }\nexport {\n  barBand,\n  checkBarGap,\n  barKind,\n  barMarks,\n  bridgeMarks,\n  bridgeSpans,\n  farEnd,\n  runningTotals,\n  slotBand,\n}\n",
      "type": "registry:lib",
      "target": "@lib/chart-bar-marks.ts"
    }
  ],
  "type": "registry:lib"
}