Mpandroidchart: Display int values in X Axis?

Created on 10 Aug 2016  路  6Comments  路  Source: PhilJay/MPAndroidChart

I've used previous versions of the lib and it is awesome. Now I started using 3.0.0. Beta 1 and I have some issues.
How can I display accurate integer values on X axis? getFormattedValue() of AxisValueFormatter returns float and I can't force it to display X axis grid lines and labels only on whole numbers.

Things like:
@Override public String getFormattedValue(float value, AxisBase axis) { return String.valueOf( (int) value); }

are not acceptable. I do not want to show "1" when the actual value is 1.1 or similar. The old x-index based approach for setting values and labels was way better for most use cases.

Any idea how can I force the X labels/grid lines on specific values - e.g. never show label on 1.3 but on 1, 2, 3.. etc. otherwise I may end up formatting the values and show to the user 1, 2, 3 when the actual values are 0.8, 2.1, 3.4 and I'll have different spacing between the vertical grid lines

Most helpful comment

This can be done with the granularity feature of the axis.

What this means is that you can control at which point the recalculation of the axis values should stop. If your minimum interval on the x-axis is e.g. 1, then call xAxis.setGranularity(1f), which will prevent the x-axis to recalculate any further.

Have a look at this formatter which is used in this example.

All 6 comments

i got the exact same problem

In the case above I'm talking about integer values but for another project I need to show dates - for each day I have a single value on X axis. I want to be sure that the vertical grid axis on X matches the x position of the value and thus display a label on the X axis with the date.

If I zoom in I don't want the labels and grid axis on X axis to be recalculated and changed - I want to keep their position so that I still have grid axis and label exactly on the value.

As you can see in the image below, grid axis, labels and values match exactly. The chart in the image is zoomed in. If I zoom out the grix axis, labels and values keep their alignment. (the chart in the lib is generated using older version of the library when index-based approach was used for adding Entries)
chart_1

This can be done with the granularity feature of the axis.

What this means is that you can control at which point the recalculation of the axis values should stop. If your minimum interval on the x-axis is e.g. 1, then call xAxis.setGranularity(1f), which will prevent the x-axis to recalculate any further.

Have a look at this formatter which is used in this example.

@PhilJay thanks for that, im to used to the old way of setting x axis values , plus lets say i have 21 x String points i want , and showing them initial is bloated, so i want to show a small figure and as they zoom in more x points are visible . How would i do that ?

_Edit_ Dw i got it , thanks for the libary

@PhilJay Thanks for the info. My initial tests are promising =)

Woa, thanks for this thread. I was searching exactly this, but was difficult to get this term to search.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

OnlyInAmerica picture OnlyInAmerica  路  3Comments

Nima9Faraji picture Nima9Faraji  路  3Comments

andreyfel picture andreyfel  路  3Comments

JungYongWook picture JungYongWook  路  3Comments

botondbutuza picture botondbutuza  路  3Comments