[Chartjs]-How to set axes' step size in Chart.js 2?

24πŸ‘

How can I set step size here?

Straight from the samples (Linear Scale, step size):

By setting a stepSize value.

scales: {
  xAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Month'
    }
  }],
  yAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Value'
    },
    ticks: {
      min: 0,
      max: 100,

      // forces step size to be 5 units
      stepSize: 5 // <----- This prop sets the stepSize
    }
  }]
}

Here’s a live example:

var ctx = document.getElementById('chartJSContainer').getContext('2d')

new Chart(ctx, {
  type: 'line',
  data: {
    labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
    datasets: [
      {
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },  
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    scales: {
      yAxes: [{
        ticks: {
          reverse: false,
          stepSize: 3
        },
      }]
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.3.0/Chart.js"></script>
<body>
    <canvas id="chartJSContainer" width="600" height="400"></canvas>
</body>

0πŸ‘

this.data = {
labels: Xaxis,
datasets: [   
  
  {
    label: "Primary",
    data:Primyaxis,
    type: 'line',
    borderColor:'#eddb1c',
    backgroundColor:'#FFF3D6',
    fill: false,
    pointBorderColor: 'yellow',
    pointBackgroundColor: 'yellow',
    borderWidth: 1.5

  },
  {
    label: "Secondary",
    data: Secyaxis,
    type: 'line',
    borderColor:'#FF7A96',
    backgroundColor:'#EAC3CC',
    fill: false,
    pointBorderColor: 'red',
    pointBackgroundColor: 'red'
   
  }
 
 
   ]
};


   this.options = {
    scales: {
  xAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Extra Distance Interval From Origin'
    }
  }],
  yAxes: [{
    display: true,
    scaleLabel: {
      display: true,
      labelString: 'Percentage of Employees'
    },
    ticks: {
      // min: 50,
      max: 100,

      // forces step size to be 5 units
      stepSize: 2 // <----- This prop sets the stepSize
    }
  }]
},

  pan: {
    enabled: true,
  },
  zoom: {
    enabled: true,         
  },
responsive: true,
}}

Leave a comment