{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bump-chart",
  "title": "Bump chart",
  "description": "Rank-over-time lines that draw in with popping dots and names at the line ends; hovering or arrowing through periods and neighbours highlights one item and reads its change.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://goodui.dev/r/goodui-motion.json",
    "https://goodui.dev/r/locale.json"
  ],
  "files": [
    {
      "path": "registry/components/charts/bump-chart.tsx",
      "content": "\"use client\"\n\nimport * as React from \"react\"\nimport { motion, useReducedMotion } from \"motion/react\"\nimport { cn } from \"@/lib/utils\"\nimport { useLabels } from \"@/lib/goodui-locale\"\nimport { GooduiTokens, spring } from \"@/lib/goodui-motion\"\n\nexport type BumpSeries = {\n  key: string\n  label: string\n  /** Rank per period, 1 = top. */\n  ranks: number[]\n  /** Any CSS color. Defaults to the theme chart colors in series order (--chart-1 … --chart-5). */\n  color?: string\n}\n\nexport type BumpChartLabels = {\n  summary: (name: string, seriesCount: number, periodCount: number) => string\n  /** Announced for the active point; `change` is up/down/same text or null for the first period. */\n  point: (label: string, rank: string, period: string, change: string | null) => string\n  rank: (n: number) => string\n  up: (n: number) => string\n  down: (n: number) => string\n  same: string\n  columnPeriod: string\n  noData: string\n}\nconst DEFAULT_LABELS: BumpChartLabels = {\n  summary: (name, s, p) => `${name ? name + \": \" : \"\"}rank of ${s} items over ${p} periods`,\n  point: (label, rank, period, change) => `${label}: ${rank} in ${period}${change ? `, ${change}` : \"\"}`,\n  rank: (n) => `#${n}`,\n  up: (n) => `up ${n}`,\n  down: (n) => `down ${n}`,\n  same: \"unchanged\",\n  columnPeriod: \"Period\",\n  noData: \"No data\",\n}\n\nexport type BumpChartProps = Omit<React.ComponentProps<\"div\">, \"children\"> & {\n  series: BumpSeries[]\n  periods: string[]\n  /** Chart name for screen readers and the table caption. */\n  title?: React.ReactNode\n  /** Vertical space per rank in px. */\n  rowHeight?: number\n  /** Space reserved for the names at the end side, in px. */\n  labelWidth?: number\n  /** \"#1 … #n\" on the start side. */\n  rankLabels?: boolean\n  strokeWidth?: number\n  /** Seconds one line takes to draw; lines stagger by 0.1 s. */\n  duration?: number\n  /** Text for built-in controls and screen readers; pass translations here. */\n  labels?: Partial<BumpChartLabels>\n}\n\nfunction useReduce() {\n  const prefers = useReducedMotion()\n  const [mounted, setMounted] = React.useState(false)\n  React.useEffect(() => setMounted(true), [])\n  return mounted && !!prefers\n}\n\nfunction useInView(ref: React.RefObject<Element | null>, threshold = 0.2) {\n  const [inView, setInView] = React.useState(false)\n  React.useEffect(() => {\n    const el = ref.current\n    if (!el || inView) return\n    if (typeof IntersectionObserver === \"undefined\") {\n      setInView(true)\n      return\n    }\n    const io = new IntersectionObserver(\n      ([e]) => {\n        if (e?.isIntersecting) {\n          setInView(true)\n          io.disconnect()\n        }\n      },\n      { threshold },\n    )\n    io.observe(el)\n    return () => io.disconnect()\n  }, [ref, inView, threshold])\n  return inView\n}\n\nfunction useWidth(ref: React.RefObject<HTMLElement | null>) {\n  const [width, setWidth] = React.useState<number | null>(null)\n  React.useLayoutEffect(() => {\n    const el = ref.current\n    if (!el) return\n    // A hidden parent (display:none) measures 0: keep the last real width instead of collapsing the chart.\n    const measure = () => {\n      const w = Math.round(el.clientWidth)\n      if (w > 0) setWidth(Math.max(120, w))\n    }\n    measure()\n    const ro = new ResizeObserver(measure)\n    ro.observe(el)\n    return () => ro.disconnect()\n  }, [ref])\n  return width\n}\n\n/** Rank-over-time chart: lines draw in, dots pop at every rank, names sit at the line ends, and hovering or arrowing highlights one item. */\nexport function BumpChart({\n  series,\n  periods,\n  title,\n  rowHeight = 36,\n  labelWidth = 88,\n  rankLabels = true,\n  strokeWidth = 2.5,\n  duration = 1.1,\n  labels,\n  className,\n  \"aria-label\": ariaLabel,\n  ...props\n}: BumpChartProps) {\n  const l = useLabels(\"bump-chart\", DEFAULT_LABELS, labels)\n  const reduce = useReduce()\n  const rootRef = React.useRef<HTMLDivElement>(null)\n  const plotRef = React.useRef<HTMLDivElement>(null)\n  const inView = useInView(rootRef)\n  const width = useWidth(plotRef)\n  const [active, setActive] = React.useState<{ s: number; p: number } | null>(null)\n  const [hot, setHot] = React.useState<string | null>(null)\n\n  const m = periods.length\n  const maxRank = Math.max(series.length, ...series.flatMap((s) => s.ranks.slice(0, m)))\n  const colorOf = (s: BumpSeries, i: number) => s.color ?? `var(--chart-${(i % 5) + 1})`\n  const W = width ?? 0\n  const padL = rankLabels ? 30 : 10\n  const padR = labelWidth + 12\n  const padT = 10\n  const padB = 24\n  const plotW = Math.max(10, W - padL - padR)\n  const plotH = Math.max(rowHeight, (maxRank - 1) * rowHeight)\n  const height = padT + plotH + padB\n  const xOf = (i: number) => padL + (m > 1 ? (i / (m - 1)) * plotW : plotW / 2)\n  const yOf = (rank: number) => padT + (maxRank > 1 ? ((rank - 1) / (maxRank - 1)) * plotH : plotH / 2)\n  const dx = m > 1 ? plotW / (m - 1) : 0\n  // Period labels thin out when columns are closer than 40px; the last period keeps its label.\n  const every = Math.max(1, Math.ceil(40 / Math.max(1, dx)))\n  const showPeriod = (i: number) => i === m - 1 || (i % every === 0 && (m - 1 - i) * dx >= 40)\n\n  const pathOf = (ranks: number[]) => {\n    let d = \"\"\n    for (let i = 0; i < m; i++) {\n      const r = ranks[i]\n      if (r === undefined) continue\n      const x = xOf(i)\n      const y = yOf(r)\n      if (!d) d = `M${x.toFixed(1)},${y.toFixed(1)}`\n      else {\n        const px = xOf(i - 1)\n        const py = yOf(ranks[i - 1] ?? r)\n        d += `C${(px + dx * 0.5).toFixed(1)},${py.toFixed(1)} ${(x - dx * 0.5).toFixed(1)},${y.toFixed(1)} ${x.toFixed(1)},${y.toFixed(1)}`\n      }\n    }\n    return d\n  }\n\n  const name = typeof title === \"string\" ? title : \"\"\n  const summary = ariaLabel ?? (m && series.length ? l.summary(name, series.length, m) : l.noData)\n  const act = active ? series[active.s] : undefined\n  const actRank = act ? act.ranks[active?.p ?? 0] : undefined\n  const changeText = (s: BumpSeries, p: number) => {\n    if (p === 0) return null\n    const cur = s.ranks[p]\n    const prev = s.ranks[p - 1]\n    if (cur === undefined || prev === undefined) return null\n    return cur < prev ? l.up(prev - cur) : cur > prev ? l.down(cur - prev) : l.same\n  }\n  const announce = act && active && actRank !== undefined ? l.point(act.label, l.rank(actRank), periods[active.p] ?? \"\", changeText(act, active.p)) : \"\"\n  const highlight = hot ?? (act ? act.key : null)\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if (!series.length || !m) return\n    const cur = active ?? { s: 0, p: m - 1 }\n    if (e.key === \"ArrowLeft\" || e.key === \"ArrowRight\") {\n      e.preventDefault()\n      setActive({ s: cur.s, p: Math.min(m - 1, Math.max(0, cur.p + (e.key === \"ArrowLeft\" ? -1 : 1))) })\n    } else if (e.key === \"ArrowUp\" || e.key === \"ArrowDown\") {\n      e.preventDefault()\n      // Move by rank at the current period, so Up goes to the item drawn above.\n      const dir = e.key === \"ArrowUp\" ? -1 : 1\n      const order = series.map((s, i) => ({ i, r: s.ranks[cur.p] ?? Infinity })).sort((a, b) => a.r - b.r)\n      const pos = order.findIndex((o) => o.i === cur.s)\n      const next = order[Math.min(order.length - 1, Math.max(0, pos + dir))]\n      if (next) setActive({ s: next.i, p: cur.p })\n    } else if (e.key === \"Home\" || e.key === \"End\") {\n      e.preventDefault()\n      setActive({ s: cur.s, p: e.key === \"Home\" ? 0 : m - 1 })\n    } else if (e.key === \"Escape\") setActive(null)\n  }\n\n  const ready = inView || reduce\n  const ease = [0.65, 0, 0.35, 1] as const\n  const tipX = active ? xOf(active.p) : 0\n  const tipY = active && actRank !== undefined ? yOf(actRank) : 0\n  const tipRight = tipX > W * 0.55\n\n  return (\n    <div ref={rootRef} className={cn(\"pc-bump-chart flex w-full min-w-0 flex-col gap-2\", className)} {...props}>\n      <GooduiTokens />\n      <div\n        ref={plotRef}\n        role=\"group\"\n        aria-roledescription=\"bump chart\"\n        aria-label={summary}\n        tabIndex={0}\n        onKeyDown={onKeyDown}\n        onFocus={() => setActive((a) => a ?? (series.length && m ? { s: 0, p: m - 1 } : null))}\n        onBlur={() => setActive(null)}\n        onPointerLeave={() => {\n          setActive(null)\n          setHot(null)\n        }}\n        className=\"relative w-full rounded-md outline-none focus-visible:ring-3 focus-visible:ring-ring/50\"\n        style={{ height }}\n      >\n        {width !== null && series.length > 0 && m > 0 && (\n          <svg width={W} height={height} viewBox={`0 0 ${W} ${height}`} className=\"absolute inset-0 overflow-visible\" aria-hidden=\"true\">\n            {rankLabels &&\n              Array.from({ length: maxRank }, (_, i) => (\n                <text key={i} x={padL - 10} y={yOf(i + 1)} dy=\".35em\" textAnchor=\"end\" className=\"fill-muted-foreground text-[11px] tabular-nums\">\n                  {l.rank(i + 1)}\n                </text>\n              ))}\n            {periods.map((p, i) => (\n              <g key={i}>\n                <line x1={xOf(i)} x2={xOf(i)} y1={padT - 4} y2={padT + plotH + 4} className=\"stroke-border\" strokeWidth={1} />\n                {showPeriod(i) && (\n                  <text x={xOf(i)} y={height - 6} textAnchor={i === 0 ? \"start\" : i === m - 1 ? \"end\" : \"middle\"} className=\"fill-muted-foreground text-[11px]\">\n                    {p}\n                  </text>\n                )}\n              </g>\n            ))}\n            {series.map((s, i) => {\n              const d = pathOf(s.ranks)\n              const color = colorOf(s, i)\n              const dim = highlight !== null && highlight !== s.key\n              const last = s.ranks[m - 1]\n              return (\n                <g key={s.key} style={{ opacity: dim ? 0.22 : 1, transition: reduce ? undefined : \"opacity .2s\" }}>\n                  <motion.path\n                    d={d}\n                    fill=\"none\"\n                    style={{ stroke: color }}\n                    strokeWidth={strokeWidth}\n                    strokeLinecap=\"round\"\n                    initial={false}\n                    animate={{ pathLength: ready ? 1 : 0, opacity: ready ? 1 : 0 }}\n                    transition={{ pathLength: reduce ? { duration: 0 } : { duration, delay: i * 0.1, ease }, opacity: { duration: 0.01 } }}\n                  />\n                  <path d={d} fill=\"none\" stroke=\"transparent\" strokeWidth={16} className=\"cursor-pointer\" onPointerEnter={() => setHot(s.key)} onPointerLeave={() => setHot(null)} />\n                  {s.ranks.slice(0, m).map((r, p) => (\n                    <motion.g\n                      key={p}\n                      initial={false}\n                      animate={{ x: xOf(p), y: yOf(r), scale: ready ? 1 : 0, opacity: ready ? 1 : 0 }}\n                      transition={{ x: { duration: 0 }, y: { duration: 0 }, scale: reduce ? { duration: 0 } : { ...spring.play, delay: i * 0.1 + (m > 1 ? (p / (m - 1)) * duration : 0) }, opacity: { duration: 0.01 } }}\n                      className=\"cursor-pointer\"\n                      onPointerEnter={() => {\n                        setHot(s.key)\n                        setActive({ s: i, p })\n                      }}\n                      onPointerLeave={() => setActive(null)}\n                    >\n                      <circle r={11} fill=\"transparent\" />\n                      <circle r={active && active.s === i && active.p === p ? strokeWidth + 3.5 : strokeWidth + 2} style={{ fill: color }} className=\"stroke-background\" strokeWidth={2} />\n                    </motion.g>\n                  ))}\n                  {last !== undefined && (\n                    <motion.text\n                      x={padL + plotW + 12}\n                      y={yOf(last)}\n                      dy=\".35em\"\n                      className={cn(\"text-[12px]\", dim ? \"fill-muted-foreground\" : \"fill-foreground font-medium\")}\n                      initial={false}\n                      animate={{ opacity: ready ? 1 : 0, x: ready ? 0 : -6 }}\n                      transition={reduce ? { duration: 0 } : { duration: 0.35, delay: i * 0.1 + duration * 0.9 }}\n                    >\n                      {s.label}\n                    </motion.text>\n                  )}\n                </g>\n              )\n            })}\n          </svg>\n        )}\n        {width !== null && (series.length === 0 || m === 0) && <div className=\"absolute inset-0 grid place-items-center text-sm text-muted-foreground\">{l.noData}</div>}\n        {act && active && actRank !== undefined && (\n          <div\n            className=\"pointer-events-none absolute z-10 flex flex-col gap-0.5 rounded-md border bg-popover px-2.5 py-1.5 text-xs whitespace-nowrap text-popover-foreground shadow-[var(--pc-e2)]\"\n            style={{ left: tipX, top: tipY, transform: `translate(${tipRight ? \"calc(-100% - 14px)\" : \"14px\"}, -50%)` }}\n          >\n            <span className=\"text-muted-foreground\">{periods[active.p]}</span>\n            <span className=\"flex items-center gap-1.5 font-semibold\">\n              <span className=\"inline-block size-2 rounded-full\" style={{ background: colorOf(act, active.s) }} />\n              {act.label}\n              <span className=\"ms-1 tabular-nums\">{l.rank(actRank)}</span>\n            </span>\n            {changeText(act, active.p) && <span className=\"text-muted-foreground\">{changeText(act, active.p)}</span>}\n          </div>\n        )}\n        <span className=\"sr-only\" aria-live=\"polite\">\n          {announce}\n        </span>\n      </div>\n      {series.length > 0 && m > 0 && (\n        <div className=\"sr-only\">\n          <table>\n          <caption>{summary}</caption>\n          <thead>\n            <tr>\n              <th scope=\"col\">{l.columnPeriod}</th>\n              {series.map((s) => (\n                <th key={s.key} scope=\"col\">\n                  {s.label}\n                </th>\n              ))}\n            </tr>\n          </thead>\n          <tbody>\n            {periods.map((p, i) => (\n              <tr key={i}>\n                <th scope=\"row\">{p}</th>\n                {series.map((s) => (\n                  <td key={s.key}>{s.ranks[i] === undefined ? \"\" : l.rank(s.ranks[i] ?? 0)}</td>\n                ))}\n              </tr>\n            ))}\n          </tbody>\n        </table>\n        </div>\n      )}\n    </div>\n  )\n}\n",
      "type": "registry:component",
      "target": "components/goodui/bump-chart.tsx"
    }
  ],
  "categories": [
    "components",
    "charts"
  ],
  "type": "registry:component"
}