How to change x-axis interval in chart.js?

Member

by jerad , in category: Javascript , 2 months ago

How to change x-axis interval in chart.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 2 months ago

@jerad 

To change the x-axis interval in Chart.js, you can customize the ticks on the x-axis by specifying the stepSize property in the scales option of the chart configuration object.


Here's an example code snippet to change the x-axis interval to 2:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
var chartData = {
  labels: ['January', 'February', 'March', 'April', 'May', 'June'],
  datasets: [{
    data: [10, 20, 15, 25, 30, 35]
  }]
};

var chartOptions = {
  scales: {
    x: {
      ticks: {
        stepSize: 2 // change the x-axis interval to 2
      }
    }
  }
};

var myChart = new Chart(document.getElementById('myChart'), {
  type: 'bar',
  data: chartData,
  options: chartOptions
});


In this example, the x-axis interval is set to 2 using the stepSize property in the ticks option of the x-axis scale. You can adjust the stepSize value to change the interval as needed for your chart.