{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "segmented-bar",
  "title": "Segmented bar",
  "author": "superzero11 <https://github.com/superzero11/uiset>",
  "description": "One whole split into up to six parts, with shares printed where they fit: the readable answer to a donut.",
  "dependencies": [
    "@ui-sdk/core@0.1.0-beta.5",
    "@ui-sdk/react@0.1.0-beta.5"
  ],
  "registryDependencies": [
    "https://uiset.dev/r/uiset.json",
    "https://uiset.dev/r/chart.json"
  ],
  "files": [
    {
      "path": "registry/uiset/ui/segmented-bar.tsx",
      "content": "\"use client\"\n\nimport type { ChartSelectChange } from \"./chart/points\"\nimport type { ReactNode } from \"react\"\nimport { ChartFrame, reactPlot } from \"./chart\"\nimport {\n  segmentedBarFrame,\n  segmentedBarHeight,\n  segmentedBarPlot,\n  type SegmentedBarOptions,\n  type SegmentedBarPart,\n} from \"./chart/plots/segmented-bar\"\n\ntype SegmentedBarProps = SegmentedBarOptions & {\n  title: string\n  description?: ReactNode\n  selected?: string | null\n  onSelect?: (key: string | null, details: ChartSelectChange) => void\n  actions?: ReactNode\n  className?: string\n}\n\n/** One whole split into parts, read at a glance: what a donut is usually asked to do. */\nfunction SegmentedBar({ title, description, selected, onSelect, actions, className, ...options }: SegmentedBarProps) {\n  const frame = segmentedBarFrame(options)\n  return (\n    <ChartFrame\n      title={title}\n      description={description}\n      height={segmentedBarHeight(options)}\n      className={[frame.className, className].filter(Boolean).join(\" \")}\n      series={frame.series}\n      table={frame.table}\n      rows={frame.rows}\n      selected={selected}\n      onSelect={onSelect}\n      actions={actions}\n      plot={(context) => reactPlot(segmentedBarPlot(options, context))}\n    />\n  )\n}\n\nexport { SegmentedBar }\nexport type { SegmentedBarPart, SegmentedBarProps }\n",
      "type": "registry:ui",
      "target": "@ui/segmented-bar.tsx"
    },
    {
      "path": "registry/uiset/ui/chart/plots/segmented-bar.ts",
      "content": "// The segmented bar's geometry: one whole split into parts.\nimport { seriesColor, focusKey, type ChartFrameData, type ChartGeometry, type ChartPlotContext, type ChartPoint } from \"../plot\"\nimport { formatValue, numberFormat } from \"../scale\"\nimport { tooltipRow, v } from \"../vnode\"\n\ntype SegmentedBarPart = { key: string; label: string; value: number }\n\ntype SegmentedBarOptions = {\n  /** Up to six parts of one whole, in the order they should read. Fold a long tail into \"Other\". */\n  parts: readonly SegmentedBarPart[]\n  format?: (value: number) => string\n  /** Reads numbers in this locale; pass it when rendering on a server. */\n  locale?: string\n  /**\n   * \"tall\" (28px) prints each part's share inside it where it fits; \"thin\" (8px) is for a\n   * bar beside its figures, like in a table row.\n   */\n  variant?: \"tall\" | \"thin\"\n}\n\nconst GAP = 2\n\nfunction segmentedParts({ parts, format: formatOption, locale, variant = \"tall\" }: SegmentedBarOptions) {\n  const series = parts.map((p) => ({ key: p.key, label: p.label, shape: \"rect\" as const }))\n  const total = parts.reduce((sum, p) => sum + Math.max(0, p.value), 0)\n  const format = numberFormat(locale, formatOption)\n  const share = (n: number) =>\n    new Intl.NumberFormat(locale, { style: \"percent\", maximumFractionDigits: total && n / total < 0.1 ? 1 : 0 }).format(total ? n / total : 0)\n  const thick = variant === \"tall\" ? 28 : 8\n  return { series, total, format, share, thick, height: thick + 16 }\n}\n\n/** The plot's height: the bar and room around it. */\nconst segmentedBarHeight = (options: SegmentedBarOptions) => segmentedParts(options).height\n\nfunction segmentedBarFrame(options: SegmentedBarOptions): ChartFrameData {\n  const { parts } = options\n  const { series, format, share } = segmentedParts(options)\n  return {\n    className: \"segmented-bar\",\n    series,\n    table: {\n      head: [\"\", \"Amount\", \"Share\"],\n      numeric: [false, true, true],\n      rows: parts.map((p) => ({ key: p.key, cells: [p.label, formatValue(p.value, format), share(p.value)] })),\n    },\n    rows: () => parts.map((p) => ({ part: p.label, amount: formatValue(p.value, format), share: share(p.value) })),\n  }\n}\n\nfunction segmentedBarPlot(options: SegmentedBarOptions, { width, shown, active, selected: pinned }: ChartPlotContext): ChartGeometry {\n  const { parts, variant = \"tall\" } = options\n  const { series, total, format, share, thick, height } = segmentedParts(options)\n  const shownParts = parts.filter((p) => p.value > 0)\n  const gaps = GAP * Math.max(0, shownParts.length - 1)\n  const usable = Math.max(0, width - gaps)\n  const focus = focusKey(active, pinned, (key) => shownParts.some((p) => p.key === key))\n  let x = 0\n  const blocks = shownParts.map((p) => {\n    const w = total ? (p.value / total) * usable : 0\n    const block = { p, x, w }\n    x += w + GAP\n    return block\n  })\n  const y = 8\n  const points: ChartPoint[] = blocks.map(({ p, x: bx, w }) => ({\n    key: p.key,\n    label: `${p.label}: ${formatValue(p.value, format)}, ${share(p.value)}`,\n    x: bx,\n    y: 0,\n    width: Math.max(w, 1),\n    height,\n    anchor: { x: bx + w / 2, y },\n  }))\n  const svg = blocks.map(({ p, x: bx, w }, i) => {\n    const first = i === 0\n    const last = i === blocks.length - 1\n    const r = variant === \"thin\" ? thick / 2 : 4\n    const text = share(p.value)\n    const fits = variant === \"tall\" && text.length * 7.2 + 16 <= w\n    return v(\n      \"g\",\n      {\n        key: p.key,\n        className: \"chart-series\",\n        \"data-dim\": !shown(p.key) || (focus !== null && focus !== p.key) ? \"\" : undefined,\n        style: { \"--series\": seriesColor(series, p.key) },\n      },\n      v(\"rect\", { className: \"chart-bar\", x: bx, y, width: Math.max(0, w), height: thick, rx: variant === \"thin\" ? (first || last ? r : 0) : r }),\n      fits && v(\"text\", { className: \"segmented-bar-label\", x: bx + 8, y: y + thick / 2, dy: \"0.34em\" }, text),\n    )\n  })\n  return {\n    svg,\n    points,\n    tooltip: (key) => {\n      const p = parts.find((part) => part.key === key)!\n      return [tooltipRow(seriesColor(series, p.key), `${formatValue(p.value, format)} · ${share(p.value)}`, p.label, \"rect\")]\n    },\n  }\n}\n\nexport { segmentedBarFrame, segmentedBarHeight, segmentedBarPlot }\nexport type { SegmentedBarOptions, SegmentedBarPart }\n",
      "type": "registry:file",
      "target": "@ui/chart/plots/segmented-bar.ts"
    }
  ],
  "type": "registry:ui"
}