{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart-bar",
  "title": "Bar chart",
  "description": "Hatched bars, outlined by the pen as you scroll. Vertical or horizontal, one series, grouped side by side, or stacked.",
  "dependencies": [
    "svg-scroll-draw@^2.29.0"
  ],
  "registryDependencies": [
    "https://scrollfx-ui.vercel.app/r/plotter.json",
    "https://scrollfx-ui.vercel.app/r/chart.json"
  ],
  "files": [
    {
      "path": "registry/ui/chart-bar.tsx",
      "content": "'use client';\n\n/**\n * chart-bar — a bar chart a pen plots as you scroll: the gridlines first,\n * then each bar hatched in, one after another, then outlined, the first\n * series in the accent colour. Upright or across, one series, grouped side by\n * side, or stacked; each series hatched at its own angle, so they read apart\n * without colour.\n *\n * Built on svg-scroll-draw (MIT): https://github.com/DhruvilChauahan0210/scrollfx\n * Needs: react, svg-scroll-draw, lib/chart and lib/plotter (installed with it).\n * Styling: Tailwind CSS with shadcn/ui tokens.\n *\n * The axis labels, the legend and a data table for screen readers are in the\n * server HTML; the strokes are made in the browser at the chart's size and\n * finish as the chart reaches the middle of the screen. Reduced motion shows\n * the chart drawn.\n */\n\nimport { useMemo } from 'react';\nimport 'svg-scroll-draw/no-flash.css';\nimport {\n  ACCENT, DataTable, Legend, PHASE, PenLayers, XLabels, YLabels, formatter, gutter, hatchMarks, linear, niceTicks, num, rect, seriesOf, toPath, useSize,\n  type ChartTrigger, type ChartConfig, type Datum, type Mark, type Pen,\n} from '@/lib/chart';\n\nexport interface ChartBarProps {\n  /** One object per bar (or group of bars). */\n  data?: Datum[];\n  /** The key in each object that labels its bar. */\n  index?: string;\n  /** The series to draw, by key: a label and, if you like, a colour. Defaults to every number in the first object. */\n  config?: ChartConfig;\n  /** Upright bars (columns), or bars across with the labels down the left. */\n  orientation?: 'vertical' | 'horizontal';\n  /** With several series: stack them in one bar, rather than group them side by side. */\n  stacked?: boolean;\n  /** The plot's height in pixels. Across, it defaults to a row per bar. */\n  height?: number;\n  /** How numbers on the axis are written (Intl.NumberFormat options). */\n  format?: Intl.NumberFormatOptions;\n  /** Show the key. By default, when there is more than one series. */\n  legend?: boolean;\n  /** The first series' colour. */\n  accent?: string;\n  /** What the chart shows, for screen readers (it captions the data table). */\n  label?: string;\n  /** `scroll`: inked as it rises, done at mid-screen. `view`: played once as it comes into view. */\n  trigger?: ChartTrigger;\n  /** With `view`, how long the chart takes, in ms. */\n  duration?: number;\n  className?: string;\n}\n\nconst SAMPLE: Datum[] = [\n  { month: 'Jan', orders: 186 }, { month: 'Feb', orders: 305 }, { month: 'Mar', orders: 237 },\n  { month: 'Apr', orders: 173 }, { month: 'May', orders: 209 }, { month: 'Jun', orders: 264 },\n];\n\nexport function ChartBar({\n  data = SAMPLE,\n  index = 'month',\n  config,\n  orientation = 'vertical',\n  stacked = false,\n  height,\n  format,\n  legend,\n  accent = ACCENT,\n  label = 'Bar chart',\n  trigger = 'scroll',\n  duration = 1800,\n  className = '',\n}: ChartBarProps) {\n  const series = seriesOf(data, index, config, accent);\n  const across = orientation === 'horizontal';\n  const totals = data.map((d) => (stacked ? series.reduce((m, s) => m + num(d[s.key]), 0) : Math.max(...series.map((s) => num(d[s.key])))));\n  const ticks = niceTicks(Math.min(0, ...totals), Math.max(...totals), across ? 5 : 4);\n  const [lo, hi] = [ticks[0], ticks[ticks.length - 1]];\n  const fmt = formatter(format);\n  const n = data.length;\n  const grouped = !stacked && series.length > 1;\n  const plotHeight = height ?? (across ? n * (grouped ? 20 * series.length + 18 : 42) : 240);\n  const band = (i: number) => (i + 0.5) / n;\n  const [ref, size] = useSize<HTMLDivElement>();\n\n  const pens = useMemo((): Pen[] | null => {\n    if (!size) return null;\n    const { w, h } = size;\n    // v runs along the value axis, c across the categories; flip them for bars across.\n    const len = across ? w : h;\n    const cross = across ? h : w;\n    const v = linear(lo, hi, 0, len);\n    const pt = (c: number, val: number): [number, number] => (across ? [val, c] : [c, len - val]);\n    const axes: Mark[] = ticks.map((t) => {\n      const base = t === 0 || t === lo;\n      return { d: toPath([pt(0, v(t)), pt(cross, v(t))]), width: base ? 1 : 0.6, opacity: base ? 0.9 : 0.22 };\n    });\n    const step = cross / n;\n    const groupW = step * (grouped ? 0.78 : 0.6);\n    const fill: Mark[] = [];\n    const outline: Mark[] = [];\n    data.forEach((d, i) => {\n      const c0 = step * i + (step - groupW) / 2;\n      let acc = 0;\n      series.forEach((s, k) => {\n        const value = num(d[s.key]);\n        const gap = grouped ? Math.min(4, (groupW / series.length) * 0.12) : 0;\n        const bw = grouped ? groupW / series.length - gap : groupW;\n        const c = grouped ? c0 + k * (bw + gap) + gap / 2 : c0;\n        const from = stacked ? acc : 0;\n        const to = from + value;\n        acc = to;\n        const [a, b] = [v(Math.min(from, to)), v(Math.max(from, to))];\n        if (b - a < 0.5) return;\n        const box = across ? rect(a, c, b - a, bw) : rect(c, len - b, bw, b - a);\n        fill.push(...hatchMarks(box, s.hatch, { opacity: 0.75, width: 0.9, inset: 2.5, reverse: across }));\n        // Up one side, across the end, down the other: the base is the axis (or the bar below).\n        outline.push({ d: toPath([pt(c, a), pt(c, b), pt(c + bw, b), pt(c + bw, a)]), color: s.color, width: k === 0 ? 1.8 : 1.4 });\n      });\n    });\n    return [\n      { marks: axes, ...PHASE.axes, spread: 0.6 },\n      { marks: fill, ...PHASE.fill, spread: 2.5 },\n      { marks: outline, ...PHASE.ink, spread: 1.5 },\n    ];\n  }, [size, data, index, config, stacked, across, accent, lo, hi]);\n\n  const valueTicks = ticks.map((t) => ({ label: fmt(t), at: (t - lo) / (hi - lo) }));\n  const categories = data.map((d, i) => ({ label: String(d[index]), at: across ? 1 - band(i) : band(i) }));\n  return (\n    <figure className={`w-full ${className}`}>\n      <DataTable caption={label} head={[index, ...series.map((s) => s.label)]} rows={data.map((d) => [String(d[index]), ...series.map((s) => num(d[s.key]))])} />\n      <div aria-hidden>\n        <div className=\"flex\">\n          <YLabels ticks={across ? categories : valueTicks} />\n          <div ref={ref} data-slot=\"plot\" className=\"relative min-w-0 flex-1\" style={{ height: plotHeight }}>\n            {size && pens ? <PenLayers size={size} pens={pens} trigger={trigger} duration={duration} /> : null}\n          </div>\n        </div>\n        <XLabels ticks={across ? valueTicks : categories} offset={gutter((across ? categories : valueTicks).map((t) => t.label))} />\n        {(legend ?? series.length > 1) && <Legend series={series} kind=\"hatch\" className=\"mt-5\" />}\n      </div>\n    </figure>\n  );\n}\n",
      "type": "registry:ui",
      "target": "components/scrollfx/chart-bar.tsx"
    }
  ],
  "type": "registry:ui"
}