我认为您不能同时使用 maxTicksLimit 和 stepsize 。解决它的一种方法是在配置之前声明数据并在那里计算 stepSize。然后你可以从那里输入最大值、最小值和步长值。我也使用了fixedStepSize。
let data =[
{x:1, y:0.49},
{x:2, y:0.6},
{x:3, y:-0.18},
{x:4, y:0.76},
{x:5, y:0.48},
{x:6, y:0.76},
{x:7, y:0.1},
{x:8, y:-0.24},
{x:9, y:0.34},
{x:10, y:0.42},
{x:11, y:0.79},
{x:12, y:-0.21}
]
let values = data.map(item => item.y)
let min = Math.min(...values)
let max = Math.max(...values)
let stepSize = ((Math.abs(min) + max) / 2);
var config = {
type: 'scatter',
data: {
datasets: [{
label: "My First dataset",
backgroundColor: chartColors.red,
borderColor: chartColors.red,
showLine: true,
data: data,
lineTension: 0,
fill: false,
},]
},
options: {
responsive: true,
title: {
display: true,
text: 'Chart.js Line Chart'
},
tooltips: {
mode: 'label',
},
hover: {
mode: 'nearest',
intersect: true
},
scales: {
xAxes: {
display: true,
scaleLabel: {
display: true,
labelString: 'Test'
},
ticks:
{
min: 1,
max:17,
stepSize: 5,
},
distribution: 'linear',
},
yAxes: {
display: true,
beginAtZero: true,
type: 'linear',
max: max,
min: min,
ticks: {
fontColor: "#ffffff",
stepSize: stepSize,
fixedStepSize:stepSize,
},
scaleLabel: {
display: true,
labelString: 'Value'
}
}
}
}
};