0
votes

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?

1
How can expense be negative? Some rich. - AlphaQ
@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
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
@AlphaQ I have line and bar charts option along with the pie chart.How can i show negative labels in pie chart?? - KJEjava48
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. - Spektakulatius

1 Answers

0
votes

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) + " %";
    }
}