默认情况下,chart.js 似乎均匀地间隔开其 x 标签,而不是在空间中绘制 (x,y) 对。
var options = {
type: 'line',
data: {
labels: [0, 1, 10],
datasets: [{
data: [1, 2, 1]
}]
},
}
这会将“1”标签放置在 x 轴上 0 和 10 之间的中间 ( here's a fiddle )。如何让它定位到 10 的 1/10?
最佳答案
根据 Chart.js xAxis linear scale : strange behavior您需要一个线性 x 轴,并以 x,y 对形式提供数据。
所以,它需要是:
var ctx = document.getElementById('chartJSContainer').getContext('2d');
var scatterChart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
label: 'Linear Dataset',
data: [{ x: 0, y: 1}
,{x: 1, y: 2 }
,{ x: 10, y: 1} ]
}]
},
options: {
scales: {
xAxes: [{
type: 'linear'
,position: 'bottom'
}]
}
}
});
关于javascript - Chart.js 折线图,具有正确间隔的 x 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55622348/