从 firebase 检索数据并在 android studio MPAndroidChart 中绘制条形图 [重复]

Retrive data from firebase and draw barchart in android studio MPAndroidChart [duplicate]

提问人:Nouf 提问时间:11/15/2023 最后编辑:Frank van PuffelenNouf 更新时间:11/15/2023 访问量:13

问:

这是 firebase 中的数据:

enter image description here

在 Java 中,代码不会在图表中绘制列来表示停车的小时数和数量。

private void getDataDAY() {
//       we make for loop to matching

        ArrayList<String> X_realTime = new ArrayList<>();
        ArrayList<Integer> x = new ArrayList<>();
        ArrayList<Integer> y = new ArrayList<>();
        ArrayList<String> y_realTime = new ArrayList<>();
//        barlistX = new ArrayList<>();
//        axis = barChart.getXAxis();

        databaseReference1.child("AStatistics").addListenerForSingleValueEvent(new ValueEventListener() {

            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                //check if username is exist OR not in database
                final String NameDay = snapshot.child("Day").child("NameDay").getValue(String.class);
                final String hour = snapshot.child("Day").child("hour").getValue().toString();
                X_realTime.add(hour);
                final String NumOfParking = snapshot.child("Day").child("Num Parkinglot").getValue().toString();
                y_realTime.add(NumOfParking);
                //switch case each hour

                if (X_realTime.get(count).equals("7")) {
                    x.add(1);
                } else {
                    if (X_realTime.get(count).equals("8")) {
                        x.add(2);
                    } else {
                        if (X_realTime.get(count).equals("9")) {
                            x.add(3);
                        } else {
                            if (X_realTime.get(count).equals("10")) {
                                x.add(4);
                            } else {
                                if (X_realTime.get(count).equals("11")) {
                                    x.add(5);
                                } else {
                                    if (X_realTime.get(count).equals("12")) {
                                        x.add(6);
                                    } else {
                                        x.add(7);
                                    }
                                }
                            }

                        }

                    }
                }

                //for y


                if (y_realTime.get(count).equals("1")) {
                    y.add(1);
                } else {
                    if (y_realTime.get(count).equals("2")) {
                        y.add(2);
                    }
                }
                count++;

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }

        });

        barlistX = new ArrayList<>();
//      enter graph x-> number of parking

//   to format in general shape in X
        axis = barChart.getXAxis();


        for (int i = 0; i < x.size(); ++i) {
            barlistX.add(new BarEntry(x.get(i), y.get(i)));
        }
        axis.setValueFormatter(new IndexAxisValueFormatter(times));


        axis.setPosition(XAxis.XAxisPosition.BOTTOM);
        axis.setGranularity(1);
        Description XAxisDescription = new Description();
        XAxisDescription.setText(DayName);
        barChart.setDescription(XAxisDescription);
        axis.setGranularityEnabled(true);

    }
}

我想在条形图中绘制柱状图

java android firebase-realtime-database 条形图

评论


答: 暂无答案