# Scatter Chart

> How two measures go together, one dot per thing, in groups, with a third measure as the dot's size.

Page: https://uicontext.com/docs/components/scatter-chart

Group: Charts.

## When to use

How two measures go together, one dot per thing, in groups, with a third measure as the dot's size. Use [Line Chart](https://uicontext.com/docs/components/line-chart.md) instead when one measure changes over time.

## Usage

```ts
import { ScatterChart } from "./ui/uicontext/components/charts/Scatter.ts"

export const Deals = <M>(h: Html.H<M>) =>
	ScatterChart(h, {
		props: {
			label: "Deals",
			x: { label: "Days to close" },
			y: { label: "Deal size", format: { style: "currency", currency: "USD" } },
			series: [{ id: "won", label: "Won", points: [{ id: "a", label: "Acme", x: 21, y: 12_000 }, { id: "b", label: "Globex", x: 64, y: 48_000 }] }],
		},
		onEmit: { Highlighted: () => undefined, Selected: () => undefined, RequestedRetry: () => undefined },
	})
```

## Examples

Each example is a complete file. Its imports point where the CLI installs the source.

### Accounts by plan

The example the page opens with: Scatter Chart in “Accounts by plan”.

```ts title="src/demo.ts" source="www/src/examples/scatter-chart/demo.ts"
import type { Html } from "effect/ui/html"
import { ScatterChart } from "./ui/uicontext/components/charts/Scatter.ts"

// accounts by seats and yearly revenue, split by plan: who pays more than their size suggests
const accounts: ReadonlyArray<readonly [string, string, number, number]> = [
	["northwind", "Northwind", 42, 18_400], ["globex", "Globex", 120, 61_000], ["initech", "Initech", 18, 6_200],
	["umbrella", "Umbrella", 260, 148_000], ["hooli", "Hooli", 85, 29_500], ["vandelay", "Vandelay", 12, 3_900],
	["stark", "Stark Industries", 310, 205_000], ["wayne", "Wayne Enterprises", 190, 96_000], ["acme", "Acme", 64, 34_800],
	["soylent", "Soylent", 28, 9_100], ["tyrell", "Tyrell", 150, 118_000], ["cyberdyne", "Cyberdyne", 96, 41_200],
	["oscorp", "Oscorp", 220, 102_500], ["wonka", "Wonka", 8, 2_600], ["dunder", "Dunder Mifflin", 34, 8_700],
	["pied", "Pied Piper", 22, 12_900], ["massive", "Massive Dynamic", 270, 171_000], ["gringotts", "Gringotts", 58, 39_600],
]

const plan = (seats: number) => (seats >= 150 ? "enterprise" : seats >= 50 ? "business" : "team")

export const AccountsBySize = <M>(h: Html.H<M>) =>
	ScatterChart(h, {
		props: {
			label: "Accounts by seats and revenue",
			x: { label: "Seats", zero: true },
			y: { label: "Yearly revenue", format: { style: "currency", currency: "USD" } },
			series: [
				{ id: "team", label: "Team", points: [] as Array<{ id: string; label: string; x: number; y: number }> },
				{ id: "business", label: "Business", points: [] as Array<{ id: string; label: string; x: number; y: number }> },
				{ id: "enterprise", label: "Enterprise", points: [] as Array<{ id: string; label: string; x: number; y: number }> },
			].map((s) => ({ ...s, points: accounts.filter(([, , seats]) => plan(seats) === s.id).map(([id, label, x, y]) => ({ id, label, x, y })) })),
		},
		onEmit: { Highlighted: () => undefined, Selected: () => undefined, RequestedRetry: () => undefined },
	})
```

### Sized by a third measure

`size` draws each dot's area from a third value, here visits; the tooltip and table name it.

