Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change X-Axis Interval on Chart.js

I have a line graph on Chart.js and am trying to edit the intervals on the x-axis and the y-axis.

My y-axis intervals are functioning as expected, but my x-axis intervals are not.

I tried the following code

let myChart = new Chart(inputChart, {
    type: 'line',
    data: {
          labels: [1,2,....,360] // list of values from python script
          data: [360 random numbers here] 
    }
    options: {
        scales: {
            yAxes: [{
                id:'main-axis',
                ticks: {
                     stepSize: 40 // this worked as expected
                        }
                   }],
            xAxes: [{
                id: 'main-x-axis',
                ticks: {
                    stepSize: 30 // this did not work as expected
                }
            }]
        }
    }
})

With 360 datapoints, I just want to basically see 12 intervals (in increments of 30), but I am seeing 90 intervals in increments of 4 instead. Am I just using the wrong property for stepSize? If so what is the correct property?

like image 390
kashmoney Avatar asked Nov 28 '18 16:11

kashmoney


People also ask

How do you change the X-axis interval?

To change the interval between axis labels, expand Labels, and then under Interval between labels, select Specify interval unit and type the number you want in the text box.

Can the X-axis have different intervals?

The X-axis contains either strings of text or a date under each set of data points. You can choose to have this axis only display the text or date at certain intervals, but the process is a little different depending on what kind of axis you have.

How do you change the x-axis intervals in R plot?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

What is scales in Chart JS?

The linear scale is used to chart numerical data. It can be placed on either the x or y-axis. The scatter chart type automatically configures a line chart to use one of these scales for the x-axis. As the name suggests, linear interpolation is used to determine where a value lies on the axis.


1 Answers

It can be done using the maxTicksLimit option of xAxes, see this working fiddle -> http://jsfiddle.net/Lzo5g01n/3/

xAxes: [{
    type: 'time',
    ticks: {
        autoSkip: true,
        maxTicksLimit: 20
    }
}]
like image 134
Kunal Khivensara Avatar answered Dec 15 '22 18:12

Kunal Khivensara