{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "heatmap",
  "title": "Heatmap",
  "author": "superzero11 <https://github.com/superzero11/uiset>",
  "description": "Magnitude on a grid in one hue, with a scale and arrows that move by cell and by row.",
  "dependencies": [
    "@ui-sdk/core@0.1.0-beta.5",
    "@ui-sdk/react@0.1.0-beta.5"
  ],
  "registryDependencies": [
    "https://uiset.dev/r/uiset.json",
    "https://uiset.dev/r/chart.json"
  ],
  "files": [
    {
      "path": "registry/uiset/ui/heatmap.tsx",
      "content": "\"use client\"\n\nimport type { ChartSelectChange } from \"./chart/points\"\nimport type { ReactNode } from \"react\"\nimport { ChartFrame, reactPlot } from \"./chart\"\nimport { heatmapFrame, heatmapHeight, heatmapPlot, type HeatmapOptions } from \"./chart/plots/heatmap\"\n\ntype HeatmapProps<Datum> = HeatmapOptions<Datum> & {\n  title: string\n  description?: ReactNode\n  height?: number\n  selected?: string | null\n  onSelect?: (key: string | null, details: ChartSelectChange) => void\n  actions?: ReactNode\n  className?: string\n}\n\n/** Magnitude on a grid, in one hue: the more, the stronger. Arrows move by cell and row. */\nfunction Heatmap<Datum>({ title, description, height, selected, onSelect, actions, className, ...options }: HeatmapProps<Datum>) {\n  const frame = heatmapFrame(options)\n  return (\n    <ChartFrame\n      title={title}\n      description={description}\n      height={heatmapHeight(options.rows, height)}\n      columns={frame.columns}\n      className={[frame.className, className].filter(Boolean).join(\" \")}\n      series={frame.series}\n      table={frame.table}\n      rows={frame.rows}\n      selected={selected}\n      onSelect={onSelect}\n      actions={actions}\n      plot={(context) => reactPlot(heatmapPlot(options, context))}\n    />\n  )\n}\n\nexport { Heatmap }\nexport type { HeatmapProps }\n",
      "type": "registry:ui",
      "target": "@ui/heatmap.tsx"
    },
    {
      "path": "registry/uiset/ui/chart/plots/heatmap.ts",
      "content": "// The heatmap's geometry: magnitude on a grid, in one hue.\nimport { focusKey, type ChartFrameData, type ChartGeometry, type ChartPlotContext, type ChartPoint } from \"../plot\"\nimport { extent, formatValue, numberFormat } from \"../scale\"\nimport { v, type VChild } from \"../vnode\"\n\ntype HeatmapOptions<Datum> = {\n  data: readonly Datum[]\n  /** Row and column labels; the grid follows the order given in `rows` and `columns`. */\n  rows: readonly string[]\n  columns: readonly string[]\n  row: (datum: Datum) => string\n  column: (datum: Datum) => string\n  value: (datum: Datum) => number | null | undefined\n  /** Names the measure in readings: \"Sessions\". */\n  measure: string\n  format?: (value: number) => string\n  /** Reads numbers in this locale; pass it when rendering on a server. */\n  locale?: string\n}\n\nconst STEPS = 7\nconst charWidth = 6.6\n\nfunction heatmapParts<Datum>({ data, row, column, value, format: formatOption, locale }: HeatmapOptions<Datum>) {\n  const cells = new Map<string, number | null>()\n  for (const d of data) {\n    const n = value(d)\n    cells.set(`${row(d)}\\u0001${column(d)}`, n === null || n === undefined || !Number.isFinite(n) ? null : n)\n  }\n  const at = (r: string, c: string) => cells.get(`${r}\\u0001${c}`) ?? null\n  const format = numberFormat(locale, formatOption)\n  const [low, high] = extent([...cells.values()])\n  const min = low ?? 0\n  const max = high ?? 0\n  const stepOf = (n: number) => (max === min ? Math.ceil(STEPS / 2) : Math.min(STEPS, 1 + Math.floor(((n - min) / (max - min)) * STEPS)))\n  return { at, format, min, max, stepOf }\n}\n\n/** The plot's height: given, or enough for every row. */\nconst heatmapHeight = (rows: readonly string[], height?: number) => height ?? Math.max(120, rows.length * 30 + 52)\n\nfunction heatmapFrame<Datum>(options: HeatmapOptions<Datum>): ChartFrameData {\n  const { rows, columns, measure } = options\n  const { at, format } = heatmapParts(options)\n  return {\n    className: \"heatmap\",\n    series: [{ key: \"value\", label: measure, shape: \"rect\" }],\n    columns: columns.length,\n    table: {\n      head: [\"\", ...columns],\n      numeric: [false, ...columns.map(() => true)],\n      rows: rows.map((r) => ({ key: r, cells: [r, ...columns.map((c) => formatValue(at(r, c), format))] })),\n    },\n    rows: () => rows.flatMap((r) => columns.map((c) => ({ row: r, column: c, [measure]: formatValue(at(r, c), format) }))),\n  }\n}\n\nfunction heatmapPlot<Datum>(options: HeatmapOptions<Datum>, { width, height: h, active, selected: pinned }: ChartPlotContext): ChartGeometry {\n  const { rows, columns, measure } = options\n  const { at, format, min, max, stepOf } = heatmapParts(options)\n  const keyOf = (r: number, c: number) => `${r}:${c}`\n  const left = Math.min(120, Math.max(0, ...rows.map((r) => r.length)) * charWidth + 12)\n  const top = 4\n  const legendBand = 30\n  const bottom = 20 + legendBand\n  const cellW = (width - left) / Math.max(1, columns.length)\n  const cellH = (h - top - bottom) / Math.max(1, rows.length)\n  const gap = 2\n  const focus = focusKey(active, pinned, (key) => {\n    const [r, c] = key.split(\":\").map(Number)\n    return Number.isInteger(r) && Number.isInteger(c) && r! >= 0 && c! >= 0 && r! < rows.length && c! < columns.length\n  })\n  const labelEvery = Math.max(1, Math.ceil((Math.max(0, ...columns.map((c) => c.length)) * charWidth + 6) / cellW))\n\n  const points: ChartPoint[] = rows.flatMap((r, i) =>\n    columns.map((c, j) => ({\n      key: keyOf(i, j),\n      label: `${r}, ${c}: ${measure} ${formatValue(at(r, c), format)}`,\n      x: left + j * cellW,\n      y: top + i * cellH,\n      width: cellW,\n      height: cellH,\n      anchor: { x: left + j * cellW + cellW / 2, y: top + i * cellH },\n    })),\n  )\n\n  const legendY = h - legendBand + 8\n  const swatch = Math.min(28, (width - left - 80) / STEPS)\n  const svg: VChild[] = [\n    v(\"g\", { key: \"rows\", className: \"chart-axis-labels\", \"data-axis\": \"rows\" }, ...rows.map((r, i) => v(\"text\", { key: r, x: left - 8, y: top + i * cellH + cellH / 2, dy: \"0.32em\", textAnchor: \"end\" }, r))),\n    v(\n      \"g\",\n      { key: \"columns\", className: \"chart-axis-labels\", \"data-axis\": \"columns\" },\n      ...columns.map((c, j) => (j % labelEvery === 0 ? v(\"text\", { key: c, x: left + j * cellW + cellW / 2, y: top + rows.length * cellH + 14, textAnchor: \"middle\" }, c) : null)),\n    ),\n    ...rows.flatMap((r, i) =>\n      columns.map((c, j) => {\n        const n = at(r, c)\n        const key = keyOf(i, j)\n        return v(\"rect\", {\n          key,\n          className: \"heatmap-cell\",\n          \"data-empty\": n === null ? \"\" : undefined,\n          \"data-active\": focus === key ? \"\" : undefined,\n          x: left + j * cellW + gap / 2,\n          y: top + i * cellH + gap / 2,\n          width: Math.max(0, cellW - gap),\n          height: Math.max(0, cellH - gap),\n          rx: Math.min(4, cellW / 4, cellH / 4),\n          style: n === null ? undefined : { \"--cell\": `var(--chart-seq-${stepOf(n)})` },\n        })\n      }),\n    ),\n    // The scale: one hue, light to strong, with its ends labeled.\n    v(\n      \"g\",\n      { key: \"scale\", className: \"heatmap-scale\", \"aria-hidden\": \"true\" },\n      v(\"text\", { className: \"heatmap-scale-label\", x: left, y: legendY + 8, dy: \"0.32em\", textAnchor: \"start\" }, formatValue(min, format)),\n      ...Array.from({ length: STEPS }, (_, k) =>\n        v(\"rect\", { key: k, className: \"heatmap-cell\", x: left + 44 + k * (swatch + 2), y: legendY, width: swatch, height: 16, rx: 3, style: { \"--cell\": `var(--chart-seq-${k + 1})` } }),\n      ),\n      v(\"text\", { className: \"heatmap-scale-label\", x: left + 44 + STEPS * (swatch + 2) + 6, y: legendY + 8, dy: \"0.32em\" }, formatValue(max, format)),\n    ),\n  ]\n  return {\n    svg,\n    points,\n    tooltip: (key) => {\n      const [i, j] = key.split(\":\").map(Number)\n      const r = rows[i!]!\n      const c = columns[j!]!\n      return [\n        v(\"span\", { key: \"title\", className: \"chart-tooltip-title\" }, r, \", \", c),\n        v(\"span\", { key: \"row\", className: \"chart-tooltip-row heatmap-tooltip-row\" }, v(\"strong\", {}, formatValue(at(r, c), format)), v(\"span\", {}, measure)),\n      ]\n    },\n  }\n}\n\nexport { heatmapFrame, heatmapHeight, heatmapPlot }\nexport type { HeatmapOptions }\n",
      "type": "registry:file",
      "target": "@ui/chart/plots/heatmap.ts"
    }
  ],
  "type": "registry:ui"
}