{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "candlestick-chart",
  "type": "registry:component",
  "title": "Candlestick Chart",
  "description": "OHLC candlesticks with wicks, a price axis, a volume strip, a crosshair with a price tag and an OHLC tooltip. Ships a deterministic sampleCandles helper.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "charts",
    "candlestick",
    "ohlc",
    "trading",
    "stocks",
    "crypto",
    "volume"
  ],
  "docs": "https://hyperblur.dev/effects/candlestick-chart",
  "files": [
    {
      "path": "registry/hyperblur/candlestick-chart.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/candlestick-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface Candle {\n  label: string;\n  open: number;\n  high: number;\n  low: number;\n  close: number;\n  volume?: number;\n}\n\nexport interface CandlestickChartProps extends React.HTMLAttributes<HTMLDivElement> {\n  candles?: Candle[];\n  height?: number;\n  colors?: { up?: string; down?: string };\n  showVolume?: boolean;\n  priceFormatter?: (n: number) => string;\n  volumeFormatter?: (n: number) => string;\n  animate?: boolean;\n}\n\nconst MONTHS = [\"Jan\", \"Feb\", \"Mar\", \"Apr\", \"May\", \"Jun\", \"Jul\", \"Aug\", \"Sep\", \"Oct\", \"Nov\", \"Dec\"];\n\nconst css = `\n.hb-cdl { position: relative; display: block; width: 100%; min-width: 0; box-sizing: border-box; color: #ededed; font-family: inherit; touch-action: pan-y; }\n.hb-cdl *, .hb-cdl *::before, .hb-cdl *::after { box-sizing: border-box; }\n.hb-cdl-svg { display: block; width: 100%; overflow: visible; outline: none; cursor: crosshair; }\n.hb-cdl-svg:focus-visible { outline: 2px solid #7fb3ff; outline-offset: 4px; border-radius: 4px; }\n.hb-cdl-grid { stroke: #262626; stroke-width: 1; stroke-dasharray: 3 5; }\n.hb-cdl-base { stroke: #262626; stroke-width: 1; }\n.hb-cdl-tick {\n  fill: #8f8f8f;\n  font-size: 10.5px;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-cdl-xl { fill: #8f8f8f; font-size: 10.5px; font-family: inherit; transition: fill .15s ease; }\n.hb-cdl-xl[data-on=\"true\"] { fill: #ededed; }\n.hb-cdl-c {\n  animation: hb-cdl-in .55s cubic-bezier(.22, .8, .24, 1) backwards;\n  animation-delay: calc(var(--hb-cdl-i, 0) * 12ms);\n  transition: opacity .15s ease;\n}\n.hb-cdl-c[data-dim=\"true\"] { opacity: .45; }\n.hb-cdl-v {\n  animation: hb-cdl-vol .55s cubic-bezier(.22, .8, .24, 1) backwards;\n  animation-delay: calc(var(--hb-cdl-i, 0) * 12ms);\n  transition: opacity .15s ease;\n}\n.hb-cdl-v[data-dim=\"true\"] { opacity: .35; }\n.hb-cdl-cross { stroke: #5a5a5a; stroke-width: 1; stroke-dasharray: 3 4; pointer-events: none; }\n.hb-cdl-tagbg { fill: #ededed; }\n.hb-cdl-tagtx {\n  fill: #0a0a0a;\n  font-size: 10.5px;\n  font-weight: 600;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-cdl-tip {\n  position: absolute;\n  z-index: 2;\n  min-width: 132px;\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-cdl-tip-h { display: flex; justify-content: space-between; gap: 12px; margin-bottom: 2px; color: #8f8f8f; }\n.hb-cdl-tip-h b { font-weight: 600; font-variant-numeric: tabular-nums; }\n.hb-cdl-tip-r { display: flex; justify-content: space-between; gap: 14px; color: #8f8f8f; }\n.hb-cdl-tip-r b {\n  color: #ededed;\n  font-weight: 500;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-cdl[data-animate=\"false\"] .hb-cdl-c, .hb-cdl[data-animate=\"false\"] .hb-cdl-v { animation: none; }\n@keyframes hb-cdl-in {\n  from { opacity: 0; transform: scaleY(0); }\n}\n@keyframes hb-cdl-vol {\n  from { opacity: 0; transform: scaleY(0); }\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-cdl-c, .hb-cdl-v { animation: none !important; }\n  .hb-cdl-c, .hb-cdl-v, .hb-cdl-xl { transition: none !important; }\n}\n`;\n\nfunction f(n: number) {\n  return n.toFixed(2);\n}\n\nfunction hash(n: number) {\n  let x = (n + 0x9e3779b9) | 0;\n  x = Math.imul(x ^ (x >>> 16), 0x85ebca6b);\n  x = Math.imul(x ^ (x >>> 13), 0xc2b2ae35);\n  return (x ^ (x >>> 16)) >>> 0;\n}\n\nexport function sampleCandles(count = 48, base = 100, seed = 0): Candle[] {\n  const out: Candle[] = [];\n  const total = Math.max(1, Math.round(count));\n  let prev = Math.round(base * 100);\n  const start = Date.UTC(2026, 8, 1);\n  const salt = Math.round(seed) * 7919;\n  for (let i = 0; i < total; i++) {\n    const open = prev;\n    const drift = (hash(Math.floor(i / 7) + 313 + salt) % 5) - 2;\n    const step = Math.max(1, Math.round(open * 0.006));\n    const move = ((hash(i * 3 + 1 + salt) % 2001) - 1000) / 1000;\n    const delta = Math.round(move * step * 3) + drift * Math.round(step / 2);\n    const close = Math.max(100, open + delta);\n    const body = Math.max(open, close);\n    const floor = Math.min(open, close);\n    const high = body + 1 + (hash(i * 5 + 2 + salt) % (step * 2));\n    const low = Math.max(50, floor - 1 - (hash(i * 7 + 3 + salt) % (step * 2)));\n    const volume = 900 + (hash(i * 11 + 4 + salt) % 1800) + Math.abs(delta) * Math.round(400 / step);\n    const d = new Date(start + i * 86400000);\n    out.push({\n      label: `${MONTHS[d.getUTCMonth()]} ${d.getUTCDate()}`,\n      open: open / 100,\n      high: high / 100,\n      low: low / 100,\n      close: close / 100,\n      volume,\n    });\n    prev = close;\n  }\n  return out;\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 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\nconst priceNf = new Intl.NumberFormat(\"en-US\", { minimumFractionDigits: 2, maximumFractionDigits: 2 });\nconst volNf = new Intl.NumberFormat(\"en-US\", { notation: \"compact\", maximumFractionDigits: 1 });\n\nconst DEFAULT_CANDLES = sampleCandles(48, 100);\n\nexport function CandlestickChart({\n  candles = DEFAULT_CANDLES,\n  height = 240,\n  colors,\n  showVolume = true,\n  priceFormatter,\n  volumeFormatter,\n  animate = true,\n  className = \"\",\n  ...rest\n}: CandlestickChartProps) {\n  const [ref, width] = useWidth(360);\n  const [cross, setCross] = React.useState<{ i: number; y: number | null } | null>(null);\n\n  const up = colors?.up ?? \"#34d399\";\n  const down = colors?.down ?? \"#fb7185\";\n  const price = priceFormatter ?? ((n: number) => priceNf.format(n));\n  const vol = volumeFormatter ?? ((n: number) => volNf.format(n));\n\n  const n = candles.length;\n  const hi = n ? Math.max(...candles.map((c) => c.high)) : 1;\n  const lo = n ? Math.min(...candles.map((c) => c.low)) : 0;\n  const rawSpan = Math.max(hi - lo, Math.abs(hi) * 0.01, 0.01);\n  const step = niceStep(rawSpan / 4);\n  const yMin = Math.floor((lo - rawSpan * 0.04) / step) * step;\n  let yMax = Math.ceil((hi + rawSpan * 0.04) / step) * step;\n  if (yMax <= yMin) yMax = yMin + step;\n  const ticks: number[] = [];\n  for (let v = yMin; v <= yMax + step / 1000 && ticks.length < 10; v += step) ticks.push(v);\n\n  const padL = 4;\n  const padR = Math.max(40, ...ticks.map((t) => price(t).length * 6.4 + 12));\n  const padT = 8;\n  const padB = 20;\n  const volH = showVolume ? Math.max(26, Math.round(height * 0.2)) : 0;\n  const vGap = showVolume ? 8 : 0;\n  const panelH = Math.max(20, height - padT - padB - volH - vGap);\n  const innerW = Math.max(1, width - padL - padR);\n  const slot = innerW / Math.max(1, n);\n  const bodyW = Math.max(1.5, Math.min(slot * 0.62, 14));\n  const yOf = (v: number) => padT + panelH - ((v - yMin) / (yMax - yMin)) * panelH;\n  const xOf = (i: number) => padL + slot * (i + 0.5);\n  const volTop = padT + panelH + vGap;\n  const maxVol = Math.max(1, ...candles.map((c) => c.volume ?? 0));\n  const every = Math.max(1, Math.ceil(52 / slot));\n\n  const locate = (e: React.PointerEvent<SVGSVGElement>) => {\n    const rect = e.currentTarget.getBoundingClientRect();\n    const sx = width / Math.max(1, rect.width);\n    const vx = (e.clientX - rect.left) * sx;\n    const vy = (e.clientY - rect.top) * (height / Math.max(1, rect.height));\n    const i = Math.min(n - 1, Math.max(0, Math.floor((vx - padL) / slot)));\n    setCross({ i, y: vy >= padT && vy <= padT + panelH ? vy : null });\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent<SVGSVGElement>) => {\n    const cur = cross?.i ?? 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      setCross(null);\n      return;\n    }\n    if (next !== null && n > 0) {\n      e.preventDefault();\n      setCross({ i: next, y: yOf(candles[next].close) });\n    }\n  };\n\n  const first = candles[0];\n  const last = candles[n - 1];\n  const summary = n\n    ? `Candlestick chart with ${n} candles from ${first.label} to ${last.label}. Opened at ${price(first.open)}, closed at ${price(last.close)}, high ${price(hi)}, low ${price(lo)}.`\n    : \"Candlestick chart with no data\";\n\n  const act = cross && n ? candles[cross.i] : null;\n  let tip: React.ReactNode = null;\n  if (act && cross) {\n    const x = xOf(cross.i);\n    const flip = x > width * 0.55;\n    const change = act.open ? ((act.close - act.open) / act.open) * 100 : 0;\n    tip = (\n      <div\n        className=\"hb-cdl-tip\"\n        aria-hidden=\"true\"\n        style={{\n          left: x,\n          top: padT + 4,\n          transform: flip ? \"translateX(calc(-100% - 14px))\" : \"translateX(14px)\",\n        }}\n      >\n        <div className=\"hb-cdl-tip-h\">\n          <span>{act.label}</span>\n          <b style={{ color: act.close >= act.open ? up : down }}>\n            {`${change >= 0 ? \"+\" : \"\"}${change.toFixed(2)}%`}\n          </b>\n        </div>\n        <div className=\"hb-cdl-tip-r\">\n          Open <b>{price(act.open)}</b>\n        </div>\n        <div className=\"hb-cdl-tip-r\">\n          High <b>{price(act.high)}</b>\n        </div>\n        <div className=\"hb-cdl-tip-r\">\n          Low <b>{price(act.low)}</b>\n        </div>\n        <div className=\"hb-cdl-tip-r\">\n          Close <b>{price(act.close)}</b>\n        </div>\n        {typeof act.volume === \"number\" ? (\n          <div className=\"hb-cdl-tip-r\">\n            Volume <b>{vol(act.volume)}</b>\n          </div>\n        ) : null}\n      </div>\n    );\n  }\n\n  const tagY = cross && cross.y !== null ? cross.y : null;\n  const tagPrice = tagY !== null ? yMax - ((tagY - padT) / panelH) * (yMax - yMin) : 0;\n  const tagLabel = tagY !== null ? price(tagPrice) : \"\";\n  const tagW = padR - 2;\n\n  return (\n    <div\n      ref={ref}\n      className={`hb-cdl ${className}`}\n      data-animate={animate ? \"true\" : \"false\"}\n      {...rest}\n    >\n      <style href=\"hb-candlestick-chart\" precedence=\"default\">{css}</style>\n      <div style={{ position: \"relative\" }} onPointerLeave={() => setCross(null)}>\n        <svg\n          className=\"hb-cdl-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={locate}\n          onPointerDown={locate}\n          onKeyDown={onKeyDown}\n          onFocus={(e) => {\n            if (!cross && n > 0 && e.currentTarget.matches(\":focus-visible\")) {\n              setCross({ i: n - 1, y: yOf(candles[n - 1].close) });\n            }\n          }}\n          onBlur={() => setCross(null)}\n        >\n          {ticks.map((t, i) => (\n            <g key={i}>\n              <line\n                className={i === 0 ? \"hb-cdl-base\" : \"hb-cdl-grid\"}\n                x1={padL}\n                x2={width - padR}\n                y1={f(yOf(t))}\n                y2={f(yOf(t))}\n              />\n              <text className=\"hb-cdl-tick\" x={width - padR + 8} y={f(yOf(t))} dominantBaseline=\"middle\">\n                {price(t)}\n              </text>\n            </g>\n          ))}\n          {candles.map((c, i) => {\n            const isUp = c.close >= c.open;\n            const color = isUp ? up : down;\n            const x = xOf(i);\n            const yTop = yOf(Math.max(c.open, c.close));\n            const yBot = yOf(Math.min(c.open, c.close));\n            const bh = Math.max(1, yBot - yTop);\n            const mid = (yTop + yBot) / 2;\n            const dim = cross !== null && cross.i !== i;\n            const vh = showVolume ? ((c.volume ?? 0) / maxVol) * volH : 0;\n            return (\n              <g key={i}>\n                <g\n                  className=\"hb-cdl-c\"\n                  data-dim={dim ? \"true\" : \"false\"}\n                  style={{\n                    transformOrigin: `${f(x)}px ${f(mid)}px`,\n                    [\"--hb-cdl-i\" as string]: i,\n                  }}\n                >\n                  <line x1={f(x)} x2={f(x)} y1={f(yOf(c.high))} y2={f(yOf(c.low))} stroke={color} strokeWidth={1.25} />\n                  <rect\n                    x={f(x - bodyW / 2)}\n                    y={f(yTop)}\n                    width={f(bodyW)}\n                    height={f(bh)}\n                    rx={Math.min(1.5, bodyW / 4).toFixed(2)}\n                    fill={color}\n                  />\n                </g>\n                {showVolume && vh > 0 ? (\n                  <rect\n                    className=\"hb-cdl-v\"\n                    data-dim={dim ? \"true\" : \"false\"}\n                    x={f(x - bodyW / 2)}\n                    y={f(volTop + volH - vh)}\n                    width={f(bodyW)}\n                    height={f(vh)}\n                    fill={color}\n                    fillOpacity={0.45}\n                    style={{\n                      transformOrigin: `${f(x)}px ${f(volTop + volH)}px`,\n                      [\"--hb-cdl-i\" as string]: i,\n                    }}\n                  />\n                ) : null}\n              </g>\n            );\n          })}\n          {Array.from({ length: n }, (_, i) =>\n            i % every === 0 ? (\n              <text\n                key={i}\n                className=\"hb-cdl-xl\"\n                data-on={cross && cross.i === i ? \"true\" : \"false\"}\n                x={f(xOf(i))}\n                y={height - 5}\n                textAnchor=\"middle\"\n              >\n                {candles[i].label}\n              </text>\n            ) : null\n          )}\n          {cross && n ? (\n            <g>\n              <line\n                className=\"hb-cdl-cross\"\n                x1={f(xOf(cross.i))}\n                x2={f(xOf(cross.i))}\n                y1={padT}\n                y2={volTop + volH}\n              />\n              {tagY !== null ? (\n                <>\n                  <line className=\"hb-cdl-cross\" x1={padL} x2={width - padR} y1={f(tagY)} y2={f(tagY)} />\n                  <rect\n                    className=\"hb-cdl-tagbg\"\n                    x={width - padR + 2}\n                    y={f(tagY - 9)}\n                    width={f(tagW)}\n                    height={18}\n                    rx={4}\n                  />\n                  <text\n                    className=\"hb-cdl-tagtx\"\n                    x={f(width - padR + 2 + tagW / 2)}\n                    y={f(tagY)}\n                    textAnchor=\"middle\"\n                    dominantBaseline=\"middle\"\n                  >\n                    {tagLabel}\n                  </text>\n                </>\n              ) : null}\n            </g>\n          ) : null}\n        </svg>\n        {tip}\n      </div>\n    </div>\n  );\n}\n"
    }
  ]
}