{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "constellation-scatter-plot",
  "title": "Constellation Scatter Plot",
  "description": "A scatter plot drawn as a night sky: points twinkle, each segment is joined into a constellation, and pointing at a star lights up its whole group.",
  "dependencies": [],
  "files": [
    {
      "path": "registry/new-york/constellation-scatter-plot/constellation-scatter-plot.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface StarPoint {\n  label: string;\n  /** Position on each axis. */\n  x: number;\n  y: number;\n}\n\nexport interface Constellation {\n  label: string;\n  points: StarPoint[];\n}\n\nexport interface ConstellationScatterPlotProps extends Omit<\n  React.ComponentPropsWithoutRef<\"section\">,\n  \"title\"\n> {\n  theme?: \"dark\" | \"light\";\n  title?: string;\n  subtitle?: string;\n  groups?: Constellation[];\n  xLabel?: string;\n  yLabel?: string;\n  /** Colour of the constellation in focus. */\n  color?: string;\n  /** Height of the plot, in px. */\n  height?: number;\n  formatX?: (value: number) => string;\n  formatY?: (value: number) => string;\n}\n\nconst PALETTES = {\n  dark: {\n    card: \"#08090A\",\n    surface: \"#0F1011\",\n    border: \"rgba(255,255,255,0.08)\",\n    grid: \"rgba(255,255,255,0.05)\",\n    text: \"#F7F8F8\",\n    soft: \"#D0D6E0\",\n    sub: \"#8A8F98\",\n    faint: \"#62666D\",\n    tip: \"#161718\",\n    tipShadow: \"0 4px 24px rgba(0,0,0,0.4)\",\n    chip: \"rgba(255,255,255,0.05)\",\n    chipOn: \"rgba(255,255,255,0.1)\",\n    accent: \"#5E6AD2\",\n    line: \"rgba(255,255,255,0.16)\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    surface: \"#F7F8F8\",\n    border: \"rgba(8,9,10,0.08)\",\n    grid: \"rgba(8,9,10,0.06)\",\n    text: \"#08090A\",\n    soft: \"#3C3F44\",\n    sub: \"#62666D\",\n    faint: \"#8A8F98\",\n    tip: \"#FFFFFF\",\n    tipShadow: \"0 3px 12px rgba(8,9,10,0.09)\",\n    chip: \"rgba(8,9,10,0.04)\",\n    chipOn: \"#FFFFFF\",\n    accent: \"#5E6AD2\",\n    line: \"rgba(8,9,10,0.18)\",\n  },\n};\n\ntype Palette = (typeof PALETTES)[\"dark\"];\n\nconst CARD_FONT =\n  '\"Inter Variable\", Inter, \"SF Pro Display\", -apple-system, BlinkMacSystemFont, sans-serif';\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\n/** The chart is drawn at its real width, so its text stays the same size on a phone. */\nfunction useWidth(ref: React.RefObject<HTMLElement | null>) {\n  const [width, setWidth] = React.useState(700);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) =>\n      setWidth(Math.max(240, Math.round(entry.contentRect.width))),\n    );\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [ref]);\n  return width;\n}\n\nfunction Stat({\n  value,\n  label,\n  pal,\n}: {\n  value: string;\n  label: string;\n  pal: Palette;\n}) {\n  return (\n    <div className=\"text-left sm:text-right\">\n      <p className=\"m-0 text-[18px] leading-[1.2] font-[510] tracking-[-0.018em] tabular-nums\">\n        {value}\n      </p>\n      <p\n        className=\"m-0 mt-0.5 text-[12px] tracking-[-0.011em]\"\n        style={{ color: pal.sub }}\n      >\n        {label}\n      </p>\n    </div>\n  );\n}\n\nfunction Pills({\n  label,\n  options,\n  value,\n  onChange,\n  pal,\n}: {\n  label: string;\n  options: string[];\n  value: number;\n  onChange: (index: number) => void;\n  pal: Palette;\n}) {\n  return (\n    <div\n      className=\"flex rounded-full p-0.5\"\n      style={{ background: pal.chip }}\n      role=\"radiogroup\"\n      aria-label={label}\n    >\n      {options.map((option, i) => (\n        <button\n          key={option}\n          type=\"button\"\n          role=\"radio\"\n          aria-checked={i === value}\n          onClick={() => onChange(i)}\n          className=\"h-7 cursor-pointer rounded-full px-3 text-[13px] font-[510] tracking-[-0.011em] transition-colors outline-none focus-visible:ring-2\"\n          style={{\n            background: i === value ? pal.chipOn : \"transparent\",\n            color: i === value ? pal.text : pal.sub,\n          }}\n        >\n          {option}\n        </button>\n      ))}\n    </div>\n  );\n}\n\nfunction Tip({\n  x,\n  y,\n  width,\n  pal,\n  children,\n}: {\n  x: number;\n  y: number;\n  width: number;\n  pal: Palette;\n  children: React.ReactNode;\n}) {\n  return (\n    <div\n      aria-hidden=\"true\"\n      className=\"pointer-events-none absolute z-10 -translate-x-1/2 rounded-lg px-2.5 py-1.5 text-[12px] tracking-[-0.011em] whitespace-nowrap\"\n      style={{\n        left: Math.max(110, Math.min(width - 110, x)),\n        top: Math.max(0, y - 42),\n        background: pal.tip,\n        color: pal.sub,\n        boxShadow: `inset 0 0 0 1px ${pal.border}, ${pal.tipShadow}`,\n      }}\n    >\n      {children}\n    </div>\n  );\n}\n\n/** A loose cluster of accounts around a centre, from integer noise so server and client agree. */\nfunction cluster(\n  label: string,\n  seed: number,\n  count: number,\n  cx: number,\n  cy: number,\n  rx: number,\n  ry: number,\n): Constellation {\n  let state = seed;\n  const next = () => {\n    state = (state * 9301 + 49297) % 233280;\n    return state / 233280 - 0.5;\n  };\n  return {\n    label,\n    points: Array.from({ length: count }, (_, i) => ({\n      label: `${label} account ${i + 1}`,\n      x: Math.round(cx + next() * rx * 2),\n      y: Math.round(cy + next() * ry * 2),\n    })),\n  };\n}\n\nconst GROUPS: Constellation[] = [\n  cluster(\"Scale\", 23, 8, 76, 78, 16, 15),\n  cluster(\"Growth\", 67, 9, 50, 46, 16, 13),\n  cluster(\"Starter\", 11, 9, 22, 20, 15, 11),\n  cluster(\"At risk\", 89, 7, 76, 22, 14, 12),\n];\n\nconst LEFT = 36;\nconst RIGHT = 12;\nconst TOP = 12;\nconst BOTTOM = 24;\n/** Twinkles last two or four seconds, so they all line up again every four. */\nconst TWINKLES = [2000, 4000];\n\n/** The shortest lines that join every star in a group, so a constellation never crosses itself much. */\nfunction links(points: StarPoint[]) {\n  if (points.length < 2) return [] as [number, number][];\n  const joined = new Set([0]);\n  const edges: [number, number][] = [];\n  while (joined.size < points.length) {\n    let best: [number, number] | null = null;\n    let shortest = Infinity;\n    for (const a of joined) {\n      points.forEach((p, b) => {\n        if (joined.has(b)) return;\n        const d = Math.hypot(p.x - points[a].x, p.y - points[a].y);\n        if (d < shortest) {\n          shortest = d;\n          best = [a, b];\n        }\n      });\n    }\n    if (!best) break;\n    edges.push(best);\n    joined.add(best[1]);\n  }\n  return edges;\n}\n\nexport function ConstellationScatterPlot({\n  theme = \"dark\",\n  title = \"Accounts by health\",\n  subtitle = \"Usage against revenue, joined up by segment.\",\n  groups = GROUPS,\n  xLabel = \"Usage\",\n  yLabel = \"Revenue\",\n  color,\n  height = 290,\n  formatX = (n) => `${Math.round(n)}%`,\n  formatY = (n) => `$${Math.round(n)}k`,\n  className,\n  style,\n  ...props\n}: ConstellationScatterPlotProps) {\n  const pal = PALETTES[theme];\n  const accent = color ?? pal.accent;\n  const reduce = useReducedMotion();\n  const boxRef = React.useRef<HTMLDivElement>(null);\n  const width = useWidth(boxRef);\n  const starRefs = React.useRef<(SVGCircleElement | null)[]>([]);\n  const [picked, setPicked] = React.useState(0);\n  const [hover, setHover] = React.useState<{\n    group: number;\n    star: number;\n  } | null>(null);\n\n  const edges = React.useMemo(\n    () => groups.map((g) => links(g.points)),\n    [groups],\n  );\n  const all = groups.flatMap((g) => g.points);\n  const maxX = Math.max(1, ...all.map((p) => p.x)) * 1.04;\n  const maxY = Math.max(1, ...all.map((p) => p.y)) * 1.06;\n  const span = width - LEFT - RIGHT;\n  const base = height - BOTTOM;\n  const x = (v: number) => LEFT + (v / maxX) * span;\n  const y = (v: number) => base - (v / maxY) * (base - TOP);\n  // Pointing at a star shows its group; otherwise the pill decides. The first pill shows everything.\n  const focus = hover ? hover.group : picked - 1;\n  const inFocus = focus < 0 ? all : (groups[focus]?.points ?? []);\n  const avg = (list: number[]) =>\n    list.reduce((n, v) => n + v, 0) / Math.max(1, list.length);\n  const star = hover ? groups[hover.group]?.points[hover.star] : null;\n\n  // Stars twinkle on their own animations, so nothing re-renders.\n  React.useEffect(() => {\n    if (reduce) return;\n    const runs = starRefs.current.map((el, i) =>\n      el?.animate([{ opacity: 0.08 }, { opacity: 0.34 }, { opacity: 0.08 }], {\n        duration: TWINKLES[i % 2],\n        delay: -((i * 370) % 2000),\n        iterations: Infinity,\n      }),\n    );\n    return () => runs.forEach((run) => run?.cancel());\n  }, [reduce, all.length]);\n\n  // Where each group's stars begin in the flat list of twinkling halos.\n  const offsets = groups.map((_, i) =>\n    groups.slice(0, i).reduce((n, g) => n + g.points.length, 0),\n  );\n\n  return (\n    <section\n      className={`w-full rounded-2xl p-5 sm:p-6 ${className ?? \"\"}`}\n      style={{\n        background: pal.card,\n        boxShadow: `inset 0 0 0 1px ${pal.border}`,\n        color: pal.text,\n        fontFamily: CARD_FONT,\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-start justify-between gap-x-8 gap-y-3\">\n        <div>\n          <h3 className=\"m-0 text-[17px] leading-[1.33] font-[590] tracking-[-0.012em]\">\n            {title}\n          </h3>\n          <p\n            className=\"m-0 mt-0.5 text-[13px] tracking-[-0.011em]\"\n            style={{ color: pal.sub }}\n          >\n            {subtitle}\n          </p>\n        </div>\n        <div className=\"flex gap-7\">\n          <Stat\n            value={String(inFocus.length)}\n            label={\n              focus < 0 ? \"Accounts\" : (groups[focus]?.label ?? \"Accounts\")\n            }\n            pal={pal}\n          />\n          <Stat\n            value={formatX(avg(inFocus.map((p) => p.x)))}\n            label={`Average ${xLabel.toLowerCase()}`}\n            pal={pal}\n          />\n          <Stat\n            value={formatY(avg(inFocus.map((p) => p.y)))}\n            label={`Average ${yLabel.toLowerCase()}`}\n            pal={pal}\n          />\n        </div>\n      </div>\n\n      {!all.length ? (\n        <p className=\"py-16 text-center text-sm\" style={{ color: pal.sub }}>\n          No data available.\n        </p>\n      ) : (\n        <div\n          ref={boxRef}\n          className=\"relative mt-5\"\n          onPointerLeave={() => setHover(null)}\n        >\n          <svg\n            viewBox={`0 0 ${width} ${height}`}\n            className=\"block w-full\"\n            style={{ height }}\n            role=\"img\"\n            aria-label={groups\n              .map((g) => `${g.label}: ${g.points.length} accounts`)\n              .join(\", \")}\n          >\n            {[0, 0.5, 1].map((t) => (\n              <g key={t}>\n                <line\n                  x1={LEFT}\n                  x2={width - RIGHT}\n                  y1={y(maxY * t)}\n                  y2={y(maxY * t)}\n                  stroke={pal.grid}\n                />\n                <text\n                  x={LEFT - 8}\n                  y={y(maxY * t) + 4}\n                  textAnchor=\"end\"\n                  fontSize=\"11\"\n                  fill={pal.faint}\n                >\n                  {formatY(maxY * t)}\n                </text>\n                <text\n                  x={x(maxX * t)}\n                  y={height - 6}\n                  textAnchor={t === 0 ? \"start\" : t === 1 ? \"end\" : \"middle\"}\n                  fontSize=\"11\"\n                  fill={pal.faint}\n                >\n                  {formatX(maxX * t)}\n                </text>\n              </g>\n            ))}\n            {groups.map((g, gi) => {\n              const on = focus === gi;\n              const quiet = focus >= 0 && !on;\n              const midX = avg(g.points.map((p) => p.x));\n              const topY = Math.max(...g.points.map((p) => p.y));\n              return (\n                <g\n                  key={g.label}\n                  className=\"motion-safe:transition-opacity motion-safe:duration-300\"\n                  style={{ opacity: quiet ? 0.25 : 1 }}\n                >\n                  {edges[gi].map(([a, b]) => (\n                    <line\n                      key={`${a}-${b}`}\n                      x1={x(g.points[a].x)}\n                      y1={y(g.points[a].y)}\n                      x2={x(g.points[b].x)}\n                      y2={y(g.points[b].y)}\n                      stroke={on ? accent : pal.line}\n                      strokeWidth={on ? 1.25 : 1}\n                      className=\"motion-safe:transition-[stroke] motion-safe:duration-300\"\n                    />\n                  ))}\n                  {g.points.map((p, si) => {\n                    const index = offsets[gi] + si;\n                    const lit = hover?.group === gi && hover.star === si;\n                    return (\n                      <g\n                        key={p.label}\n                        onPointerEnter={() => setHover({ group: gi, star: si })}\n                      >\n                        <circle\n                          cx={x(p.x)}\n                          cy={y(p.y)}\n                          r=\"12\"\n                          fill=\"transparent\"\n                        />\n                        <circle\n                          ref={(el) => {\n                            starRefs.current[index] = el;\n                          }}\n                          cx={x(p.x)}\n                          cy={y(p.y)}\n                          r=\"8\"\n                          fill={on ? accent : pal.soft}\n                          opacity=\"0.18\"\n                        />\n                        <circle\n                          cx={x(p.x)}\n                          cy={y(p.y)}\n                          r={lit ? 4.5 : 3}\n                          fill={lit ? pal.text : on ? accent : pal.soft}\n                          className=\"motion-safe:transition-[r] motion-safe:duration-150\"\n                        />\n                      </g>\n                    );\n                  })}\n                  <text\n                    x={x(midX)}\n                    y={y(topY) - 12}\n                    textAnchor=\"middle\"\n                    fontSize=\"12\"\n                    fontWeight=\"510\"\n                    fill={on ? pal.text : pal.sub}\n                  >\n                    {g.label}\n                  </text>\n                </g>\n              );\n            })}\n          </svg>\n\n          {star && (\n            <Tip x={x(star.x)} y={y(star.y)} width={width} pal={pal}>\n              <span className=\"font-[510]\" style={{ color: pal.text }}>\n                {star.label}\n              </span>{\" \"}\n              {formatX(star.x)}, {formatY(star.y)}\n            </Tip>\n          )}\n        </div>\n      )}\n\n      <div className=\"mt-3 flex flex-wrap items-center justify-between gap-2\">\n        <Pills\n          label=\"Segment\"\n          options={[\"All\", ...groups.map((g) => g.label)]}\n          value={picked}\n          onChange={setPicked}\n          pal={pal}\n        />\n        <span className=\"text-[12px]\" style={{ color: pal.sub }}>\n          {xLabel} across, {yLabel.toLowerCase()} up\n        </span>\n      </div>\n    </section>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}