{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "area-chart",
  "type": "registry:component",
  "title": "Area Chart",
  "description": "Smooth monotone line over a gradient area with a hover crosshair, dot and tooltip, and a draw-in animation.",
  "author": "Hyperblur <hello@hyperblur.dev>",
  "dependencies": [],
  "registryDependencies": [],
  "categories": [
    "charts",
    "chart",
    "area",
    "line",
    "svg",
    "timeseries",
    "analytics"
  ],
  "docs": "https://hyperblur.dev/effects/area-chart",
  "files": [
    {
      "path": "registry/hyperblur/area-chart.tsx",
      "type": "registry:component",
      "target": "components/hyperblur/area-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nexport interface AreaChartDatum {\n  label: string;\n  value: number;\n}\n\nexport interface AreaChartProps extends React.HTMLAttributes<HTMLDivElement> {\n  data: AreaChartDatum[];\n  color?: string;\n  height?: number;\n  smooth?: boolean;\n  showDots?: boolean;\n  valueFormatter?: (n: number) => string;\n  locale?: string;\n  animate?: boolean;\n}\n\nconst css = `\n.hb-area { position: relative; display: block; width: 100%; min-width: 0; color: #ededed; touch-action: pan-y; }\n.hb-area-svg { display: block; width: 100%; overflow: visible; }\n.hb-area-grid { stroke: #262626; stroke-width: 1; stroke-dasharray: 3 5; }\n.hb-area-base { stroke: #262626; stroke-width: 1; }\n.hb-area-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-area-xl { fill: #8f8f8f; font-size: 11px; font-family: inherit; transition: fill .2s ease; }\n.hb-area-xl[data-on=\"true\"] { fill: #ededed; }\n.hb-area-line {\n  fill: none;\n  stroke-linecap: round;\n  stroke-linejoin: round;\n  stroke-dasharray: 1;\n  stroke-dashoffset: 0;\n  transition: stroke-dashoffset 1.3s cubic-bezier(.22, .8, .24, 1), opacity .15s ease;\n}\n.hb-area-fill { transition: opacity .9s ease .4s; }\n.hb-area-dots { transition: opacity .4s ease 1s; }\n.hb-area[data-ready=\"false\"] .hb-area-line { stroke-dashoffset: 1; opacity: 0; }\n.hb-area[data-ready=\"false\"] .hb-area-fill,\n.hb-area[data-ready=\"false\"] .hb-area-dots { opacity: 0; }\n.hb-area-cross { stroke: #3a3a3a; stroke-width: 1; stroke-dasharray: 3 4; pointer-events: none; }\n.hb-area-tip {\n  position: absolute;\n  z-index: 2;\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 .1s ease, top .1s ease;\n}\n.hb-area-tip-l { color: #8f8f8f; }\n.hb-area-tip-v {\n  color: #ededed;\n  font-family: var(--font-mono, ui-monospace, SFMono-Regular, Menlo, monospace);\n  font-variant-numeric: tabular-nums;\n}\n.hb-area-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-area-line, .hb-area-fill, .hb-area-dots, .hb-area-xl, .hb-area-tip { transition: none !important; }\n  .hb-area[data-ready=\"false\"] .hb-area-line { stroke-dashoffset: 0 !important; opacity: 1 !important; }\n  .hb-area[data-ready=\"false\"] .hb-area-fill,\n  .hb-area[data-ready=\"false\"] .hb-area-dots { opacity: 1 !important; }\n}\n`;\n\nfunction r(n: number) {\n  return Math.round(n * 100) / 100;\n}\n\nfunction monotone(pts: [number, number][]) {\n  const n = pts.length;\n  if (n < 2) return \"\";\n  const m: number[] = [];\n  for (let i = 0; i < n - 1; i++) {\n    const dx = pts[i + 1][0] - pts[i][0];\n    m.push(dx === 0 ? 0 : (pts[i + 1][1] - pts[i][1]) / dx);\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++) {\n    t[i] = m[i - 1] * m[i] <= 0 ? 0 : (2 * m[i - 1] * m[i]) / (m[i - 1] + m[i]);\n  }\n  let d = `M${r(pts[0][0])},${r(pts[0][1])}`;\n  for (let i = 0; i < n - 1; i++) {\n    const dx = (pts[i + 1][0] - pts[i][0]) / 3;\n    d += `C${r(pts[i][0] + dx)},${r(pts[i][1] + t[i] * dx)} ${r(pts[i + 1][0] - dx)},${r(pts[i + 1][1] - t[i + 1] * dx)} ${r(pts[i + 1][0])},${r(pts[i + 1][1])}`;\n  }\n  return d;\n}\n\nfunction straight(pts: [number, number][]) {\n  return pts.map((p, i) => `${i === 0 ? \"M\" : \"L\"}${r(p[0])},${r(p[1])}`).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\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 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\nexport function AreaChart({\n  data,\n  color = \"#2f7bff\",\n  height = 220,\n  smooth = true,\n  showDots = false,\n  valueFormatter,\n  locale = \"en-US\",\n  animate = true,\n  className = \"\",\n  ...rest\n}: AreaChartProps) {\n  const [ref, width] = useWidth(360);\n  const ready = useReady(animate);\n  const [active, setActive] = React.useState<number | null>(null);\n  const uid = React.useId().replace(/[^a-zA-Z0-9]/g, \"\");\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 vals = data.map((d) => (Number.isFinite(d.value) ? d.value : 0));\n  const hi = vals.length ? Math.max(...vals) : 0;\n  const lo = vals.length ? Math.min(...vals, 0) : 0;\n  const step = niceStep((hi - lo) / 4);\n  const top = lo + step * 4;\n  const ticks = [0, 1, 2, 3, 4].map((i) => lo + step * i);\n\n  const padL = Math.max(28, ...ticks.map((t) => axisFmt(t).length * 6.4 + 10));\n  const padR = 12;\n  const padT = 12;\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 - lo) / (top - lo || 1)) * innerH;\n  const xOf = (i: number) => (data.length < 2 ? padL + innerW / 2 : padL + (i / (data.length - 1)) * innerW);\n  const every = Math.max(1, Math.ceil(46 / (innerW / Math.max(1, data.length - 1))));\n\n  const pts: [number, number][] = vals.map((v, i) => [xOf(i), yOf(v)]);\n  const line = smooth ? monotone(pts) : straight(pts);\n  const base = padT + innerH;\n  const area =\n    pts.length > 1 ? `${line}L${r(pts[pts.length - 1][0])},${base}L${r(pts[0][0])},${base}Z` : \"\";\n\n  const label = data.length\n    ? `Area chart with ${data.length} points, from ${data[0].label} at ${tipFmt(vals[0])} to ${data[data.length - 1].label} at ${tipFmt(vals[vals.length - 1])}. Low ${tipFmt(Math.min(...vals))}, high ${tipFmt(hi)}.`\n    : \"Area chart with no data\";\n\n  const onMove = (e: React.PointerEvent<SVGRectElement>) => {\n    if (!data.length) return;\n    const box = e.currentTarget.getBoundingClientRect();\n    const x = ((e.clientX - box.left) / (box.width || 1)) * innerW + padL;\n    const idx = data.length < 2 ? 0 : Math.round(((x - padL) / innerW) * (data.length - 1));\n    setActive(Math.min(data.length - 1, Math.max(0, idx)));\n  };\n\n  const act = active === null ? null : data[active];\n  const tipX = active === null ? 0 : Math.min(Math.max(xOf(active), 44), width - 44);\n  const tipY = active === null ? 0 : Math.max(yOf(vals[active]) - 12, 44);\n\n  return (\n    <div\n      ref={ref}\n      className={`hb-area ${className}`}\n      data-ready={ready ? \"true\" : \"false\"}\n      style={{ height }}\n      {...rest}\n    >\n      <style href=\"hb-area-chart\" precedence=\"default\">{css}</style>\n      <svg\n        className=\"hb-area-svg\"\n        viewBox={`0 0 ${width} ${height}`}\n        width={width}\n        height={height}\n        role=\"img\"\n        aria-label={label}\n      >\n        <defs>\n          <linearGradient id={`hb-area-${uid}`} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n            <stop offset=\"0%\" stopColor={color} stopOpacity=\"0.34\" />\n            <stop offset=\"100%\" stopColor={color} stopOpacity=\"0\" />\n          </linearGradient>\n        </defs>\n        {ticks.map((t, i) => (\n          <g key={i}>\n            <line\n              className={i === 0 ? \"hb-area-base\" : \"hb-area-grid\"}\n              x1={padL}\n              x2={width - padR}\n              y1={yOf(t)}\n              y2={yOf(t)}\n            />\n            <text className=\"hb-area-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          i % every === 0 ? (\n            <text\n              key={i}\n              className=\"hb-area-xl\"\n              data-on={active === i ? \"true\" : \"false\"}\n              x={xOf(i)}\n              y={height - 6}\n              textAnchor={data.length > 1 && i === 0 ? \"start\" : data.length > 1 && i === data.length - 1 ? \"end\" : \"middle\"}\n            >\n              {d.label}\n            </text>\n          ) : null\n        )}\n        {area ? <path className=\"hb-area-fill\" d={area} fill={`url(#hb-area-${uid})`} /> : null}\n        {pts.length > 1 ? (\n          <path className=\"hb-area-line\" d={line} pathLength={1} stroke={color} strokeWidth={2} />\n        ) : null}\n        {showDots ? (\n          <g className=\"hb-area-dots\">\n            {pts.map((p, i) => (\n              <circle key={i} cx={p[0]} cy={p[1]} r={3} fill=\"#111111\" stroke={color} strokeWidth={1.75} />\n            ))}\n          </g>\n        ) : null}\n        {active !== null ? (\n          <g>\n            <line className=\"hb-area-cross\" x1={xOf(active)} x2={xOf(active)} y1={padT} y2={base} />\n            <circle cx={xOf(active)} cy={yOf(vals[active])} r={8} fill={color} opacity=\"0.2\" />\n            <circle cx={xOf(active)} cy={yOf(vals[active])} r={4} fill={color} stroke=\"#111111\" strokeWidth={1.5} />\n          </g>\n        ) : null}\n        <rect\n          x={padL}\n          y={padT}\n          width={innerW}\n          height={innerH + padB - 4}\n          fill=\"transparent\"\n          onPointerMove={onMove}\n          onPointerDown={onMove}\n          onPointerLeave={() => setActive(null)}\n        />\n      </svg>\n      {act ? (\n        <div className=\"hb-area-tip\" aria-hidden=\"true\" style={{ left: tipX, top: tipY }}>\n          <div className=\"hb-area-tip-l\">{act.label}</div>\n          <div className=\"hb-area-tip-v\">{tipFmt(vals[active as number])}</div>\n        </div>\n      ) : null}\n      <ul className=\"hb-area-sr\">\n        {data.map((d, i) => (\n          <li key={i}>\n            {d.label}: {tipFmt(vals[i])}\n          </li>\n        ))}\n      </ul>\n    </div>\n  );\n}\n"
    }
  ]
}