i`m seeing the percentage without the "%" symbol in the chart.
how do i fix it? this is code im creating xVals and yVals:
for (int i = 0; i < parentArray.length(); i++) {
JSONObject finalObject = parentArray.getJSONObject(i);//get the current json object which is representaion of roomate object
yVals1.add(new Entry(((float) finalObject.getDouble("amountPayed")), pos));
xVals.add(finalObject.getString("firstName"));
}
here i`m setting the prefrences:
public void setPieChartPrefrences() {
mPieChartData.setUsePercentValues(true);
mPieChartData.setHoleColor(Color.rgb(235, 235, 235));
mPieChartData.setDescription("");
mPieChartData.setDrawCenterText(true);
mPieChartData.setRotationAngle(0);
mPieChartData.setRotationEnabled(true);
mPieChartData.setCenterText("讻诪讛 砖讬诇诪谞讜");
mPieChartData.setCenterTextSize(25);
mPieChartData.setCenterTextColor(Color.parseColor("#02438b"));
mPieChartData.animateXY(1000, 1000);
mPieChartData.setCenterTextSize(10f);
}
You need to add a formatter to draw the % sign.
https://github.com/PhilJay/MPAndroidChart/wiki/The-ValueFormatter-interface
pieData.setValueFormatter(new PercentFormatter());
still not displaying %
With the latest version (3.1.0), you should also set pieChart.setUsePercentValues(true).
data.setValueFormatter(new PercentFormatter());
pieChart.setUsePercentValues(true);
If you do not initialise your PercentFormatter with the pie chart it will not render the %
The correct way to do this (if you want percentage sign displayed) is:
pieData.setValueFormatter(new PercentFormatter(pieChart))
pieChart.setUsePercentValues(true)
The comment in the PercentageFormatter is really missleading on the constructor that takes the PieChart
// Can be used to remove percent signs if the chart isn't in percent mode
seeing as you need it set to have the %
This will work well For percentage in formatted mode. MP Android Library 3.1.0
public class MyValueFormatter extends PercentFormatter {
DecimalFormat mFormat;
PieChart mPieChart;
public MyValueFormatter(DecimalFormat format, PieChart pieChart){
mFormat = format;
mPieChart = pieChart;
}
@Override
public String getFormattedValue(float value) {
return mFormat.format(value) + "%";
}
@Override
public String getPieLabel(float value, PieEntry pieEntry) {
if (mPieChart != null && mPieChart.isUsePercentValuesEnabled()) {
// Converted to percent
return getFormattedValue(value);
} else {
// raw value, skip percent sign
return mFormat.format(value);
}
}
}
Use above class in your code.
pieData.setValueFormatter(new MyValueFormatter(mFormat, pieChart));
pieChart.setUsePercentValues(true);
User Above line to for formated percent value. pass percent format and piechart.
Most helpful comment
If you do not initialise your
PercentFormatterwith the pie chart it will not render the %The correct way to do this (if you want percentage sign displayed) is:
The comment in the
PercentageFormatteris really missleading on the constructor that takes thePieChartseeing as you need it set to have the %