MPAndroidChart: How to set x axis labels at fixed interval

sanjay picture sanjay · Apr 9, 2017 · Viewed 11.7k times · Source

I am displaying realtime chart which should display values as per second. My X axis is time in second. But I am not able to display fixed time interval in x axis ie. 0,1,2,... so on. X axis value is automatically calculated and time interval between two x values goes to 20 seconds which I don't want. I need your help to fix this.

Any help would be greatly appreciated.

Answer

Luke Smith picture Luke Smith · Jul 10, 2018

Theres an alternative solution to this that will work on the normal MPAndroidChart (iOS or Android). You can manually set the range covered by the axis, by using axisMinimum and axisMaximum properties. You can also set the number of axis labels by using setLabelCount. With a combination of these, you can get even intervals in-between axis labels. For example, with an axisMinimum of 0, an axisMaximum of 100, and setLabelCount set with 5 labels, you end up with a label at the top and bottom of the range (0 and 100 respectively), and 3 labels inbetween, which gives you a fixed gap of 25. Swift 3 code:

chart.xAxis.setLabelCount(5, /*force: */true)
chart.xAxis.axisMinimum = 100
chart.xAxis.axisMaximum = 0

Gives you even intervals : 0, 25, 50, 75, 100.