How two measures go together, one dot per thing, in groups, with a third measure as the dot's size.
- 8.6 kB JS 5.1 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- Right-to-left
- 13 tests
| Point | Group | Seats | Yearly revenue |
|---|---|---|---|
| Northwind | Team | 42 | $18,400.00 |
| Initech | Team | 18 | $6,200.00 |
| Vandelay | Team | 12 | $3,900.00 |
| Soylent | Team | 28 | $9,100.00 |
| Wonka | Team | 8 | $2,600.00 |
| Dunder Mifflin | Team | 34 | $8,700.00 |
| Pied Piper | Team | 22 | $12,900.00 |
| Globex | Business | 120 | $61,000.00 |
| Hooli | Business | 85 | $29,500.00 |
| Acme | Business | 64 | $34,800.00 |
| Cyberdyne | Business | 96 | $41,200.00 |
| Gringotts | Business | 58 | $39,600.00 |
| Umbrella | Enterprise | 260 | $148,000.00 |
| Stark Industries | Enterprise | 310 | $205,000.00 |
| Wayne Enterprises | Enterprise | 190 | $96,000.00 |
| Tyrell | Enterprise | 150 | $118,000.00 |
| Oscorp | Enterprise | 220 | $102,500.00 |
| Massive Dynamic | Enterprise | 270 | $171,000.00 |
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 suggestsconst 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 }, })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 instead when one measure changes over time.
Usage
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
Sized by a third measure
size draws each dot's area from a third value, here visits; the tooltip and table name it.
| Name | Load time | Bounce rate | Visits |
|---|---|---|---|
| / | 1.1 sec | 32% | 48,200 |
| /pricing | 1.6 sec | 41% | 21,400 |
| /docs | 0.9 sec | 22% | 33,900 |
| /blog | 2.8 sec | 63% | 12,100 |
| /signup | 1.3 sec | 28% | 9,800 |
| /changelog | 2.1 sec | 47% | 4,300 |
| /customers | 3.4 sec | 71% | 3,100 |
| /careers | 2.5 sec | 58% | 2,200 |
| /integrations | 1.9 sec | 36% | 7,600 |
| /security | 1.4 sec | 31% | 2,900 |
| /status | 0.7 sec | 18% | 6,400 |
| /contact | 1.2 sec | 44% | 3,700 |
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 visitsconst 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 |
|---|---|---|---|
labelrequired | string | — | — |
seriesrequired | ScatterSeries[] | — | — |
xrequired | ScatterAxis | — | — |
yrequired | 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
pnpm dlx shadcn@latest add checkfu/uicontext/scatter-chart