Home > Net >  How to display data in arraylist from database on chart MPAndroidchart
How to display data in arraylist from database on chart MPAndroidchart

Time:12-04

Im trying to display the data i have in firestore database to my chart, ive set up an trial data on firestore to use. im new to using mpchart and firestore so im confused on how to display the data on the piechart and linegraph

This is my code so far



public class MainActivity extends AppCompatActivity {
    private PieChart pieChart;
    LineChart lineChart;
    LineData lineData;
    LineDataSet lineDataSet;
    ArrayList lineEntries;
    FirebaseFirestore db = FirebaseFirestore.getInstance();


    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pieChart = findViewById(R.id.piechartmain);
        setPieChart();
        loadPiechartData();

        lineChart = findViewById(R.id.linechart);
        lineChart.setTouchEnabled(true);
        lineChart.setPinchZoom(true);

        getEntries();
        lineDataSet = new LineDataSet(lineEntries, "");
        lineData = new LineData(lineDataSet);
        lineChart.setData(lineData);
        lineData.setDrawValues(true);
        lineData.setValueTextSize(12f);
        lineData.setValueTextColor(Color.BLACK);
        lineDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
        lineDataSet.setValueTextColor(Color.BLACK);
        lineDataSet.setValueTextSize(18f);
        lineChart.getDescription().setEnabled(false);



    }

    private void getEntries() {
        lineEntries = new ArrayList<>();
        lineEntries.add(new Entry(1f, 0));
        lineEntries.add(new Entry(2f, 1));
        lineEntries.add(new Entry(3f, 2));
        lineEntries.add(new Entry(4f, 3));
        lineEntries.add(new Entry(5f, 4));

    }

    private void setPieChart() {
        pieChart.setDrawHoleEnabled(true);
        pieChart.setUsePercentValues(true);
        pieChart.setEntryLabelTextSize(12);
        pieChart.setEntryLabelColor(Color.BLACK);
        pieChart.setCenterText("expenses");
        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() {


        ArrayList<PieEntry> entries = new ArrayList<>();
        String label = "type";

        db.collection("Warranty")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if (task.isSuccessful()) {
                            for (QueryDocumentSnapshot document : task.getResult()) {
                                Log.d("TAG", document.getId()   " => "   document.getData());
                            }
                        } else {
                            Log.d("TAG", "Error getting documents: ", task.getException());
                        }
                    }
                });

        entries.add(new PieEntry(0.2f,"food"));
        entries.add(new PieEntry(0.15f,"elec"));
        entries.add(new PieEntry(0.20f,"school"));
        entries.add(new PieEntry(0.12f,"entertainment"));
        entries.add(new PieEntry(0.25f,"Gas"));

        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, "monthly expense");
        dataSet.setColors(colors);

        PieData data = new PieData(dataSet);
        data.setDrawValues(true);
        data.setValueFormatter(new PercentFormatter(pieChart));
        data.setValueTextSize(12f);
        data.setValueTextColor(Color.BLACK);

        pieChart.setData(data);
        pieChart.invalidate();
        pieChart.animateY(1000, Easing.EaseInOutQuad);
    }

}

</androidx.constraintlayout.widget.ConstraintLayout>

here is my database enter image description here

im kinda lost on how to display data from firestore since i searched their query already, though i cant display it on the chart. im new so please guide me, thanks!

CodePudding user response:

If you need to have an ArrayList full of PieEntry objects, then you have to iterate through the result, as you already do, and add those objects to the list:

    db.collection("Warranty")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        List<PieEntry> entries = new ArrayList<>();
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            PieEntry entry = document.toObject(PieEntry.class);
                            entries.add()entry;
                        }
                        //Do what you need to do with the list.
                    } else {
                        Log.d("TAG", "Error getting documents: ", task.getException());
                    }
                }
            });

Please also note that Firebase API is asynchronous. So you cannot simply use the entries list outside the callback.

  • Related