user2659110
user2659110

Reputation:

How to hide zero values in pie chart - MPChart android

Is there way to suppress zero value text from MP android chart (pie chart)

Upvotes: 6

Views: 7025

Answers (4)

Kishan Solanki
Kishan Solanki

Reputation: 14618

Kotlin Developers:

class MyCustomValueFormatter : ValueFormatter() {
override fun getFormattedValue(value: Float): String {
    return if (value == 0f) {
        ""
    } else {
        value.roundToInt().toString() + " %"
    }
  }
}

and use this like

pieData.setValueFormatter(MyCustomValueFormatter())

Upvotes: 1

Ali Zarei
Ali Zarei

Reputation: 3753

Use custom formatter and return empty string if value==0.0f

public class CustomPercentFormatter implements IValueFormatter {

    private DecimalFormat mFormat;

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

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

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        if (value == 0.0f)
            return "";
        return mFormat.format(value) + " %";
    }
}

set formatter :

lineData.setValueFormatter(new CustomPercentFormatter());

Upvotes: 7

savepopulation
savepopulation

Reputation: 11921

You can implement a new Formatter for MPChart as Ali Zarei declared in his answer. But you may still need the digits setup option of library which already implemented in DefaultValueFormatter

To achieve this, you can just create a new formatter and extend it from DefaultValueFormatter and Override the getFormattedValue method of DefaultValueFormatter and check if the value is bigger than 0 or not.

public class NonZeroChartValueFormatter extends DefaultValueFormatter {

    public NonZeroChartValueFormatter(int digits) {
        super(digits);
    }

    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex,
                                    ViewPortHandler viewPortHandler) {
        if (value > 0) {
            return mFormat.format(value);
        } else {
            return "";
        }
    }
}

Upvotes: 3

Philipp Jahoda
Philipp Jahoda

Reputation: 51411

How about that?

if(value > 0) dataSet.add(...)
else {
   // do nothing
}

Or write your own ValueFormatter to suppress zeros.

Upvotes: 6

Related Questions