Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chart values is rounded when present on mpandroidchart

This is my list of my BarEntry On MPAndroidChart:v3.0.3 enter image description here

as you can see its values on 3 index of array. My question is why when these values shows on chart their values is rounded ? enter image description here

this my codes:

 barEntriesCur.add(new BarEntry(1, (float) Double.parseDouble(incomeModel.getIncome_Payable_Cur()) / 1000000));
 barEntriesCur.add(new BarEntry(2, (float) Double.parseDouble(incomeModel.getNosazi_Payable_Cur()) / 1000000));
 barEntriesCur.add(new BarEntry(3, (float) Double.parseDouble(incomeModel.getSenfi_Payable_Cur()) / 1000000));

 barEntriesPre.add(new BarEntry(1, (float) Double.parseDouble(incomeModel.getIncome_Payable_Pre()) / 1000000));
 barEntriesPre.add(new BarEntry(2, (float) Double.parseDouble(incomeModel.getNosazi_Payable_Pre()) / 1000000));
 barEntriesPre.add(new BarEntry(3, (float) Double.parseDouble(incomeModel.getSenfi_Payable_Pre()) / 1000000));


BarDataSet barDataSet = new BarDataSet(barEntriesCur, "cur");
barDataSet.setColors(AXisValueFormatter.SAFACUSTOM_COLORS2);
BarDataSet barDataSet1 = new BarDataSet(barEntriesPre, "pre");
barDataSet1.setColors(AXisValueFormatter.SAFACUSTOM_COLORS3);
BarData data = new BarData(barDataSet, barDataSet1);
mBinding.barChartReport.setData(data);
float groupSpace = 0.20f;
float barSpace = 0.02f;
float barWidth = 0.35f;
data.setBarWidth(barWidth);
data.setValueTextSize(10f);
mBinding.barChartReport.groupBars(1, groupSpace, barSpace);

mBinding.barChartReport.getAxisLeft().setStartAtZero(true);
mBinding.barChartReport.setPinchZoom(false);
mBinding.barChartReport.setBackgroundColor(Color.TRANSPARENT); 
mBinding.barChartReport.setDrawGridBackground(false);
mBinding.barChartReport.getDescription().setEnabled(false);
mBinding.barChartReport.setFitBars(true);
mBinding.barChartReport.invalidate();
like image 806
Cyrus the Great Avatar asked Sep 03 '26 23:09

Cyrus the Great


1 Answers

Implemented your code and here is solution. But first explanation: This happens because values are in big range, you have 5595 and 590 on the same Graph, so default value formatter removes decimals, because it thinks it is not important.

Solution is to implement your custom formatter:

public class MyDecimalValueFormatter implements IValueFormatter {
        @Override
        public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
            return String.valueOf(value);
        }
    }

And then set it to dataset:

data.setValueFormatter(new MyDecimalValueFormatter());

Tested with your code and it works (with your data):

enter image description here

like image 137
Misha Akopov Avatar answered Sep 05 '26 16:09

Misha Akopov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!