{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "distribution-02",
  "description": "A box and whiskers per group on a shared axis, to compare spread and median across groups.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/chart-panel.json",
    "https://www.dashboardblocks.com/r/radix/distribution.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/distribution/distribution-02.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  BlockStateText,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { ChartPanelTable } from '@/registry/components/dashboardblocks/chart-panel'\nimport {\n  BoxPlot,\n  DistributionKey,\n  getNiceTicks,\n  quantile,\n} from '@/registry/components/dashboardblocks/distribution'\nimport { formatNumber } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface DistributionGroup {\n  count: number\n  high: number\n  label: string\n  low: number\n  median: number\n  q1: number\n  q3: number\n}\n\ninterface Distribution2Props extends BlockStateProps {\n  description: string\n  formatter?: (value: number) => string\n  /** Leave out, or pass none, to show the empty state. */\n  groups?: DistributionGroup[]\n  tickFormatter?: (value: number) => string\n  whiskerLabel?: string\n  title: string\n  /** @default 'tickets' */\n  unit?: string\n}\n\nfunction sample(count: number, scale: number, seed: number) {\n  return Array.from({ length: count }, (_, index) => {\n    const u = ((index * 2_654_435_761 + seed * 40_503) % 4_294_967_296) / 4_294_967_296\n    return scale * -Math.log(1 - u * 0.985)\n  }).sort((a, b) => a - b)\n}\n\nconst round = (value: number) => Math.round(value * 100) / 100\n\nconst exampleProps: Distribution2Props = {\n  description: 'Hours from opened to resolved for each team over the last 30 days.',\n  formatter: (value) => {\n    const digits = value < 10 ? 1 : 0\n    return `${formatNumber(value, { maximumFractionDigits: digits, minimumFractionDigits: digits })} h`\n  },\n  groups: [\n    { count: 386, label: 'Billing', scale: 3.1 },\n    { count: 512, label: 'Account', scale: 4.6 },\n    { count: 274, label: 'Onboarding', scale: 6.2 },\n    { count: 628, label: 'Technical', scale: 9.8 },\n  ].map(({ count, label, scale }, seed) => {\n    const values = sample(count, scale, seed + 1)\n    return {\n      count,\n      high: round(quantile(values, 0.95)),\n      label,\n      low: round(quantile(values, 0.05)),\n      median: round(quantile(values, 0.5)),\n      q1: round(quantile(values, 0.25)),\n      q3: round(quantile(values, 0.75)),\n    }\n  }),\n  tickFormatter: (value) => `${formatNumber(value)} h`,\n  title: 'Resolution time by team',\n  unit: 'tickets',\n}\n\nconst Distribution2 = (props: Distribution2Props) => {\n  const {\n    description,\n    formatter = (value) => formatNumber(value),\n    groups = [],\n    errorMessage,\n    status,\n    tickFormatter = formatter,\n    title,\n    unit = 'tickets',\n    whiskerLabel = '5th–95th percentile',\n  } = props\n  const state = getBlockState(status, groups)\n  const ticks = getNiceTicks(0, Math.max(0, ...groups.map((group) => group.high)), 5)\n  const domain: [number, number] = [ticks[0], ticks[ticks.length - 1]]\n  const slowest = groups.reduce<DistributionGroup | undefined>(\n    (best, group) => (best && best.median >= group.median ? best : group),\n    undefined,\n  )\n  const fastest = groups.reduce<DistributionGroup | undefined>(\n    (best, group) => (best && best.median <= group.median ? best : group),\n    undefined,\n  )\n\n  return (\n    <BlockStateProvider errorMessage={errorMessage} state={state}>\n      <Card>\n        <CardContent>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex flex-col gap-0.5'>\n              <h2 className='font-semibold tracking-tight'>{title}</h2>\n              <p className='text-muted-foreground text-sm'>{description}</p>\n            </header>\n            <div className='flex flex-col gap-3 border-t pt-4'>\n              {state ? (\n                <BlockStateText className='text-sm' />\n              ) : (\n                slowest &&\n                fastest &&\n                slowest !== fastest && (\n                  <p className='text-muted-foreground text-sm'>\n                    <span className='text-foreground font-medium'>{slowest.label}</span>{' '}\n                    is slowest, with half its {unit} taking over{' '}\n                    {formatter(slowest.median)}. {fastest.label} is quickest, at{' '}\n                    {formatter(fastest.median)}.\n                  </p>\n                )\n              )}\n              {state !== 'empty' && state !== 'error' && (\n                <ul className='text-muted-foreground flex flex-wrap items-center gap-x-4 gap-y-1 text-xs'>\n                  <li className='flex items-center gap-1.5'>\n                    <DistributionKey shape='box' />\n                    Middle half\n                  </li>\n                  <li className='flex items-center gap-1.5'>\n                    <DistributionKey shape='median' />\n                    Median\n                  </li>\n                  <li className='flex items-center gap-1.5'>\n                    <DistributionKey shape='whisker' />\n                    {whiskerLabel}\n                  </li>\n                </ul>\n              )}\n            </div>\n            <BoxPlot\n              aria-label={`${title} chart`}\n              domain={domain}\n              format={formatter}\n              rows={groups}\n              tickFormat={tickFormatter}\n              ticks={ticks}\n              unit={unit}\n              whiskerLabel={whiskerLabel}\n            />\n            <ChartPanelTable\n              caption={`${title}: ${description}`}\n              columns={[\n                { key: 'label', label: 'Group' },\n                { format: formatter, key: 'low', label: 'Low' },\n                { format: formatter, key: 'q1', label: '25th percentile' },\n                { format: formatter, key: 'median', label: 'Median' },\n                { format: formatter, key: 'q3', label: '75th percentile' },\n                { format: formatter, key: 'high', label: 'High' },\n                { key: 'count', label: unit.charAt(0).toUpperCase() + unit.slice(1) },\n              ]}\n              rows={groups}\n            />\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  Distribution2,\n  exampleProps as distribution2ExampleProps,\n  type Distribution2Props,\n  type DistributionGroup,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/distribution/distribution-02.tsx"
    }
  ],
  "meta": {
    "shows": "A box and whiskers per group on a shared axis, to compare spread and median across groups.",
    "size": "half",
    "usedWith": [
      "distribution-03",
      "data-table-04"
    ],
    "component": "Distribution2",
    "props": "{ description: string; formatter?: (value: number) => string; groups?: { count: number; high: number; label: string; low: number; median: number; q1: number; q3: number }[]; tickFormatter?: (value: number) => string; whiskerLabel?: string; title: string; unit?: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
