{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "scatter-03",
  "description": "Campaigns plotted by cost per acquisition and conversion rate, sized by spend and coloured by channel, against a target cost.",
  "dependencies": [
    "recharts"
  ],
  "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/scatter.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/scatter/scatter-03.tsx",
      "content": "'use client'\n\nimport {\n  BlockStateProvider,\n  BlockStateText,\n  type BlockStateProps,\n  type BlockStatus,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  chartAxisProps,\n  chartGridProps,\n  chartReferenceLabel,\n  chartReferenceProps,\n  chartValueAxisWidth,\n} from '@/registry/components/dashboardblocks/chart'\nimport {\n  ChartPanelFigure,\n  ChartPanelLegend,\n  ChartPanelTable,\n} from '@/registry/components/dashboardblocks/chart-panel'\nimport {\n  ScatterTooltipContent,\n  scatterPalette,\n} from '@/registry/components/dashboardblocks/scatter'\nimport { formatCurrency, formatPercent } from '@/registry/lib/dashboardblocks/format'\nimport {\n  CartesianGrid,\n  LabelList,\n  ReferenceLine,\n  ResponsiveContainer,\n  Scatter,\n  ScatterChart,\n  Tooltip,\n  XAxis,\n  YAxis,\n  ZAxis,\n} from 'recharts'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface Campaign {\n  channel: string\n  cpa: number\n  /** Conversion rate, 0–1. */\n  conversion: number\n  label: string\n  spend: number\n}\n\ninterface Scatter3Props extends BlockStateProps {\n  /** Leave out, or pass none, to show the empty state. */\n  campaigns?: Campaign[]\n  channels: string[]\n  description: string\n  /** A cost per acquisition to stay under, drawn as a line. */\n  status?: BlockStatus\n  target?: { label: string; value: number }\n  title: string\n}\n\nconst exampleProps: Scatter3Props = {\n  campaigns: [\n    {\n      channel: 'Search',\n      conversion: 0.061,\n      cpa: 38,\n      label: 'Brand terms',\n      spend: 18_400,\n    },\n    {\n      channel: 'Search',\n      conversion: 0.034,\n      cpa: 72,\n      label: 'Competitor terms',\n      spend: 26_900,\n    },\n    {\n      channel: 'Search',\n      conversion: 0.029,\n      cpa: 96,\n      label: 'Generic terms',\n      spend: 41_200,\n    },\n    {\n      channel: 'Social',\n      conversion: 0.018,\n      cpa: 64,\n      label: 'Retargeting',\n      spend: 12_300,\n    },\n    {\n      channel: 'Social',\n      conversion: 0.009,\n      cpa: 141,\n      label: 'Lookalikes',\n      spend: 22_700,\n    },\n    {\n      channel: 'Social',\n      conversion: 0.006,\n      cpa: 188,\n      label: 'Video awareness',\n      spend: 30_100,\n    },\n    {\n      channel: 'Partners',\n      conversion: 0.047,\n      cpa: 52,\n      label: 'Affiliates',\n      spend: 9_800,\n    },\n    {\n      channel: 'Partners',\n      conversion: 0.022,\n      cpa: 118,\n      label: 'Sponsorships',\n      spend: 15_600,\n    },\n  ],\n  channels: ['Search', 'Social', 'Partners'],\n  description: 'Each bubble is a campaign from last quarter, sized by what it spent.',\n  target: { label: 'Target', value: 100 },\n  title: 'Campaign efficiency',\n}\n\nconst money = (value: number) => formatCurrency(value, { compact: value >= 1_000 })\nconst percent = (value: number) => formatPercent(value, { minimumFractionDigits: 1 })\n\nconst Scatter3 = (props: Scatter3Props) => {\n  const {\n    campaigns = [],\n    channels,\n    description,\n    errorMessage,\n    status,\n    target,\n    title,\n  } = props\n  const state = getBlockState(status, campaigns)\n  const colorOf = (channel: string) =>\n    scatterPalette[Math.max(0, channels.indexOf(channel)) % scatterPalette.length]\n  const totalSpend = campaigns.reduce((sum, campaign) => sum + campaign.spend, 0)\n  const best = [...campaigns].sort((a, b) => a.cpa - b.cpa)[0]\n  const worst = [...campaigns].sort((a, b) => b.cpa - a.cpa)[0]\n  const overTarget = target\n    ? campaigns.filter((campaign) => campaign.cpa > target.value)\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            {state ? (\n              <div className='border-t pt-4'>\n                <BlockStateText className='text-sm' />\n              </div>\n            ) : (\n              best &&\n              worst && (\n                <p className='text-muted-foreground border-t pt-4 text-sm'>\n                  <span className='text-foreground font-medium'>{best.label}</span>{' '}\n                  acquires at {money(best.cpa)} and{' '}\n                  <span className='text-foreground font-medium'>{worst.label}</span> at{' '}\n                  {money(worst.cpa)}.\n                  {target &&\n                    ` ${overTarget.length} of ${campaigns.length} campaigns cost more than the ${money(target.value)} ${target.label.toLowerCase()}.`}{' '}\n                  Further left and higher up is better.\n                </p>\n              )\n            )}\n            {state !== 'empty' && state !== 'error' && (\n              <div className='flex flex-wrap items-center justify-between gap-x-4 gap-y-2'>\n                <ChartPanelLegend\n                  items={channels.map((channel) => ({\n                    color: colorOf(channel),\n                    label: channel,\n                  }))}\n                />\n                <span className='text-muted-foreground flex items-center gap-1.5 text-xs'>\n                  <span aria-hidden className='flex items-end gap-0.5'>\n                    <span className='border-muted-foreground size-1.5 rounded-full border' />\n                    <span className='border-muted-foreground size-2.5 rounded-full border' />\n                    <span className='border-muted-foreground size-3.5 rounded-full border' />\n                  </span>\n                  Size is spend\n                </span>\n              </div>\n            )}\n            <ChartPanelFigure className='h-72'>\n              <ResponsiveContainer width='100%' height='100%'>\n                <ScatterChart\n                  aria-label={`${title} chart`}\n                  margin={{ top: 12, right: 16, bottom: 16, left: 0 }}\n                >\n                  <CartesianGrid {...chartGridProps} />\n                  <XAxis\n                    {...chartAxisProps}\n                    dataKey='cpa'\n                    domain={[0, 'auto']}\n                    label={{\n                      fill: 'var(--color-muted-foreground)',\n                      fontSize: 11,\n                      offset: -12,\n                      position: 'insideBottom',\n                      value: 'Cost per acquisition',\n                    }}\n                    name='Cost per acquisition'\n                    tickFormatter={money}\n                    type='number'\n                  />\n                  <YAxis\n                    {...chartAxisProps}\n                    dataKey='conversion'\n                    domain={[0, 'auto']}\n                    name='Conversion'\n                    tickFormatter={percent}\n                    type='number'\n                    width={chartValueAxisWidth}\n                  />\n                  <ZAxis dataKey='spend' name='Spend' range={[80, 1_200]} />\n                  {target && (\n                    <ReferenceLine\n                      {...chartReferenceProps}\n                      x={target.value}\n                      label={{\n                        ...chartReferenceLabel,\n                        value: `${target.label} ${money(target.value)}`,\n                        position: 'insideTopRight',\n                      }}\n                    />\n                  )}\n                  <Tooltip\n                    content={({ active, payload }) => {\n                      const campaign = payload?.[0]?.payload as Campaign | undefined\n                      if (!active || !campaign) return null\n                      return (\n                        <ScatterTooltipContent\n                          color={colorOf(campaign.channel)}\n                          rows={[\n                            { label: 'Spend', value: money(campaign.spend) },\n                            { label: 'Cost per acquisition', value: money(campaign.cpa) },\n                            { label: 'Conversion', value: percent(campaign.conversion) },\n                          ]}\n                          title={campaign.label}\n                        />\n                      )\n                    }}\n                    cursor={false}\n                  />\n                  {channels.map((channel) => (\n                    <Scatter\n                      key={channel}\n                      data={campaigns.filter((campaign) => campaign.channel === channel)}\n                      fill={colorOf(channel)}\n                      fillOpacity={0.55}\n                      name={channel}\n                      stroke={colorOf(channel)}\n                      strokeWidth={1.5}\n                    >\n                      <LabelList\n                        dataKey='label'\n                        content={({ value, width, x, y }) =>\n                          value === best?.label || value === worst?.label ? (\n                            <text\n                              x={Number(x) + Number(width ?? 0) / 2}\n                              y={Number(y) - 4}\n                              textAnchor='middle'\n                              fontSize={11}\n                              fontWeight={500}\n                              fill='var(--color-foreground)'\n                            >\n                              {value}\n                            </text>\n                          ) : null\n                        }\n                      />\n                    </Scatter>\n                  ))}\n                </ScatterChart>\n              </ResponsiveContainer>\n            </ChartPanelFigure>\n            <ChartPanelTable\n              caption={`${title}: ${description}. ${money(totalSpend)} spent in total.`}\n              columns={[\n                { key: 'label', label: 'Campaign' },\n                { key: 'channel', label: 'Channel' },\n                { format: money, key: 'spend', label: 'Spend' },\n                { format: money, key: 'cpa', label: 'Cost per acquisition' },\n                { format: percent, key: 'conversion', label: 'Conversion' },\n              ]}\n              rows={campaigns}\n            />\n          </section>\n        </CardContent>\n      </Card>\n    </BlockStateProvider>\n  )\n}\n\nexport {\n  Scatter3,\n  exampleProps as scatter3ExampleProps,\n  type Campaign,\n  type Scatter3Props,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/scatter/scatter-03.tsx"
    }
  ],
  "meta": {
    "shows": "Campaigns plotted by cost per acquisition and conversion rate, sized by spend and coloured by channel, against a target cost.",
    "size": "full",
    "usedWith": [
      "data-table-03",
      "spend-02"
    ],
    "component": "Scatter3",
    "props": "{ campaigns?: { channel: string; cpa: number; conversion: number; label: string; spend: number }[]; channels: string[]; description: string; status?: \"loading\" | \"error\"; target?: { label: string; value: number }; title: string; errorMessage?: string }"
  },
  "type": "registry:component"
}
