0

In my Android application I have to show my income and expense amounts in a piechart. The expense amounts will be negative. The amounts are of type float. So is it possible to display both negative and positive values in a pie chart using MPAndroidChart library?

I have tried with some negative and positive integer values and the result is horible. The pie-chart will behave crazy like a rummy board or illumination screen.

If I can't have negative values in a pie chart, is it possible to show the labels in negative by even though the actual values are positive?

David Rawson
  • 20,912
  • 7
  • 88
  • 124
KJEjava48
  • 1,967
  • 7
  • 40
  • 69
  • How can expense be negative? Some rich. – AlphaQ Dec 10 '16 at 11:44
  • @AlphaQ that not the issue here.If not expense i have something like credit cards and loans that always have negative values, So its not have to bother about expense having negative value.The question is about showing negative and positive values in piechart. – KJEjava48 Dec 10 '16 at 11:49
  • 1
    You **cannot** have negative values in a pie chart. All the values should add up to 100 and negative values ain't gonna help. So, try using negative labels or use line/bar charts (recommended). – AlphaQ Dec 10 '16 at 11:51
  • @AlphaQ I have line and bar charts option along with the pie chart.How can i show negative labels in pie chart?? – KJEjava48 Dec 10 '16 at 11:57
  • @AlphaQ Yes we have both bar chart and line chart.We also like to implement a piechart. So is any way to implement piechart.Its only a matter to show the value is -ve from the piechart.So its ok with showing the label with -ve.So is there any way to do so?? – KJEjava48 Dec 10 '16 at 12:12
  • Refer [here](http://stackoverflow.com/questions/27566916/remove-description-from-pie-chart/27638188#27638188). – AlphaQ Dec 10 '16 at 12:18
  • @AlphaQ that link is all about disabling the text.Nothing specified about show negavtive values. – KJEjava48 Dec 10 '16 at 12:21
  • This question is similar to this one: https://stackoverflow.com/questions/38922125/android-mpandroidchart-how-to-customise-bar-values – David Rawson Dec 12 '16 at 08:41
  • 1
    I think he means not using a negativ value in the pie chart but show the absolute value and present it as negative. I think simply remember it as "negative value" and do a Math.abs(value) and attaching the "minus" or whatever sign to the text will provide the solution to you. Maybe you can color up negativ values with red and positive with black like the most users will expect. – Pwnstar Dec 12 '16 at 08:41
  • @NoFuchsGavin Yes u are right, I also found thats the only possible solution that i can apply if there is no possible way to create piechart with -ve values. – KJEjava48 Dec 12 '16 at 09:03

1 Answers1

0

You are essentially asking how to customise the labels on an MPAndroidChart chart. This can be done easily by writing your own implementation of the interface IValueFormatter.

IValueFormatter has one method that you need to implement:

getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler)`

You use the parameters of the method to construct the label you want as a String. You want a minus sign "-" in front so you simply do this:

return "-" + mFormat.format(value) + " %";

Here is the complete implementation:

public class NegativePercentFormatter implements IValueFormatter

    protected DecimalFormat mFormat;

    public NegativePercentFormatter() {
        mFormat = new DecimalFormat("###,###,##0.0");
    }


    public NegativePercentFormatter(DecimalFormat format) {
        this.mFormat = format;
    }

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        return "-" + mFormat.format(value) + " %";
    }
}
David Rawson
  • 20,912
  • 7
  • 88
  • 124