Dennis Dennis - 1 year ago 123
Android Question

How to format values inside MPAndroidChart?

2 Questions about the MPAndroidChart library.
All my yvalues are integers, but displayed as Decimals.
How can I get them displayed as integers (without the digits)?
How can I prevent that the ylabels are also shows as decimals?
I know there is a setFormatter for the yvalues, just don't understand how to use...

Answer Source

Have a look at the ValueFormatter interface provided by the library.

With that interface, you can custom format all values that are displayed in the chart (also y-labels). Simply create your own class that implements the interface and return whatever you want to be displayed.


chart.setValueFormatter(new YourValueFormatter());
YLabels yl = chart.getYLabels();
yl.setFormatter(new YourValueFormatter());

UPDATE (for versions 2.0.0+ of this library):

Now, a ValueFormatter can be set for each DataSet separately, or the same ValueFormatter can be set for the whole data object containig all DataSets. Furthermore, the YLabels class is now called YAxis.


// set for whole data object (individual DataSets also possible)
LineData data = new LineData(...);
data.setValueFormatter(new YourValueFormatter());

// YLabels are now called YAxis
YAxis yAxis = mChart.getAxisLeft(); // get the left or right axis
yAxis.setValueFormatter(new YourAxisValueFormatter());

UPDATE (for versions 3.0.0+ of this library):

The interfaces for formatting have been renamed and extended in their functionality. Now, the IAxisValueFormatter can be used to format values of both XAxis and YAxis.

Link to the IValueFormatter documentation.

Link to the IAxisValueFormatter documentation.