Scatter Chart
Plot points as dots to show how two values relate, or size each dot for a bubble chart, with a touch tooltip, pan and zoom, and dark mode built in.
Installation
This also adds Chart, which draws the preset, and installs the npm packages @tanstack/charts and d3-shape.
Usage
datais an array of{ x, y }points, both numbers. The axes fit rounded ranges to the data.- Each side is inset by 4%, so dots at the edges aren't cut off.
- Touch the chart to see a point's values in a tooltip. Tap to pin it. Set
labelso a screen reader can say what the chart shows.
Customize
Colors
color fills every dot (default var(--primary)). A theme color such as var(--chart-2) switches with dark mode. A fixed color stays the same. Pass colors to give each dot its own color. The list repeats if it's shorter than the data.
For groups with a legend and named tooltips, use <ui-chart> with a z channel instead.
Shape
radius sets every dot's radius in px (default 4). Pass sizes to give each dot its own radius, which shows a third value as the dot's size. Points past the end of sizes use radius.
Axes and range
xAxisLabel and yAxisLabel add axis titles. Horizontal gridlines are on by default: showGrid turns them off, and showXGrid adds vertical ones.
xMin,xMax,yMinandyMaxfix a bound instead of fitting the data, for example to compare two charts on one scale.xTickFormatandyTickFormatturn tick values into labels.tickCountsets roughly how many ticks each axis gets. The axis picks round values near it.
Padding
padding insets the plot, each side as a fraction (0 to 1) of its size. Sides you leave out keep the 4% default.
Size
The chart fills its container's width and is 200px tall. Set height for another height and width for a fixed width, both in px. Use height rather than a height class, because the preset always sets a height.
Interaction
Taps
pointTap emits the tapped { x, y } point. A tap on empty space emits nothing. Set tooltip to false to turn the tooltip off.
pointTap fires inside the touch, so wrap router navigation in setTimeout (see the setTimeout rule).
Pan and zoom
Set zoomable to let users drag to pan, with +, − and 1:1 buttons. zoomAxes limits it to one axis.
To zoom from your own controls, set showZoomControls to false and call zoomIn(), zoomOut() or resetZoom() on the component. See Pan and zoom for how it behaves.
API
UiScatterChart
XYDatum is { x: number; y: number }. ChartPadding is { top?, right?, bottom?, left? }, each a fraction (0–1) of the plot's size. Both are exported from ../components/ui/chart.
scatterChartDefinition
Returns the TanStack Charts definition <ui-scatter-chart> renders: one dot mark. The options are the inputs that shape the chart, with the same defaults: data, color, colors, radius, sizes, tickCount, showGrid, showXGrid, xAxisLabel, yAxisLabel, padding, xMin, xMax, yMin, yMax, xTickFormat, yTickFormat and tooltip. Size, label, color scheme and zoom are inputs of <ui-chart> instead.
Start from it to add marks of your own, such as a trend line, and render the result with <ui-chart>. Put the trend line first so the dots draw over it. ci: 0 leaves out the confidence band.
Related
- Chart: groups, trend lines and other marks
- Line Chart: points joined by a line
- Bar Chart: values as bars
