{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "multi-line-chart",
  "type": "registry:component",
  "title": "Multi-line Chart",
  "description": "Smooth lines on shared axes with an interactive legend, a crosshair that snaps to the nearest point and a tooltip listing every series.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "charts",
    "line",
    "chart",
    "multi-series",
    "crosshair",
    "legend",
    "analytics"
  ],
  "docs": "https://hyperblur.dev/effects/multi-line-chart",
  "files": [
    {
      "path": "registry/hyperblur/multi-line-chart.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/multi-line-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface LineSeries {\n  name: string;\n  data: number[];\n  color?: string;\n}\n\nexport interface MultiLineChartProps extends React.HTMLAttributes<HTMLDivElement> {\n  series?: LineSeries[];\n  labels?: string[];\n  colors?: string[];\n  height?: number;\n  smooth?: boolean;\n  legend?: \"top\" | \"bottom\" | \"none\";\n  valueFormatter?: (n: number) => string;\n  locale?: string;\n  animate?: boolean;\n}\n\nconst PALETTE = [\"#2f7bff\", \"#8b5cf6\", \"#34d399\", \"#fbbf24\", \"#fb7185\", \"#7fb3ff\"];\n\nconst DEFAULT_LABELS = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"];\n\nconst DEFAULT_SERIES: LineSeries[] = [\n  { name: \"Organic\", data: [32, 36, 35, 41, 47, 46, 54, 59, 57, 66, 71, 78] },\n  { name: \"Paid\", data: [24, 28, 33, 31, 38, 42, 40, 45, 52, 49, 55, 58] },\n  { name: \"Referral\", data: [12, 14, 13, 17, 16, 21, 24, 23, 27, 30, 29, 35] },\n];\n\nconst css = `\n.hb-mlc { position: relative; display: flex; flex-direction: column; gap: 8px; width: 100%; min-width: 0; box-sizing: border-box; color: #ededed; font-family: inherit; }\n.hb-mlc *, .hb-mlc *::before, .hb-mlc *::after { box-sizing: border-box; }\n.hb-mlc-plot { position: relative; width: 100%; min-width: 0; touch-action: pan-y; }\n.hb-mlc-svg { display: block; width: 100%; overflow: visible; outline: none; }\n.hb-mlc-svg:focus-visible { outline: 2px solid #7fb3ff; outline-offset: 4px; border-radius: 4px; }\n.hb-mlc-grid { stroke: #262626; stroke-width: 1; stroke-dasharray: 3 5; }\n.hb-mlc-base { stroke: #262626; stroke-width: 1; }\n.hb-mlc-tick {\n  fill: #8f8f8f;\n  font-size: 11px;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-mlc-xl { fill: #8f8f8f; font-size: 11px; font-family: inherit; transition: fill .15s ease; }\n.hb-mlc-xl[data-on=\"true\"] { fill: #ededed; }\n.hb-mlc-line {\n  fill: none;\n  stroke-width: 2;\n  stroke-linecap: round;\n  stroke-linejoin: round;\n  stroke-dasharray: 1;\n  stroke-dashoffset: 0;\n  animation: hb-mlc-draw 1.3s cubic-bezier(.22, .8, .24, 1) backwards;\n  animation-delay: calc(var(--hb-mlc-i, 0) * 120ms);\n  transition: opacity .25s ease, stroke-width .2s ease;\n}\n.hb-mlc-line[data-off=\"true\"] { opacity: 0; }\n.hb-mlc-line[data-dim=\"true\"] { opacity: .3; }\n.hb-mlc-cross { stroke: #3a3a3a; stroke-width: 1; stroke-dasharray: 3 4; pointer-events: none; }\n.hb-mlc-dot { transition: opacity .2s ease; pointer-events: none; }\n.hb-mlc-tip {\n  position: absolute;\n  z-index: 2;\n  min-width: 110px;\n  padding: 7px 10px;\n  border-radius: 8px;\n  border: 1px solid #262626;\n  background: #171717;\n  box-shadow: 0 10px 24px -8px rgba(0, 0, 0, .8);\n  font-size: 11px;\n  line-height: 1.45;\n  white-space: nowrap;\n  pointer-events: none;\n}\n.hb-mlc-tip-h { margin-bottom: 2px; color: #8f8f8f; }\n.hb-mlc-tip-r { display: flex; align-items: center; gap: 6px; color: #ededed; }\n.hb-mlc-tip-d { width: 6px; height: 6px; border-radius: 2px; flex: none; }\n.hb-mlc-tip-v {\n  margin-left: auto;\n  padding-left: 12px;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-mlc-legend { display: flex; flex-wrap: wrap; gap: 4px; margin: 0; padding: 0; list-style: none; }\n.hb-mlc-btn {\n  display: inline-flex;\n  align-items: center;\n  gap: 7px;\n  padding: 3px 9px 3px 7px;\n  border: 1px solid transparent;\n  border-radius: 999px;\n  background: transparent;\n  color: #ededed;\n  font: inherit;\n  font-size: 11.5px;\n  line-height: 1.3;\n  cursor: pointer;\n  transition: background-color .15s ease, color .15s ease;\n}\n.hb-mlc-btn:hover { background: #1a1a1a; }\n.hb-mlc-btn:focus-visible { outline: 2px solid #7fb3ff; outline-offset: 1px; }\n.hb-mlc-btn[aria-pressed=\"false\"] { color: #6b6b6b; }\n.hb-mlc-btn[aria-pressed=\"false\"] .hb-mlc-name { text-decoration: line-through; }\n.hb-mlc-sw { width: 14px; height: 3px; border-radius: 2px; flex: none; transition: opacity .2s ease; }\n.hb-mlc-btn[aria-pressed=\"false\"] .hb-mlc-sw { opacity: .35; }\n.hb-mlc-name { white-space: nowrap; }\n.hb-mlc[data-animate=\"false\"] .hb-mlc-line { animation: none; }\n@keyframes hb-mlc-draw {\n  from { stroke-dashoffset: 1; opacity: 0; }\n  3% { opacity: 1; }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-mlc-line { animation: none !important; }\n  .hb-mlc-line, .hb-mlc-dot, .hb-mlc-btn { transition: none !important; }\n}\n`;\n\nfunction f(n: number) {\n  return n.toFixed(2);\n}\n\nfunction niceStep(raw: number) {\n  if (!(raw > 0)) return 1;\n  const exp = Math.pow(10, Math.floor(Math.log10(raw)));\n  const m = raw / exp;\n  const nice = m <= 1 ? 1 : m <= 2 ? 2 : m <= 2.5 ? 2.5 : m <= 5 ? 5 : 10;\n  return nice * exp;\n}\n\nfunction monotone(pts: [number, number][]) {\n  const n = pts.length;\n  if (n === 0) return \"\";\n  if (n === 1) return `M${f(pts[0][0])},${f(pts[0][1])}`;\n  const dx: number[] = [];\n  const m: number[] = [];\n  for (let i = 0; i < n - 1; i++) {\n    dx[i] = pts[i + 1][0] - pts[i][0] || 1;\n    m[i] = (pts[i + 1][1] - pts[i][1]) / dx[i];\n  }\n  const t: number[] = new Array(n).fill(0);\n  t[0] = m[0];\n  t[n - 1] = m[n - 2];\n  for (let i = 1; i < n - 1; i++) t[i] = m[i - 1] * m[i] <= 0 ? 0 : (m[i - 1] + m[i]) / 2;\n  for (let i = 0; i < n - 1; i++) {\n    if (m[i] === 0) {\n      t[i] = 0;\n      t[i + 1] = 0;\n    } else {\n      const a = t[i] / m[i];\n      const b = t[i + 1] / m[i];\n      const s = a * a + b * b;\n      if (s > 9) {\n        const k = 3 / Math.sqrt(s);\n        t[i] = k * a * m[i];\n        t[i + 1] = k * b * m[i];\n      }\n    }\n  }\n  let d = `M${f(pts[0][0])},${f(pts[0][1])}`;\n  for (let i = 0; i < n - 1; i++) {\n    const [x0, y0] = pts[i];\n    const [x1, y1] = pts[i + 1];\n    d += `C${f(x0 + dx[i] / 3)},${f(y0 + (t[i] * dx[i]) / 3)} ${f(x1 - dx[i] / 3)},${f(y1 - (t[i + 1] * dx[i]) / 3)} ${f(x1)},${f(y1)}`;\n  }\n  return d;\n}\n\nfunction straight(pts: [number, number][]) {\n  return pts.map(([x, y], i) => `${i === 0 ? \"M\" : \"L\"}${f(x)},${f(y)}`).join(\"\");\n}\n\nfunction useWidth(fallback: number) {\n  const ref = React.useRef<HTMLDivElement>(null);\n  const [width, setWidth] = React.useState(fallback);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el || typeof ResizeObserver === \"undefined\") return;\n    const ro = new ResizeObserver((entries) => {\n      const w = Math.round(entries[0].contentRect.width);\n      if (w > 0) setWidth(w);\n    });\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n  return [ref, width] as const;\n}\n\nexport function MultiLineChart({\n  series = DEFAULT_SERIES,\n  labels = DEFAULT_LABELS,\n  colors = PALETTE,\n  height = 200,\n  smooth = true,\n  legend = \"bottom\",\n  valueFormatter,\n  locale = \"en-US\",\n  animate = true,\n  className = \"\",\n  ...rest\n}: MultiLineChartProps) {\n  const [ref, width] = useWidth(320);\n  const [hidden, setHidden] = React.useState<number[]>([]);\n  const [idx, setIdx] = React.useState<number | null>(null);\n  const [hoverSeries, setHoverSeries] = React.useState<number | null>(null);\n\n  const compact = React.useMemo(\n    () => new Intl.NumberFormat(locale, { notation: \"compact\", maximumFractionDigits: 1 }),\n    [locale]\n  );\n  const full = React.useMemo(() => new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }), [locale]);\n  const axisFmt = (v: number) => (valueFormatter ? valueFormatter(v) : compact.format(v));\n  const tipFmt = (v: number) => (valueFormatter ? valueFormatter(v) : full.format(v));\n\n  const items = series.map((s, si) => ({ ...s, si, color: s.color ?? colors[si % colors.length] }));\n  const n = Math.max(labels.length, ...items.map((s) => s.data.length), 0);\n  const nums = items.flatMap((s) => s.data.filter((v) => Number.isFinite(v)));\n  const hi = nums.length ? Math.max(...nums) : 1;\n  const lo = nums.length ? Math.min(...nums) : 0;\n  const fromZero = lo >= 0 && lo < hi * 0.5;\n  const step = niceStep((hi - (fromZero ? 0 : lo)) / 4);\n  const yMin = fromZero ? 0 : Math.floor(lo / step) * step;\n  let yMax = Math.ceil(hi / step) * step;\n  if (yMax <= yMin) yMax = yMin + step;\n  const ticks: number[] = [];\n  for (let v = yMin; v <= yMax + step / 1000 && ticks.length < 9; v += step) ticks.push(v);\n\n  const padL = Math.max(28, ...ticks.map((t) => axisFmt(t).length * 6.4 + 10));\n  const padR = 8;\n  const padT = 10;\n  const padB = 24;\n  const inset = 8;\n  const innerW = Math.max(1, width - padL - padR);\n  const innerH = Math.max(1, height - padT - padB);\n  const yOf = (v: number) => padT + innerH - ((v - yMin) / (yMax - yMin)) * innerH;\n  const xOf = (i: number) => padL + inset + (n > 1 ? (i / (n - 1)) * (innerW - inset * 2) : (innerW - inset * 2) / 2);\n  const slot = n > 1 ? (innerW - inset * 2) / (n - 1) : innerW;\n  const every = Math.max(1, Math.ceil(40 / slot));\n\n  const paths = items.map((s) => {\n    const pts: [number, number][] = [];\n    s.data.forEach((v, i) => {\n      if (Number.isFinite(v)) pts.push([xOf(i), yOf(v)]);\n    });\n    return smooth ? monotone(pts) : straight(pts);\n  });\n\n  const toggle = (si: number) =>\n    setHidden((h) => (h.includes(si) ? h.filter((x) => x !== si) : [...h, si]));\n\n  const pointerIdx = (e: React.PointerEvent<SVGSVGElement>) => {\n    const rect = e.currentTarget.getBoundingClientRect();\n    const vx = (e.clientX - rect.left) * (width / Math.max(1, rect.width));\n    const raw = n > 1 ? ((vx - padL - inset) / (innerW - inset * 2)) * (n - 1) : 0;\n    setIdx(Math.min(n - 1, Math.max(0, Math.round(raw))));\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent<SVGSVGElement>) => {\n    const cur = idx ?? n - 1;\n    let next: number | null = null;\n    if (e.key === \"ArrowLeft\") next = Math.max(0, cur - 1);\n    else if (e.key === \"ArrowRight\") next = Math.min(n - 1, cur + 1);\n    else if (e.key === \"Home\") next = 0;\n    else if (e.key === \"End\") next = n - 1;\n    else if (e.key === \"Escape\") {\n      setIdx(null);\n      return;\n    }\n    if (next !== null) {\n      e.preventDefault();\n      setIdx(next);\n    }\n  };\n\n  const summary = items.length\n    ? `Line chart with ${items.length} series over ${n} points. ${items\n        .map((s) => {\n          const vals = s.data.filter((v) => Number.isFinite(v));\n          return vals.length ? `${s.name} goes from ${tipFmt(vals[0])} to ${tipFmt(vals[vals.length - 1])}` : s.name;\n        })\n        .join(\". \")}.`\n    : \"Line chart with no data\";\n\n  const legendEl =\n    legend === \"none\" ? null : (\n      <ul className=\"hb-mlc-legend\">\n        {items.map((s) => {\n          const on = !hidden.includes(s.si);\n          return (\n            <li key={s.si}>\n              <button\n                type=\"button\"\n                className=\"hb-mlc-btn\"\n                aria-pressed={on}\n                onClick={() => toggle(s.si)}\n                onMouseEnter={() => setHoverSeries(s.si)}\n                onMouseLeave={() => setHoverSeries(null)}\n                onFocus={() => setHoverSeries(s.si)}\n                onBlur={() => setHoverSeries(null)}\n              >\n                <span className=\"hb-mlc-sw\" style={{ background: s.color }} />\n                <span className=\"hb-mlc-name\">{s.name}</span>\n              </button>\n            </li>\n          );\n        })}\n      </ul>\n    );\n\n  const visible = items.filter((s) => !hidden.includes(s.si));\n  let tip: React.ReactNode = null;\n  if (idx !== null && n > 0) {\n    const x = xOf(idx);\n    const flip = x > width * 0.55;\n    tip = (\n      <div\n        className=\"hb-mlc-tip\"\n        aria-hidden=\"true\"\n        style={{\n          left: x,\n          top: padT + 4,\n          transform: flip ? \"translateX(calc(-100% - 12px))\" : \"translateX(12px)\",\n        }}\n      >\n        <div className=\"hb-mlc-tip-h\">{labels[idx] ?? `Point ${idx + 1}`}</div>\n        {visible.map((s) => (\n          <div key={s.si} className=\"hb-mlc-tip-r\">\n            <span className=\"hb-mlc-tip-d\" style={{ background: s.color }} />\n            {s.name}\n            <span className=\"hb-mlc-tip-v\">{Number.isFinite(s.data[idx]) ? tipFmt(s.data[idx]) : \"-\"}</span>\n          </div>\n        ))}\n        {visible.length === 0 ? <div className=\"hb-mlc-tip-r\">No series shown</div> : null}\n      </div>\n    );\n  }\n\n  return (\n    <div\n      ref={ref}\n      className={`hb-mlc ${className}`}\n      data-animate={animate ? \"true\" : \"false\"}\n      {...rest}\n    >\n      <style href=\"hb-multi-line-chart\" precedence=\"default\">{css}</style>\n      {legend === \"top\" ? legendEl : null}\n      <div className=\"hb-mlc-plot\" onPointerLeave={() => setIdx(null)}>\n        <svg\n          className=\"hb-mlc-svg\"\n          viewBox={`0 0 ${width} ${height}`}\n          width={width}\n          height={height}\n          role=\"img\"\n          aria-label={summary}\n          tabIndex={0}\n          onPointerMove={pointerIdx}\n          onPointerDown={pointerIdx}\n          onKeyDown={onKeyDown}\n          onFocus={(e) => {\n            if (idx === null && e.currentTarget.matches(\":focus-visible\")) setIdx(n - 1);\n          }}\n          onBlur={() => setIdx(null)}\n        >\n          {ticks.map((t, i) => (\n            <g key={i}>\n              <line\n                className={i === 0 ? \"hb-mlc-base\" : \"hb-mlc-grid\"}\n                x1={padL}\n                x2={width - padR}\n                y1={f(yOf(t))}\n                y2={f(yOf(t))}\n              />\n              <text className=\"hb-mlc-tick\" x={padL - 8} y={f(yOf(t))} textAnchor=\"end\" dominantBaseline=\"middle\">\n                {axisFmt(t)}\n              </text>\n            </g>\n          ))}\n          {Array.from({ length: n }, (_, i) =>\n            i % every === 0 ? (\n              <text\n                key={i}\n                className=\"hb-mlc-xl\"\n                data-on={idx === i ? \"true\" : \"false\"}\n                x={f(xOf(i))}\n                y={height - 6}\n                textAnchor=\"middle\"\n              >\n                {labels[i] ?? \"\"}\n              </text>\n            ) : null\n          )}\n          {idx !== null ? (\n            <line className=\"hb-mlc-cross\" x1={f(xOf(idx))} x2={f(xOf(idx))} y1={padT} y2={padT + innerH} />\n          ) : null}\n          {items.map((s, k) => {\n            const off = hidden.includes(s.si);\n            const dim = hoverSeries !== null && hoverSeries !== s.si;\n            return (\n              <path\n                key={s.si}\n                className=\"hb-mlc-line\"\n                data-off={off ? \"true\" : \"false\"}\n                data-dim={dim ? \"true\" : \"false\"}\n                d={paths[k]}\n                pathLength={1}\n                stroke={s.color}\n                style={{ [\"--hb-mlc-i\" as string]: s.si }}\n              />\n            );\n          })}\n          {idx !== null\n            ? visible.map((s) =>\n                Number.isFinite(s.data[idx]) ? (\n                  <circle\n                    key={s.si}\n                    className=\"hb-mlc-dot\"\n                    cx={f(xOf(idx))}\n                    cy={f(yOf(s.data[idx]))}\n                    r={4}\n                    fill=\"#0a0a0a\"\n                    stroke={s.color}\n                    strokeWidth={2}\n                  />\n                ) : null\n              )\n            : null}\n        </svg>\n        {tip}\n      </div>\n      {legend === \"bottom\" ? legendEl : null}\n    </div>\n  );\n}\n"
    }
  ]
}