{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "distribution-03",
  "description": "The median, mean and percentiles of a set of values, with a note when a long tail pulls the mean away.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/distribution.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/distribution/distribution-03.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  SkeletonText,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  BoxPlot,\n  DistributionKey,\n  getNiceTicks,\n  quantile,\n  summarize,\n} from '@/registry/components/dashboardblocks/distribution'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  formatCurrency,\n  formatNumber,\n  formatPercent,\n} from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface Distribution3Props extends BlockStateProps {\n  description: string\n  formatter?: (value: number) => string\n  title: string\n  /** @default 'orders' */\n  unit?: string\n  /** Raw values, in any order. Leave out, or pass none, to show the empty state. */\n  values?: number[]\n}\n\nconst exampleProps: Distribution3Props = {\n  description: 'The value of every order placed in the last 7 days.',\n  formatter: (value) => formatCurrency(value),\n  title: 'Order value',\n  unit: 'orders',\n  values: Array.from({ length: 420 }, (_, index) => {\n    const u = ((index * 2_654_435_761 + 12_345) % 4_294_967_296) / 4_294_967_296\n    return Math.round(\n      18 + 52 * Math.exp(1.05 * Math.sqrt(-2 * Math.log(1 - u * 0.999)) - 0.9),\n    )\n  }),\n}\n\nconst PERCENTILES = [0.1, 0.25, 0.5, 0.75, 0.9]\n\nconst Distribution3 = (props: Distribution3Props) => {\n  const {\n    description,\n    formatter = (value) => formatNumber(value),\n    errorMessage,\n    status,\n    title,\n    unit = 'orders',\n    values = [],\n  } = props\n  const state = getBlockState(status, values)\n  const blank = state === 'empty' || state === 'error'\n\n  const sorted = [...values].sort((a, b) => a - b)\n  const summary = summarize(sorted)\n  const [p10, p25, p50, p75, p90] = PERCENTILES.map((p) => quantile(sorted, p))\n  const skew = summary.median > 0 ? summary.mean / summary.median - 1 : 0\n  const ticks = getNiceTicks(0, Math.max(p90, summary.mean), 4)\n  const domain: [number, number] = [ticks[0], ticks[ticks.length - 1]]\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-2 gap-x-6 gap-y-4 border-t pt-4 @md:grid-cols-3'>\n              <div className='col-span-2 flex flex-col gap-1 @md:col-span-1'>\n                <dt className='text-muted-foreground text-sm'>Median</dt>\n                <dd>\n                  <KPIValue value={summary.median} format={formatter} />\n                </dd>\n              </div>\n              {!blank && (\n                <div className='flex flex-col gap-1'>\n                  <dt className='text-muted-foreground text-sm'>Mean</dt>\n                  <dd>\n                    <KPIValue\n                      className='text-2xl'\n                      value={summary.mean}\n                      format={formatter}\n                    />\n                  </dd>\n                </div>\n              )}\n              {!blank && (\n                <div className='flex flex-col gap-1'>\n                  <dt className='text-muted-foreground text-sm'>\n                    {unit.charAt(0).toUpperCase()}\n                    {unit.slice(1)}\n                  </dt>\n                  <dd>\n                    <KPIValue\n                      className='text-2xl'\n                      value={summary.count}\n                      format={(value) => formatNumber(value)}\n                    />\n                  </dd>\n                </div>\n              )}\n            </dl>\n            <BlockReady className='text-sm'>\n              <p className='text-muted-foreground text-sm'>\n                {skew > 0.1\n                  ? `The mean is ${formatPercent(skew, { maximumFractionDigits: 0 })} above the median: a few large ${unit} pull it up, so the median is the typical one.`\n                  : skew < -0.1\n                    ? `The mean is ${formatPercent(-skew, { maximumFractionDigits: 0 })} below the median: a few small ${unit} pull it down, so the median is the typical one.`\n                    : 'The mean and median are close, so the spread is fairly even.'}\n              </p>\n            </BlockReady>\n            {!blank && (\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='mean' />\n                  Mean\n                </li>\n                <li className='flex items-center gap-1.5'>\n                  <DistributionKey shape='whisker' />\n                  10th–90th percentile\n                </li>\n              </ul>\n            )}\n            <BoxPlot\n              aria-label={`${title} chart`}\n              domain={domain}\n              format={formatter}\n              rowHeight={56}\n              rows={[\n                {\n                  count: summary.count,\n                  high: p90,\n                  label: title,\n                  low: p10,\n                  mean: summary.mean,\n                  median: p50,\n                  q1: p25,\n                  q3: p75,\n                },\n              ]}\n              ticks={ticks}\n              unit={unit}\n              whiskerLabel='10th–90th percentile'\n            />\n            {!blank && (\n              <dl className='grid grid-cols-5 border-t pt-3'>\n                {[p10, p25, p50, p75, p90].map((value, index) => (\n                  <div key={PERCENTILES[index]} className='flex flex-col gap-0.5'>\n                    <dt className='text-muted-foreground text-xs'>\n                      p{Math.round(PERCENTILES[index] * 100)}\n                    </dt>\n                    <dd className='text-sm tabular-nums'>\n                      {state ? <SkeletonText className='w-[3em]' /> : formatter(value)}\n                    </dd>\n                  </div>\n                ))}\n              </dl>\n            )}\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  Distribution3,\n  exampleProps as distribution3ExampleProps,\n  type Distribution3Props,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/distribution/distribution-03.tsx"
    }
  ],
  "meta": {
    "shows": "The median, mean and percentiles of a set of values, with a note when a long tail pulls the mean away.",
    "size": "half",
    "usedWith": [
      "distribution-01",
      "stat-group-01"
    ],
    "component": "Distribution3",
    "props": "{ description: string; formatter?: (value: number) => string; title: string; unit?: string; values?: number[]; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
