{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "combo-chart",
  "title": "Combo Chart",
  "description": "A bar and line combo chart on two axes: bars for an amount like revenue, a smooth line for a rate like margin, with a dashed target, bars that grow in one after another, a line that draws over them, and a column tooltip with the change from the month before.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/combo-chart/combo-chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\nimport { clsx, type ClassValue } from \"clsx\";\nimport { twMerge } from \"tailwind-merge\";\n\nfunction cn(...inputs: ClassValue[]) {\n  return twMerge(clsx(inputs));\n}\n\nexport type ChartTheme = \"dark\" | \"light\";\n\nexport interface ComboPoint {\n  label: string;\n  /** Value drawn as a bar, on the left axis. */\n  bar: number;\n  /** Value drawn as a line, on the right axis. */\n  line: number;\n}\n\nexport interface ComboChartProps extends Omit<\n  React.ComponentPropsWithoutRef<\"div\">,\n  \"title\"\n> {\n  theme?: ChartTheme;\n  title?: string;\n  subtitle?: string;\n  data?: ComboPoint[];\n  barLabel?: string;\n  lineLabel?: string;\n  barColor?: string;\n  lineColor?: string;\n  /** Optional goal for the line series, drawn as a dashed rule on the right axis. */\n  lineTarget?: number;\n  /** Height of the plot area, in px. */\n  height?: number;\n  formatBar?: (value: number) => string;\n  formatLine?: (value: number) => string;\n}\n\nconst PALETTES = {\n  dark: {\n    card: \"#0E0E0E\",\n    border: \"rgba(255,255,255,0.08)\",\n    text: \"#F4F5F7\",\n    sub: \"rgba(255,255,255,0.55)\",\n    grid: \"rgba(255,255,255,0.07)\",\n    axis: \"rgba(255,255,255,0.45)\",\n    tip: \"rgba(20,20,20,0.96)\",\n    tipBorder: \"rgba(255,255,255,0.12)\",\n    band: \"rgba(255,255,255,0.04)\",\n    bar: \"#8FB8FF\",\n    line: \"#F2A841\",\n  },\n  light: {\n    card: \"#FFFFFF\",\n    border: \"rgba(0,0,0,0.07)\",\n    text: \"#111317\",\n    sub: \"rgba(17,19,23,0.55)\",\n    grid: \"rgba(17,19,23,0.08)\",\n    axis: \"rgba(17,19,23,0.5)\",\n    tip: \"rgba(255,255,255,0.98)\",\n    tipBorder: \"rgba(0,0,0,0.08)\",\n    band: \"rgba(17,19,23,0.04)\",\n    bar: \"#3B6FD8\",\n    line: \"#D9822B\",\n  },\n};\n\nconst DEFAULT_DATA: ComboPoint[] = [\n  { label: \"Jan\", bar: 182000, line: 61.2 },\n  { label: \"Feb\", bar: 196000, line: 62.8 },\n  { label: \"Mar\", bar: 214000, line: 60.4 },\n  { label: \"Apr\", bar: 228000, line: 63.9 },\n  { label: \"May\", bar: 221000, line: 65.1 },\n  { label: \"Jun\", bar: 247000, line: 66.3 },\n  { label: \"Jul\", bar: 262000, line: 64.7 },\n  { label: \"Aug\", bar: 259000, line: 67.2 },\n  { label: \"Sep\", bar: 284000, line: 68.5 },\n  { label: \"Oct\", bar: 301000, line: 69.1 },\n  { label: \"Nov\", bar: 326000, line: 70.4 },\n  { label: \"Dec\", bar: 352000, line: 71.8 },\n];\n\nconst money = new Intl.NumberFormat(\"en-US\", {\n  style: \"currency\",\n  currency: \"USD\",\n  notation: \"compact\",\n  maximumFractionDigits: 1,\n});\n\nfunction useReducedMotion() {\n  return React.useSyncExternalStore(\n    (callback) => {\n      const query = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n      query.addEventListener(\"change\", callback);\n      return () => query.removeEventListener(\"change\", callback);\n    },\n    () => window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches,\n    () => false,\n  );\n}\n\nfunction useWidth(ref: React.RefObject<HTMLElement | null>) {\n  const [width, setWidth] = React.useState(0);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) =>\n      setWidth(entry.contentRect.width),\n    );\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, [ref]);\n  return width;\n}\n\n/** 0 → 1 (ease-out) once the element scrolls into view; 1 straight away with reduced motion. */\nfunction useReveal(\n  ref: React.RefObject<HTMLElement | null>,\n  reduce: boolean,\n  duration = 1400,\n) {\n  const [t, setT] = React.useState(0);\n  React.useEffect(() => {\n    const el = ref.current;\n    if (!el || reduce) return;\n    let raf = 0;\n    const io = new IntersectionObserver(\n      ([entry]) => {\n        if (!entry.isIntersecting) return;\n        io.disconnect();\n        const start = performance.now();\n        const tick = (now: number) => {\n          const k = Math.min(1, (now - start) / duration);\n          setT(k);\n          if (k < 1) raf = requestAnimationFrame(tick);\n        };\n        raf = requestAnimationFrame(tick);\n      },\n      { threshold: 0.2 },\n    );\n    io.observe(el);\n    return () => {\n      io.disconnect();\n      cancelAnimationFrame(raf);\n    };\n  }, [ref, reduce, duration]);\n  return reduce ? 1 : t;\n}\n\nfunction niceScale(min: number, max: number, count = 4) {\n  const span = Math.max(1e-9, max - min);\n  const raw = span / count;\n  const mag = Math.pow(10, Math.floor(Math.log10(raw)));\n  const step =\n    [1, 2, 2.5, 5, 10].map((m) => m * mag).find((s) => s >= raw) ?? 10 * mag;\n  const lo = Math.floor(min / step) * step;\n  return { min: lo, max: lo + step * count, step };\n}\n\nconst easeOut = (t: number) => 1 - Math.pow(1 - Math.min(1, Math.max(0, t)), 3);\n\nfunction smooth(pts: [number, number][]) {\n  if (pts.length < 2) return pts.length ? `M${pts[0][0]},${pts[0][1]}` : \"\";\n  let d = `M${pts[0][0]},${pts[0][1]}`;\n  for (let i = 0; i < pts.length - 1; i++) {\n    const p0 = pts[i - 1] ?? pts[i];\n    const p1 = pts[i];\n    const p2 = pts[i + 1];\n    const p3 = pts[i + 2] ?? p2;\n    // Catmull-Rom to Bezier, tension 0.5.\n    d += `C${p1[0] + (p2[0] - p0[0]) / 6},${p1[1] + (p2[1] - p0[1]) / 6} ${p2[0] - (p3[0] - p1[0]) / 6},${p2[1] - (p3[1] - p1[1]) / 6} ${p2[0]},${p2[1]}`;\n  }\n  return d;\n}\n\nconst M = { top: 16, right: 46, bottom: 30, left: 50 };\n\nexport function ComboChart({\n  theme = \"dark\",\n  title = \"Revenue and gross margin\",\n  subtitle = \"Monthly, FY 2025\",\n  data = DEFAULT_DATA,\n  barLabel = \"Revenue\",\n  lineLabel = \"Gross margin\",\n  barColor,\n  lineColor,\n  lineTarget = 68,\n  height = 300,\n  formatBar = (v) => money.format(v),\n  formatLine = (v) => `${v.toFixed(1)}%`,\n  className,\n  style,\n  ...props\n}: ComboChartProps) {\n  const pal = PALETTES[theme];\n  const bc = barColor ?? pal.bar;\n  const lc = lineColor ?? pal.line;\n  const reduce = useReducedMotion();\n  const plotRef = React.useRef<HTMLDivElement>(null);\n  const width = useWidth(plotRef);\n  const reveal = useReveal(plotRef, reduce);\n  const [active, setActive] = React.useState<number | null>(null);\n  const id = React.useId();\n\n  const n = data.length;\n  const plotW = Math.max(0, width - M.left - M.right);\n  const slot = n ? plotW / n : 0;\n  const barW = Math.max(4, Math.min(38, slot * 0.56));\n  const xAt = (i: number) => M.left + slot * (i + 0.5);\n  const left = niceScale(0, Math.max(1, ...data.map((d) => d.bar)) * 1.05);\n  const lineVals = data.map((d) => d.line).concat(lineTarget ?? []);\n  const right = niceScale(Math.min(...lineVals) - 2, Math.max(...lineVals) + 2);\n  const yBar = (v: number) =>\n    M.top + height - ((v - left.min) / (left.max - left.min)) * height;\n  const yLine = (v: number) =>\n    M.top + height - ((v - right.min) / (right.max - right.min)) * height;\n  const ticks = Array.from({ length: 5 }, (_, i) => i);\n  const linePts = data.map(\n    (d, i) => [xAt(i), yLine(d.line)] as [number, number],\n  );\n  // The line starts drawing once the bars are about half grown.\n  const lineT = easeOut((reveal - 0.35) / 0.65);\n  const every = Math.max(1, Math.ceil(34 / Math.max(1, slot)));\n\n  const pick = (clientX: number) => {\n    const r = plotRef.current?.getBoundingClientRect();\n    if (!r || !n) return;\n    const i = Math.floor((clientX - r.left - M.left) / Math.max(1, slot));\n    setActive(i >= 0 && i < n ? i : null);\n  };\n  const onKey = (event: React.KeyboardEvent) => {\n    if (!n) return;\n    if (event.key === \"ArrowRight\" || event.key === \"ArrowLeft\") {\n      event.preventDefault();\n      const dir = event.key === \"ArrowRight\" ? 1 : -1;\n      setActive((a) =>\n        Math.max(\n          0,\n          Math.min(n - 1, a === null ? (dir > 0 ? 0 : n - 1) : a + dir),\n        ),\n      );\n    } else if (event.key === \"Home\") setActive(0);\n    else if (event.key === \"End\") setActive(n - 1);\n    else if (event.key === \"Escape\") setActive(null);\n  };\n\n  const total = data.reduce((s, d) => s + d.bar, 0);\n  const avgLine = n ? data.reduce((s, d) => s + d.line, 0) / n : 0;\n  const tipX = active === null ? 0 : xAt(active);\n  const tipFlip = tipX > width - 190;\n  const summary = `${title}. ${barLabel} from ${formatBar(data[0]?.bar ?? 0)} in ${data[0]?.label} to ${formatBar(data[n - 1]?.bar ?? 0)} in ${data[n - 1]?.label}; ${lineLabel} from ${formatLine(data[0]?.line ?? 0)} to ${formatLine(data[n - 1]?.line ?? 0)}.`;\n\n  return (\n    <div\n      className={cn(\n        \"w-full overflow-hidden rounded-[20px] p-5 sm:p-7\",\n        className,\n      )}\n      style={{\n        background: pal.card,\n        border: `1px solid ${pal.border}`,\n        color: pal.text,\n        fontFamily: \"Inter, -apple-system, BlinkMacSystemFont, sans-serif\",\n        ...style,\n      }}\n      {...props}\n    >\n      <div className=\"flex flex-wrap items-start justify-between gap-4\">\n        <div className=\"min-w-0\">\n          <p className=\"text-[15px] font-semibold tracking-[-0.01em]\">\n            {title}\n          </p>\n          {subtitle && (\n            <p className=\"mt-1 text-[12.5px]\" style={{ color: pal.sub }}>\n              {subtitle}\n            </p>\n          )}\n        </div>\n        <div className=\"flex gap-6 text-right\">\n          <div>\n            <p className=\"text-[11.5px]\" style={{ color: pal.sub }}>\n              {barLabel}\n            </p>\n            <p className=\"mt-0.5 text-[20px] leading-tight font-semibold tracking-[-0.02em] tabular-nums\">\n              {formatBar(total)}\n            </p>\n          </div>\n          <div>\n            <p className=\"text-[11.5px]\" style={{ color: pal.sub }}>\n              Avg {lineLabel.toLowerCase()}\n            </p>\n            <p className=\"mt-0.5 text-[20px] leading-tight font-semibold tracking-[-0.02em] tabular-nums\">\n              {formatLine(avgLine)}\n            </p>\n          </div>\n        </div>\n      </div>\n\n      <div className=\"mt-4 flex flex-wrap gap-x-5 gap-y-2 text-[12px] font-medium\">\n        <span className=\"flex items-center gap-1.5\">\n          <span\n            className=\"h-3 w-2.5 rounded-[3px]\"\n            style={{ background: bc }}\n          />\n          {barLabel}\n          <span style={{ color: pal.sub }}>(left)</span>\n        </span>\n        <span className=\"flex items-center gap-1.5\">\n          <span\n            className=\"relative h-0.5 w-4 rounded-full\"\n            style={{ background: lc }}\n          >\n            <span\n              className=\"absolute top-1/2 left-1/2 size-1.5 -translate-1/2 rounded-full\"\n              style={{ background: lc }}\n            />\n          </span>\n          {lineLabel}\n          <span style={{ color: pal.sub }}>(right)</span>\n        </span>\n        {lineTarget !== undefined && (\n          <span\n            className=\"flex items-center gap-1.5\"\n            style={{ color: pal.sub }}\n          >\n            <span\n              className=\"w-4 border-t border-dashed\"\n              style={{ borderColor: lc }}\n            />\n            Target {formatLine(lineTarget)}\n          </span>\n        )}\n      </div>\n\n      <div\n        ref={plotRef}\n        role=\"img\"\n        aria-label={summary}\n        tabIndex={0}\n        onKeyDown={onKey}\n        onPointerMove={(e) => pick(e.clientX)}\n        onPointerDown={(e) => pick(e.clientX)}\n        onPointerLeave={(e) => e.pointerType === \"mouse\" && setActive(null)}\n        onBlur={() => setActive(null)}\n        className=\"relative mt-4 w-full rounded-lg outline-none focus-visible:ring-2\"\n        style={{ height: height + M.top + M.bottom }}\n      >\n        {width > 0 && (\n          <svg\n            width={width}\n            height={height + M.top + M.bottom}\n            className=\"block overflow-visible\"\n            aria-hidden=\"true\"\n          >\n            <defs>\n              <linearGradient id={`${id}-bar`} x1=\"0\" y1=\"0\" x2=\"0\" y2=\"1\">\n                <stop offset=\"0%\" stopColor={bc} />\n                <stop offset=\"100%\" stopColor={bc} stopOpacity={0.55} />\n              </linearGradient>\n            </defs>\n            {ticks.map((k) => {\n              const y = M.top + height - (k / 4) * height;\n              return (\n                <g key={k}>\n                  <line\n                    x1={M.left}\n                    x2={M.left + plotW}\n                    y1={y}\n                    y2={y}\n                    stroke={pal.grid}\n                    strokeDasharray={k ? \"3 4\" : undefined}\n                  />\n                  <text\n                    x={M.left - 10}\n                    y={y + 4}\n                    textAnchor=\"end\"\n                    fontSize={11}\n                    fill={pal.axis}\n                    className=\"tabular-nums\"\n                  >\n                    {formatBar(left.min + left.step * k)}\n                  </text>\n                  <text\n                    x={M.left + plotW + 10}\n                    y={y + 4}\n                    fontSize={11}\n                    fill={pal.axis}\n                    className=\"tabular-nums\"\n                  >\n                    {formatLine(right.min + right.step * k).replace(\".0%\", \"%\")}\n                  </text>\n                </g>\n              );\n            })}\n            {active !== null && (\n              <rect\n                x={xAt(active) - slot / 2}\n                y={M.top}\n                width={slot}\n                height={height}\n                fill={pal.band}\n                rx={6}\n              />\n            )}\n            {data.map((d, i) => {\n              // Bars grow one after another from left to right.\n              const t = easeOut(\n                (reveal * 1.6 - (i / Math.max(1, n)) * 0.6) / 1,\n              );\n              const top = yBar(left.min + (d.bar - left.min) * t);\n              const h = Math.max(0, M.top + height - top);\n              const r = Math.min(6, barW / 2, h);\n              const x = xAt(i) - barW / 2;\n              return (\n                <path\n                  key={d.label}\n                  d={`M${x},${M.top + height}V${top + r}Q${x},${top} ${x + r},${top}H${x + barW - r}Q${x + barW},${top} ${x + barW},${top + r}V${M.top + height}Z`}\n                  fill={`url(#${id}-bar)`}\n                  opacity={active === null || active === i ? 1 : 0.4}\n                  style={{ transition: \"opacity 160ms\" }}\n                />\n              );\n            })}\n            {lineTarget !== undefined && (\n              <line\n                x1={M.left}\n                x2={M.left + plotW}\n                y1={yLine(lineTarget)}\n                y2={yLine(lineTarget)}\n                stroke={lc}\n                strokeOpacity={0.6}\n                strokeDasharray=\"5 5\"\n              />\n            )}\n            <path\n              d={smooth(linePts)}\n              fill=\"none\"\n              stroke={lc}\n              strokeWidth={2.4}\n              strokeLinecap=\"round\"\n              pathLength={1}\n              strokeDasharray=\"1 1\"\n              strokeDashoffset={1 - lineT}\n            />\n            {linePts.map(([x, y], i) => (\n              <circle\n                key={i}\n                cx={x}\n                cy={y}\n                r={active === i ? 5 : 3.2}\n                fill={pal.card}\n                stroke={lc}\n                strokeWidth={2}\n                opacity={lineT >= (i + 0.5) / n ? 1 : 0}\n                style={{ transition: \"r 160ms, opacity 200ms\" }}\n              />\n            ))}\n            {data.map((d, i) =>\n              i % every === 0 ? (\n                <text\n                  key={d.label}\n                  x={xAt(i)}\n                  y={M.top + height + 20}\n                  textAnchor=\"middle\"\n                  fontSize={11}\n                  fill={active === i ? pal.text : pal.axis}\n                >\n                  {d.label}\n                </text>\n              ) : null,\n            )}\n          </svg>\n        )}\n        {active !== null && width > 0 && (\n          <div\n            className=\"pointer-events-none absolute top-2 z-10 min-w-[170px] rounded-xl px-3 py-2.5 text-[12px] shadow-xl\"\n            style={{\n              left: tipFlip ? undefined : tipX + slot / 2 + 8,\n              right: tipFlip ? width - tipX + slot / 2 + 8 : undefined,\n              background: pal.tip,\n              border: `1px solid ${pal.tipBorder}`,\n              backdropFilter: \"blur(8px)\",\n            }}\n          >\n            <p className=\"mb-1.5 font-semibold\">{data[active].label}</p>\n            <div className=\"flex items-center justify-between gap-4\">\n              <span\n                className=\"flex items-center gap-1.5\"\n                style={{ color: pal.sub }}\n              >\n                <span\n                  className=\"size-2 rounded-[2px]\"\n                  style={{ background: bc }}\n                />\n                {barLabel}\n              </span>\n              <span className=\"font-semibold tabular-nums\">\n                {formatBar(data[active].bar)}\n              </span>\n            </div>\n            <div className=\"mt-1 flex items-center justify-between gap-4\">\n              <span\n                className=\"flex items-center gap-1.5\"\n                style={{ color: pal.sub }}\n              >\n                <span\n                  className=\"size-2 rounded-full\"\n                  style={{ background: lc }}\n                />\n                {lineLabel}\n              </span>\n              <span className=\"font-semibold tabular-nums\">\n                {formatLine(data[active].line)}\n              </span>\n            </div>\n            {active > 0 && (\n              <p\n                className=\"mt-2 border-t pt-1.5 tabular-nums\"\n                style={{ borderColor: pal.tipBorder, color: pal.sub }}\n              >\n                {data[active].bar >= data[active - 1].bar ? \"▲\" : \"▼\"}{\" \"}\n                {Math.abs(\n                  ((data[active].bar - data[active - 1].bar) /\n                    data[active - 1].bar) *\n                    100,\n                ).toFixed(1)}\n                % vs {data[active - 1].label}\n              </p>\n            )}\n          </div>\n        )}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}