0

I'm using a pie chart in my activity. When I set a big value into the entries, it doesn't show the value correctly.

For example:

My value is 627050000 but the chart shows is 627049986,

or

my value is 477470000 but the chart shows is 477470016.

Here's my code

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    double num1 = 627050000 ;
    double num2 = 477470000 ;

    setupPieChart();
}

private void setupPieChart() {

    List<PieEntry> pieEntries = new ArrayList<>();

    pieEntries.add(new PieEntry((float) num1 , "num1"));
    pieEntries.add(new PieEntry((float) num2 , "num2"));

    PieDataSet dataSet = new PieDataSet(pieEntries , "numbers");

    PieData data = new PieData(dataSet);
    PieChart pieChart = (PieChart) findViewById(R.id.piechart);
    pieChart.setTransparentCircleAlpha(1);
    pieChart.getDescription().setEnabled(false);

    //add legend to chart

    Legend legend = pieChart.getLegend();
    legend.setForm(Legend.LegendForm.CIRCLE);
    legend.setOrientation(Legend.LegendOrientation.VERTICAL);
    legend.setDirection(Legend.LegendDirection.RIGHT_TO_LEFT);
    legend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);

    //add colors to dataset

    ArrayList<Integer> colors = new ArrayList<>();
    colors.add(Color.CYAN);
    colors.add(Color.GREEN);
    colors.add(Color.RED);

    dataSet.setColors(colors);

    pieChart.setData(data);
    pieChart.animateY(1000);
    pieChart.invalidate();
}

I read about ValueFormatter class also using BigDecimal but I didn't understand. Please help and explain in a simple way.

Thanks.

kit
  • 1,166
  • 5
  • 16
  • 23
Hamid
  • 51
  • 7

1 Answers1

0

That happens because you convert double numbers into float. MPAndroidChart will draw an entry with a float value. You define double num1 = 627050000; but you store it as new PieEntry((float) num1 , "num1")

Note that:

float: The float data type is a single-precision 32-bit IEEE 754 floating point....use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead

double: The double data type is a double-precision 64-bit IEEE 754 floating point. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

(source)

In the aforementioned conversion you lose in the precision of the number. Define your values as float instead of double

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39
  • thanks. but i think the problem is related to the library because i added a toast to show the value of num1 and num2 after casting to float and it was correct. although these numbers are just for test and I get numbers from sqlite table which are doubles. – Hamid Nov 17 '18 at 20:13
  • how can i use big decimal or flaot here ? most of the numbers in my app are doubles – Hamid Nov 17 '18 at 20:54
  • float num1 = 627050000 ; float num2 = 477470000 ; – Nikos Hidalgo Nov 18 '18 at 00:05
  • are you still getting the same values as before? – Nikos Hidalgo Nov 18 '18 at 14:30
  • no i set float num1 = 627050000 ; . then pieEntries.add(new PieEntry(num1 , "num1")); bud still doesnt work . – Hamid Nov 18 '18 at 15:23