我正在使用http://www.chartjs.org/上的折线图

正如你可以看到Y轴的最大值(130)和最小值(60)是自动选择的,我想要最大值= 500和最小值=0。这可能吗?


当前回答


>最佳解决方案


  "options":{
                    scales: {
                                yAxes: [{
                                    display: true,
                                    ticks: {
                                        suggestedMin: 0, //min
                                        suggestedMax: 100 //max 
                                    }
                                }]
                            }
                    }

其他回答

window.onload = function(){
var ctx = document.getElementById("canvas").getContext("2d");
window.myLine = new Chart(ctx ,{
          type: 'line',
          data: yourData,
          options: {
            scales: {
                yAxes: [{
                    ticks: {
                        beginAtZero:true,
                        min: 0,
                        max: 500    
                    }
                  }]
               }
            }
});

我在v2中配置了“选项”。

你应该阅读文档: http://www.chartjs.org/docs/#scales-linear-scale

关于这个问题,有很多相互矛盾的答案,其中大多数对我没有任何影响。

我终于能够用chart.options.scales.xAxes[0].ticks设置(或检索当前)x轴最小和最大显示值。最小值(即使最小值和最大值只是分配给图表的数据的子集)。

在我的例子中使用时间刻度,我使用:

chart.options.scales.xAxes[0].ticks.min = 1590969600000;  //Jun 1, 2020
chart.options.scales.xAxes[0].ticks.max = 1593561600000;  //Jul 1, 2020
chart.update();

(我发现不需要设置步长值或beginAtZero等)

上面的答案对我不起作用。可能从11年起,选项名称就发生了变化,但以下内容对我来说是有用的:

ChartJsProvider.setOptions
  scaleBeginAtZero: true

从最新版本v3.9.1开始,你可以像这样设置你的比例:

options:{
    scales:{
        y:{
            beginAtZero: true,
            max: 500 // values over 500 will be hidden, OR
            suggestedMax: 500, // maximum will be 500, unless there is a higher value
        }
    }
}

对于chart.js V2 (beta),使用:

var options = {
    scales: {
        yAxes: [{
            display: true,
            ticks: {
                suggestedMin: 0,    // minimum will be 0, unless there is a lower value.
                // OR //
                beginAtZero: true   // minimum value will be 0.
            }
        }]
    }
};

有关线轴配置的详细信息,请参阅chart.js文档。