How to retrieve data from Cloud Firestore (a DateTime Field and an array of strings field) using Flutter and then display this data on a chart

0

I am using Cloud Firestore as a database for my app which is being created using Flutter. I am trying to get 2 fields, one that is a DateTime and one that is an array of strings. However, I can't seem to figure out how to retrieve data from Firestore and then add that data as data for a chart(in this case a pie and line chart).

    class PieChartDataGraph {
      DateTime date;
      String books;
      PieChartDataGraph(this.date, this.books);
      PieChartDataGraph.fromJSON(Map<String, dynamic> json) {
        date = json['date'];
        books = json['books'];
      }
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = Map<String, dynamic>();
        data['date'] = this.date;
        data['books'] = this.books;
        return data;
      }
    }

Then I try and get the data from Firestore:

Future<List<PieChartDataGraph>>
                                                      getPieChartData() async {
                                                    FirebaseUser user =
                                                        await _auth
                                                            .currentUser();
                                                    var val = await Firestore
                                                        .instance
                                                        .collection("users")
                                                        .document(user.uid)
                                                        .collection("moments")
                                                        .orderBy('date')
                                                        .where('date',
                                                            isGreaterThanOrEqualTo: isWeekly
                                                                ? today.subtract(
                                                                    Duration(
                                                                        days: today
                                                                            .weekday))
                                                                : today.subtract(
                                                                    Duration(
                                                                        days:
                                                                            30)))
                                                        
                                                        .getDocuments();

                                                    var documents =
                                                        val.documents;

                                                    if (documents.length > 0) {
                                                      try {
                                                        return documents
                                                            .map((document) {
                                                          PieChartDataGraph
                                                              pieChartData =
                                                              PieChartDataGraph
                                                                  .fromJSON(Map<
                                                                          String,
                                                                          dynamic>.from(
                                                                      document
                                                                          .data));
                                                          return pieChartData;
                                                        });
                                                      } catch (e) {
                                                        return [];
                                                      }
                                                    }
                                                    return [];
                                                  }

How can I use this to get the data from the Firestore database and then return the data as data points for a chart dynamically (in this case for both a line and pie chart).

Pie Chart in my Code:

PieChart(
                                                                    PieChartData(
                                                                        borderData:
                                                                            FlBorderData(
                                                                          show:
                                                                              false,
                                                                        ),
                                                                        sectionsSpace:
                                                                            0,
                                                                        centerSpaceRadius:
                                                                            40,
                                                                        sections:
                                                                            showingSections()),
                                                                  ),

Data being used in Pie Chart (added manually)

List<PieChartSectionData> showingSections() {
  return List.generate(4, (i) {
    final double fontSize = 16;
    final double radius = 50;

    switch (i) {
      case 0:
        return PieChartSectionData(
          color: const Color(0xff0293ee),
          value: 40,
          title: '40%',
          radius: radius,
          titleStyle: TextStyle(
              fontSize: fontSize,
              fontWeight: FontWeight.bold,
              color: const Color(0xffffffff)),
        );
      case 1:
        return PieChartSectionData(
          color: const Color(0xfff8b250),
          value: 30,
          title: '30%',
          radius: radius,
          titleStyle: TextStyle(
              fontSize: fontSize,
              fontWeight: FontWeight.bold,
              color: const Color(0xffffffff)),
        );
      case 2:
        return PieChartSectionData(
          color: const Color(0xff845bef),
          value: 15,
          title: '15%',
          radius: radius,
          titleStyle: TextStyle(
              fontSize: fontSize,
              fontWeight: FontWeight.bold,
              color: const Color(0xffffffff)),
        );
      case 3:
        return PieChartSectionData(
          color: const Color(0xff13d38e),
          value: 15,
          title: '15%',
          radius: radius,
          titleStyle: TextStyle(
              fontSize: fontSize,
              fontWeight: FontWeight.bold,
              color: const Color(0xffffffff)),
        );
      default:
        return null;
    }
  });
}

Please note that I am looking at the FL Chart package.

Thank you for your help in advance.

flutter
dart
charts
google-cloud-firestore
asked on Stack Overflow Sep 20, 2020 by Nighthawk • edited Sep 22, 2020 by Nighthawk

1 Answer

0

you need to create some 'chart data model' and convert the retrieved firestore data to this custom data model. After that you can pass the 'chart data model' as argument and use it inside the chart widgets fields as you wish

answered on Stack Overflow Sep 20, 2020 by Omer Gamliel

User contributions licensed under CC BY-SA 3.0