Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart: How to set x axis labels at fixed interval

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.

like image 664
sanjay Avatar asked Apr 09 '17 08:04

sanjay


2 Answers

You can use a modified version of the library that allows users to tell the graph which labels should be drawn.

See the example below:

leftAxis.setShowSpecificLabelPositions(true);
leftAxis.setSpecificLabelPositions(new float[]{0, 10, 20, 50, 100, 300});

Reference link: https://github.com/PhilJay/MPAndroidChart/pull/2692

Modified Library link: https://github.com/philippeauriach/MPAndroidChart/tree/custom-labels

like image 41
fgueli Avatar answered Sep 18 '22 12:09

fgueli


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 = 0
chart.xAxis.axisMaximum = 100

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

like image 150
Luke Smith Avatar answered Sep 19 '22 12:09

Luke Smith