{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bar-chart",
  "type": "registry:component",
  "title": "Bar Chart",
  "description": "Vertical bars with rounded tops, grid lines, optional stacked series, grow-in motion and a hover tooltip.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "charts",
    "chart",
    "bar",
    "svg",
    "stacked",
    "analytics",
    "tooltip"
  ],
  "docs": "https://hyperblur.dev/effects/bar-chart",
  "files": [
    {
      "path": "registry/hyperblur/bar-chart.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/bar-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface BarChartDatum {\n  label: string;\n  value: number;\n  value2?: number;\n}\n\nexport interface BarChartProps extends React.HTMLAttributes<HTMLDivElement> {\n  data: BarChartDatum[];\n  color?: string;\n  color2?: string;\n  height?: number;\n  showGrid?: boolean;\n  valueFormatter?: (n: number) => string;\n  locale?: string;\n  animate?: boolean;\n  seriesLabels?: [string, string];\n}\n\nconst css = `\n.hb-bar { position: relative; display: block; width: 100%; min-width: 0; color: #ededed; }\n.hb-bar-svg { display: block; width: 100%; overflow: visible; }\n.hb-bar-grid { stroke: #262626; stroke-width: 1; stroke-dasharray: 3 5; }\n.hb-bar-base { stroke: #262626; stroke-width: 1; }\n.hb-bar-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-bar-xl { fill: #8f8f8f; font-size: 11px; font-family: inherit; transition: fill .2s ease; }\n.hb-bar-xl[data-on=\"true\"] { fill: #ededed; }\n.hb-bar-b {\n  transform: scaleY(1);\n  transition: transform .8s cubic-bezier(.22, .8, .24, 1) var(--hb-bar-d, 0ms), opacity .2s ease;\n}\n.hb-bar[data-ready=\"false\"] .hb-bar-b { transform: scaleY(0); }\n.hb-bar-b[data-dim=\"true\"] { opacity: .4; }\n.hb-bar-tip {\n  position: absolute;\n  z-index: 2;\n  min-width: 64px;\n  padding: 6px 9px;\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.35;\n  white-space: nowrap;\n  pointer-events: none;\n  transform: translate(-50%, -100%);\n  transition: left .12s ease, top .12s ease, opacity .12s ease;\n}\n.hb-bar-tip-l { color: #8f8f8f; }\n.hb-bar-tip-r { display: flex; align-items: center; gap: 6px; }\n.hb-bar-tip-d { width: 6px; height: 6px; border-radius: 2px; flex: none; }\n.hb-bar-tip-v {\n  margin-left: auto;\n  padding-left: 8px;\n  color: #ededed;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-bar-sr {\n  position: absolute;\n  width: 1px;\n  height: 1px;\n  margin: -1px;\n  padding: 0;\n  overflow: hidden;\n  clip: rect(0 0 0 0);\n  white-space: nowrap;\n  border: 0;\n}\n@media (prefers-reduced-motion: reduce) {\n  .hb-bar-b, .hb-bar-xl, .hb-bar-tip { transition: none !important; }\n  .hb-bar[data-ready=\"false\"] .hb-bar-b { transform: scaleY(1) !important; }\n}\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\nfunction useReady(animate: boolean) {\n  const [ready, setReady] = React.useState(!animate);\n  React.useEffect(() => {\n    if (!animate) return;\n    let a = 0;\n    let b = 0;\n    a = requestAnimationFrame(() => {\n      b = requestAnimationFrame(() => setReady(true));\n    });\n    return () => {\n      cancelAnimationFrame(a);\n      cancelAnimationFrame(b);\n    };\n  }, [animate]);\n  return animate ? ready : true;\n}\n\nfunction pos(n: number) {\n  return Number.isFinite(n) ? Math.max(0, n) : 0;\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 f = raw / exp;\n  const nice = f <= 1 ? 1 : f <= 2 ? 2 : f <= 2.5 ? 2.5 : f <= 5 ? 5 : 10;\n  return nice * exp;\n}\n\nfunction topRound(x: number, y: number, w: number, h: number, rad: number) {\n  const k = Math.max(0, Math.min(rad, w / 2, h));\n  const f = (n: number) => Math.round(n * 100) / 100;\n  if (k === 0) return `M${f(x)},${f(y + h)}V${f(y)}H${f(x + w)}V${f(y + h)}Z`;\n  return `M${f(x)},${f(y + h)}V${f(y + k)}Q${f(x)},${f(y)} ${f(x + k)},${f(y)}H${f(x + w - k)}Q${f(x + w)},${f(y)} ${f(x + w)},${f(y + k)}V${f(y + h)}Z`;\n}\n\nexport function BarChart({\n  data,\n  color = \"#2f7bff\",\n  color2 = \"#6b7a99\",\n  height = 200,\n  showGrid = true,\n  valueFormatter,\n  locale = \"en-US\",\n  animate = true,\n  seriesLabels = [\"Value\", \"Other\"],\n  className = \"\",\n  ...rest\n}: BarChartProps) {\n  const [ref, width] = useWidth(320);\n  const ready = useReady(animate);\n  const [active, setActive] = 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(\n    () => new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }),\n    [locale]\n  );\n  const axisFmt = (n: number) => (valueFormatter ? valueFormatter(n) : compact.format(n));\n  const tipFmt = (n: number) => (valueFormatter ? valueFormatter(n) : full.format(n));\n\n  const stacked = data.some((d) => pos(d.value2 ?? 0) > 0);\n  const totals = data.map((d) => pos(d.value) + pos(d.value2 ?? 0));\n  const peak = totals.length ? Math.max(...totals) : 0;\n  const step = niceStep(peak / 4);\n  const top = Math.max(step * 4, step);\n  const ticks = [0, 1, 2, 3, 4].map((i) => (top / 4) * i);\n\n  const padL = Math.max(28, ...ticks.map((t) => axisFmt(t).length * 6.4 + 10));\n  const padR = 6;\n  const padT = 10;\n  const padB = 24;\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 / top) * innerH;\n  const n = Math.max(1, data.length);\n  const slot = innerW / n;\n  const barW = Math.max(4, Math.min(slot * 0.6, 44));\n  const every = Math.max(1, Math.ceil(34 / slot));\n\n  const peakIdx = totals.indexOf(peak);\n  const label = data.length\n    ? `Bar chart with ${data.length} bars${stacked ? \" in two series\" : \"\"}. Highest: ${data[peakIdx].label}, ${tipFmt(peak)}.`\n    : \"Bar chart with no data\";\n\n  const tipX = active === null ? 0 : Math.min(Math.max(padL + slot * (active + 0.5), 52), width - 52);\n  const tipY = active === null ? 0 : Math.max(yOf(totals[active]) - 8, stacked ? 56 : 44);\n  const act = active === null ? null : data[active];\n\n  return (\n    <div\n      ref={ref}\n      className={`hb-bar ${className}`}\n      data-ready={ready ? \"true\" : \"false\"}\n      style={{ height }}\n      onMouseLeave={() => setActive(null)}\n      {...rest}\n    >\n      <style href=\"hb-bar-chart\" precedence=\"default\">{css}</style>\n      <svg\n        className=\"hb-bar-svg\"\n        viewBox={`0 0 ${width} ${height}`}\n        width={width}\n        height={height}\n        role=\"img\"\n        aria-label={label}\n      >\n        {ticks.map((t, i) => (\n          <g key={i}>\n            {showGrid || i === 0 ? (\n              <line\n                className={i === 0 ? \"hb-bar-base\" : \"hb-bar-grid\"}\n                x1={padL}\n                x2={width - padR}\n                y1={yOf(t)}\n                y2={yOf(t)}\n              />\n            ) : null}\n            <text className=\"hb-bar-tick\" x={padL - 8} y={yOf(t)} textAnchor=\"end\" dominantBaseline=\"middle\">\n              {axisFmt(t)}\n            </text>\n          </g>\n        ))}\n        {data.map((d, i) => {\n          const cx = padL + slot * (i + 0.5);\n          const x = cx - barW / 2;\n          const v1 = pos(d.value);\n          const v2 = pos(d.value2 ?? 0);\n          const h1 = (v1 / top) * innerH;\n          const h2 = (v2 / top) * innerH;\n          const base = padT + innerH;\n          return (\n            <g key={i}>\n              <g\n                className=\"hb-bar-b\"\n                data-on={active === i ? \"true\" : \"false\"}\n                data-dim={active !== null && active !== i ? \"true\" : \"false\"}\n                style={{\n                  transformOrigin: `${cx}px ${base}px`,\n                  [\"--hb-bar-d\" as string]: `${i * 45}ms`,\n                }}\n              >\n                {h1 > 0 ? <path d={topRound(x, base - h1, barW, h1, v2 > 0 ? 0 : 5)} fill={color} /> : null}\n                {h2 > 0 ? <path d={topRound(x, base - h1 - h2, barW, h2, 5)} fill={color2} /> : null}\n              </g>\n              {i % every === 0 ? (\n                <text\n                  className=\"hb-bar-xl\"\n                  data-on={active === i ? \"true\" : \"false\"}\n                  x={cx}\n                  y={height - 6}\n                  textAnchor=\"middle\"\n                >\n                  {d.label}\n                </text>\n              ) : null}\n              <rect\n                x={padL + slot * i}\n                y={padT}\n                width={slot}\n                height={innerH + padB - 4}\n                fill=\"transparent\"\n                onMouseEnter={() => setActive(i)}\n                onMouseMove={() => setActive(i)}\n              />\n            </g>\n          );\n        })}\n      </svg>\n      {act ? (\n        <div className=\"hb-bar-tip\" aria-hidden=\"true\" style={{ left: tipX, top: tipY }}>\n          <div className=\"hb-bar-tip-l\">{act.label}</div>\n          <div className=\"hb-bar-tip-r\">\n            {stacked ? <span className=\"hb-bar-tip-d\" style={{ background: color }} /> : null}\n            {stacked ? seriesLabels[0] : null}\n            <span className=\"hb-bar-tip-v\">{tipFmt(act.value)}</span>\n          </div>\n          {stacked ? (\n            <div className=\"hb-bar-tip-r\">\n              <span className=\"hb-bar-tip-d\" style={{ background: color2 }} />\n              {seriesLabels[1]}\n              <span className=\"hb-bar-tip-v\">{tipFmt(act.value2 ?? 0)}</span>\n            </div>\n          ) : null}\n        </div>\n      ) : null}\n      <ul className=\"hb-bar-sr\">\n        {data.map((d, i) => (\n          <li key={i}>\n            {d.label}: {tipFmt(d.value)}\n            {typeof d.value2 === \"number\" ? `, ${tipFmt(d.value2)}` : \"\"}\n          </li>\n        ))}\n      </ul>\n    </div>\n  );\n}\n"
    }
  ]
}