{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bullet-chart",
  "type": "registry:component",
  "title": "Bullet Chart",
  "description": "One measure against its target and its bands (poor, fair, good) in a single slim row: the value as a bar, the target as a tick, the bands as steps of grey behind them. Several rows stack, each on its own scale.",
  "categories": [
    "viz"
  ],
  "dependencies": [
    "d3-scale"
  ],
  "devDependencies": [
    "@types/d3-scale"
  ],
  "registryDependencies": [
    "@rivier-ui/theme",
    "@rivier-ui/chart",
    "@rivier-ui/cn"
  ],
  "files": [
    {
      "path": "registry/viz/bullet-chart.tsx",
      "type": "registry:component",
      "target": "components/viz/bullet-chart.tsx",
      "content": "\"use client\";\n/**\n * @name bullet-chart\n * @category viz\n * @description One measure against its target and its bands (poor, fair, good) in a single slim row: the value as a bar, the target as a tick, the bands as steps of grey behind them. Several rows stack, each on its own scale.\n * @when Showing how a few measures stand against their goals in little space: a dashboard of targets, a scorecard. For one value against a limit with nothing else use radial-progress or progress; to compare many values with each other use bar-chart.\n * @example\n * <BulletChart\n *   label=\"Quarter targets\"\n *   items={[\n *     { label: \"Revenue\", value: 270, target: 250, ranges: [150, 225, 300] },\n *     { label: \"New customers\", value: 1400, target: 1600, ranges: [1000, 1500, 2000] },\n *   ]}\n * />\n */\nimport { scaleLinear } from \"d3-scale\";\nimport { useState, type ComponentProps, type KeyboardEvent } from \"react\";\nimport { cn } from \"@/lib/cn\";\nimport { ChartDataTable, formatNumber, type ChartFormat } from \"@/components/viz/chart\";\n\nexport type BulletItem = {\n  label: string;\n  /** A short note under the label: a unit or a period. */\n  note?: string;\n  value: number;\n  target?: number;\n  /** The upper edges of the bands from worst to best: [poor, fair, good]. The last is the end of the scale unless `max` is set. */\n  ranges?: readonly number[];\n  max?: number;\n};\n\nexport type BulletChartLabels = { measure: string; value: string; target: string; bands: string };\n\nexport type BulletChartProps = Omit<ComponentProps<\"div\">, \"children\"> &\n  Omit<ChartFormat, \"timeZone\"> & {\n    items: readonly BulletItem[];\n    /** What the chart shows, for assistive technology. */\n    label: string;\n    labels?: Partial<BulletChartLabels>;\n  };\n\nconst LABELS: BulletChartLabels = { measure: \"Measure\", value: \"Value\", target: \"Target\", bands: \"Bands\" };\n/** Band greys from the worst (strongest) to the best (faintest): a mix of the text colour into the surface. */\nconst BANDS = [26, 17, 10, 6];\n\nexport function BulletChart({ items, label, labels: given, locale, formatValue, className, ...props }: BulletChartProps) {\n  const labels = { ...LABELS, ...given };\n  const number = formatValue ?? ((value: number) => formatNumber(value, locale));\n  const [focus, setFocus] = useState(0);\n  const table = {\n    caption: label,\n    columns: [labels.measure, labels.value, labels.target, labels.bands],\n    rows: items.map((item) => [item.label, number(item.value), item.target === undefined ? \"\" : number(item.target), (item.ranges ?? []).map(number).join(\", \")]),\n  };\n  const onKeyDown = (event: KeyboardEvent<HTMLUListElement>): void => {\n    const steps: Record<string, number> = { ArrowDown: 1, ArrowRight: 1, ArrowUp: -1, ArrowLeft: -1 };\n    let next = focus;\n    if (event.key in steps) next = Math.max(0, Math.min(items.length - 1, focus + (steps[event.key] ?? 0)));\n    else if (event.key === \"Home\") next = 0;\n    else if (event.key === \"End\") next = items.length - 1;\n    else return;\n    event.preventDefault();\n    setFocus(next);\n    (event.currentTarget.children[next] as HTMLElement | undefined)?.focus();\n  };\n\n  return (\n    <div data-slot=\"bullet-chart\" className={cn(\"@container w-full min-w-0\", className)} {...props}>\n      {/* Each row is a focusable item that says its own values: the roving tab stop of a list. */}\n      <ul aria-label={label} className=\"flex flex-col gap-4\" onKeyDown={onKeyDown}>\n        {items.map((item, index) => {\n          const end = item.max ?? Math.max(item.value, item.target ?? 0, ...(item.ranges ?? [])) * (item.max === undefined && !item.ranges?.length ? 1.1 : 1);\n          const scale = scaleLinear()\n            .domain([Math.min(0, item.value), end || 1])\n            .range([0, 100])\n            .clamp(true);\n          const bands = [...(item.ranges ?? [])].sort((a, b) => a - b);\n          const spoken = `${item.label}: ${labels.value} ${number(item.value)}${item.target === undefined ? \"\" : `, ${labels.target} ${number(item.target)}`}`;\n          return (\n            <li\n              key={index}\n              tabIndex={index === focus ? 0 : -1}\n              aria-label={spoken}\n              onFocus={() => setFocus(index)}\n              className=\"grid grid-cols-1 items-center gap-x-4 gap-y-1 rounded-control @sm:grid-cols-[minmax(6rem,10rem)_1fr]\"\n            >\n              <div className=\"min-w-0\" aria-hidden=\"true\">\n                <div className=\"truncate text-sm font-medium\">{item.label}</div>\n                {item.note ? <div className=\"truncate text-xs text-muted-foreground\">{item.note}</div> : null}\n              </div>\n              <div className=\"flex min-w-0 items-center gap-3\" aria-hidden=\"true\">\n                <div\n                  className=\"relative h-6 min-w-0 flex-1 overflow-hidden rounded-[min(var(--radius),4px)]\"\n                  style={{ backgroundColor: `color-mix(in oklab, var(--foreground) ${BANDS[3]}%, var(--background))` }}\n                >\n                  {bands.map((edge, b) => (\n                    <span\n                      key={b}\n                      className=\"absolute inset-y-0 start-0\"\n                      style={{\n                        width: `${scale(edge)}%`,\n                        zIndex: bands.length - b,\n                        backgroundColor: `color-mix(in oklab, var(--foreground) ${BANDS[Math.min(b, BANDS.length - 1)]}%, var(--background))`,\n                      }}\n                    />\n                  ))}\n                  <span className=\"absolute inset-y-[30%] start-0 z-10 rounded-e-[min(var(--radius),3px)] bg-chart-1\" style={{ width: `${scale(item.value)}%` }} />\n                  {item.target === undefined ? null : <span className=\"absolute inset-y-[12%] z-20 w-0.5 bg-foreground\" style={{ insetInlineStart: `calc(${scale(item.target)}% - 1px)` }} />}\n                </div>\n                <span className=\"w-16 shrink-0 text-end text-sm tabular-nums\">{number(item.value)}</span>\n              </div>\n            </li>\n          );\n        })}\n      </ul>\n      <ChartDataTable {...table} />\n    </div>\n  );\n}\n"
    }
  ],
  "meta": {
    "category": "viz",
    "when": "Showing how a few measures stand against their goals in little space: a dashboard of targets, a scorecard. For one value against a limit with nothing else use radial-progress or progress; to compare many values with each other use bar-chart.",
    "example": "<BulletChart\n  label=\"Quarter targets\"\n  items={[\n    { label: \"Revenue\", value: 270, target: 250, ranges: [150, 225, 300] },\n    { label: \"New customers\", value: 1400, target: 1600, ranges: [1000, 1500, 2000] },\n  ]}\n/>",
    "client": true
  }
}
