{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "breakdown-04",
  "description": "How each category's share of a total moved between two periods, as a dot for each period joined by a line.",
  "dependencies": [
    "recharts"
  ],
  "registryDependencies": [
    "https://www.dashboardblocks.com/r/radix/change-amount.json",
    "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/chart-panel.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/breakdown/breakdown-04.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  BlockStateText,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { BreakdownLegend } from '@/registry/components/dashboardblocks/breakdown'\nimport { ChangeAmount } from '@/registry/components/dashboardblocks/change-amount'\nimport {\n  chartBarCursor,\n  ChartPlaceholder,\n  ChartTooltipFrame,\n  ChartTooltipItem,\n} from '@/registry/components/dashboardblocks/chart'\nimport { ChartPanelTable } from '@/registry/components/dashboardblocks/chart-panel'\nimport {\n  formatNumber,\n  formatPercent,\n  formatPoints,\n  formatSigned,\n} from '@/registry/lib/dashboardblocks/format'\nimport {\n  Bar,\n  BarChart,\n  ResponsiveContainer,\n  Tooltip,\n  XAxis,\n  YAxis,\n  type YAxisTickContentProps,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface ShareShift {\n  current: number\n  label: string\n  previous: number\n}\n\ninterface Breakdown4Props extends BlockStateProps {\n  currentLabel: string\n  description: string\n  /** Leave out, or pass none, to show the empty state. */\n  items?: ShareShift[]\n  previousLabel: string\n  title: string\n}\n\nconst exampleProps: Breakdown4Props = {\n  currentLabel: 'This quarter',\n  description:\n    'Share of new signups from each acquisition channel, against last quarter.',\n  items: [\n    { current: 34.2, label: 'Organic search', previous: 29.8 },\n    { current: 22.6, label: 'Direct', previous: 24.1 },\n    { current: 18.9, label: 'Paid social', previous: 13.4 },\n    { current: 14.1, label: 'Referral', previous: 17.2 },\n    { current: 10.2, label: 'Email', previous: 15.5 },\n  ],\n  previousLabel: 'Last quarter',\n  title: 'Channel share shift',\n}\n\nconst CURRENT_COLOR = 'var(--color-chart-2)'\nconst PREVIOUS_COLOR = 'var(--color-muted-foreground)'\nconst ROW_HEIGHT_PX = 44\nconst DOT_RADIUS_PX = 5\nconst LABEL_AXIS_PX = 116\n\ninterface ShiftRow extends ShareShift {\n  delta: number\n  range: [number, number]\n}\n\nconst formatShare = (value: number) =>\n  formatPercent(value / 100, { minimumFractionDigits: 1 })\n\nconst describeShift = (delta: number) =>\n  delta === 0\n    ? 'No change'\n    : `${delta > 0 ? 'Up' : 'Down'} ${formatPoints(Math.abs(delta))}`\n\ninterface ShiftShapeProps {\n  height?: number\n  payload?: ShiftRow\n  width?: number\n  x?: number\n  y?: number\n}\n\nconst ShiftShape = ({\n  height = 0,\n  payload,\n  width = 0,\n  x = 0,\n  y = 0,\n}: ShiftShapeProps) => {\n  if (!payload) return null\n  const [low, high] = payload.range\n  const at = (value: number) =>\n    high > low ? x + ((value - low) / (high - low)) * width : x\n  const middle = y + height / 2\n  return (\n    <g>\n      <line\n        x1={x}\n        x2={x + width}\n        y1={middle}\n        y2={middle}\n        stroke={CURRENT_COLOR}\n        strokeOpacity={0.35}\n        strokeWidth={3}\n        strokeLinecap='round'\n      />\n      <circle\n        cx={at(payload.previous)}\n        cy={middle}\n        r={DOT_RADIUS_PX}\n        fill={PREVIOUS_COLOR}\n        stroke='var(--color-background)'\n        strokeWidth={2}\n      />\n      <circle\n        cx={at(payload.current)}\n        cy={middle}\n        r={DOT_RADIUS_PX}\n        fill={CURRENT_COLOR}\n        stroke='var(--color-background)'\n        strokeWidth={2}\n      />\n    </g>\n  )\n}\n\nconst Breakdown4 = (props: Breakdown4Props) => {\n  const {\n    currentLabel,\n    description,\n    items = [],\n    previousLabel,\n    errorMessage,\n    status,\n    title,\n  } = props\n  const state = getBlockState(status, items)\n  const rows: ShiftRow[] = items.map((item) => ({\n    ...item,\n    delta: Math.round((item.current - item.previous) * 10) / 10,\n    range: [Math.min(item.current, item.previous), Math.max(item.current, item.previous)],\n  }))\n  const values = items.flatMap((item) => [item.current, item.previous])\n  const pad = (Math.max(...values) - Math.min(...values)) * 0.08 || 1\n  const domain: [number, number] = [\n    Math.max(0, Math.min(...values) - pad),\n    Math.max(...values) + pad,\n  ]\n  const gainer = rows.reduce<ShiftRow | undefined>(\n    (best, row) => (best && best.delta >= row.delta ? best : row),\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                gainer &&\n                gainer.delta > 0 && (\n                  <p className='text-muted-foreground text-sm'>\n                    <span className='text-foreground font-medium'>{gainer.label}</span>{' '}\n                    gained the most, up {formatPoints(gainer.delta)} to{' '}\n                    {formatShare(gainer.current)}.\n                  </p>\n                )\n              )}\n              <BreakdownLegend\n                items={[\n                  { color: PREVIOUS_COLOR, label: previousLabel },\n                  { color: CURRENT_COLOR, label: currentLabel },\n                ]}\n              />\n            </div>\n            {state ? (\n              <ChartPlaceholder height={5 * ROW_HEIGHT_PX} />\n            ) : (\n              <div\n                role='img'\n                aria-label={`${title} chart`}\n                className='w-full'\n                style={{ height: rows.length * ROW_HEIGHT_PX }}\n              >\n                <ResponsiveContainer width='100%' height='100%'>\n                  <BarChart\n                    accessibilityLayer={false}\n                    barCategoryGap={0}\n                    data={rows}\n                    layout='vertical'\n                    margin={{ bottom: 0, left: 0, right: 0, top: 0 }}\n                  >\n                    <XAxis\n                      allowDataOverflow\n                      dataKey='current'\n                      domain={domain}\n                      hide\n                      type='number'\n                    />\n                    <YAxis\n                      axisLine={false}\n                      dataKey='label'\n                      tick={{\n                        dx: -LABEL_AXIS_PX + 8,\n                        fill: 'var(--color-foreground)',\n                        fontSize: 14,\n                        textAnchor: 'start',\n                      }}\n                      tickLine={false}\n                      type='category'\n                      width={LABEL_AXIS_PX}\n                    />\n                    <YAxis\n                      axisLine={false}\n                      dataKey='label'\n                      orientation='right'\n                      tick={({ payload, x, y }: YAxisTickContentProps) => {\n                        const row = rows.find((item) => item.label === payload?.value)\n                        if (!row) return <g />\n                        return (\n                          <text\n                            x={Number(x) + 8}\n                            y={Number(y)}\n                            textAnchor='start'\n                            dominantBaseline='middle'\n                          >\n                            <tspan fill='var(--color-foreground)' fontSize={14}>\n                              {formatShare(row.current)}\n                            </tspan>\n                            <tspan\n                              dx={6}\n                              fill='var(--color-muted-foreground)'\n                              fontSize={11}\n                            >\n                              {formatSigned(row.delta, (value) =>\n                                formatNumber(value, { minimumFractionDigits: 1 }),\n                              )}\n                            </tspan>\n                          </text>\n                        )\n                      }}\n                      tickLine={false}\n                      type='category'\n                      width={84}\n                      yAxisId='values'\n                    />\n                    <Tooltip\n                      content={({ active, payload }) => {\n                        const row = payload?.[0]?.payload as ShiftRow | undefined\n                        if (!active || !row) return null\n                        return (\n                          <ChartTooltipFrame>\n                            <span className='font-medium'>{row.label}</span>\n                            <ChartTooltipItem\n                              color={CURRENT_COLOR}\n                              name={currentLabel}\n                              value={formatShare(row.current)}\n                            />\n                            <ChartTooltipItem\n                              color={PREVIOUS_COLOR}\n                              name={previousLabel}\n                              value={formatShare(row.previous)}\n                            />\n                            <ChangeAmount\n                              change={row.delta}\n                              className='text-muted-foreground'\n                            >\n                              {formatPoints(Math.abs(row.delta))}\n                            </ChangeAmount>\n                          </ChartTooltipFrame>\n                        )\n                      }}\n                      cursor={chartBarCursor}\n                    />\n                    <Bar\n                      dataKey='range'\n                      shape={(shape: ShiftShapeProps) => <ShiftShape {...shape} />}\n                    />\n                  </BarChart>\n                </ResponsiveContainer>\n              </div>\n            )}\n            <ChartPanelTable\n              caption={`${title}: ${description}`}\n              columns={[\n                { key: 'label', label: 'Channel' },\n                { key: 'previous', label: previousLabel },\n                { key: 'current', label: currentLabel },\n                { key: 'change', label: 'Change' },\n              ]}\n              rows={rows.map((row) => ({\n                change: describeShift(row.delta),\n                current: formatShare(row.current),\n                label: row.label,\n                previous: formatShare(row.previous),\n              }))}\n            />\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport { Breakdown4, exampleProps as breakdown4ExampleProps, type Breakdown4Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/breakdown/breakdown-04.tsx"
    }
  ],
  "meta": {
    "shows": "How each category's share of a total moved between two periods, as a dot for each period joined by a line.",
    "size": "half",
    "usedWith": [
      "chart-panel-03",
      "data-table-03"
    ],
    "component": "Breakdown4",
    "props": "{ currentLabel: string; description: string; items?: { current: number; label: string; previous: number }[]; previousLabel: string; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
