{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-area-marks",
  "title": "Chart area marks",
  "description": "Areas in the cartesian frame: their spans, stacked or as shares, with a hidden one folded where it would stack, and a stream’s layers around a centre line.",
  "dependencies": [
    "d3-shape"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/chart-frame.json",
    "https://kinoui.com/r/chart-line-marks.json",
    "https://kinoui.com/r/chart-math.json"
  ],
  "files": [
    {
      "path": "registry/lib/chart-area-marks.ts",
      "content": "import type { MarkKind, SeriesSpans } from '@/registry/lib/chart-frame'\nimport { highestPlace } from '@/registry/lib/chart-line-marks'\nimport {\n  addsUp,\n  type ChartStack,\n  numberOf,\n  hiddenSpans,\n  rangeOf,\n  reachOf,\n  seriesSpans,\n  type SeriesValueOf,\n  spanExtent,\n  type Span,\n} from '@/registry/lib/chart-math'\nimport {\n  type Series,\n  stack as d3Stack,\n  stackOffsetNone,\n  stackOffsetSilhouette,\n  stackOffsetWiggle,\n  stackOrderInsideOut,\n  stackOrderNone,\n} from 'd3-shape'\n\n/*\n * Areas: each series fills from its baseline to its value, from zero side by\n * side, or stacked on the areas before it by `stack` group. A value given as\n * `[from, to]` fills exactly that. A hidden series folds into the one below\n * it, and the areas above settle onto what is left.\n *\n * Streams: every series stacked in one order, the whole stream around a\n * centre line (wiggle or silhouette) or up from zero, each layer from where\n * the one under it ends.\n */\n\nconst areaMarks: MarkKind = {\n  mark: 'area',\n  stacks: true,\n  spans: ({ count, valueOf, series, shown, stack, stackOf }) => {\n    const drawn = stackedValues(valueOf, stack)\n    const spans = seriesSpans(count, drawn, shown, stack, stackOf)\n    return {\n      spans: {\n        ...spans,\n        ...hiddenSpans(count, drawn, series, shown, stack, stackOf),\n      },\n      extent: shown.length === 0 ? null : spanExtent(spans),\n    }\n  },\n  // Over the highest of the row's areas.\n  reach: (spans, shown, index, scale) => scale.place(reachOf(spans, shown, index)),\n}\n\n/**\n * The values as the areas draw them: stacked, a missing value counts as\n * nothing, so the areas above it stay whole; side by side it leaves a gap.\n */\nfunction stackedValues(valueOf: SeriesValueOf, stack: ChartStack): SeriesValueOf {\n  if (!addsUp(stack)) return valueOf\n  return (series, index) => {\n    const value = valueOf(series, index)\n    return typeof value === 'number' || rangeOf(value) ? value : 0\n  }\n}\n\n/**\n * Where a stream's layers sit: centred so its edges change as little as\n * they can (`wiggle`), centred on zero (`silhouette`), or stacked up from\n * zero (`zero`).\n */\ntype StreamOffset = 'wiggle' | 'silhouette' | 'zero'\n\nconst OFFSETS = {\n  wiggle: stackOffsetWiggle,\n  silhouette: stackOffsetSilhouette,\n  zero: stackOffsetNone,\n} as const\n\n/**\n * A stream's layers: every series stacked in `order`, bottom first, one\n * stream whatever `stack` the config gives, around a centre line or up from\n * zero as `offset` says. A missing value counts as nothing; a hidden series\n * thins to nothing where it sits, so the layers around it close over it.\n */\nfunction streamMarks(offset: StreamOffset, order: readonly string[]): MarkKind {\n  return {\n    mark: 'area',\n    stacks: true,\n    spans: ({ count, valueOf, series, shown }) => {\n      const spans = streamSpans(count, valueOf, inOrder(order, series), shown, offset)\n      return {\n        spans,\n        extent: shown.length === 0 || count === 0 ? null : streamExtent(spans),\n      }\n    },\n    // Over the top of the stream.\n    reach: highestPlace,\n  }\n}\n\n/** `series` in `order`, any `order` doesn't name after it in their own order. */\nfunction inOrder(order: readonly string[], series: readonly string[]): string[] {\n  return [\n    ...order.filter((key) => series.includes(key)),\n    ...series.filter((key) => !order.includes(key)),\n  ]\n}\n\n/**\n * Each layer's span at each of `count` stops, stacked in `order`, bottom\n * first, by `offset`: a hidden one, not in `shown`, with nothing in it.\n * Throws, naming the row, on a value below zero or a range, which a stream\n * can't stack.\n */\nfunction streamSpans(\n  count: number,\n  valueOf: SeriesValueOf,\n  order: readonly string[],\n  shown: readonly string[],\n  offset: StreamOffset,\n): SeriesSpans {\n  const values = Array.from({ length: count }, (_, at) =>\n    Object.fromEntries(\n      order.map((key) => {\n        const value = streamValue(valueOf(key, at), key, at)\n        return [key, shown.includes(key) ? value : 0]\n      }),\n    ),\n  )\n  const stacked = stackIn(order, values, stackOrderNone, offset)\n  return Object.fromEntries(\n    stacked.map((layer) => [layer.key, layer.map((point): Span => [point[0], point[1]])]),\n  )\n}\n\n/** A layer's value at a stop as a stream stacks it: missing counts as nothing; below zero and ranges throw. */\nfunction streamValue(value: unknown, key: string, at: number): number {\n  if (rangeOf(value)) {\n    throw new Error(\n      `Row ${at} gives \\`${key}\\` a range: a stream stacks single values, each from where the layer under it ends.`,\n    )\n  }\n  const number = numberOf(value)\n  if (number !== null && number < 0) {\n    throw new Error(\n      `Row ${at} gives \\`${key}\\` ${number}: a stream stacks its layers up from nothing, so it can't place a value below zero.`,\n    )\n  }\n  return number ?? 0\n}\n\n/** The stack d3 makes of `values`, one row per stop, its series `keys` in `order`, by `offset`. */\nfunction stackIn(\n  keys: readonly string[],\n  values: readonly Record<string, number>[],\n  order: typeof stackOrderNone,\n  offset: StreamOffset,\n): Series<Record<string, number>, string>[] {\n  return d3Stack<Record<string, number>, string>()\n    .keys(keys)\n    .value((row, key) => row[key] ?? 0)\n    .order(order)\n    .offset(OFFSETS[offset])([...values])\n}\n\n/** The lowest and highest any layer reaches, all of them stacked into one stream. */\nfunction streamExtent(spans: SeriesSpans): Span {\n  let low = Infinity\n  let high = -Infinity\n  for (const layer of Object.values(spans)) {\n    for (const span of layer) {\n      if (!span) continue\n      low = Math.min(low, span[0])\n      high = Math.max(high, span[1])\n    }\n  }\n  return Number.isFinite(low) ? [low, high] : [0, 0]\n}\n\n/**\n * The order a stream stacks `keys` in, bottom first, from their values at\n * `count` stops: d3's inside-out, the layers that swell first nearest the\n * middle and the later ones toward the edges, balanced by their totals, so\n * the stream's edges change least.\n */\nfunction streamOrder(\n  count: number,\n  valueOf: SeriesValueOf,\n  keys: readonly string[],\n): string[] {\n  const values = Array.from({ length: count }, (_, at) =>\n    Object.fromEntries(keys.map((key) => [key, streamValue(valueOf(key, at), key, at)])),\n  )\n  const stacked = stackIn(keys, values, stackOrderInsideOut, 'zero')\n  const order: string[] = []\n  for (const layer of stacked) order[layer.index] = layer.key\n  return order\n}\n\nexport {\n  areaMarks,\n  stackedValues,\n  type StreamOffset,\n  streamExtent,\n  streamMarks,\n  streamOrder,\n  streamSpans,\n}\n",
      "type": "registry:lib",
      "target": "@lib/chart-area-marks.ts"
    }
  ],
  "type": "registry:lib"
}