【问题标题】:How to retrieve data from firestore into your MPChart like barchart and piechart in android studio如何在 android studio 中将数据从 firestore 检索到 MPChart 中,例如 barchart 和 piechart
【发布时间】:2021-06-09 14:44:07
【问题描述】:

// 这是我的条形图和饼图的 Java 代码我不知道如何将数据从 Firestore 检索到我的饼图或条形图中

public class StatisticFragment extends Fragment {

FirebaseAuth fAuth;
FirebaseFirestore db;
String userID;

LineChart lineChart;
LineData lineData;
LineDataSet lineDataSet;
ArrayList lineEntries;

BarChart weeklyIncomeBarchart;
ArrayList<BarEntry> barEntryArrayList;
ArrayList<String> labelsName;
ArrayList<WeeklyIncomeChart> weeklyIncomeCharts = new ArrayList<>();

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_statistic, container, false);

    getActivity().setTitle("Statistic");

    fAuth = FirebaseAuth.getInstance();
    db = FirebaseFirestore.getInstance();

    lineChart = v.findViewById(R.id.lineChart);
    getEntries();
    lineDataSet = new LineDataSet(lineEntries, "");
    lineData = new LineData(lineDataSet);
    lineChart.setData(lineData);
    lineDataSet.setColors(ColorTemplate.JOYFUL_COLORS);
    lineDataSet.setValueTextColor(Color.BLACK);
    lineDataSet.setValueTextSize(18f);

    //Weekly Income Barchart
    weeklyIncomeBarchart = v.findViewById(R.id.weeklyIncomeBarchart);

    barEntryArrayList = new ArrayList<>();
    labelsName = new ArrayList<>();
    fillWeeklyIncome();
    for(int i = 0; i < weeklyIncomeCharts.size(); i ++){
        String days = weeklyIncomeCharts.get(i).getDays();
        int income = weeklyIncomeCharts.get(i).getIncome();
        barEntryArrayList.add(new BarEntry(i, income));
        labelsName.add(days);
    }

    BarDataSet barDataSet = new BarDataSet(barEntryArrayList, "Days");
    barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);
    Description description = new Description();
    description.setText("Weekly Income");
    weeklyIncomeBarchart.setDescription(description);
    BarData barData = new BarData(barDataSet);
    weeklyIncomeBarchart.setData(barData);

    //set XAxis values formater
    XAxis xAxis = weeklyIncomeBarchart.getXAxis();
    xAxis.setValueFormatter(new IndexAxisValueFormatter(labelsName));

    //set position of labels(Days name)
    xAxis.setPosition(XAxis.XAxisPosition.TOP);
    xAxis.setDrawGridLines(false);
    xAxis.setDrawAxisLine(false);
    xAxis.setGranularity(1f);
    xAxis.setLabelCount(labelsName.size());
    xAxis.setLabelRotationAngle(270);
    weeklyIncomeBarchart.animateY(2000);
    weeklyIncomeBarchart.invalidate();

    return v;
}

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

private void fillWeeklyIncome(){
    weeklyIncomeCharts.clear();
    weeklyIncomeCharts.add(new WeeklyIncomeChart("Sunday", 150));
    weeklyIncomeCharts.add(new WeeklyIncomeChart("Monday", 200));
    weeklyIncomeCharts.add(new WeeklyIncomeChart("Tuesday", 250));
    weeklyIncomeCharts.add(new WeeklyIncomeChart("Wednesday", 300));
    weeklyIncomeCharts.add(new WeeklyIncomeChart("Thursday", 350));
    weeklyIncomeCharts.add(new WeeklyIncomeChart("Friday", 400));
    weeklyIncomeCharts.add(new WeeklyIncomeChart("Saturday", 450));
}

}

//这是我的xml代码:

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/holo_green_light">


        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <com.github.mikephil.charting.charts.LineChart
                android:id="@+id/lineChart"
                android:layout_width="match_parent"
                android:layout_height="200dp" />

            <com.github.mikephil.charting.charts.BarChart
                android:id="@+id/weeklyIncomeBarchart"
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:layout_marginTop="20dp"
                android:layout_below="@+id/lineChart"/>


        </RelativeLayout>



</FrameLayout>

//这个可以运行,但唯一的问题是我想使用firestore中的数据作为图表的数据但不知道怎么做

【问题讨论】:

  • 到目前为止你有什么尝试?
  • 我是 android studio 的初学者,所以我只能做饼图和条形图,但是从 firestore 中检索数据的部分对我来说很难在这种类型的 MPChart 上实现跨度>
  • 请添加您正在使用的代码,并告诉我们究竟是什么不符合您的预期。
  • 对不起,我是新来使用 stackoverflow 我已经发布了我的代码

标签: android android-studio google-cloud-firestore bar-chart pie-chart


【解决方案1】:

如果您想从 Firestore 中获取数据,可以使用以下代码作为示例:

DocumentReference docRef = db.collection("cities").document("SF");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
        if (task.isSuccessful()) {
            DocumentSnapshot document = task.getResult();
            if (document.exists()) {
                Log.d(TAG, "DocumentSnapshot data: " + document.getData());
            } else {
                Log.d(TAG, "No such document");
            }
        } else {
            Log.d(TAG, "get failed with ", task.getException());
        }
    }
});

您可以获取有关读取操作如何工作的更多信息here

最好的问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2021-10-25
    相关资源
    最近更新 更多