{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-scatter",
  "title": "Scatter chart",
  "description": "Points plotted left to right as small marks, one shape per series, with an optional fitted trend line drawn last.",
  "dependencies": [
    "svg-scroll-draw@^2.29.0"
  ],
  "registryDependencies": [
    "https://scrollfx-ui.vercel.app/r/plotter.json",
    "https://scrollfx-ui.vercel.app/r/chart.json"
  ],
  "files": [
    {
      "path": "registry/ui/chart-scatter.tsx",
      "content": "'use client';\n\n/**\n * chart-scatter — a scatter chart a pen plots as you scroll: the axes and\n * grid first, then every point as a small mark, left to right (circles in the\n * accent for the first group, then squares, triangles, diamonds and crosses in\n * ink), then, if you ask for it, a fitted trend line through each group (in\n * ink, for a single group).\n *\n * Built on svg-scroll-draw (MIT): https://github.com/DhruvilChauahan0210/scrollfx\n * Needs: react, svg-scroll-draw, lib/chart and lib/plotter (installed with it).\n * Styling: Tailwind CSS with shadcn/ui tokens.\n *\n * The axis labels, the legend and a data table for screen readers are in the\n * server HTML; the strokes are made in the browser at the chart's size and\n * finish as the chart reaches the middle of the screen. Reduced motion shows\n * the chart drawn.\n */\n\nimport { useMemo } from 'react';\nimport 'svg-scroll-draw/no-flash.css';\nimport {\n  ACCENT, DataTable, Legend, PHASE, PenLayers, XLabels, YLabels, formatter, gutter, linear, marker, niceTicks, num, seriesOf, toPath, useSize,\n  type ChartTrigger, type ChartConfig, type Datum, type Mark, type Pen,\n} from '@/lib/chart';\n\nexport interface ChartScatterProps {\n  /** One object per point. */\n  data?: Datum[];\n  /** The key holding each point's x value. */\n  x?: string;\n  /** The key holding each point's y value. */\n  y?: string;\n  /** The key that puts each point in a group (a series). Leave it out for one group. */\n  group?: string;\n  /** Labels (and colours, if you like) for the groups, by group name. */\n  config?: ChartConfig;\n  /** What the x axis measures, written under it. */\n  xLabel?: string;\n  /** What the y axis measures, written above it. */\n  yLabel?: string;\n  /** A least-squares line through each group, drawn last. */\n  trend?: boolean;\n  /** The plot's height in pixels; it takes the full width. */\n  height?: number;\n  /** How numbers on the axes are written (Intl.NumberFormat options). */\n  format?: Intl.NumberFormatOptions;\n  /** Show the key. By default, when there is more than one group. */\n  legend?: boolean;\n  /** The first group's colour. */\n  accent?: string;\n  /** What the chart shows, for screen readers (it captions the data table). */\n  label?: string;\n  /** `scroll`: inked as it rises, done at mid-screen. `view`: played once as it comes into view. */\n  trigger?: ChartTrigger;\n  /** With `view`, how long the chart takes, in ms. */\n  duration?: number;\n  className?: string;\n}\n\n// Sample points: two loose groups with a rising trend, from integer arithmetic only, so the server and the browser agree.\nconst SAMPLE: Datum[] = Array.from({ length: 36 }, (_, i) => {\n  const plan = i % 3 === 0 ? 'team' : 'solo';\n  const x = 4 + ((i * 37) % 53) + (plan === 'team' ? 12 : 0);\n  const wobble = ((i * 7919) % 29) - 14;\n  return { seats: x, hours: Math.round(18 + x * (plan === 'team' ? 1.25 : 0.8) + wobble), plan };\n});\nconst SAMPLE_CONFIG: ChartConfig = { solo: { label: 'Solo' }, team: { label: 'Team' } };\n\nexport function ChartScatter({\n  data = SAMPLE,\n  x = 'seats',\n  y = 'hours',\n  group = data === SAMPLE ? 'plan' : undefined,\n  config = data === SAMPLE ? SAMPLE_CONFIG : undefined,\n  xLabel = data === SAMPLE ? 'Seats' : undefined,\n  yLabel = data === SAMPLE ? 'Hours saved' : undefined,\n  trend = false,\n  height = 260,\n  format,\n  legend,\n  accent = ACCENT,\n  label = 'Scatter chart',\n  trigger = 'scroll',\n  duration = 1800,\n  className = '',\n}: ChartScatterProps) {\n  const names = group ? (config ? Object.keys(config) : [...new Set(data.map((d) => String(d[group])))]) : ['all'];\n  const series = seriesOf([], '', Object.fromEntries(names.map((g) => [g, { label: config?.[g]?.label ?? g, color: config?.[g]?.color }])), accent);\n  const xs = data.map((d) => num(d[x])), ys = data.map((d) => num(d[y]));\n  const xt = niceTicks(Math.min(...xs), Math.max(...xs), 6);\n  const yt = niceTicks(Math.min(0, ...ys), Math.max(...ys), 4);\n  const [x0, x1, y0, y1] = [xt[0], xt[xt.length - 1], yt[0], yt[yt.length - 1]];\n  const fmt = formatter(format);\n  const groupOf = (d: Datum) => (group ? String(d[group]) : 'all');\n  const [ref, size] = useSize<HTMLDivElement>();\n\n  const pens = useMemo((): Pen[] | null => {\n    if (!size) return null;\n    const { w, h } = size;\n    const sx = linear(x0, x1, 0, w), sy = linear(y0, y1, h, 0);\n    const axes: Mark[] = [\n      ...yt.map((t) => ({ d: toPath([[0, sy(t)], [w, sy(t)]]), width: t === y0 ? 1 : 0.6, opacity: t === y0 ? 0.9 : 0.2 })),\n      ...xt.map((t) => (t === x0 ? { d: toPath([[0, h], [0, 0]]), width: 1, opacity: 0.9 } : { d: toPath([[sx(t), h], [sx(t), 0]]), width: 0.6, opacity: 0.14 })),\n    ];\n    const points: Mark[] = data\n      .map((d) => ({ d, s: series.find((s) => s.key === groupOf(d)) }))\n      .filter((p) => p.s)\n      .sort((a, b) => num(a.d[x]) - num(b.d[x]))\n      .map(({ d, s }) => ({ d: marker(s!.marker, sx(num(d[x])), sy(num(d[y])), 3.4), color: s!.color, width: 1.3 }));\n    const lines: Mark[] = [];\n    if (trend) {\n      for (const s of series) {\n        const pts = data.filter((d) => groupOf(d) === s.key).map((d) => [num(d[x]), num(d[y])]);\n        if (pts.length < 2) continue;\n        const mx = pts.reduce((m, p) => m + p[0], 0) / pts.length, my = pts.reduce((m, p) => m + p[1], 0) / pts.length;\n        const sxx = pts.reduce((m, p) => m + (p[0] - mx) ** 2, 0);\n        if (!sxx) continue;\n        const slope = pts.reduce((m, p) => m + (p[0] - mx) * (p[1] - my), 0) / sxx;\n        const lo = Math.min(...pts.map((p) => p[0])), hi = Math.max(...pts.map((p) => p[0]));\n        lines.push({ d: toPath([[sx(lo), sy(my + slope * (lo - mx))], [sx(hi), sy(my + slope * (hi - mx))]]), color: series.length > 1 ? s.color : 'currentColor', width: 1.4, opacity: 0.85 });\n      }\n    }\n    return [\n      { marks: axes, ...PHASE.axes, spread: 0.6 },\n      { marks: points, from: 1.0, to: lines.length ? 0.64 : PHASE.ink.to, spread: 3 },\n      { marks: lines, from: 0.74, to: PHASE.ink.to, spread: 0.4 * (lines.length - 1) },\n    ];\n  }, [size, data, x, y, group, config, trend, accent, x0, x1, y0, y1]);\n\n  const yTicks = yt.map((t) => ({ label: fmt(t), at: (t - y0) / (y1 - y0) }));\n  return (\n    <figure className={`w-full ${className}`}>\n      <DataTable caption={label} head={[xLabel ?? x, yLabel ?? y, ...(group ? [group] : [])]} rows={data.map((d) => [num(d[x]), num(d[y]), ...(group ? [config?.[groupOf(d)]?.label ?? groupOf(d)] : [])])} />\n      <div aria-hidden>\n        {yLabel && <p data-slot=\"y-title\" className=\"mb-3 font-mono text-[11px] uppercase tracking-[0.14em] text-muted-foreground\">{yLabel}</p>}\n        <div className=\"flex\">\n          <YLabels ticks={yTicks} />\n          <div ref={ref} data-slot=\"plot\" className=\"relative min-w-0 flex-1\" style={{ height }}>\n            {size && pens ? <PenLayers size={size} pens={pens} trigger={trigger} duration={duration} /> : null}\n          </div>\n        </div>\n        <XLabels ticks={xt.map((t) => ({ label: fmt(t), at: (t - x0) / (x1 - x0) }))} offset={gutter(yTicks.map((t) => t.label))} />\n        {xLabel && <p data-slot=\"x-title\" className=\"mt-3 text-right font-mono text-[11px] uppercase tracking-[0.14em] text-muted-foreground\">{xLabel}</p>}\n        {(legend ?? series.length > 1) && <Legend series={series} kind=\"marker\" className=\"mt-4\" />}\n      </div>\n    </figure>\n  );\n}\n",
      "type": "registry:ui",
      "target": "components/scrollfx/chart-scatter.tsx"
    }
  ],
  "type": "registry:ui"
}