Mpandroidchart: percentage without the "%" in pie chart

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

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);

}

Most helpful comment

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 %

All 7 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andreyfel picture andreyfel  路  3Comments

vishvendu picture vishvendu  路  3Comments

rohitkumarbhagat picture rohitkumarbhagat  路  3Comments

manucheri picture manucheri  路  3Comments

thanhcly920 picture thanhcly920  路  3Comments