{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "distribution-04",
  "description": "This week's distribution against last week's as a dashed line, with both medians and the change at the median and p90.",
  "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",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/distribution/distribution-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { ChartPanelTable } from '@/registry/components/dashboardblocks/chart-panel'\nimport {\n  type DistributionBin,\n  DistributionKey,\n  Histogram,\n  formatDistributionShare,\n  quantileFromBins,\n} from '@/registry/components/dashboardblocks/distribution'\nimport { KPIChange, KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport { formatNumber } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface Distribution4Props extends BlockStateProps {\n  /** Leave out, or pass none, to show the empty state. */\n  current?: number[]\n  /** @default 'This week' */\n  currentLabel?: string\n  description: string\n  /** Bin edges, one more than the counts. */\n  edges: number[]\n  formatter?: (value: number) => string\n  lowerIsBetter?: boolean\n  previous?: number[]\n  /** @default 'Last week' */\n  previousLabel?: string\n  title: string\n}\n\nconst exampleProps: Distribution4Props = {\n  current: [1_240, 6_820, 11_480, 9_360, 5_910, 3_120, 1_640, 910, 480, 260, 140, 90],\n  currentLabel: 'This week',\n  description:\n    'Largest contentful paint this week against last, as a share of each week.',\n  edges: [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6],\n  formatter: (value) => `${formatNumber(value)} s`,\n  lowerIsBetter: true,\n  previous: [620, 3_940, 8_210, 9_870, 7_630, 5_020, 3_110, 1_890, 1_050, 610, 340, 210],\n  previousLabel: 'Last week',\n  title: 'Page load time',\n}\n\nconst Distribution4 = (props: Distribution4Props) => {\n  const {\n    current = [],\n    currentLabel = 'This week',\n    description,\n    edges,\n    formatter = (value) => formatNumber(value),\n    lowerIsBetter = true,\n    previous = [],\n    previousLabel = 'Last week',\n    errorMessage,\n    status,\n    title,\n  } = props\n  const state = getBlockState(status, current)\n\n  const toBins = (counts: number[]): DistributionBin[] =>\n    counts.map((count, index) => ({ count, x0: edges[index], x1: edges[index + 1] }))\n  const bins = toBins(current)\n  const previousBins = toBins(previous)\n  const currentTotal = current.reduce((sum, count) => sum + count, 0) || 1\n  const previousTotal = previous.reduce((sum, count) => sum + count, 0) || 1\n\n  const stats = [\n    { label: 'Median', p: 0.5 },\n    { label: 'p90', p: 0.9 },\n  ].map(({ label, p }) => ({\n    before: quantileFromBins(previousBins, p),\n    label,\n    now: quantileFromBins(bins, p),\n  }))\n  const median = stats[0]\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            <dl className='grid grid-cols-1 gap-x-6 gap-y-4 border-t pt-4 @md:grid-cols-2'>\n              {stats.map(\n                (stat, index) =>\n                  (index === 0 || (state !== 'empty' && state !== 'error')) && (\n                    <div key={stat.label} className='flex flex-col gap-1'>\n                      <dt className='text-muted-foreground text-sm'>\n                        {stat.label}, {currentLabel.toLowerCase()}\n                      </dt>\n                      <dd>\n                        <KPIValue\n                          className={index === 0 ? undefined : 'text-2xl'}\n                          value={stat.now}\n                          format={formatter}\n                        />\n                      </dd>\n                      <dd>\n                        <KPIChange\n                          comparison={previousLabel.toLowerCase()}\n                          goodDirection={lowerIsBetter ? 'down' : 'up'}\n                          previous={stat.before}\n                          value={stat.now}\n                        />\n                      </dd>\n                    </div>\n                  ),\n              )}\n            </dl>\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='bar' />\n                  {currentLabel}\n                </li>\n                <li className='flex items-center gap-1.5'>\n                  <DistributionKey color='var(--color-muted-foreground)' shape='line' />\n                  {previousLabel}\n                </li>\n                <li className='flex items-center gap-1.5'>\n                  <DistributionKey shape='median' />\n                  Medians\n                </li>\n              </ul>\n            )}\n            <Histogram\n              aria-label={`${title} chart`}\n              bins={bins}\n              className='h-60'\n              compare={previous}\n              compareLabel={previousLabel}\n              formatEdge={formatter}\n              label={currentLabel}\n              markers={[\n                { label: currentLabel, value: median.now },\n                { label: previousLabel, value: median.before },\n              ]}\n              tickEvery={2}\n            />\n            <ChartPanelTable\n              caption={`${title}: ${description} Median ${formatter(median.now)} ${currentLabel.toLowerCase()}, ${formatter(median.before)} ${previousLabel.toLowerCase()}.`}\n              columns={[\n                { key: 'label', label: 'Range' },\n                { key: 'current', label: currentLabel },\n                { key: 'previous', label: previousLabel },\n              ]}\n              rows={current.map((count, index) => ({\n                current: formatDistributionShare(count / currentTotal),\n                label: `${formatter(edges[index])}–${formatter(edges[index + 1])}`,\n                previous: formatDistributionShare((previous[index] ?? 0) / previousTotal),\n              }))}\n            />\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  Distribution4,\n  exampleProps as distribution4ExampleProps,\n  type Distribution4Props,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/distribution/distribution-04.tsx"
    }
  ],
  "meta": {
    "shows": "This week's distribution against last week's as a dashed line, with both medians and the change at the median and p90.",
    "size": "half",
    "usedWith": [
      "distribution-01",
      "stat-group-02"
    ],
    "component": "Distribution4",
    "props": "{ current?: number[]; currentLabel?: string; description: string; edges: number[]; formatter?: (value: number) => string; lowerIsBetter?: boolean; previous?: number[]; previousLabel?: string; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
