ScatterChart
Overview
The ScatterChart component draws points by two values, x across and y up, in groups, with an optional third value as each point's size for a bubble chart.
It wraps Recharts' ScatterChart and comes
from the @allxsmith/bestax-charts package, which the Charts guide
shows how to install. You give it series, each a group of points with data of its own, and the
dataKey that names each point's x and y keys, and it draws them with Bulma's colors, a legend
whose entries hide and show groups, a tooltip in a Bulma box, and a data table screen readers read.
Import
import { ScatterChart } from '@allxsmith/bestax-charts';
Usage
Basic Usage
Each series is a group of points with its own data, and dataKey names the keys of a point that
hold its x and y values. label names the chart; it's required, because it's what a screen reader
announces.
function example() { const morning = [ { hours: 2, score: 58 }, { hours: 5, score: 71 }, { hours: 7, score: 80 }, { hours: 11, score: 91 }, ]; const evening = [ { hours: 1, score: 49 }, { hours: 3, score: 61 }, { hours: 6, score: 70 }, { hours: 10, score: 83 }, ]; return ( <ScatterChart label="Study hours and exam scores" dataKey={{ x: 'hours', y: 'score' }} series={[ { name: 'morning', label: 'Morning class', color: 'primary', data: morning, }, { name: 'evening', label: 'Evening class', color: 'info', data: evening, }, ]} xAxisLabel="Hours studied" yAxisLabel="Score" hasLegend /> ); }
Shapes
A series' shape draws its points as circles (the default), crosses, diamonds, squares, stars,
triangles or wyes. Giving the groups different shapes tells them apart by more than color. The
legend draws the same shapes, and so does the tooltip's series row, which a point nameKey names
gets when there's more than one group.
function example() { const groups = [ { name: 'a', label: 'Group A', color: 'primary', shape: 'circle' }, { name: 'b', label: 'Group B', color: 'info', shape: 'triangle' }, { name: 'c', label: 'Group C', color: 'warning', shape: 'star' }, ] as const; return ( <ScatterChart label="Three groups" dataKey={{ x: 'x', y: 'y' }} series={groups.map((group, index) => ({ ...group, data: [ { x: 1 + index, y: 20 + index * 10 }, { x: 4 + index, y: 35 + index * 8 }, { x: 7 + index, y: 30 + index * 12 }, ], }))} hasLegend /> ); }
Naming Points
nameKey names a key of each point that names it, such as a person or a product. The name heads
the point's row in the data table and titles its tooltip. Without one, a point is called by its
series and its place in it, such as "Group A 3".
function example() { const students = [ { student: 'Ava', hours: 2, score: 58 }, { student: 'Ben', hours: 4, score: 66 }, { student: 'Cleo', hours: 7, score: 80 }, { student: 'Dev', hours: 9, score: 88 }, ]; return ( <ScatterChart label="Study hours and exam scores" dataKey={{ x: 'hours', y: 'score' }} nameKey="student" series={[{ name: 'class', label: 'Class', data: students }]} xAxisLabel="Hours studied" yAxisLabel="Score" showTable height={240} /> ); }
Bubble Chart
dataKey.z names a third value that sizes each point, which makes a bubble chart. zAxisLabel
says what the sizes show, for the data table and the tooltip, and pointSizeRange sets the area
of the smallest and the largest point, in square pixels.
function example() { const products = [ { product: 'Lamp', price: 35, rating: 4.1, sold: 1200 }, { product: 'Desk', price: 240, rating: 4.6, sold: 310 }, { product: 'Chair', price: 160, rating: 3.8, sold: 540 }, { product: 'Shelf', price: 90, rating: 4.3, sold: 860 }, { product: 'Clock', price: 25, rating: 4.4, sold: 1500 }, ]; return ( <ScatterChart label="Products by price, rating and sales" dataKey={{ x: 'price', y: 'rating', z: 'sold' }} nameKey="product" series={[ { name: 'products', label: 'Products', color: 'link', data: products }, ]} xAxisLabel="Price" yAxisLabel="Rating" zAxisLabel="Units sold" pointSizeRange={[80, 1200]} yAxisProps={{ domain: [3, 5] }} scatterProps={{ fillOpacity: 0.7 }} /> ); }
Axes, Grid and Reference Lines
xAxisLabel and yAxisLabel title the axes, and head the values in the data table and the
tooltip; hasXAxis and hasYAxis turn the axes off. grid picks the grid lines: both (the
default), horizontal, vertical or none. referenceLines draws lines across the chart at an
x value or a y value, each with an optional label and color.
function example() { const scores = [ { hours: 1, score: 49 }, { hours: 3, score: 61 }, { hours: 5, score: 71 }, { hours: 6, score: 70 }, { hours: 9, score: 88 }, ]; return ( <ScatterChart label="Scores against the pass mark" dataKey={{ x: 'hours', y: 'score' }} series={[ { name: 'scores', label: 'Students', color: 'success', data: scores }, ]} xAxisLabel="Hours studied" yAxisLabel="Score" grid="horizontal" referenceLines={[ { y: 70, label: 'Pass mark', color: 'danger' }, { x: 6, label: 'Six hours' }, ]} /> ); }
xAxisProps and yAxisProps reach Recharts' axes, for a domain that doesn't start at zero, for
example.
Legend and Hidden Series
hasLegend draws a legend below the chart, or above it with legendPosition="top". Each entry is
a toggle button that hides or shows its group. The chart keeps that state itself, starting from
defaultHiddenSeries, or you hold it with hiddenSeries and onHiddenSeriesChange.
function example() { const point = (x: number, y: number) => ({ x, y }); const [hidden, setHidden] = useState(['online']); return ( <> <Buttons mb="3"> <Button size="small" onClick={() => setHidden([])}> Show all </Button> </Buttons> <ScatterChart label="Study hours and exam scores" dataKey={{ x: 'x', y: 'y' }} series={[ { name: 'morning', label: 'Morning class', color: 'primary', data: [point(2, 58), point(5, 71), point(9, 88)], }, { name: 'evening', label: 'Evening class', color: 'info', data: [point(1, 49), point(6, 70), point(10, 83)], }, { name: 'online', label: 'Online class', color: 'warning', data: [point(2, 64), point(5, 68), point(9, 85)], }, ]} hasLegend legendPosition="top" hiddenSeries={hidden} onHiddenSeriesChange={setHidden} /> </> ); }
Formatting Values
Values are written as useNumberFormat writes a number, in the
LocaleProvider's locale. valueFormatter and unit write the y values your way, on the y axis,
in the tooltip and in the data table, xValueFormatter and xUnit do the same for the x values,
and zValueFormatter and zUnit for the z values that size the points.
function example() { const products = [ { price: 35, rating: 4.1 }, { price: 240, rating: 4.6 }, { price: 160, rating: 3.8 }, { price: 90, rating: 4.3 }, ]; const dollars = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0, }); return ( <ScatterChart label="Price and rating" dataKey={{ x: 'price', y: 'rating' }} series={[{ name: 'products', label: 'Products', data: products }]} xAxisLabel="Price" yAxisLabel="Rating" xValueFormatter={value => dollars.format(value)} unit=" ★" yAxisProps={{ domain: [3, 5] }} /> ); }
Translating the Headings
The data table and the tooltip head their columns with the axis titles, and two headings of the
chart's own: the points' names (Name) and their series (Series, written when there's more than
one). labels translates those two.
function example() { return ( <ScatterChart label="Heures d’étude et notes" dataKey={{ x: 'heures', y: 'note' }} series={[ { name: 'matin', label: 'Matin', data: [ { heures: 2, note: 12 }, { heures: 8, note: 16 }, ], }, { name: 'soir', label: 'Soir', data: [ { heures: 3, note: 11 }, { heures: 9, note: 15 }, ], }, ]} xAxisLabel="Heures" yAxisLabel="Note" labels={{ name: 'Nom', series: 'Groupe' }} showTable height={220} /> ); }
Colors
A series' color takes a Bulma color name (primary, info, grey-dark and the rest), a shade
of one of Bulma's six palette colors (primary-30, info-dark), or any CSS color. Bulma names
draw with Bulma's own CSS variables, so they follow Theme and dark mode.
A series with no color takes the next of Bulma's palette colors.
function example() { const spread = (offset: number) => [ { x: 1, y: 10 + offset }, { x: 3, y: 18 + offset }, { x: 5, y: 15 + offset }, ]; return ( <ScatterChart label="Three shades" dataKey={{ x: 'x', y: 'y' }} series={[ { name: 'light', label: 'Light', color: 'primary-30', data: spread(0) }, { name: 'mid', label: 'Mid', color: 'primary-50', data: spread(10) }, { name: 'dark', label: 'Dark', color: 'primary-70', data: spread(20) }, ]} hasLegend height={220} /> ); }
Showing the Data Table
The data table is always there for screen readers, with a row per point. showTable puts it on
screen too, below the chart, and showLabel shows the chart's label above it as a caption.
function example() { return ( <ScatterChart label="Study hours and exam scores" showLabel showTable dataKey={{ x: 'hours', y: 'score' }} series={[ { name: 'morning', label: 'Morning class', data: [ { hours: 2, score: 58 }, { hours: 7, score: 80 }, ], }, { name: 'evening', label: 'Evening class', data: [{ hours: 3, score: 61 }], }, ]} xAxisLabel="Hours studied" yAxisLabel="Score" height={200} /> ); }
Recharts Props and Children
Each Recharts part the chart draws takes its own props after the chart's: scatterProps for the
Scatter that draws every point (here, see-through points with an outline), chartProps for Recharts' ScatterChart,
and xAxisProps, yAxisProps and tooltipProps. children adds more Recharts elements, such as
a ReferenceArea.
function example() { const points = [ { x: 2, y: 40 }, { x: 3, y: 44 }, { x: 3.05, y: 44.5 }, { x: 5, y: 52 }, { x: 5.04, y: 51.6 }, ]; return ( <ScatterChart label="Overlapping points" dataKey={{ x: 'x', y: 'y' }} series={[ { name: 'points', label: 'Points', color: 'link', data: points }, ]} scatterProps={{ fillOpacity: 0.4, stroke: 'var(--bulma-link)' }} chartProps={{ margin: { top: 20, right: 20, bottom: 0, left: 0 } }} xAxisProps={{ domain: [0, 6] }} yAxisProps={{ domain: [30, 60] }} /> ); }
Size
The chart fills its container's width and is 300 pixels tall. height changes that, and width
and height take pixels or a percentage of the container. A chart sized in percent draws once the
browser has measured its container, and its legend and data table render on the server either
way.
<ScatterChart label="Two points" dataKey={{ x: 'x', y: 'y' }} series={[ { name: 'points', label: 'Points', data: [ { x: 1, y: 2 }, { x: 3, y: 5 }, ], }, ]} width={320} height={180} />
Theming
The chart draws its grid, axes and text from the same --bulma-chart-* variables as every chart
in the package; BarChart's Theming section lists them.
Accessibility
- The chart is a
<figure>named bylabel, and its drawing is named by the same caption.descriptionis read with the name of both. The data table takes the label as its caption. - A data table of every point renders beside the drawing, visually hidden with Bulma's
is-sr-only, so a screen reader reads the values the points show (WAI: complex images). Each row's header names its point, bynameKeyor by its series and place, and a column names its series when there's more than one.showTableputs it on screen. - The drawing is a tab stop. Once it has focus, the left and right arrow keys step through the
points one at a time, every shown series in turn, in the order of
seriesand then of each series'data, and the tooltip shows each point's values. A dashed crosshair marks the point. - Each legend entry is a
buttonwitharia-pressed. A hidden series' swatch is drawn hollow and its label greyed, so its state isn't told by color alone. The keyboard skips a hidden series' points, and the data table keeps them. - Groups are told apart by color alone unless you give them different
shapes, which the legend draws as well, and the tooltip's series row. A tooltip without that row names the group in its title when there's more than one. The swatches arearia-hidden; each entry's text says which series it is.
Related Components
BarChart: Values drawn as bars, one group per category.RadarChart: Series drawn as shapes on spokes, one per category.Table: The Bulma table the chart's data table is drawn with.LocaleProvider: The locale the chart writes its numbers in.- Helper Props: Bulma helper props for spacing, color, etc.
Additional Resources
Props
| Prop | Type | Default | Description |
|---|---|---|---|
series | ScatterChartSeries[] | — | Required. The groups of points to draw, in order, each with points of its own. |
dataKey | { x: string; y: string; z?: string } | — | Required. The keys of each point that hold its values: x and y place it, and z, when set, sizes it. |
nameKey | string | — | A key of each point that names it, such as country. The name heads the point's row in the data table and titles its tooltip. Without one, a point is called by its series and its place in it, such as "Group A 3". |
pointSizeRange | [number, number] | [64, 576] | The area of the smallest and the largest point, in square pixels, when dataKey.z sizes them. Ignored without dataKey.z, when every point takes Recharts' default size. |
grid | 'horizontal' | 'vertical' | 'both' | 'none' | 'both' | Which grid lines to draw. |
hasXAxis | boolean | true | Draws the x axis. |
hasYAxis | boolean | true | Draws the y axis. |
xAxisLabel | string | — | A title for the x axis. It also heads the x values in the data table and the tooltip, which use dataKey.x without one. |
yAxisLabel | string | — | A title for the y axis. It also heads the y values in the data table and the tooltip, which use dataKey.y without one. |
zAxisLabel | string | — | What the point sizes show, such as "Population". It heads the z values in the data table and the tooltip, which use dataKey.z without one. The chart draws no axis for it. |
gridProps | Partial<CartesianGridProps> | — | Props passed to Recharts' CartesianGrid, after the chart's own. |
xAxisProps | Partial<XAxisProps> | — | Props passed to Recharts' XAxis, after the chart's own, such as domain. |
yAxisProps | Partial<YAxisProps> | — | Props passed to Recharts' YAxis, after the chart's own, such as domain. |
zAxisProps | Partial<ZAxisProps> | — | Props passed to Recharts' ZAxis, which sizes the points by dataKey.z, after the chart's own. |
referenceLines | { x?: string | number; y?: number; label?: string; color?: ChartColor }[] | — | Lines drawn across the chart at given values, such as an average. x is a number on the x axis here, and y a number on the y axis. |
valueFormatter | ChartValueFormatter | — | Writes each y value on the y axis, in the tooltip and in the data table. Without one, they're written as useNumberFormat writes a number, in the LocaleProvider's locale. xValueFormatter and zValueFormatter write the x and z values. |
unit | string | — | Written after every y value, such as kg. xUnit and zUnit follow the x and z values. |
xValueFormatter | ChartValueFormatter | — | Writes each x value on the x axis, in the tooltip and in the data table. Without one, they're written as useNumberFormat writes a number, in the LocaleProvider's locale. |
xUnit | string | — | Written after every x value, such as cm. |
zValueFormatter | ChartValueFormatter | — | Writes each z value, the one that sizes the points, in the tooltip and in the data table. Without one, they're written as useNumberFormat writes a number, in the LocaleProvider's locale. |
zUnit | string | — | Written after every z value, such as people. |
labels | { name?: string; series?: string } | — | The headings the chart writes itself, in the data table and the tooltip, to translate them. |
tooltipProps | Partial<TooltipProps> | — | Props passed to Recharts' Tooltip, after the chart's own. |
scatterProps | Partial<Omit<ScatterProps, 'ref'>> | — | Props passed to the Recharts Scatter that draws every point, after the chart's own, such as fillOpacity for points that overlap. |
chartProps | Partial<React.ComponentProps<typeof RechartsScatterChart>> | — | Props passed to Recharts' ScatterChart, after the chart's own, such as margin. |
className | string | — | Additional CSS classes to apply. |
label | string | — | Required. What the chart shows, such as "Monthly sales". It names the chart for assistive tech, and captions its data table. Shown above the chart with showLabel. |
description | string | — | A sentence or two on what the chart says, such as the trend it shows. A screen reader reads it with the chart's name. Not shown on screen. |
showLabel | boolean | false | Shows label above the chart as its caption. |
showTable | boolean | false | Shows the chart's data table below it. The table is always there for screen readers, which read the numbers from it; this puts it on screen as well. |
hasTooltip | boolean | true | Shows a tooltip with the values under the pointer, or under the keyboard focus as the arrow keys move through the chart. |
width | ChartSize | '100%' | The chart's width, in pixels or as a percentage of its container. A percentage makes the chart follow its container's size as the browser measures it. Recharts draws in the browser either way: a server render holds the figure, its caption, legend and data table, and the drawing appears once the page hydrates. |
height | ChartSize | 300 | The chart's height, in pixels or as a percentage of its container, which then needs a height of its own. |
children | React.ReactNode | — | More Recharts elements to draw inside the chart, such as a Brush, a ReferenceArea, or <defs> for a pattern a color names with url(#id). |
hasLegend | boolean | false | Shows a legend. Each entry is a toggle button that hides or shows its series. |
legendPosition | 'top' | 'bottom' | 'bottom' | Where the legend sits. |
hiddenSeries | string[] | — | The names of the series that are hidden, when you hold that state. |
defaultHiddenSeries | string[] | — | The names of the series hidden to begin with, when the chart holds it. |
onHiddenSeriesChange | (hiddenSeries: string[]) => void | — | Called with the names of the hidden series when the legend changes them. |
ref | React.Ref<HTMLElement> | — | Ref forwarded to the root <figure>. |
... | All standard HTML attributes and Bulma helper props | — | See Helper Props |
Types:
ChartColor: Bulma color |`${(typeof paletteColors)[number]}-${(typeof validColorShades)[number]}`|(string & {})— A color for a series, a slice or a mark. A Bulma color name such asprimaryorgrey-light, a shade of one of Bulma's six palette colors such asprimary-60orinfo-dark, or any CSS color, such as#3e8ed0,var(--brand)orurl(#stripes)for a pattern the chart's children define. A Bulma name draws with Bulma's own CSS variable, so it followsThemeand dark mode.ChartRow:Record<string, any>— One row of a chart'sdata: an object whose keys the chart'sdataKeyand series name.ChartSize:number|`${number}%`— A chart's width or height: pixels, or a percentage of its container.ChartValueFormatter:(value: number) => string— Writes a value for the axes, the tooltip, the labels and the data table.ScatterChartSeries: One group of points, with points of its own. Its fields:name:string— Required. A name for the group, unique among the series.hiddenSeriesholds it, and the legend, the tooltip and the data table show it when the group has nolabel.data:readonly ChartRow[]— Required. The group's points: one object each, with the keysdataKeynames for its x and y values, and its size whendataKey.zis set.shape:'circle'|'cross'|'diamond'|'square'|'star'|'triangle'|'wye'— The shape of the group's points. The legend draws it too, and so does the tooltip's series row, which a pointnameKeynames gets when there's more than one group. Giving the groups different shapes tells them apart by more than color, for readers who can't tell the colors apart. Default'circle'.label:string— What the legend, the tooltip and the data table call it. Defaults toname.color:ChartColor— Its color. Without one, it takes the next of Bulma's palette colors.