# Scatter Chart (/ui/components/react/scatter-chart)

A scatter chart plots each item by 2 measures to show how they relate, and a bubble chart adds a third as size. Like every chart here it is a recipe: [Recharts](https://recharts.github.io) draws it and the [chart layer](/ui/components/react/charts) themes it. Each example below is a complete file to paste and adapt.

## Installation [#installation]

Recharts is the only extra dependency, with `react-is` at the same version as `react`:

```bash
pnpm add recharts react-is   # or: npm install recharts react-is / yarn add recharts react-is / bun add recharts react-is
```

The examples import the chart layer from `@/components/chart`. If your project doesn't have it yet, copy it from the [Charts](/ui/components/react/charts#the-chart-layer) page first.

## Examples [#examples]

### Default [#default]

Both axes are numeric and each `Scatter` holds its own data. A point can sit next to any other, so use the first 3 series colors only, and a stroke in the surface color keeps overlapping points apart. The tooltip takes its heading from the hovered row through a small wrapper around `ChartTooltip`, defined outside the chart so it keeps its state between renders.

```tsx
'use client'

import { CartesianGrid, Legend, Scatter, ScatterChart, Tooltip, XAxis, YAxis, type TooltipContentProps } from 'recharts'
import { Chart, ChartLegend, ChartTooltip, chartColors, useChartMotion } from '@/components/chart'

const organic = [
  { campaign: 'Spring sale', spend: 1200, orders: 96 },
  { campaign: 'Newsletter', spend: 1800, orders: 132 },
  { campaign: 'Referral week', spend: 2400, orders: 171 },
  { campaign: 'Summer drop', spend: 3100, orders: 188 },
  { campaign: 'Back to school', spend: 3600, orders: 236 },
  { campaign: 'Harvest sale', spend: 4200, orders: 251 },
  { campaign: 'Black Friday', spend: 5100, orders: 309 },
  { campaign: 'Holiday gifts', spend: 5800, orders: 322 },
]

const paid = [
  { campaign: 'Spring sale', spend: 1500, orders: 58 },
  { campaign: 'Newsletter', spend: 2200, orders: 94 },
  { campaign: 'Referral week', spend: 2900, orders: 103 },
  { campaign: 'Summer drop', spend: 3400, orders: 141 },
  { campaign: 'Back to school', spend: 4100, orders: 152 },
  { campaign: 'Harvest sale', spend: 4700, orders: 196 },
  { campaign: 'Black Friday', spend: 5400, orders: 204 },
  { campaign: 'Holiday gifts', spend: 6200, orders: 247 },
]

const compact = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', notation: 'compact' })
const money = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })
const count = new Intl.NumberFormat('en-US')

function CampaignTooltip(props: TooltipContentProps) {
  return (
    <ChartTooltip
      {...props}
      label={props.payload[0]?.payload.campaign}
      formatValue={(value, name) => (name === 'Spend' ? money.format(value) : count.format(value))}
    />
  )
}

export default function ScatterChartDefault() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart className="h-72">
        <ScatterChart responsive aria-label="Orders against marketing spend per campaign, by channel">
          <CartesianGrid />
          <XAxis
            type="number"
            dataKey="spend"
            name="Spend"
            tickFormatter={(value) => compact.format(value)}
            tickLine={false}
            axisLine={false}
            niceTicks="snap125"
          />
          <YAxis
            type="number"
            dataKey="orders"
            name="Orders"
            tickFormatter={(value) => count.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
            niceTicks="snap125"
          />
          <Tooltip cursor={false} content={CampaignTooltip} />
          <Legend itemSorter={null} content={<ChartLegend />} />
          <Scatter name="Organic" data={organic} fill={chartColors[0]} stroke="var(--background)" {...motion.series} />
          <Scatter name="Paid" data={paid} fill={chartColors[1]} stroke="var(--background)" {...motion.series} />
        </ScatterChart>
      </Chart>
    </div>
  )
}
```

### Bubble [#bubble]

`ZAxis` maps a third measure to the area of each point. A bubble is the `-soft` tint with a 1 px outline, so overlapping bubbles stay readable, and with 1 series the tooltip hides its swatches.

```tsx
'use client'

import { CartesianGrid, Scatter, ScatterChart, Tooltip, XAxis, YAxis, ZAxis, type TooltipContentProps } from 'recharts'
import { Chart, ChartTooltip, chartColors, useChartMotion } from '@/components/chart'

const data = [
  { product: 'Linen shirt', price: 68, rating: 4.6, sales: 48200 },
  { product: 'Canvas tote', price: 34, rating: 4.8, sales: 39600 },
  { product: 'Wool beanie', price: 28, rating: 4.3, sales: 31400 },
  { product: 'Leather belt', price: 54, rating: 4.1, sales: 26900 },
  { product: 'Cotton socks', price: 12, rating: 4.4, sales: 18700 },
  { product: 'Denim jacket', price: 118, rating: 4.7, sales: 22300 },
  { product: 'Rain coat', price: 142, rating: 3.9, sales: 12800 },
  { product: 'Enamel pin', price: 8, rating: 4.0, sales: 9300 },
]

const price = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 })
const rating = new Intl.NumberFormat('en-US', { minimumFractionDigits: 1 })

function BubbleTooltip(props: TooltipContentProps) {
  return (
    <ChartTooltip
      {...props}
      label={props.payload[0]?.payload.product}
      hideIndicator
      formatValue={(value, name) => (name === 'Rating' ? rating.format(value) : price.format(value))}
    />
  )
}

export default function ScatterChartBubble() {
  const motion = useChartMotion()

  return (
    <div className="w-full max-w-2xl">
      <Chart className="h-72">
        <ScatterChart responsive aria-label="Products by price and rating, sized by sales">
          <CartesianGrid />
          <XAxis
            type="number"
            dataKey="price"
            name="Price"
            tickFormatter={(value) => price.format(value)}
            tickLine={false}
            axisLine={false}
            niceTicks="snap125"
          />
          <YAxis
            type="number"
            dataKey="rating"
            name="Rating"
            domain={[3.5, 5]}
            ticks={[3.5, 4, 4.5, 5]}
            tickFormatter={(value) => rating.format(value)}
            tickLine={false}
            axisLine={false}
            width="auto"
          />
          <ZAxis type="number" dataKey="sales" name="Sales" range={[80, 1200]} />
          <Tooltip cursor={false} content={BubbleTooltip} />
          <Scatter
            name="Products"
            data={data}
            fill="var(--secondary-soft)"
            stroke={chartColors[0]}
            strokeWidth={1}
            {...motion.series}
          />
        </ScatterChart>
      </Chart>
    </div>
  )
}
```

## Accessibility [#accessibility]

- Pass `aria-label` to the chart with what it shows (`"Products by price and rating, sized by sales"`). It becomes the accessible name of the SVG.
- The chart takes focus, and the arrow keys move the tooltip from point to point.
- Name both axes with `name`, since the tooltip prints those names beside the values.
- A chart with 2 or 3 groups has a `Legend`. Past 3 groups, draw 1 chart per group.
- Nothing animates under reduced motion.
