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...
-
Have you solved it out? i mean... what did you put inside the getFormattedValue() method of the ValueFormatter interface? – gumuruh Jul 09 '15 at 09:37
2 Answers
Have a look at the IValueFormatter
interface provided by the library. With that interface, you can completely customize what gets displayed on the chart based on your own logic.
Usage:
chart.setValueFormatter(new YourValueFormatter());
YLabels yl = chart.getYLabels();
yl.setFormatter(new YourValueFormatter());
UPDATE (for versions 2.0.0+ of this [library][2]):
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
.
Example:
// 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][2]):
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
. The IValueFormatter
interface is used to customize chart values.
Link to the ValueFormatter documentation.

- 50,880
- 24
- 180
- 187
-
-
Phil, since when do YLabels support the setValueFormatter() method? I'm trying to do the same but there is no such method...(I need the LargeValueFormatter()) for my case, and it doesn't seem to work if I set the LargeValueFormatter(). to the chart itself either. – Mike Dec 02 '14 at 12:38
-
Are you using the latest version of the library (1.7.4)? https://github.com/PhilJay/MPAndroidChart/releases – Philipp Jahoda Dec 02 '14 at 12:39
-
I'm pretty sure I do. I updated the library on the 24th of November. And your latest release was on the 18th. – Mike Dec 02 '14 at 12:50
-
2First. I just noticed that the method name is changed for the YLabels to setFormatter(new YourValueFormatter()). You should update your answer as well so others won't be distracted as well. Thanks! – Mike Dec 02 '14 at 13:09
-
2Cutting off decimal positions doesn't change the labels correctly - after having 2.0 - 2.4 it changes to 2 - 2 (duplicates). Any ideas on how to correct it? – James Cameron Jan 11 '15 at 21:55
-
i used this a variable with NumberFormat type and set it into "NumberFormat.getIntegerInstance();" but still returned the Decimal once it's rendered. Any clues @JamesCameron ? – gumuruh Jul 09 '15 at 09:38
-
return Math.round(value)+""; in getFormattedValue worked for me! – Sudheesh Mohan Jun 14 '16 at 07:36
-
@PhilippJahoda how do I send my ArrayList which has x-axis labels to the axis value formatter. I've used `xAxis.setValueFormatter(new DayAxisValueFormatter(mChart));` which sets date as Jan 1st, Jan 2nd not the dates which I have – Prabs Feb 18 '17 at 05:50
-
I'm using `xAxis.setLabelRotationAngle(-30f);` which is adjusting the date based on the space and slanting it a bit, so that labels doesn't overlap. Amazing library @PhilippJahoda – Prabs Feb 18 '17 at 06:05
-
what if i want to replace numbers with string label? please suggests for it – Anand Savjani May 17 '17 at 12:35
-
-
1@PhilippJahoda The link to the IAxisValueFormatter points to a 404, could you fix this? – Abby Jun 28 '19 at 14:04
-
-
2_PercentFormatter_ class has 2 different constructors. Empty constructor did not help me although I used percent mode. If `pieData.setValueFormatter(new PercentFormatter());` doesn't show percentage icon, you should use second constructor like that : `pieData.setValueFormatter(new PercentFormatter(pieChart));` – TeachMeJava Dec 26 '19 at 20:25
If all you want is to change the number of decimal places on the values, the
DefaultValueFormatter
will suffice.
pieDataSet.setDefaultValueFormatter(new DefaultValueFormatter(digits = 1))
//where digits is the number of decimal places
The example above will format 88.65 as 88.7

- 3,456
- 2
- 28
- 32