How to set max and min value for Y axis

后端 未结 17 2037
既然无缘
既然无缘 2020-11-27 11:08

I am using line chart from http://www.chartjs.org/ \"enter

As you can see max value (1

相关标签:
17条回答
  • 2020-11-27 11:41
    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    
                        }
                      }]
                   }
                }
    });
    

    I configure with 'options' in v2.

    You should read documentation: http://www.chartjs.org/docs/#scales-linear-scale

    0 讨论(0)
  • 2020-11-27 11:41

    I wrote a js to display values from 0 to 100 in y-axis with a gap of 20.

    This is my script.js

    //x-axis
    var vehicles = ["Trucks", "Cars", "Bikes", "Jeeps"];
    
    //The percentage of vehicles of each type
    var percentage = [41, 76, 29, 50];
    
    var ctx = document.getElementById("barChart");
    var lineChart = new Chart(ctx, {
      type: 'bar',
      data: {
        labels: vehicles,
        datasets: [{
          data: percentage,
          label: "Percentage of vehicles",
          backgroundColor: "#3e95cd",
          fill: false
        }]
      },
      options: {
        scales: {
          yAxes: [{
            ticks: {
              beginAtZero: true,
              min: 0,
              max: 100,
              stepSize: 20,
            }
          }]
        }
      }
    });

    This is the graph displayed on the web.

    0 讨论(0)
  • 2020-11-27 11:42

    You have to overrride the scale, try this: (applies to ChartJS v1.x)

    window.onload = function(){
        var ctx = document.getElementById("canvas").getContext("2d");
        window.myLine = new Chart(ctx).Line(lineChartData, {
            scaleOverride : true,
            scaleSteps : 10,
            scaleStepWidth : 50,
            scaleStartValue : 0 
        });
    }
    
    0 讨论(0)
  • 2020-11-27 11:42

    ChartJS v2.4.0

    As shown in the examples at https://github.com/jtblin/angular-chart.js on the 7th of febuary 2017 (since this seems to be subject to frequent change):

    var options = {
        yAxes: [{
            ticks: {
                min: 0,
                max: 100,
                stepSize: 20
            }
        }]
    }
    

    This will result in 5 y-axis values as such:

    100
    80
    60
    40
    20
    0
    
    0 讨论(0)
  • 2020-11-27 11:44

    For chart.js V2 (beta), use:

    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.
                }
            }]
        }
    };
    

    See chart.js documentation on linear axes configuration for more details.

    0 讨论(0)
提交回复
热议问题