1

I am trying to show a bar grpah (MPAndroidChart) which has values in decimal. but the graph shows them without decimal. My code for value formatter looks like this:

protected class MyValueFormatter implements ValueFormatter{
    @Override
    public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
        Log.d(Constants.TAG,"Initial value == "+value+" == "+entry.getVal());
        DecimalFormat df = new DecimalFormat("#.###");
        df.setRoundingMode(RoundingMode.CEILING);
        Log.d(Constants.TAG,"Formatted value == "+df.format(value));
        return df.format((double)value);
    }
}

Following is the data used to draw the graph chart

"data":[[16003376.986129051,10003344],[25089516.75475807,20089516],[39517705.32395167,30517705],[2490973.063333333,3090973]]

Following is the value formatter log

Initial value == 1.6003376E7 == 1.6003376E7
Formatted value == 16003376
Initial value == 1.0003344E7 == 1.0003344E7
Formatted value == 10003344
Initial value == 1.6003376E7 == 1.6003376E7
Formatted value == 16003376
Initial value == 1.0003344E7 == 1.0003344E7
......many more

My concern is:

  • The original value is converted from 16003376.986129051 to 1.6003376E7
  • Graph shows value on top of the bar as 16003376, since decimal handling is not implemented yet
  • How do i get the original value to convert it into 3 decimal figure since the decimal values are very important from financial point of view

Bar Chart

Bar Chart with marker view

Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
Prakash
  • 123
  • 15

1 Answers1

0

MPAndroidChart stores y-values inside an Entry as a float. As can be seen in this commonly referenced article on floating point, there are some numbers that cannot be accurately represented using floating points.

Instead of using the raw value of your parsed data as a value for your Entry, consider rounding the value to 3 decimal places first. This is a good idea anyway since we would like to maintain a separation of concerns between our model layer and our view layer and Entry is essentially part of the view layer. If you do this, your ValueFormatter will perform as expected.

So, when you are making the entries. Don't do this:

float rawX = rawDataSource.getFloat(0);
float rawY = rawDataSource.getFloat(1);
BarEntry entry = new BarEntry(rawX,rawY); //no!! need to round first

Do this instead:

float roundedX = (float) Math.round(rawDataSource.getFloat(0) * 100.0) / 100.0;
float roundedY = (float) Math.round(rawDataSource.getFloat(0) * 100.0) / 100.0;
BarEntry entry = new BarEntry(roundedX, roundedY);
David Rawson
  • 20,912
  • 7
  • 88
  • 124