1👍
You should change datasets to have pairs of numbers (x, y). See documentation.
var options = {
type: 'scatter',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [{x:12,y:7}, {x:19,y:11}, {x:3,y:5}, {x:5,y:8}, {x:2,y:3}, {x:3,y:7}],
borderWidth: 1,
showLine: true,
pointBackgroundColor: 'red',
borderColor: 'blue',
backgroundColor: 'blue',
}]
},
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
background-color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
0👍
The main problem in your code is the definition of the x-axis. The x-axis represents the labels
, which are strings. Therefore, you cannot define numeric min
, max
and ticks.stepSize
options.
In order to obtain a scatter chart with given data, you could change its type
to 'line'
and define showLine: false
on each dataset.
Please take a look at your amended code below. I changed y
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
fill: false,
showLine: false
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
fill: false,
showLine: false
}
]
},
options: {
scales: {
y: {
min: 0,
max: 50,
ticks: {
stepSize: 10
}
}
}
}
};
new Chart('chartJSContainer', options);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.0.0-beta/chart.min.js"></script>
<canvas id="chartJSContainer"></canvas>
Source:stackexchange.com