{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-line-marks",
  "title": "Chart line marks",
  "description": "Lines in the cartesian frame: their values, which never stack, how far they reach, and their points as fractions of the plot.",
  "dependencies": [],
  "registryDependencies": [
    "https://kinoui.com/r/chart-frame.json",
    "https://kinoui.com/r/chart-math.json",
    "https://kinoui.com/r/line-path.json"
  ],
  "files": [
    {
      "path": "registry/lib/chart-line-marks.ts",
      "content": "import type { MarkKind, SeriesSpans } from '@/registry/lib/chart-frame'\nimport { numberOf, type SeriesValueOf, type Span } from '@/registry/lib/chart-math'\nimport { type Band, bandCentre, type LinearScale } from '@/registry/lib/chart-scale'\nimport type { LinePoint } from '@/registry/lib/line-path'\n\n/*\n * Lines: each series' line runs through its values, one for each category,\n * and never stacks. Its span in a row is its value at both ends. The points\n * a line or an area is drawn through, as fractions of the plot, come from\n * here too.\n */\n\nconst lineMarks: MarkKind = {\n  mark: 'line',\n  stacks: false,\n  spans: ({ count, valueOf, series, shown }) => ({\n    spans: valueSpans(count, valueOf, series),\n    extent: valueExtent(count, valueOf, shown),\n  }),\n  // Over the highest of the row's points.\n  reach: highestPlace,\n}\n\n/** Each series' value at each of `count` stops, as a span from it to itself, null where it is missing. */\nfunction valueSpans(\n  count: number,\n  valueOf: SeriesValueOf,\n  series: readonly string[],\n): SeriesSpans {\n  return Object.fromEntries(\n    series.map((key) => [\n      key,\n      Array.from({ length: count }, (_, at): Span | null => {\n        const value = numberOf(valueOf(key, at))\n        return value === null ? null : [value, value]\n      }),\n    ]),\n  )\n}\n\n/** The lowest and highest values of `keys` at each of `count` stops, or null with none. */\nfunction valueExtent(\n  count: number,\n  valueOf: SeriesValueOf,\n  keys: readonly string[],\n): Span | null {\n  const values = Array.from({ length: count }, (_, at) =>\n    keys.flatMap((key) => {\n      const value = numberOf(valueOf(key, at))\n      return value === null ? [] : [value]\n    }),\n  ).flat()\n  return values.length === 0 ? null : [Math.min(...values), Math.max(...values)]\n}\n\n/** The highest place on `scale` the far end of a shown series' span reaches in row `index`, or null with none. */\nfunction highestPlace(\n  spans: SeriesSpans,\n  shown: readonly string[],\n  index: number,\n  scale: LinearScale,\n): number | null {\n  let far: number | null = null\n  for (const key of shown) {\n    const span = spans[key]?.[index]\n    if (!span) continue\n    const place = scale.place(span[1])\n    far = far === null ? place : Math.max(far, place)\n  }\n  return far\n}\n\n/**\n * A series' points through `categories`, as fractions of the plot from the\n * reading start and the top: the line at the far end of its span, and with\n * `withBase`, an area's baseline at the near end. Where it has no value,\n * and all along when it lies `flat`, they sit on `baseline`, the fraction of\n * the value axis marks grow from.\n */\nfunction seriesPoints(\n  categories: readonly { key: string; index: number; band: Band }[],\n  spans: readonly (Span | null)[],\n  scale: LinearScale,\n  baseline: number,\n  flat: boolean,\n  withBase: boolean,\n): LinePoint[] {\n  const rest = 1 - baseline\n  return categories.map((category) => {\n    const own = spans[category.index] ?? null\n    const span = flat ? null : own\n    return {\n      key: category.key,\n      // At the band's centre, so a line runs through the middle of the bars beside it.\n      x: bandCentre(category.band),\n      y: span ? 1 - scale.place(span[1]) : rest,\n      ...(withBase ? { base: span ? 1 - scale.place(span[0]) : rest } : {}),\n      defined: own !== null,\n    }\n  })\n}\n\nexport { highestPlace, lineMarks, seriesPoints, valueExtent, valueSpans }\n",
      "type": "registry:lib",
      "target": "@lib/chart-line-marks.ts"
    }
  ],
  "type": "registry:lib"
}