title | productId | components |
---|---|---|
React Scatter chart |
x-charts |
ScatterChart, ScatterChartPro, ScatterPlot, ChartsGrid |
Scatter charts express the relation between two variables, using points in a surface.
Scatter chart series should contain a data
property containing an array of objects.
Those objects require the x
and y
properties.
With an optional id
property if more optimization is needed.
{{"demo": "BasicScatter.js"}}
If your data is stored in an array of objects, you can use the dataset
helper prop.
It accepts an array of objects such as dataset={[{a: 1, b: 32, c: 873}, {a: 2, b: 41, c: 182}, ...]}
.
You can reuse this data when defining the series.
The scatter series work a bit differently than in other charts.
You need to specify the datasetKeys
properties which is an object that requires the x
and y
keys.
With an optional id
and z
keys if needed.
{{"demo": "ScatterDataset.js"}}
Since scatter elements can be small, interactions do not require hovering exactly over an element. When the pointer is in the drawing area, the closest scatter element will be used for interactions (tooltip or highlights). To do so, the chart computes Voronoi cells which map the pointer position to the closest element.
You can define a maximal radius with the voronoiMaxRadius
prop.
If the distance with the pointer is larger than this radius, no item will be selected.
Or set the disableVoronoi
prop to true
to trigger interactions only when hovering exactly over an element instead of Voronoi cells.
{{"demo": "VoronoiInteraction.js"}}
Scatter Chart provides an onItemClick
handler for handling clicks on specific scatter items.
It has the following signature.
const onItemClick = (
event, // The mouse event.
params, // An object that identifies the clicked elements.
) => {};
{{"demo": "ScatterClick.js"}}
If disableVoronoi=true
, users need to click precisely on the scatter element, and the mouse event will come from this element.
Otherwise, the click behavior will be the same as defined in the interaction section and the mouse event will come from the svg component.
As with other charts, you can modify the series color either directly, or with the color palette.
You can also modify the color by using axes colorMap
which maps values to colors.
The scatter charts use by priority:
- The z-axis color
- The y-axis color
- The x-axis color
- The series color
:::info
The z-axis is a third axis that allows to customize scatter points independently from their position.
It can be provided with zAxis
props.
The value to map can either come from the z
property of series data, or from the zAxis data.
Here are three ways to set z value to 5.
<ScatterChart
// First option
series={[{ data: [{ id: 0, x: 1, y: 1, z: 5 }] }]}
// Second option
zAxis={[{ data: [5] }]}
// Third option
dataset={[{ price: 5 }]}
zAxis={[{ dataKey: 'price' }]}
/>
:::
Learn more about the colorMap
properties in the Styling docs.
{{"demo": "ColorScale.js"}}
You can add a grid in the background of the chart with the grid
prop.
See Axis—Grid documentation for more information.
{{"demo": "GridDemo.js"}}
The shape of points in a scatter chart can be customized by passing a component to the marker
slot.
If you want the legend and tooltip to match, then you also need to customize the labelMarkType
of each series, as shown in the example below.
{{"demo": "ScatterCustomShape.js"}}
You can customize the size of points in a scatter chart using the markerSize
prop of every series.
For circles, the markerSize
is the radius of the point in pixels.
{{"demo": "ScatterCustomSize.js"}}
You can customize the plotting of the data in a scatter chart by providing custom components as children
of the ScatterChart
component.
A scatter chart's series can be accessed through the useScatterSeries
hook.
This hook returns the order of the series and information about the series themselves, including their data points, color, etc.
See Custom components to learn how to further customize your charts.
{{"demo": "CustomScatter.js"}}