{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scatter-chart",
  "title": "Scatter chart",
  "author": "superzero11 <https://github.com/superzero11/uiset>",
  "description": "Two measures per item, up to three groups, with hover that finds the nearest point.",
  "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/scatter-chart.tsx",
      "content": "\"use client\"\n\nimport type { ChartSelectChange } from \"./chart/points\"\nimport type { ReactNode } from \"react\"\nimport { ChartFrame, reactPlot } from \"./chart\"\nimport { scatterFrame, scatterPlot, type ScatterOptions, type ScatterSeries } from \"./chart/plots/scatter\"\n\ntype ScatterChartProps<Datum> = ScatterOptions<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/** Two measures per item. The pointer finds the nearest point; arrows go left to right. */\nfunction ScatterChart<Datum>({ title, description, height = 280, selected, onSelect, actions, className, ...options }: ScatterChartProps<Datum>) {\n  const frame = scatterFrame(options)\n  return (\n    <ChartFrame\n      title={title}\n      description={description}\n      height={height}\n      nearest={frame.nearest}\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(scatterPlot(options, context))}\n    />\n  )\n}\n\nexport { ScatterChart }\nexport type { ScatterChartProps, ScatterSeries }\n",
      "type": "registry:ui",
      "target": "@ui/scatter-chart.tsx"
    },
    {
      "path": "registry/uiset/ui/chart/plots/scatter.ts",
      "content": "// The scatter chart's geometry: two measures per item, in up to three groups.\nimport { seriesColor, focusKey, type ChartFrameData, type ChartGeometry, type ChartPlotContext, type ChartPoint } from \"../plot\"\nimport { formatValue, linear, numberFormat, tickFormatter } from \"../scale\"\nimport { tooltipRow, v, type VChild } from \"../vnode\"\n\ntype ScatterSeries<Datum> = {\n  key: string\n  label: string\n  /** Which data belong to this series. */\n  match: (datum: Datum) => boolean\n}\n\ntype ScatterOptions<Datum> = {\n  data: readonly Datum[]\n  /** Names each point in readings and tooltips: \"Acme\". */\n  label: (datum: Datum) => string\n  x: (datum: Datum) => number\n  y: (datum: Datum) => number\n  xLabel: string\n  yLabel: string\n  /** Up to three groups. Every pair must stay apart for color-blind readers, so no more. */\n  series?: ScatterSeries<Datum>[]\n  formatX?: (value: number) => string\n  formatY?: (value: number) => string\n  /** Reads numbers in this locale; pass it when rendering on a server. */\n  locale?: string\n}\n\nconst charWidth = 6.6\nconst HIT = 24\n\nfunction scatterParts<Datum>({ data, x, y, yLabel, series, formatX: fx, formatY: fy, locale }: ScatterOptions<Datum>) {\n  const formatX = numberFormat(locale, fx)\n  const formatY = numberFormat(locale, fy)\n  const groups = (series ?? [{ key: \"all\", label: yLabel, match: () => true }]).slice(0, 3)\n  const groupOf = (d: Datum) => groups.find((g) => g.match(d))\n  // Data order for the keyboard is left to right, then bottom to top.\n  const ordered = data\n    .map((d, i) => ({ d, i }))\n    .filter(({ d }) => Number.isFinite(x(d)) && Number.isFinite(y(d)) && groupOf(d))\n    .sort((a, b) => x(a.d) - x(b.d) || y(a.d) - y(b.d))\n  return { formatX, formatY, groups, groupOf, ordered }\n}\n\nfunction scatterFrame<Datum>(options: ScatterOptions<Datum>): ChartFrameData {\n  const { label, x, y, xLabel, yLabel, series } = options\n  const { formatX, formatY, groups, groupOf, ordered } = scatterParts(options)\n  return {\n    className: \"scatter-chart\",\n    nearest: true,\n    series: groups.map((g) => ({ key: g.key, label: g.label, shape: \"dot\" })),\n    table: {\n      head: [\"\", xLabel, yLabel, ...(series ? [\"Group\"] : [])],\n      numeric: [false, true, true, false],\n      rows: ordered.map(({ d, i }) => ({\n        key: `${i}`,\n        cells: [label(d), formatValue(x(d), formatX), formatValue(y(d), formatY), ...(series ? [groupOf(d)!.label] : [])],\n      })),\n    },\n    rows: () =>\n      ordered.map(({ d }) => ({\n        name: label(d),\n        [xLabel]: formatValue(x(d), formatX),\n        [yLabel]: formatValue(y(d), formatY),\n        ...(series ? { group: groupOf(d)!.label } : {}),\n      })),\n  }\n}\n\nfunction scatterPlot<Datum>(options: ScatterOptions<Datum>, { width, height: h, shown, active, selected: pinned }: ChartPlotContext): ChartGeometry {\n  const { label, x, y, xLabel, yLabel, series, locale } = options\n  const { formatX, formatY, groups, groupOf, ordered } = scatterParts(options)\n  const visible = ordered.filter(({ d }) => shown(groupOf(d)!.key))\n  const top = 10\n  const bottom = 44\n  const yScale = linear(visible.map(({ d }) => y(d)), [h - bottom, top], { zero: false })\n  const yTick = tickFormatter(yScale.ticks, locale)\n  const left = Math.max(0, ...yScale.ticks.map((t) => yTick(t).length)) * charWidth + 14\n  const right = 12\n  const xScale = linear(visible.map(({ d }) => x(d)), [left, width - right], { zero: false })\n  const xTick = tickFormatter(xScale.ticks, locale)\n  const focus = focusKey(active, pinned, (key) => visible.some(({ i }) => `${i}` === key))\n\n  const points: ChartPoint[] = visible.map(({ d, i }) => {\n    const cx = xScale(x(d))\n    const cy = yScale(y(d))\n    return {\n      key: `${i}`,\n      label: `${label(d)}: ${xLabel} ${formatValue(x(d), formatX)}, ${yLabel} ${formatValue(y(d), formatY)}${series ? `, ${groupOf(d)!.label}` : \"\"}`,\n      x: cx - HIT / 2,\n      y: cy - HIT / 2,\n      width: HIT,\n      height: HIT,\n      anchor: { x: cx, y: cy - 4 },\n    }\n  })\n\n  const svg: VChild[] = [\n    v(\n      \"g\",\n      { key: \"grid\", className: \"chart-grid\" },\n      ...yScale.ticks.map((t) => v(\"line\", { key: `y${t}`, x1: left, x2: width - right, y1: yScale(t), y2: yScale(t) })),\n      ...xScale.ticks.map((t) => v(\"line\", { key: `x${t}`, x1: xScale(t), x2: xScale(t), y1: top, y2: h - bottom })),\n    ),\n    v(\n      \"g\",\n      { key: \"labels\", className: \"chart-axis-labels\" },\n      ...yScale.ticks.map((t) => v(\"text\", { key: `y${t}`, x: left - 8, y: yScale(t), dy: \"0.32em\", textAnchor: \"end\" }, yTick(t))),\n      ...xScale.ticks.map((t) => v(\"text\", { key: `x${t}`, x: xScale(t), y: h - bottom + 16, textAnchor: \"middle\" }, xTick(t))),\n      v(\"text\", { key: \"x-title\", className: \"chart-axis-title\", x: (left + width - right) / 2, y: h - 6, textAnchor: \"middle\" }, xLabel),\n      v(\"text\", { key: \"y-title\", className: \"chart-axis-title\", x: left, y: top - 2, dy: \"-0.2em\", textAnchor: \"start\" }, yLabel),\n    ),\n    ...visible.map(({ d, i }) =>\n      v(\"circle\", {\n        key: i,\n        className: \"chart-dot\",\n        \"data-active\": focus === `${i}` ? \"\" : undefined,\n        \"data-dim\": focus !== null && focus !== `${i}` ? \"\" : undefined,\n        cx: xScale(x(d)),\n        cy: yScale(y(d)),\n        r: focus === `${i}` ? 6 : 4.5,\n        style: { \"--series\": seriesColor(groups, groupOf(d)!.key) },\n      }),\n    ),\n  ]\n  return {\n    svg,\n    points,\n    tooltip: (key) => {\n      const found = ordered.find(({ i }) => `${i}` === key)\n      if (!found) return null\n      const { d } = found\n      const color = seriesColor(groups, groupOf(d)!.key)\n      return [\n        v(\"span\", { key: \"title\", className: \"chart-tooltip-title\" }, label(d)),\n        tooltipRow(color, formatValue(x(d), formatX), xLabel, \"dot\", \"x\"),\n        tooltipRow(color, formatValue(y(d), formatY), yLabel, \"dot\", \"y\"),\n      ]\n    },\n  }\n}\n\nexport { scatterFrame, scatterPlot }\nexport type { ScatterOptions, ScatterSeries }\n",
      "type": "registry:file",
      "target": "@ui/chart/plots/scatter.ts"
    }
  ],
  "type": "registry:ui"
}