Scatter chart
Points on two numeric axes, one series or several, with axis titles and a walk through the points.
Demo
Usage
import { ScatterChart } from '@tuttn/slant'
// One series: x and y pairs, each with an optional label.
const points = [{ x: 0.42, y: 0.81, label: 'Refund policy' }, { x: 0.55, y: 0.74, label: 'Rate limits' }, /* … */]
<ScatterChart data={points} label="Retrieval quality" xLabel="Recall" yLabel="Precision"
formatX={v => v.toFixed(2)} formatY={v => v.toFixed(2)} />
// Several series: a legend names them, and its buttons show and hide each one.
<ScatterChart label="Retrieval quality" xLabel="Recall" yLabel="Precision"
series={[{ id: 'bm25', label: 'BM25', points: bm25 }, { id: 'dense', label: 'Dense', points: dense }]} />
// Fixed domains instead of fitting the data; the table shown under the chart.
<ScatterChart data={points} label="Retrieval quality" xDomain={[0, 1]} yDomain={[0, 1]} table />
// Uncontrolled, the legend starts from defaultHidden and the cursor from
// defaultActive; pass hidden/onHiddenChange and active/onActiveChange to drive them.
<ScatterChart series={series} label="Retrieval quality" hidden={hidden} onHiddenChange={setHidden}
active={point} onActiveChange={setPoint} />Props
| Prop | Type | Default | Description |
|---|---|---|---|
data | readonly ScatterDatum[] | — | One series: a point per entry. |
series | readonly ScatterSeries[] | — | Several series, each `{ id, label, points }`. Takes over from `data`. |
label | string | — | The chart's accessible name. |
xLabel | string | — | The x-axis title, under the axis, and the x value's name in announcements: "Recall". |
yLabel | string | — | The y-axis title, over the value axis, and the y value's name in announcements: "Precision". |
formatX | (value: number) => string | value => String(value) | Formats an x value on the axis, in the tooltip, the table and announcements. Without it, values read as plain digits and the axis ticks as short numbers (1.2K). |
formatY | (value: number) => string | value => String(value) | Formats a y value the same way. |
xDomain | readonly [number, number] | — | A fixed x domain, `[min, max]`. By default it fits the data, rounded out to nice ticks. Points outside a fixed domain aren't drawn or walked; the table keeps them. |
yDomain | readonly [number, number] | — | A fixed y domain, `[min, max]`. |
height | number | 220 | The height of the plot in pixels; the axis labels and titles add their own rows. The width follows the container. |
hidden | readonly string[] | — | The ids of the series left out, for a controlled legend. |
defaultHidden | readonly string[] | [] | The ids of the series left out at first, when uncontrolled. |
onHiddenChange | (hidden: string[]) => void | — | Called with the new list of hidden ids, in series order, when a legend button is pressed. |
table | boolean | false | Shows the data table under the chart. It is always there for screen readers. |
active | number | — | The point the keyboard cursor sits on, as its index in the walk (every shown point, by x), for a controlled chart. |
defaultActive | number | 0 | The point the keyboard cursor starts on. |
onActiveChange | (index: number) => void | — | Called with the walk index when the keyboard moves the cursor. |
…rest | HTMLAttributes<HTMLDivElement> | — | className, style, other attributes and the ref go on the root. The HTML `hidden` attribute is not among them: `hidden` is the legend's list of hidden series. |
ScatterDatum | { x: number; y: number; label?: string } | — | One point. `label` names it in the tooltip, the table and announcements. |
ScatterSeries | { id: string; label: string; points: ScatterDatum[]; color?: 1 | 2 | 3 | 4 | 5 } | — | One series. Its colour is the slot of its place in `series` unless `color` says. |
Keyboard
| Tab | focuses the plot, a single tab stop, and shows the cursor's point |
| Right / Left | moves to the next or previous point by x (then y), without wrapping |
| Down / Up | moves to the nearest point by x in the next or previous series; stays at the last |
| Home / End | moves to the first or last point |
| Escape | hides the tooltip until the next step |
| Enter / Space | on a legend name, shows or hides that series |
Accessibility
- The plot is a group with the
chartrole description, named bylabeland described by a summary: how many points show, then each axis' range, read by the axes' names (“24 points. Recall at 10 from 0.33 to 0.88. Precision at 10 from 0.49 to 0.86.”). - Every step of the walk is announced through the root's polite live region: the series (with several), the point's label, then both values by the axes' names, “Dense, Rate limits, Recall at 10 0.66, Precision at 10 0.80”. Without
xLabelandyLabelthe values are read asxandy. - The drawing is hidden from assistive technology; the data is always there as a table for screen readers, with the series, the point (its label, or its place in the series), x and y.
tableshows it to everyone. It keeps every point, hidden series and points outside a fixed domain too. - With two or more series the legend names them, so no series is told apart by colour alone. Its names are toggle buttons in a group named “Show series”, with
aria-pressedtrue while a series shows; a hidden series' key goes hollow and its name is struck through. The last series showing isaria-disabledand can't be hidden. - Hovering shows the point nearest the pointer within 16px without moving the cursor or speaking. Under reduced motion the points appear without fading in.
Both axes fit the data, rounded out to nice ticks, with no forced zero; xDomain and yDomain fix them instead, with the nice ticks inside. A point outside a fixed domain isn't drawn or walked. formatX and formatY write the ticks as well as the values; without them the ticks are short numbers (1.2K) and the values plain digits.
Each point is a slanted pixel in its series' colour, or the accent for one series, ringed in the surface so overlapping points stay apart. A hidden series leaves the drawing, the scales, the walk and the summary, so the shown points spread out to fill the plot. The walk's order is every shown point by x, so active is an index into that order, and it changes when a series is shown or hidden.
The axis titles are drawn level, in rows of their own: xLabel centred under the tick labels, yLabel over the value ticks at the top-left, so neither crowds a tick label however narrow the chart. The drawing is physical: under dir="rtl" the smallest x stays on the left and Right still moves to the next point; the legend and the table follow the page's direction.