{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "breakdown-03",
  "description": "A 10 by 10 grid of squares where each square is 1% of the total, coloured by category.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/breakdown.json",
    "https://www.dashboardblocks.com/r/radix/chart.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/breakdown/breakdown-03.tsx",
      "content": "'use client'\n\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  BREAKDOWN_OTHER_COLOR,\n  BreakdownKey,\n  type BreakdownSegment,\n  formatShare,\n  getTotal,\n} from '@/registry/components/dashboardblocks/breakdown'\nimport {\n  ChartPlaceholder,\n  ChartTooltipFrame,\n  ChartTooltipItem,\n} from '@/registry/components/dashboardblocks/chart'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport { formatNumber, joinDetails } from '@/registry/lib/dashboardblocks/format'\nimport { useState } from 'react'\nimport { Scatter, ScatterChart, Tooltip, XAxis, YAxis } from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\nimport { cn } from '@/lib/utils'\n\ninterface Breakdown3Props extends BlockStateProps {\n  description: string\n  /** Leave out, or pass none, to show the empty state. */\n  segments?: BreakdownSegment[]\n  title: string\n}\n\nconst exampleProps: Breakdown3Props = {\n  description: 'Active workspaces on each plan.',\n  segments: [\n    { color: 'var(--color-chart-2)', label: 'Free', value: 5_840 },\n    { color: 'var(--color-chart-3)', label: 'Pro', value: 2_310 },\n    { color: 'var(--color-chart-4)', label: 'Team', value: 1_120 },\n    { color: BREAKDOWN_OTHER_COLOR, label: 'Enterprise', value: 430 },\n  ],\n  title: 'Plan distribution',\n}\n\nconst SIDE = 10\nconst CELLS = SIDE * SIDE\nconst GRID_PX = 176\nconst CELL_PX = GRID_PX / SIDE\nconst GAP_PX = 2\n\ninterface Square {\n  segment: number\n  x: number\n  y: number\n}\n\nfunction toCells(segments: BreakdownSegment[]) {\n  const total = getTotal(segments)\n  if (total === 0) return segments.map(() => 0)\n  const exact = segments.map((segment) => (Math.max(0, segment.value) / total) * CELLS)\n  const counts = exact.map(Math.floor)\n  let remaining = CELLS - counts.reduce((sum, count) => sum + count, 0)\n  const order = exact\n    .map((value, index) => ({ index, remainder: value - Math.floor(value) }))\n    .sort((a, b) => b.remainder - a.remainder)\n  for (const { index } of order) {\n    if (remaining <= 0) break\n    counts[index] += 1\n    remaining -= 1\n  }\n  return counts\n}\n\nconst Breakdown3 = (props: Breakdown3Props) => {\n  const { description, segments = [], errorMessage, status, title } = props\n  const [activeIndex, setActiveIndex] = useState<number | null>(null)\n  const state = getBlockState(status, segments)\n  const total = getTotal(segments)\n  const squares: Square[] = toCells(segments)\n    .flatMap((count, segment) => Array.from({ length: count }, () => segment))\n    .map((segment, cell) => ({ segment, x: cell % SIDE, y: Math.floor(cell / SIDE) }))\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 items-center gap-6 border-t pt-4 @sm:flex-row @sm:items-start'>\n              {state ? (\n                <ChartPlaceholder className='w-44 shrink-0' height={GRID_PX} />\n              ) : (\n                <div\n                  role='img'\n                  aria-label={`${title}: ${segments\n                    .map(\n                      (segment) =>\n                        `${segment.label} ${formatShare(segment.value, total)}`,\n                    )\n                    .join(', ')}`}\n                  className='shrink-0'\n                  onPointerLeave={() => setActiveIndex(null)}\n                >\n                  <ScatterChart\n                    accessibilityLayer={false}\n                    height={GRID_PX}\n                    margin={{ bottom: 0, left: 0, right: 0, top: 0 }}\n                    width={GRID_PX}\n                  >\n                    <XAxis dataKey='x' domain={[-0.5, SIDE - 0.5]} hide type='number' />\n                    <YAxis\n                      dataKey='y'\n                      domain={[-0.5, SIDE - 0.5]}\n                      hide\n                      reversed\n                      type='number'\n                    />\n                    <Tooltip\n                      content={({ active: shown, payload }) => {\n                        const square = payload?.[0]?.payload as Square | undefined\n                        const segment = square ? segments[square.segment] : undefined\n                        if (!shown || !segment) return null\n                        return (\n                          <ChartTooltipFrame>\n                            <span className='font-medium'>{segment.label}</span>\n                            <ChartTooltipItem\n                              color={segment.color}\n                              value={joinDetails(\n                                formatNumber(segment.value),\n                                formatShare(segment.value, total),\n                              )}\n                            />\n                          </ChartTooltipFrame>\n                        )\n                      }}\n                      cursor={false}\n                    />\n                    {segments.map((segment, index) => (\n                      <Scatter\n                        key={segment.label}\n                        data={squares.filter((square) => square.segment === index)}\n                        fill={segment.color}\n                        onMouseEnter={() => setActiveIndex(index)}\n                        shape={({ cx = 0, cy = 0 }: { cx?: number; cy?: number }) => (\n                          <g>\n                            <rect\n                              x={cx - CELL_PX / 2}\n                              y={cy - CELL_PX / 2}\n                              width={CELL_PX}\n                              height={CELL_PX}\n                              fill='transparent'\n                            />\n                            <rect\n                              x={cx - CELL_PX / 2 + GAP_PX / 2}\n                              y={cy - CELL_PX / 2 + GAP_PX / 2}\n                              width={CELL_PX - GAP_PX}\n                              height={CELL_PX - GAP_PX}\n                              rx={3}\n                              fill={segment.color}\n                              opacity={\n                                activeIndex === null || activeIndex === index ? 1 : 0.35\n                              }\n                            />\n                          </g>\n                        )}\n                      />\n                    ))}\n                  </ScatterChart>\n                </div>\n              )}\n              <div className='flex w-full min-w-0 flex-col gap-3'>\n                <div className='flex flex-col gap-1'>\n                  <KPIValue value={total} format={(value) => formatNumber(value)} />\n                  <BlockReady className='text-sm'>\n                    <p className='text-muted-foreground text-sm'>\n                      Each square is 1% of the total.\n                    </p>\n                  </BlockReady>\n                </div>\n                {state === 'loading' && <BlockStateRows rows={4} />}\n                {!state && (\n                  <ul className='flex flex-col text-sm'>\n                    {segments.map((segment, index) => (\n                      <li\n                        key={segment.label}\n                        className={cn(\n                          'flex items-center gap-2 border-b py-2.5 transition-colors last:border-b-0',\n                          activeIndex === index && 'bg-muted/50',\n                        )}\n                      >\n                        <BreakdownKey color={segment.color} />\n                        <span className='min-w-0 flex-1 truncate'>{segment.label}</span>\n                        <span className='tabular-nums'>\n                          {formatNumber(segment.value)}\n                        </span>\n                        <span className='text-muted-foreground w-10 text-right tabular-nums'>\n                          {formatShare(segment.value, total)}\n                        </span>\n                      </li>\n                    ))}\n                  </ul>\n                )}\n              </div>\n            </div>\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { Breakdown3, exampleProps as breakdown3ExampleProps, type Breakdown3Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/breakdown/breakdown-03.tsx"
    }
  ],
  "meta": {
    "shows": "A 10 by 10 grid of squares where each square is 1% of the total, coloured by category.",
    "size": "half",
    "usedWith": [
      "breakdown-01",
      "chart-panel-05"
    ],
    "component": "Breakdown3",
    "props": "{ description: string; segments?: { color: string; label: string; value: number }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
