{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "bullet-chart",
  "type": "registry:component",
  "title": "Bullet chart",
  "description": "Stephen Few’s bullet graph: compare each KPI with its target against poor, satisfactory and good ranges in a single line.",
  "categories": [
    "metrics"
  ],
  "dependencies": [],
  "registryDependencies": [],
  "files": [
    {
      "path": "src/registry/overtrue/bullet-chart.tsx",
      "type": "registry:component",
      "target": "components/overtrue/bullet-chart.tsx",
      "content": "\"use client\";\nimport { useId, type ComponentProps, type ReactNode } from \"react\";\nimport { cn } from \"@/lib/utils\";\n\n/** Ascending thresholds that split the scale into three qualitative ranges. */\nexport type BulletChartRanges = [number, number, number];\nexport type BulletChartQuality = \"poor\" | \"satisfactory\" | \"good\";\n\nexport interface BulletChartItem {\n  id: string;\n  label: string;\n  sublabel?: string;\n  value: number;\n  target: number;\n  /**\n   * Upper bounds of the poor, satisfactory and good ranges, e.g. [600, 800, 1000].\n   * With `invert` the same bounds read good, satisfactory, poor.\n   */\n  ranges?: BulletChartRanges;\n  /** End of the scale. Defaults to the last range, or a rounded value above the data. */\n  max?: number;\n  /** Lower is better, e.g. response time, churn or cost. */\n  invert?: boolean;\n  /** Formats this row's value, target and gap. Overrides the chart's formatter. */\n  valueFormatter?: (value: number) => string;\n}\n\nexport interface BulletChartProps extends Omit<\n  ComponentProps<\"section\">,\n  \"title\" | \"children\"\n> {\n  items: BulletChartItem[];\n  title?: ReactNode;\n  description?: ReactNode;\n  valueFormatter?: (value: number, item: BulletChartItem) => string;\n  size?: \"default\" | \"compact\";\n  /** Paint the measure in the primary color, or by whether it meets its target. */\n  tone?: \"primary\" | \"status\";\n  showLegend?: boolean;\n}\n\nconst numberFormat = new Intl.NumberFormat(\"en-US\", {\n  maximumFractionDigits: 1,\n});\nconst wholePercent = new Intl.NumberFormat(\"en-US\", {\n  style: \"percent\",\n  maximumFractionDigits: 0,\n});\nconst precisePercent = new Intl.NumberFormat(\"en-US\", {\n  style: \"percent\",\n  maximumFractionDigits: 1,\n});\n\nconst bandShade: Record<BulletChartQuality, string> = {\n  poor: \"bg-foreground/[0.15]\",\n  satisfactory: \"bg-foreground/[0.085]\",\n  good: \"bg-foreground/[0.04]\",\n};\n\ntype Status = \"met\" | \"behind\" | \"at-risk\";\n\nconst statusFill: Record<Status, string> = {\n  met: \"bg-emerald-700 dark:bg-emerald-500\",\n  behind: \"bg-amber-700 dark:bg-amber-400\",\n  \"at-risk\": \"bg-red-600 dark:bg-red-400\",\n};\n\nfunction niceCeil(value: number) {\n  if (!(value > 0)) return 1;\n  const magnitude = 10 ** Math.floor(Math.log10(value));\n  const step = [1, 2, 2.5, 5, 10].find((n) => value <= n * magnitude) ?? 10;\n  return step * magnitude;\n}\n\nconst finite = (value: number) => (Number.isFinite(value) ? value : 0);\nconst clampPercent = (value: number, max: number) =>\n  Math.min(100, Math.max(0, (value / max) * 100));\n\nfunction measure(item: BulletChartItem, format: (value: number) => string) {\n  const value = finite(item.value);\n  const target = finite(item.target);\n  const ranges = item.ranges\n    ? ([...item.ranges].map(finite).sort((a, b) => a - b) as BulletChartRanges)\n    : undefined;\n  const peak = Math.max(value, target);\n  const max =\n    item.max && item.max > 0\n      ? item.max\n      : ranges && ranges[2] >= peak && ranges[2] > 0\n        ? ranges[2]\n        : niceCeil(Math.max(peak, ranges?.[2] ?? 0) * 1.1);\n\n  const order: BulletChartQuality[] = item.invert\n    ? [\"good\", \"satisfactory\", \"poor\"]\n    : [\"poor\", \"satisfactory\", \"good\"];\n  const bands = ranges\n    ? ranges.map((end, index) => ({\n        quality: order[index],\n        from: clampPercent(index ? ranges[index - 1] : 0, max),\n        to: clampPercent(end, max),\n      }))\n    : [];\n  const quality = ranges\n    ? order[value < ranges[0] ? 0 : value < ranges[1] ? 1 : 2]\n    : undefined;\n\n  const met = item.invert ? value <= target : value >= target;\n  const status: Status = met\n    ? \"met\"\n    : quality === \"poor\"\n      ? \"at-risk\"\n      : \"behind\";\n  const ratio = target ? value / target : undefined;\n  const gap = value - target;\n  const gapShort =\n    gap === 0\n      ? \"on target\"\n      : `${format(Math.abs(gap))} ${gap > 0 ? \"above\" : \"below\"}`;\n\n  const formattedValue = format(value);\n  const formattedTarget = format(target);\n  const summary = [\n    `${item.label}: ${formattedValue} of ${formattedTarget} target`,\n    ratio !== undefined ? ` (${wholePercent.format(ratio)})` : \"\",\n    item.invert ? \", lower is better\" : \"\",\n    quality ? `, ${quality} range` : \"\",\n    gap === 0 ? \". On target.\" : `. ${gapShort} target.`,\n  ].join(\"\");\n\n  return {\n    value,\n    target,\n    bands,\n    status,\n    ratio,\n    gapShort,\n    formattedValue,\n    formattedTarget,\n    summary,\n    valuePercent: clampPercent(value, max),\n    targetPercent: clampPercent(target, max),\n  };\n}\n\n/** Stephen Few's bullet graph: a measure against its target over qualitative ranges. */\nexport function BulletChart({\n  items,\n  title,\n  description,\n  valueFormatter,\n  size = \"default\",\n  tone = \"primary\",\n  showLegend = true,\n  className,\n  ...props\n}: BulletChartProps) {\n  const titleId = useId();\n  const compact = size === \"compact\";\n  const hasHeader = Boolean(title || description || showLegend);\n\n  return (\n    <section\n      data-slot=\"bullet-chart\"\n      data-size={size}\n      aria-labelledby={title ? titleId : undefined}\n      className={cn(\n        \"@container/bullet rounded-xl border bg-card text-card-foreground shadow-xs\",\n        compact ? \"p-4\" : \"p-5 @lg/bullet:p-6\",\n        className,\n      )}\n      {...props}\n    >\n      {hasHeader && (\n        <header\n          className={cn(\n            \"flex flex-wrap items-start justify-between gap-x-6 gap-y-3\",\n            compact ? \"mb-3\" : \"mb-5\",\n          )}\n        >\n          {(title || description) && (\n            <div className=\"min-w-0\">\n              {title && (\n                <h3\n                  id={titleId}\n                  className={cn(\n                    \"leading-tight font-semibold tracking-tight\",\n                    compact ? \"text-sm\" : \"text-base\",\n                  )}\n                >\n                  {title}\n                </h3>\n              )}\n              {description && (\n                <p\n                  className={cn(\n                    \"mt-1 text-muted-foreground\",\n                    compact ? \"text-xs\" : \"text-sm\",\n                  )}\n                >\n                  {description}\n                </p>\n              )}\n            </div>\n          )}\n          {showLegend && <Legend tone={tone} />}\n        </header>\n      )}\n\n      <ul\n        role=\"list\"\n        className={cn(\n          \"grid grid-cols-[minmax(0,1fr)_auto] gap-x-4\",\n          \"@min-[25rem]/bullet:grid-cols-[fit-content(11rem)_minmax(0,1fr)_auto] @lg/bullet:gap-x-5\",\n          compact ? \"gap-y-0.5\" : \"gap-y-1\",\n        )}\n      >\n        {items.map((item, index) => {\n          const format = item.valueFormatter\n            ? item.valueFormatter\n            : valueFormatter\n              ? (value: number) => valueFormatter(value, item)\n              : (value: number) => numberFormat.format(value);\n          const row = measure(item, format);\n          return (\n            <li\n              key={item.id}\n              tabIndex={0}\n              data-status={row.status}\n              className={cn(\n                \"group relative col-span-2 -mx-3 grid grid-cols-subgrid items-center gap-y-2 rounded-lg px-3 outline-none\",\n                \"transition-colors hover:bg-muted/60 focus-visible:bg-muted/60 focus-visible:ring-2 focus-visible:ring-ring/50\",\n                \"@min-[25rem]/bullet:col-span-3\",\n                compact ? \"py-2\" : \"py-2.5\",\n              )}\n            >\n              <span className=\"sr-only\">{row.summary}</span>\n\n              <div\n                aria-hidden=\"true\"\n                className=\"col-start-1 row-start-1 min-w-0\"\n              >\n                {compact ? (\n                  <p className=\"truncate text-[13px] leading-5\">\n                    <span className=\"font-medium\">{item.label}</span>\n                    {item.sublabel && (\n                      <span className=\"ml-1.5 text-xs text-muted-foreground\">\n                        {item.sublabel}\n                      </span>\n                    )}\n                  </p>\n                ) : (\n                  <>\n                    <p className=\"truncate text-sm leading-5 font-medium\">\n                      {item.label}\n                    </p>\n                    {item.sublabel && (\n                      <p className=\"truncate text-xs leading-4 text-muted-foreground\">\n                        {item.sublabel}\n                      </p>\n                    )}\n                  </>\n                )}\n              </div>\n\n              <div\n                aria-hidden=\"true\"\n                className=\"col-start-2 row-start-1 text-right tabular-nums @min-[25rem]/bullet:col-start-3\"\n              >\n                {compact ? (\n                  <p className=\"text-[13px] leading-5 whitespace-nowrap\">\n                    <span className=\"font-semibold\">{row.formattedValue}</span>\n                    <span className=\"text-muted-foreground\">\n                      {\" \"}\n                      / {row.formattedTarget}\n                    </span>\n                  </p>\n                ) : (\n                  <>\n                    <p className=\"text-sm leading-5 font-semibold whitespace-nowrap\">\n                      {row.formattedValue}\n                    </p>\n                    <p className=\"text-xs leading-4 whitespace-nowrap text-muted-foreground\">\n                      of {row.formattedTarget}\n                    </p>\n                  </>\n                )}\n              </div>\n\n              <div\n                aria-hidden=\"true\"\n                className={cn(\n                  \"relative col-span-2 row-start-2 @min-[25rem]/bullet:col-span-1 @min-[25rem]/bullet:col-start-2 @min-[25rem]/bullet:row-start-1\",\n                  compact ? \"h-4\" : \"h-6\",\n                )}\n              >\n                <div\n                  className={cn(\n                    \"absolute inset-0 overflow-hidden rounded-[3px]\",\n                    row.bands.length ? \"bg-foreground/[0.02]\" : \"bg-muted\",\n                  )}\n                >\n                  {row.bands.map((band) => (\n                    <div\n                      key={band.quality}\n                      className={cn(\n                        \"absolute inset-y-0\",\n                        bandShade[band.quality],\n                      )}\n                      style={{\n                        left: `${band.from}%`,\n                        width: `${band.to - band.from}%`,\n                      }}\n                    />\n                  ))}\n                </div>\n                <div\n                  className={cn(\n                    \"absolute left-0 origin-left rounded-[2px] transition-[width,transform,background-color] duration-700 ease-[cubic-bezier(0.22,1,0.36,1)] starting:[transform:scaleX(0)] motion-reduce:transition-none\",\n                    compact ? \"top-[5px] h-1.5\" : \"top-2 h-2\",\n                    tone === \"status\" ? statusFill[row.status] : \"bg-primary\",\n                  )}\n                  style={{\n                    width: `${row.valuePercent}%`,\n                    transitionDelay: `${80 + index * 70}ms`,\n                  }}\n                />\n                <div\n                  className={cn(\n                    \"absolute w-0.5 -translate-x-1/2 rounded-full bg-foreground transition-[left,opacity] duration-500 starting:opacity-0 motion-reduce:transition-none\",\n                    compact ? \"inset-y-0.5\" : \"inset-y-1\",\n                  )}\n                  style={{\n                    left: `${row.targetPercent}%`,\n                    transitionDelay: `${360 + index * 70}ms`,\n                  }}\n                />\n\n                {/* The readout slides along with the measure: at X% it is shifted\n                    by X% of its own width, so it never leaves the track and its\n                    caret always points at the end of the bar. */}\n                <div\n                  className={cn(\n                    \"pointer-events-none absolute bottom-full z-10 mb-2.5 translate-y-1 rounded-md bg-foreground px-2.5 py-1.5 font-mono text-[11px] leading-4 whitespace-nowrap text-background tabular-nums opacity-0 shadow-lg shadow-black/10 transition-[opacity,translate] duration-150\",\n                    \"group-hover:translate-y-0 group-hover:opacity-100 group-focus-visible:translate-y-0 group-focus-visible:opacity-100 motion-reduce:transition-none\",\n                  )}\n                  style={{\n                    left: `${row.valuePercent}%`,\n                    transform: `translateX(-${row.valuePercent}%)`,\n                  }}\n                >\n                  <p>\n                    <span className=\"font-semibold\">{row.formattedValue}</span>\n                    <span className=\"opacity-60\"> / {row.formattedTarget}</span>\n                  </p>\n                  <p>\n                    <span>\n                      {row.ratio !== undefined\n                        ? precisePercent.format(row.ratio)\n                        : \"—\"}\n                    </span>\n                    <span className=\"opacity-40\"> · </span>\n                    <span\n                      className={\n                        row.status === \"met\"\n                          ? \"text-emerald-400 dark:text-emerald-700\"\n                          : row.status === \"behind\"\n                            ? \"text-amber-300 dark:text-amber-800\"\n                            : \"text-red-400 dark:text-red-700\"\n                      }\n                    >\n                      {row.gapShort}\n                    </span>\n                  </p>\n                  <span\n                    className=\"absolute top-full size-2 -translate-x-1/2 -translate-y-1/2 rotate-45 rounded-[1px] bg-foreground\"\n                    style={{\n                      left: `clamp(8px, ${row.valuePercent}%, calc(100% - 8px))`,\n                    }}\n                  />\n                </div>\n              </div>\n            </li>\n          );\n        })}\n      </ul>\n    </section>\n  );\n}\n\nfunction Legend({ tone }: { tone: \"primary\" | \"status\" }) {\n  return (\n    <ul className=\"flex flex-wrap items-center gap-x-4 gap-y-1.5 text-xs text-muted-foreground\">\n      <li className=\"flex items-center gap-1.5\">\n        <span\n          aria-hidden=\"true\"\n          className=\"flex h-2.5 w-9 overflow-hidden rounded-[2px]\"\n        >\n          <span className={cn(\"flex-1\", bandShade.poor)} />\n          <span className={cn(\"flex-1\", bandShade.satisfactory)} />\n          <span className={cn(\"flex-1\", bandShade.good)} />\n        </span>\n        Poor → good\n      </li>\n      {tone === \"status\" ? (\n        (\n          [\n            [\"met\", \"Met\"],\n            [\"behind\", \"Behind\"],\n            [\"at-risk\", \"At risk\"],\n          ] as const\n        ).map(([status, label]) => (\n          <li key={status} className=\"flex items-center gap-1.5\">\n            <span\n              aria-hidden=\"true\"\n              className={cn(\"h-1.5 w-3 rounded-[1px]\", statusFill[status])}\n            />\n            {label}\n          </li>\n        ))\n      ) : (\n        <li className=\"flex items-center gap-1.5\">\n          <span\n            aria-hidden=\"true\"\n            className=\"h-1.5 w-3 rounded-[1px] bg-primary\"\n          />\n          Actual\n        </li>\n      )}\n      <li className=\"flex items-center gap-1.5\">\n        <span\n          aria-hidden=\"true\"\n          className=\"h-3 w-0.5 rounded-full bg-foreground\"\n        />\n        Target\n      </li>\n    </ul>\n  );\n}\n"
    }
  ],
  "docs": "Documentation: https://ui.overtrue.xyz/components/bullet-chart. Requires an initialized shadcn/ui project with Tailwind CSS v4."
}
