Chart.js Scatter Chart
A scatter chart consists of multiple coordinate points formed by two sets of data, examining the distribution of the points to determine whether there is some correlation between two variables or to summarize the distribution pattern of the points.
Data structure:
data: [{
x: 10,
y: 20
}, {
x: 15,
y: 10
}]
Scatter chart type The attribute is scatter.
const config = {
type: 'scatter',
data: data,
options: {
scales: {
x: {
type: 'linear',
position: 'bottom'
}
}
}
};
Next, let's create a simple scatter chart:
Example
const
ctx
=
document.
getElementById
(
'myChart'
)
;
const
data
=
{
datasets
:
[
{
label
:
'Scatter Chart Example'
,
data
:
[
{
x
:
10
,
y
:
0
}
,
{
x
:
0
,
y
:
10
}
,
{
x
:
10
,
y
:
5
}
,
{
x
:
0.5
,
y
:
5.5
}
]
,
backgroundColor
:
'rgb(255, 99, 132)'
}
]
,
}
;
const
config
=
{
type
:
'scatter'
,
data
:
data
,
options
:
{
responsive
:
true
,
// Make the chart responsive, adapting to window size changes
maintainAspectRatio
:
false
,
// Maintain the chart's original aspect ratio
scales
:
{
x
:
{
type
:
'linear'
,
position
:
'bottom'
}
}
}
}
;
const
myChart
=
new
Chart
(
ctx
,
config
)
;
Try it »
The output of the above example is: