{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "chart",
  "title": "Chart",
  "description": "Dependency-free charts in the theme's colours: a bar chart, a labelled progress row, rings and a sparkline.",
  "registryDependencies": [
    "https://www.gliakit.com/r/tokens.json"
  ],
  "files": [
    {
      "path": "src/components/ui/chart.tsx",
      "content": "\"use client\";\n\nimport * as React from \"react\";\n\nimport { cn } from \"@/lib/utils\";\n\n/** Series colours, from the theme's chart palette (--chart-1…5). */\nconst SERIES = [\"var(--chart-1)\", \"var(--chart-2)\", \"var(--chart-3)\", \"var(--chart-4)\", \"var(--chart-5)\"];\nconst seriesColor = (i: number) => SERIES[i % SERIES.length];\n\n/** 44016 -> 44k, 1.2M; small numbers as they are. */\nfunction short(v: number, prefix = \"\", suffix = \"\") {\n  const a = Math.abs(v);\n  const n = a >= 1e6 ? `${(a / 1e6).toFixed(1)}M` : a >= 1e4 ? `${Math.round(a / 1000)}k` : Number.isInteger(a) ? a.toLocaleString(\"en-GB\") : a.toFixed(1);\n  return `${v < 0 ? \"−\" : \"\"}${prefix}${n}${suffix}`;\n}\n\ninterface BarChartProps {\n  /** One value per column, or one per series (`values`) for grouped, stacked and diverging bars. */\n  data: { label: string; value?: number; values?: number[] }[];\n  /** Series names, in the order of `values`; drawn as a legend when there are two or more. */\n  series?: string[];\n  /** grouped: side by side. stacked: on top of each other. diverging: positives up, negatives down from a zero line (in and out, gains and losses). */\n  mode?: \"grouped\" | \"stacked\" | \"diverging\";\n  height?: number;\n  /** Around the numbers in the tooltip and axis, e.g. \"€\" and \"k\". */\n  prefix?: string;\n  suffix?: string;\n  /** Index of the emphasised column (one series only); defaults to the largest. */\n  highlightIndex?: number;\n  /** Grow to the height the container gives it (at least `height`), e.g. in a card stretched beside a taller one. */\n  fill?: boolean;\n  className?: string;\n}\n\n/**\n * BarChart — columns in pure CSS, no chart library. One series keeps the\n * kit's look (the largest bar in the signature gradient); several series\n * draw in the theme's chart palette, grouped, stacked or diverging around a\n * zero line, with a y-axis, a legend and a hover tooltip.\n */\nfunction BarChart({ data, series, mode = \"grouped\", height = 176, prefix = \"\", suffix = \"\", highlightIndex, fill = false, className }: BarChartProps) {\n  const cols = data.map((d) => d.values ?? [d.value ?? 0]);\n  const n = Math.max(1, ...cols.map((c) => c.length));\n  const multi = n > 1;\n  const diverging = mode === \"diverging\";\n  // The axis range: stacks sum, groups take the largest, diverging sums each sign.\n  const tops = cols.map((c) => (mode === \"stacked\" ? c.reduce((a, v) => a + Math.abs(v), 0) : Math.max(0, ...c.filter((v) => diverging ? v >= 0 : true).map(Math.abs))));\n  const bottoms = cols.map((c) => (diverging ? c.filter((v) => v < 0).reduce((a, v) => a + v, 0) : 0));\n  const max = Math.max(1, ...tops);\n  const min = Math.min(0, ...bottoms);\n  const span = max - min || 1;\n  const zero = (max / span) * 100;\n  // A bar's share of the plot above the zero line, or below it.\n  const up = (v: number) => (Math.abs(v) / max) * 100;\n  const down = (v: number) => (Math.abs(v) / (-min || 1)) * 100;\n  const hi = multi ? -1 : (highlightIndex ?? cols.findIndex((c) => c[0] === Math.max(...cols.map((x) => x[0]))));\n  const [active, setActive] = React.useState<number | null>(null);\n  const ticks = diverging ? [max, 0, min] : [max, max / 2, 0];\n  const fmt = (v: number) => short(v, prefix, suffix);\n  // The y-axis column fits its longest label (\"€120k\", \"90/100\"), at least 40px.\n  const axis = Math.max(40, Math.ceil(Math.max(...ticks.map((t) => fmt(Math.round(t)).length)) * 6.8) + 8);\n\n  return (\n    <div data-slot=\"bar-chart\" className={cn(\"flex flex-col gap-2\", className)}>\n      {/* The numbers behind the bars, for screen readers (the bars themselves are hidden from them). */}\n      <table className=\"sr-only\">\n        <thead>\n          <tr>\n            <th scope=\"col\">Label</th>\n            {(multi && series ? series.slice(0, n) : [\"Value\"]).map((s) => (\n              <th key={s} scope=\"col\">{s}</th>\n            ))}\n          </tr>\n        </thead>\n        <tbody>\n          {data.map((d, i) => (\n            <tr key={i}>\n              <th scope=\"row\">{d.label}</th>\n              {cols[i].map((v, k) => (\n                <td key={k}>{`${v < 0 ? \"−\" : \"\"}${prefix}${Math.abs(v).toLocaleString(\"en-GB\")}${suffix}`}</td>\n              ))}\n            </tr>\n          ))}\n        </tbody>\n      </table>\n      <div className={cn(\"relative\", fill && \"flex-1\")} style={fill ? { minHeight: height } : { height }}>\n        {ticks.map((t, i) => {\n          const top = ((max - t) / span) * 100;\n          return (\n            <React.Fragment key={i}>\n              <span className=\"pointer-events-none absolute left-0 -translate-y-1/2 font-mono text-[11px] text-muted-foreground\" style={{ top: `${top}%` }}>\n                {fmt(Math.round(t))}\n              </span>\n              <span aria-hidden=\"true\" data-chart-grid=\"\" className={cn(\"pointer-events-none absolute right-0 border-t\", t === 0 && diverging ? \"border-muted-foreground/40\" : \"border-dashed border-border\")} style={{ top: `${top}%`, left: axis }} />\n            </React.Fragment>\n          );\n        })}\n        <div className=\"absolute inset-y-0 right-0 flex items-stretch gap-2\" style={{ left: axis }} onPointerLeave={() => setActive(null)}>\n          {cols.map((c, i) => (\n            <div key={i} className=\"relative min-w-0 flex-1\" onPointerEnter={() => setActive(i)}>\n              {/* the column's plot: positives stack up from the zero line, negatives down */}\n              {mode === \"grouped\" ? (\n                <div className=\"absolute inset-x-0 flex items-end gap-px\" style={{ top: 0, height: `${zero}%` }}>\n                  {c.map((v, k) => (\n                    <div\n                      key={k}\n                      // Bars outside the highlight: a hatched theme draws them hatched (globals.css).\n                      data-chart-rest={!multi && i !== hi ? \"\" : undefined}\n                      data-chart-bar=\"\"\n                      className={cn(\"min-w-0 flex-1 rounded-t-md\", !multi && (i === hi ? \"bg-[image:var(--gradient-signal-135)]\" : \"bg-highlight/40\"))}\n                      style={{ height: `${up(v)}%`, ...(multi && { background: seriesColor(k) }) }}\n                    />\n                  ))}\n                </div>\n              ) : (\n                <>\n                  <div className=\"absolute inset-x-0 flex flex-col-reverse\" style={{ top: 0, height: `${zero}%`, justifyContent: \"flex-start\" }}>\n                    {c.map((v, k) =>\n                      diverging && v < 0 ? null : (\n                        <div key={k} className=\"w-full first:rounded-b-sm last:rounded-t-md\" style={{ height: `${up(v)}%`, background: seriesColor(k), marginTop: 1 }} />\n                      ),\n                    )}\n                  </div>\n                  {diverging && (\n                    <div className=\"absolute inset-x-0 flex flex-col\" style={{ top: `${zero}%`, bottom: 0 }}>\n                      {c.map((v, k) =>\n                        v < 0 ? <div key={k} className=\"w-full first:rounded-t-sm last:rounded-b-md\" style={{ height: `${down(v)}%`, background: seriesColor(k), marginTop: 1 }} /> : null,\n                      )}\n                    </div>\n                  )}\n                </>\n              )}\n              {active === i && (\n                <div\n                  role=\"status\"\n                  className={cn(\n                    \"pointer-events-none absolute top-0 z-10 rounded-md border border-border bg-popover px-2.5 py-1.5 whitespace-nowrap shadow-md\",\n                    i > cols.length * 0.7 ? \"right-0\" : i < cols.length * 0.3 ? \"left-0\" : \"left-1/2 -translate-x-1/2\",\n                  )}\n                >\n                  <p className=\"font-mono text-[11px] uppercase tracking-[0.1em] text-muted-foreground\">{data[i].label}</p>\n                  {c.map((v, k) => (\n                    <p key={k} className=\"flex items-center gap-1.5 font-heading text-sm font-semibold text-foreground tabular-nums\">\n                      {multi && <span className=\"size-2 rounded-full\" style={{ background: seriesColor(k) }} />}\n                      {multi && series?.[k] && <span className=\"font-sans text-xs font-normal text-muted-foreground\">{series[k]}</span>}\n                      {fmt(v)}\n                    </p>\n                  ))}\n                </div>\n              )}\n            </div>\n          ))}\n        </div>\n      </div>\n      <div className=\"flex gap-2 font-mono text-[10px] uppercase text-muted-foreground\" style={{ paddingLeft: axis }}>\n        {data.map((d, i) => (\n          <span key={i} className=\"min-w-0 flex-1 truncate text-center\">\n            {d.label}\n          </span>\n        ))}\n      </div>\n      {multi && series && (\n        <div className=\"flex flex-wrap items-center gap-x-4 gap-y-1 text-xs text-muted-foreground\" style={{ paddingLeft: axis }}>\n          {series.slice(0, n).map((s, k) => (\n            <span key={s} className=\"flex items-center gap-1.5\">\n              <span className=\"size-2 rounded-full\" style={{ background: seriesColor(k) }} />\n              {s}\n            </span>\n          ))}\n        </div>\n      )}\n    </div>\n  );\n}\n\n/** Horizontal progress bar with label + percentage. */\nfunction ProgressRow({\n  label,\n  pct,\n  className,\n}: {\n  label: string;\n  pct: number;\n  className?: string;\n}) {\n  return (\n    <div className={cn(\"flex items-center gap-4\", className)}>\n      <span className=\"w-24 shrink-0 text-sm text-muted-foreground\">\n        {label}\n      </span>\n      <div className=\"h-2 flex-1 overflow-hidden rounded-full bg-muted\">\n        <div\n          className=\"h-full rounded-full bg-highlight\"\n          style={{ width: `${pct}%` }}\n        />\n      </div>\n      <span className=\"w-10 shrink-0 text-right font-mono text-xs text-muted-foreground\">\n        {pct}%\n      </span>\n    </div>\n  );\n}\n\ntype Pt = [number, number];\n\n/** Smooth SVG path through points (Catmull-Rom converted to cubic Béziers). */\nfunction smoothPath(pts: Pt[]) {\n  if (!pts.length) return \"\";\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    const c1: Pt = [p1[0] + (p2[0] - p0[0]) / 6, p1[1] + (p2[1] - p0[1]) / 6];\n    const c2: Pt = [p2[0] - (p3[0] - p1[0]) / 6, p2[1] - (p3[1] - p1[1]) / 6];\n    d += ` C${c1[0]},${c1[1]} ${c2[0]},${c2[1]} ${p2[0]},${p2[1]}`;\n  }\n  return d;\n}\n\n/**\n * Sparkline — a tiny trend for stat tiles, table rows and sidebars: a line,\n * a line over a soft area, or small bars. Tone follows meaning: success when\n * the trend is good, destructive when it is bad. `stretch` fills its box.\n */\nfunction Sparkline({\n  data,\n  kind = \"line\",\n  tone = \"primary\",\n  color,\n  width = 72,\n  height = 26,\n  stretch = false,\n  className,\n}: {\n  data: number[];\n  kind?: \"line\" | \"area\" | \"bars\";\n  tone?: \"primary\" | \"success\" | \"destructive\" | \"muted\";\n  /** Any CSS colour, e.g. \"var(--chart-2)\"; overrides the tone. */\n  color?: string;\n  width?: number;\n  height?: number;\n  /** Fills the parent's width and height (set them with classes) instead of its own size. */\n  stretch?: boolean;\n  className?: string;\n}) {\n  const max = Math.max(...data);\n  const min = Math.min(kind === \"bars\" ? 0 : Infinity, ...data);\n  const pts: Pt[] = data.map((v, i) => [\n    (i * width) / Math.max(1, data.length - 1),\n    height - 2 - ((v - min) / (max - min || 1)) * (height - 4),\n  ]);\n  const stroke =\n    color ??\n    {\n      primary: \"var(--highlight, var(--primary))\",\n      success: \"var(--diff-add)\",\n      destructive: \"var(--destructive)\",\n      muted: \"var(--muted-foreground)\",\n    }[tone];\n  const line = smoothPath(pts);\n  const bar = (width / data.length) * 0.62;\n  return (\n    <svg\n      aria-hidden=\"true\"\n      width={stretch ? undefined : width}\n      height={stretch ? undefined : height}\n      viewBox={`0 0 ${width} ${height}`}\n      preserveAspectRatio={stretch ? \"none\" : undefined}\n      fill=\"none\"\n      className={cn(\"shrink-0\", stretch && \"h-full w-full\", className)}\n    >\n      {kind === \"bars\" ? (\n        data.map((v, i) => {\n          const h = ((v - min) / (max - min || 1)) * (height - 4);\n          return <rect key={i} x={(i * width) / data.length + ((width / data.length) - bar) / 2} y={height - 2 - h} width={bar} height={Math.max(1, h)} rx={1} fill={stroke} fillOpacity={i === data.length - 1 ? 1 : 0.45} />;\n        })\n      ) : (\n        <>\n          {kind === \"area\" && <path d={`${line} L${width},${height} L0,${height} Z`} fill={stroke} fillOpacity=\"0.14\" />}\n          <path d={line} stroke={stroke} strokeWidth=\"1.5\" strokeLinecap=\"round\" vectorEffect={stretch ? \"non-scaling-stroke\" : undefined} />\n        </>\n      )}\n    </svg>\n  );\n}\n\n/**\n * Rings — concentric progress rings, one per item (tasks done of 8, focus\n * hours of 2): the first item is the outer ring. Each ring is its value over\n * its own max, in the theme's chart palette unless the item sets a colour.\n */\nfunction Rings({\n  items,\n  size = 120,\n  thickness = 10,\n  gap = 4,\n  children,\n  className,\n}: {\n  items: { label: string; value: number; max?: number; color?: string }[];\n  size?: number;\n  thickness?: number;\n  gap?: number;\n  /** Shown in the middle. */\n  children?: React.ReactNode;\n  className?: string;\n}) {\n  const label = items.map((it) => `${it.label}: ${it.value} of ${it.max ?? 100}`).join(\", \");\n  // The clear circle inside the innermost ring; what sits in the middle keeps to the width that fits in it.\n  const shown = items.filter((_, i) => size / 2 - thickness / 2 - i * (thickness + gap) > 0).length;\n  const inner = Math.max(0, size - 2 * shown * thickness - 2 * Math.max(0, shown - 1) * gap);\n  return (\n    <div role=\"img\" aria-label={label} className={cn(\"relative grid shrink-0 place-items-center\", className)} style={{ width: size, height: size }}>\n      <svg viewBox={`0 0 ${size} ${size}`} className=\"absolute inset-0 -rotate-90\" aria-hidden=\"true\">\n        {items.map((it, i) => {\n          const r = size / 2 - thickness / 2 - i * (thickness + gap);\n          if (r <= 0) return null;\n          const p = Math.max(0, Math.min(1, it.value / (it.max || 100)));\n          const color = it.color ?? seriesColor(i);\n          return (\n            <g key={it.label}>\n              <circle cx={size / 2} cy={size / 2} r={r} fill=\"none\" strokeWidth={thickness} stroke={color} strokeOpacity=\"0.18\" />\n              <circle cx={size / 2} cy={size / 2} r={r} fill=\"none\" strokeWidth={thickness} strokeLinecap=\"round\" stroke={color} pathLength={100} strokeDasharray={`${p * 100} 100`} />\n            </g>\n          );\n        })}\n      </svg>\n      {children && (\n        <div className=\"relative text-center text-balance\" style={{ maxWidth: Math.round(inner * 0.82) }}>\n          {children}\n        </div>\n      )}\n    </div>\n  );\n}\n\nexport { BarChart, ProgressRow, Rings, Sparkline, seriesColor, smoothPath, type Pt };\n",
      "type": "registry:ui"
    }
  ],
  "categories": [
    "data-display"
  ],
  "type": "registry:ui"
}
