{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bar-chart",
  "title": "Bar Chart",
  "description": "A stacked bar chart, up to 5 series per bar, a reference line, a peak-value annotation, and a hover tooltip breaking down each bar's segments with a running total.",
  "dependencies": [
    "clsx",
    "tailwind-merge"
  ],
  "files": [
    {
      "path": "registry/new-york/bar-chart/bar-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 BarChartTheme = \"light\" | \"dark\";\n\nexport interface BarChartSeries {\n  visible?: boolean;\n  label: string;\n  color: string;\n}\n\nexport interface BarChartDataPoint {\n  label: string;\n  note?: string;\n  v1: number;\n  v2: number;\n  v3: number;\n  v4: number;\n  v5: number;\n}\n\nexport interface BarChartReferenceLine {\n  visible?: boolean;\n  value: number;\n  label: string;\n  color: string;\n}\n\nexport interface BarChartAnnotation {\n  visible?: boolean;\n  label: string;\n  atIndex: number;\n  value: number;\n  suffix?: string;\n  color: string;\n}\n\nexport interface BarChartProps {\n  theme?: BarChartTheme;\n  title?: string;\n  subtitle?: string;\n  chartHeight?: number;\n  showLegend?: boolean;\n  yMin?: number;\n  yMax?: number;\n  yStep?: number;\n  gapPx?: number;\n  widthRatio?: number;\n  series1?: BarChartSeries;\n  series2?: BarChartSeries;\n  series3?: BarChartSeries;\n  series4?: BarChartSeries;\n  series5?: BarChartSeries;\n  dataPoints?: BarChartDataPoint[];\n  referenceLine?: BarChartReferenceLine;\n  annotation?: BarChartAnnotation;\n  enableHover?: boolean;\n  className?: string;\n}\n\nconst VB_W = 1200;\nconst VB_H = 560;\nconst PAD_LEFT = 72;\nconst PAD_RIGHT = 150;\nconst PAD_TOP = 26;\nconst PAD_BOTTOM = 58;\n\nfunction clamp(n: number, min: number, max: number) {\n  return Math.min(max, Math.max(min, n));\n}\n\nfunction buildTicks(yMin: number, yMax: number, step: number): number[] {\n  const ticks: number[] = [];\n  let v = Math.ceil(yMin / step) * step;\n  for (; v <= yMax; v += step) ticks.push(v);\n  return ticks;\n}\n\nfunction makeGeometry(count: number, plotTop: number, yMin: number, yMax: number) {\n  const plotLeft = PAD_LEFT;\n  const plotRight = VB_W - PAD_RIGHT;\n  const plotBottom = VB_H - PAD_BOTTOM;\n  const plotWidth = plotRight - plotLeft;\n  const plotHeight = plotBottom - plotTop;\n  const xAt = (i: number) => plotLeft + (count > 1 ? (i / (count - 1)) * plotWidth : plotWidth / 2);\n  const yAt = (v: number) => plotBottom - clamp((v - yMin) / (yMax - yMin), 0, 1) * plotHeight;\n  return { plotLeft, plotRight, plotTop, plotBottom, plotWidth, plotHeight, xAt, yAt };\n}\n\nconst DARK_SERIES1: Required<BarChartSeries> = { visible: true, label: \"Critical\", color: \"#FF7A6B\" };\nconst DARK_SERIES2: Required<BarChartSeries> = { visible: true, label: \"Migration\", color: \"#F5F4F1\" };\nconst DARK_SERIES3: Required<BarChartSeries> = { visible: true, label: \"Product\", color: \"#87FFE3\" };\nconst DARK_SERIES4: Required<BarChartSeries> = { visible: true, label: \"Onboarding\", color: \"#F2A841\" };\nconst DARK_SERIES5: Required<BarChartSeries> = { visible: true, label: \"Deflected\", color: \"#8FB8FF\" };\nconst LIGHT_SERIES1: Required<BarChartSeries> = { ...DARK_SERIES1, color: \"#E5484D\" };\nconst LIGHT_SERIES2: Required<BarChartSeries> = { ...DARK_SERIES2, color: \"#0A0A0A\" };\nconst LIGHT_SERIES3: Required<BarChartSeries> = { ...DARK_SERIES3, color: \"#0E9F80\" };\nconst LIGHT_SERIES4: Required<BarChartSeries> = { ...DARK_SERIES4, color: \"#D9822B\" };\nconst LIGHT_SERIES5: Required<BarChartSeries> = { ...DARK_SERIES5, color: \"#3B6FD8\" };\n\nconst DEFAULT_DATA: BarChartDataPoint[] = [\n  { label: \"01\", note: \"\", v1: 16, v2: 25, v3: 28, v4: 17, v5: 32 },\n  { label: \"02\", note: \"\", v1: 18, v2: 29, v3: 33, v4: 20, v5: 38 },\n  { label: \"03\", note: \"\", v1: 20, v2: 32, v3: 35, v4: 21, v5: 40 },\n  { label: \"04\", note: \"\", v1: 21, v2: 34, v3: 37, v4: 22, v5: 44 },\n  { label: \"05\", note: \"\", v1: 25, v2: 40, v3: 45, v4: 27, v5: 53 },\n  { label: \"06\", note: \"\", v1: 25, v2: 39, v3: 44, v4: 26, v5: 51 },\n  { label: \"07\", note: \"\", v1: 25, v2: 40, v3: 44, v4: 27, v5: 51 },\n  { label: \"08\", note: \"reporting incident\", v1: 28, v2: 45, v3: 50, v4: 30, v5: 58 },\n  { label: \"09\", note: \"\", v1: 26, v2: 42, v3: 47, v4: 28, v5: 55 },\n  { label: \"10\", note: \"\", v1: 27, v2: 44, v3: 49, v4: 29, v5: 56 },\n  { label: \"11\", note: \"\", v1: 28, v2: 45, v3: 50, v4: 30, v5: 57 },\n  { label: \"12\", note: \"\", v1: 27, v2: 43, v3: 47, v4: 28, v5: 55 },\n  { label: \"13\", note: \"\", v1: 27, v2: 44, v3: 49, v4: 29, v5: 56 },\n  { label: \"14\", note: \"\", v1: 31, v2: 49, v3: 55, v4: 33, v5: 64 },\n  { label: \"15\", note: \"\", v1: 29, v2: 46, v3: 52, v4: 31, v5: 60 },\n  { label: \"16\", note: \"\", v1: 30, v2: 49, v3: 54, v4: 32, v5: 63 },\n  { label: \"17\", note: \"\", v1: 27, v2: 43, v3: 47, v4: 28, v5: 55 },\n  { label: \"18\", note: \"\", v1: 30, v2: 48, v3: 53, v4: 32, v5: 62 },\n  { label: \"19\", note: \"\", v1: 30, v2: 47, v3: 53, v4: 32, v5: 60 },\n  { label: \"20\", note: \"\", v1: 27, v2: 44, v3: 49, v4: 29, v5: 56 },\n  { label: \"21\", note: \"\", v1: 32, v2: 52, v3: 58, v4: 35, v5: 66 },\n  { label: \"22\", note: \"\", v1: 31, v2: 50, v3: 56, v4: 33, v5: 65 },\n  { label: \"23\", note: \"\", v1: 32, v2: 51, v3: 56, v4: 34, v5: 65 },\n  { label: \"24\", note: \"\", v1: 34, v2: 54, v3: 60, v4: 36, v5: 69 },\n];\n\nconst DEFAULT_REF_LINE: Required<BarChartReferenceLine> = { visible: false, value: 205, label: \"\", color: \"#9ca3af\" };\nconst DEFAULT_ANNOTATION: Required<BarChartAnnotation> = { visible: true, label: \"Peak\", atIndex: 23, value: 253, suffix: \"cases\", color: \"#9ca3af\" };\n\nexport function BarChart({\n  theme = \"dark\",\n  title = \"Support case volume\",\n  subtitle = \"\",\n  chartHeight = 420,\n  showLegend = true,\n  yMin = 0,\n  yMax = 280,\n  yStep = 80,\n  gapPx = 5,\n  widthRatio = 0.46,\n  series1 = theme === \"light\" ? LIGHT_SERIES1 : DARK_SERIES1,\n  series2 = theme === \"light\" ? LIGHT_SERIES2 : DARK_SERIES2,\n  series3 = theme === \"light\" ? LIGHT_SERIES3 : DARK_SERIES3,\n  series4 = theme === \"light\" ? LIGHT_SERIES4 : DARK_SERIES4,\n  series5 = theme === \"light\" ? LIGHT_SERIES5 : DARK_SERIES5,\n  dataPoints = DEFAULT_DATA,\n  referenceLine = DEFAULT_REF_LINE,\n  annotation = DEFAULT_ANNOTATION,\n  enableHover = true,\n  className,\n}: BarChartProps) {\n  const isDark = theme === \"dark\";\n  const containerRef = React.useRef<HTMLDivElement>(null);\n  const [hoverIndex, setHoverIndex] = React.useState<number | null>(null);\n  const [containerWidth, setContainerWidth] = React.useState(0);\n\n  React.useEffect(() => {\n    const el = containerRef.current;\n    if (!el) return;\n    const ro = new ResizeObserver(([entry]) => setContainerWidth(entry.contentRect.width));\n    ro.observe(el);\n    return () => ro.disconnect();\n  }, []);\n\n  // preserveAspectRatio=\"none\" stretches the SVG to fill its box; on a narrow\n  // container that non-uniform scaling squishes axis text near-illegibly, so\n  // below the viewBox's natural aspect ratio we shrink the box to match it.\n  const effectiveHeight = containerWidth > 0 ? Math.min(chartHeight, containerWidth * (VB_H / VB_W)) : chartHeight;\n\n  // Keep axis labels a readable ~13px on screen regardless of how far the viewBox is scaled down.\n  const viewScale = containerWidth > 0 ? Math.min(containerWidth / VB_W, effectiveHeight / VB_H) : 0.5;\n  const axisFontSize = Math.min(26, Math.max(12, 13 / viewScale));\n\n  const palette = {\n    cardBg: isDark ? \"#0E0E0E\" : \"#ffffff\",\n    cardBorder: isDark ? \"rgba(255,255,255,0.08)\" : \"rgba(0,0,0,0.06)\",\n    textPrimary: isDark ? \"#f4f5f7\" : \"#111317\",\n    textSub: isDark ? \"rgba(255,255,255,0.55)\" : \"rgba(17,19,23,0.55)\",\n    gridLine: isDark ? \"rgba(255,255,255,0.07)\" : \"rgba(17,19,23,0.08)\",\n    axisText: isDark ? \"rgba(255,255,255,0.7)\" : \"rgba(17,19,23,0.7)\",\n    badgeBorder: isDark ? \"rgba(255,255,255,0.12)\" : \"rgba(0,0,0,0.08)\",\n  };\n\n  const headerHeight = (title ? 30 : 0) + (subtitle ? 22 : 0);\n  const series = [series1, series2, series3, series4, series5];\n  const valueKeys: Array<keyof BarChartDataPoint> = [\"v1\", \"v2\", \"v3\", \"v4\", \"v5\"];\n  const nBar = dataPoints.length;\n  const plotTopY = PAD_TOP + (showLegend ? 34 : 0) + headerHeight;\n  const geo = makeGeometry(nBar, plotTopY, yMin, yMax);\n  const slotWidth = nBar > 1 ? geo.plotWidth / (nBar - 1) : geo.plotWidth;\n  const barWidth = slotWidth * widthRatio;\n\n  const yTicks = React.useMemo(() => buildTicks(yMin, yMax, yStep), [yMin, yMax, yStep]);\n\n  const barStacks = React.useMemo(() => {\n    return dataPoints.map((d, i) => {\n      const cx = geo.xAt(i);\n      let cursorBottom = geo.plotBottom;\n      let total = 0;\n      const segments = valueKeys.map((key, si) => {\n        const v = (d[key] as number) ?? 0;\n        total += v;\n        const segH = Math.max(0, (v / (yMax - yMin)) * geo.plotHeight);\n        const segTop = cursorBottom - segH;\n        const seg = { key, color: series[si].color, visible: series[si].visible, x: cx - barWidth / 2, y: segTop, height: segH };\n        cursorBottom = segTop - gapPx;\n        return seg;\n      });\n      const topY = geo.plotBottom - (total / (yMax - yMin)) * geo.plotHeight;\n      return { x: cx, segments, total, topY };\n    });\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [dataPoints, nBar, yMin, yMax, barWidth, gapPx, series1, series2, series3, series4, series5]);\n\n  const annotationX = geo.xAt(clamp(annotation.atIndex, 0, nBar - 1));\n  const annotationY = geo.yAt(annotation.value) - 14;\n\n  const hover = React.useMemo(() => {\n    if (hoverIndex === null) return null;\n    const idx = clamp(hoverIndex, 0, nBar - 1);\n    const d = dataPoints[idx];\n    if (!d) return null;\n    const rows = valueKeys.map((key, si) => ({ label: series[si].label, color: series[si].color, value: (d[key] as number) ?? 0 }));\n    const total = rows.reduce((sum, r) => sum + r.value, 0);\n    return { label: d.label, note: d.note, rows, total, topY: barStacks[idx]?.topY ?? geo.plotTop };\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [hoverIndex, dataPoints, nBar, barStacks, series1, series2, series3, series4, series5]);\n\n  const handleMove = (e: React.MouseEvent<HTMLDivElement>) => {\n    if (!enableHover || !containerRef.current) return;\n    const rect = containerRef.current.getBoundingClientRect();\n    const relX = clamp((e.clientX - rect.left) / rect.width, 0, 1);\n    const svgX = relX * VB_W;\n    if (svgX < geo.plotLeft || svgX > geo.plotRight) {\n      setHoverIndex(null);\n      return;\n    }\n    const idx = Math.round(((svgX - geo.plotLeft) / geo.plotWidth) * (nBar - 1));\n    setHoverIndex(clamp(idx, 0, nBar - 1));\n  };\n\n  const handleLeave = () => setHoverIndex(null);\n\n  return (\n    <div\n      className={cn(\"box-border flex w-full flex-col gap-1 overflow-hidden rounded-[20px]\", className)}\n      style={{ backgroundColor: palette.cardBg, border: `1px solid ${palette.cardBorder}`, padding: \"28px 32px\", fontFamily: \"Inter, -apple-system, BlinkMacSystemFont, sans-serif\" }}\n    >\n      {title && <div className=\"text-center text-[17px] font-bold\" style={{ color: palette.textPrimary }}>{title}</div>}\n      {subtitle && (\n        <div className=\"mx-auto mt-0.5 max-w-[760px] text-center text-[12.5px] leading-[1.5]\" style={{ color: palette.textSub }}>\n          {subtitle}\n        </div>\n      )}\n\n      {showLegend && (\n        <div className=\"mt-3.5 flex flex-wrap justify-center gap-5\">\n          {series.map(\n            (s, i) =>\n              s.visible && (\n                <div key={i} className=\"flex items-center gap-1.5\">\n                  <span className=\"inline-block h-2.5 w-2.5 rounded\" style={{ backgroundColor: s.color }} />\n                  <span className=\"text-[12.5px] font-semibold\" style={{ color: palette.textPrimary }}>\n                    {s.label}\n                  </span>\n                </div>\n              ),\n          )}\n        </div>\n      )}\n\n      <div ref={containerRef} onMouseMove={handleMove} onMouseLeave={handleLeave} className=\"relative mt-3 w-full\" style={{ height: effectiveHeight }}>\n        <svg width=\"100%\" height=\"100%\" viewBox={`0 0 ${VB_W} ${VB_H}`} preserveAspectRatio=\"none\" className=\"block overflow-visible\">\n          {yTicks.map((v) => (\n            <g key={v}>\n              <line x1={geo.plotLeft} x2={geo.plotRight} y1={geo.yAt(v)} y2={geo.yAt(v)} stroke={palette.gridLine} strokeWidth={1} />\n              <text x={geo.plotLeft - 14} y={geo.yAt(v) + axisFontSize * 0.35} textAnchor=\"end\" fontSize={axisFontSize} fill={palette.axisText}>\n                {v}\n              </text>\n            </g>\n          ))}\n\n          {dataPoints.map((d, i) => (\n            <text key={i} x={geo.xAt(i)} y={geo.plotBottom + 12 + axisFontSize} textAnchor=\"middle\" fontSize={axisFontSize} fill={palette.axisText}>\n              {d.label}\n            </text>\n          ))}\n\n          {referenceLine.visible && (\n            <>\n              <line x1={geo.plotLeft} x2={geo.plotRight} y1={geo.yAt(referenceLine.value)} y2={geo.yAt(referenceLine.value)} stroke={palette.gridLine} strokeWidth={1} />\n              <text x={geo.plotLeft + 4} y={geo.yAt(referenceLine.value) - 28} fontSize={axisFontSize} fill={referenceLine.color}>\n                {referenceLine.label}\n              </text>\n            </>\n          )}\n\n          {barStacks.map((stack, i) => (\n            <g key={`barstack-${i}`} opacity={hoverIndex === null || hoverIndex === i ? 1 : 0.35}>\n              {stack.segments.map((seg) => seg.visible && seg.height > 0 && <rect key={seg.key} x={seg.x} y={seg.y} width={barWidth} height={seg.height} rx={barWidth / 2} fill={seg.color} />)}\n            </g>\n          ))}\n        </svg>\n\n        {annotation.visible && (\n          <div\n            className=\"absolute rounded-lg px-3 py-1.5 text-[12.5px] font-bold whitespace-nowrap\"\n            style={{\n              left: `${(annotationX / VB_W) * 100}%`,\n              top: `${(annotationY / VB_H) * 100}%`,\n              transform: \"translate(calc(-100% + 14px), calc(-100% - 6px))\",\n              backgroundColor: isDark ? \"rgba(14,14,14,0.94)\" : \"rgba(255,255,255,0.96)\",\n              border: `1px solid ${annotation.color}55`,\n              boxShadow: isDark ? \"0 10px 28px rgba(0,0,0,0.5)\" : \"0 6px 20px rgba(0,0,0,0.12)\",\n              color: palette.textPrimary,\n            }}\n          >\n            {annotation.label} {dataPoints[clamp(annotation.atIndex, 0, nBar - 1)]?.label}: {Math.round(annotation.value)} {annotation.suffix}\n          </div>\n        )}\n\n        {enableHover && hoverIndex !== null && hover && (\n          <div\n            className=\"absolute mb-2 min-w-[190px] rounded-[10px] px-3.5 py-2.5 pointer-events-none\"\n            style={{\n              left: `${(geo.xAt(clamp(hoverIndex, 0, nBar - 1)) / VB_W) * 100}%`,\n              top: `${(hover.topY / VB_H) * 100}%`,\n              transform: \"translate(-30%, -100%)\",\n              backgroundColor: isDark ? \"rgba(14,14,14,0.96)\" : \"rgba(255,255,255,0.97)\",\n              border: `1px solid ${palette.badgeBorder}`,\n              boxShadow: isDark ? \"0 10px 28px rgba(0,0,0,0.5)\" : \"0 6px 20px rgba(0,0,0,0.12)\",\n            }}\n          >\n            <div className=\"mb-1.5 text-[11.5px] font-bold whitespace-nowrap\" style={{ color: palette.textPrimary }}>\n              Day {hover.label}\n              {hover.note ? ` - ${hover.note}` : \"\"}\n            </div>\n            {hover.rows.map((r, ri) => (\n              <div key={ri} className=\"mt-1 flex items-center justify-between gap-3.5\">\n                <div className=\"flex items-center gap-1.5\">\n                  <span className=\"inline-block h-2 w-2 shrink-0 rounded-full\" style={{ backgroundColor: r.color }} />\n                  <span className=\"text-[11.5px]\" style={{ color: palette.textSub }}>\n                    {r.label}\n                  </span>\n                </div>\n                <span className=\"text-[11.5px] font-bold\" style={{ color: palette.textPrimary }}>\n                  {Math.round(r.value)}\n                </span>\n              </div>\n            ))}\n            <div className=\"mt-2 flex justify-between pt-1.5\" style={{ borderTop: `1px solid ${palette.badgeBorder}` }}>\n              <span className=\"text-[11.5px]\" style={{ color: palette.textSub }}>\n                Total\n              </span>\n              <span className=\"text-[11.5px] font-extrabold\" style={{ color: palette.textPrimary }}>\n                {Math.round(hover.total)}\n              </span>\n            </div>\n          </div>\n        )}\n      </div>\n    </div>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "type": "registry:component"
}