Search code examples
javaandroidgoogle-cloud-firestorempandroidchart

How to fix display of Count in MpAndroidchart


I'm having a bit of trouble since when I get a count from my firestore database, it displays 25 instead of 1. I can't seem to solve this problem. I think it shows 1/4 of the count, since when I tried to add only three counts the output displays only 33.33.

Here is my code, I have searched for ways to fix this but I don't seem to find ways to fix it.

private void setPieChart() {
    pieChart.setDrawHoleEnabled(true);
    pieChart.setUsePercentValues(true);
    pieChart.setEntryLabelTextSize(12);
    pieChart.setEntryLabelColor(Color.BLACK);
    pieChart.setCenterTextSize(24);
    pieChart.getDescription().setEnabled(false);

    Legend l = pieChart.getLegend();
    l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
    l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
    l.setOrientation(Legend.LegendOrientation.VERTICAL);
    l.setDrawInside(false);
    l.setEnabled(true);
}
private void loadPiechartData() {
    entries = new ArrayList<>();
    db.collection("charot")
            .whereEqualTo("electricity", "lugi")
            .count()
            .get(AggregateSource.SERVER).addOnCompleteListener(task -> {
                entries.add(new PieEntry(task.getResult().getCount(), "lugi"));

                db.collection("charot")
                        .whereEqualTo("mahal", "bawi")
                        .count()
                        .get(AggregateSource.SERVER).addOnCompleteListener(task2 -> {
                            entries.add(new PieEntry(task2.getResult().getCount(), "bawi"));

                            db.collection("charot")
                                    .whereEqualTo("basta", "ewan")
                                    .count()
                                    .get(AggregateSource.SERVER).addOnCompleteListener(task3 -> {
                                    entries.add(newPieEntry(task2.getResult().getCount(),"ewan"));
                                        db.collection("charot")
                                                .whereEqualTo("tuiton", "pwede")
                                                .count()
                                    .get(AggregateSource.SERVER).addOnCompleteListener(task4-> {
                                 entries.add(new PieEntry(task4.getResult().getCount(), "pwede"));
                                                    if (task.isComplete()) {
                                                    ArrayList<Integer> colors = new ArrayList<>();
                                  for (int color : ColorTemplate.MATERIAL_COLORS) {
                                                            colors.add(color); }
                                  for (int color : ColorTemplate.VORDIPLOM_COLORS) {
                                                     colors.add(color); }
                                  PieDataSet dataSet = new PieDataSet(entries, "");
                                  dataSet.setColors(colors);
                                  PieData data = new PieData(dataSet);
                                  data.setValueFormatter(new ValueFormatter() {
                                  @Override
                                  public String getFormattedValue(float value) {
                                  return String.valueOf((int) value)}
                                  });
                                 data.setDrawValues(true);
                                 data.setValueTextSize(12f);
                                 data.setValueTextColor(Color.BLACK);
                                 pieChart.setData(data);
                                 pieChart.invalidate();
                                 pieChart.animateY(1000, Easing.EaseInOutQuad);
                                                    }
                                                });
                                    });
                        });
            });

I'm still getting the hang of Charts and even my group couldn't fix it.

This is my database enter image description here

and this is the output

enter image description here


Solution

  • You are calling pieChart.setUsePercentValues(true); in setPieChart(), so it is showing percentages instead of the actual values. Remove that or set it to false if you want to see values.