kamal kunwar
kamal kunwar

Reputation: 1

How to remove decimal value from BarChart MPAndroid Chart? I tried several methods but didn't work

enter image description hereI tried all methods but I could not remove the decimal from bar label. How can I remove decimal part from the bar label

      barData.setValueFormatter(new ValueFormatter() {
        @Override
        public String getFormattedValue(float value) {

            if (value > 0){
                return super.getFormattedValue((int)value);
            }else{
                return "";
            }
        }

    });

Upvotes: 0

Views: 546

Answers (1)

Lilya
Lilya

Reputation: 567

This might be the answer:

 @Override
        public String getFormattedValue(float value, AxisBase axis) {
            val df = DecimalFormat("#");
            df.roundingMode = RoundingMode.CEILING;
            return df.format(value);
}

Combining your code, it should look like this:

@Override
        public String getFormattedValue(float value, AxisBase axis) {
          if (value > 0) {
            val df = DecimalFormat("#");
            df.roundingMode = RoundingMode.CEILING;
            return df.format(value);
         } else {
            return "";
         } 
}    

I don't code in Java, so please, check for any syntax misspellings. :)

I reproduced it on my Line chart using Kotlin.

Read more about ValueFormatter in the docs of MPAndroidChart, please: https://weeklycoding.com/mpandroidchart-documentation/formatting-data-values/

Upvotes: 2

Related Questions