```ts title="src/bubbles.ts" source="www/src/examples/scatter-chart/bubbles.ts"
import type { Html } from "effect/ui/html"
import { ScatterChart } from "./ui/uicontext/components/charts/Scatter.ts"

// a site's pages by load time and bounce rate, each dot sized by its visits
const pages: ReadonlyArray<readonly [string, number, number, number]> = [
	["/", 1.1, 0.32, 48_200], ["/pricing", 1.6, 0.41, 21_400], ["/docs", 0.9, 0.22, 33_900], ["/blog", 2.8, 0.63, 12_100],
	["/signup", 1.3, 0.28, 9_800], ["/changelog", 2.1, 0.47, 4_300], ["/customers", 3.4, 0.71, 3_100], ["/careers", 2.5, 0.58, 2_200],
	["/integrations", 1.9, 0.36, 7_600], ["/security", 1.4, 0.31, 2_900], ["/status", 0.7, 0.18, 6_400], ["/contact", 1.2, 0.44, 3_700],
]

export const PageSpeed = <M>(h: Html.H<M>) =>
	ScatterChart(h, {
		props: {
			label: "Pages by load time and bounce rate",
			height: 320,
			x: { label: "Load time", format: { style: "unit", unit: "second" }, zero: true },
			y: { label: "Bounce rate", format: { style: "percent", digits: 0 } },
			size: { label: "Visits" },
			series: [{ id: "pages", label: "Pages", color: 7, points: pages.map(([path, x, y, size]) => ({ id: path, label: path, x, y, size })) }],
		},
		onEmit: { Highlighted: () => undefined, Selected: () => undefined, RequestedRetry: () => undefined },
	})
```

## API reference

**Props**

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `string` | — | — |
| `series` (required) | `ScatterSeries[]` | — | — |
| `x` (required) | `ScatterAxis` | — | — |
| `y` (required) | `ScatterAxis` | — | — |
| `size` | `{ label, format }` | — | What `size` measures, for the tooltip and the table: { label: "Deals", format } |
| `height` | `number` | `300` | Pixels; 300 by default |
| `tableToggle` | `boolean` | — | — |
| `status` | `ChartStatus` | — | — |
| `error` | `string` | — | — |
| `emptyText` | `string` | — | What nothing-to-show means here |
| `locale` | `string` | — | — |

**Events**

| Event | Payload | Description |
| --- | --- | --- |
| `Highlighted` | `{ series: string \| null; point: string \| null }` | The dot being read, by pointer or keys; null when none |
| `Selected` | `{ series: string; point: string }` | — |
| `RequestedRetry` | `{}` | — |

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/scatter-chart
```

With npm: `npx shadcn@latest add checkfu/uicontext/scatter-chart`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/scatter-chart`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/scatter-chart`.

It writes these files:

- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/charts/Data.ts`
- `src/ui/uicontext/components/charts/Format.ts`
- `src/ui/uicontext/components/charts/Frame.ts`
- `src/ui/uicontext/components/charts/Scale.ts`
- `src/ui/uicontext/components/charts/Scatter.ts`
- `src/ui/uicontext/components/charts/chart.stylex.ts`
- `src/ui/uicontext/components/charts/slots.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/charts/Scatter.ts](https://github.com/checkfu/uicontext/blob/main/src/charts/Scatter.ts)

## Related

- [Chart Foundations](https://uicontext.com/docs/components/chart-foundations.md) (`chart-foundations`): The scales, formats, legend and tooltip every chart shares.
- [Line Chart](https://uicontext.com/docs/components/line-chart.md) (`line-chart`): Values over time for one or many series, with dashed comparisons and forecasts.
- [Area Chart](https://uicontext.com/docs/components/area-chart.md) (`area-chart`): Totals over time and what makes them up, stacked or as shares of 100%.
- [Bar Chart](https://uicontext.com/docs/components/bar-chart.md) (`bar-chart`): Compare categories with grouped, stacked or sideways bars.
- [Combo Chart](https://uicontext.com/docs/components/combo-chart.md) (`combo-chart`): Bars and a line on one time axis, each read against its own scale.
- [Donut Chart](https://uicontext.com/docs/components/donut-chart.md) (`donut-chart`): Shares of one total, with the total in the middle.
