微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

syncfusion_flutter_charts 没有提供我所有的数据

如何解决syncfusion_flutter_charts 没有提供我所有的数据

我正在尝试使用此图表包并从 firebase 检索数据 数据是由用户添加的,例如,如果 Alex 添加了 300 然后添加了 400 我将添加到 firebase 拖曳不同的文档,一个是 300,一个是 400,就像这张照片中的所有这些文档都是一个用户

enter image description here

因此,我想从每个用户的所有文档中检索所有值和日期,并提供包含这些信息的图表 注意:我的应用程序正在将老年用户与他的观察者联系起来,老年用户将从他的家中添加他的葡萄糖值,图表将显示在观察者之家这是我的代码,它运行正确,但我的图表中只有一列我试图打印列表长度,它给了我 4,它等于文档的数量

  Widget build(BuildContext context) {
    Future getElderlyId() async {
      await FirebaseFirestore.instance
          .collection('users')
          .doc(FirebaseAuth.instance.currentUser.uid)
          .get()
          .then((data) {
        setState(() {
          elderlyId = data.data()['elderlyId'];
        });
      });
    }

return Scaffold(
    body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('glucose')
            .where('uid',isEqualTo: elderlyId)
            .snapshots(),builder: (context,snapshot) {
          if (snapshot.data == null) {
            return Center(
              child: CircularProgressIndicator(),);
          }
          final doc = snapshot.data.docs;
          return ListView.builder(
              itemCount: 1,itemBuilder: (ctx,index) {
                List<ChartData> chartData = <ChartData>[];
                for (int i = 1; i <= doc.length; i++) {
                  chartData.add(
                      ChartData(doc[index]['date'],doc[index]['glucose']));
                }
                print(chartData.length);
                return Container(
                  child: Column(
                    children: [
                      Text(
                        "رسم بياني لمستويات الضغط خلال اسبوع",style: textStyle2,),Directionality(
                        textDirection: TextDirection.ltr,child: SfCartesianChart(
                            plotAreaBorderWidth: 0,margin: EdgeInsets.all(10),primaryXAxis: CategoryAxis(
                              maximumLabelWidth: 80,primaryYAxis: CategoryAxis(
                              maximumLabelWidth: 80,enableAxisAnimation: true,// borderColor: yellow,series: <CartesianSeries<ChartData,dynamic>>[
                              ColumnSeries<ChartData,dynamic>(
                                color: yellow,dataSource: chartData,xAxisName: "days",yAxisName: "values",// color: yellow,animationDuration: 20,xValueMapper: (ChartData gelu,_) =>
                                    gelu.xValue,yValueMapper: (ChartData gelu,_) =>
                                    gelu.yValue,)
                            ]),],);
              });
        }));

} }

class ChartData {
  ChartData(this.xValue,this.yValue);
  String xValue;
  double yValue;
}

这是我的输出,如果有人能给我解决方案,请只给我第一个文档的信息

enter image description here

解决方法

我也是这里 Syncfusion_charts 的用户。

错误主要是由您的 xValue 引起的,因为它们实际上是 String,并且在您的图表中,您将它们归类为 CategoryAxis。 因此,由于它们是相同的 String,因此只有一个值。

一种解决方案可能是:

更新您的 Firebase 后端,使其可以在您的文档中使用 DateTime 而不是 String。 更新您的 xValue 使其成为 DateTime

class ChartData {
  ChartData(this.xValue,this.yValue);
  DateTime xValue;
  double yValue;
}

然后,在图表中为 primaryXAxis 使用 DateTimeAxisDateTimeCategoryAxis

,

我认为你需要这样的东西:

Widget build(BuildContext context) {
    Future getElderlyId() async {
      await FirebaseFirestore.instance
          .collection('users')
          .doc(FirebaseAuth.instance.currentUser.uid)
          .get()
          .then((data) {
        setState(() {
          elderlyId = data.data()['elderlyId'];
        });
      });
    }

    return Scaffold(
        body: StreamBuilder(
            stream: FirebaseFirestore.instance
                .collection('glucose')
                .where('uid',isEqualTo: elderlyId)
                .snapshots(),builder:
                (BuildContext context,AsyncSnapshot<QuerySnapshot> snapshot) {
              if (snapshot.hasData) {
                // return Center(
                //   child: CircularProgressIndicator(),// );

                List<ChartData> chartData = [];
                for (int index = 0;
                    index < snapshot.data.docs.length;
                    index++) {
                  DocumentSnapshot documentSnapshot = snapshot.data.docs[index];
                  chartData.add(ChartData.fromMap(documentSnapshot.data()));
                }

                return Container(
                  child: Column(
                    children: [
                      Text(
                        "رسم بياني لمستويات السكر خلال اسبوع",style: textStyle2,),Directionality(
                        textDirection: TextDirection.ltr,child: SfCartesianChart(
                            plotAreaBorderWidth: 0,margin: EdgeInsets.all(10),// primaryXAxis: CategoryAxis(
                            //   maximumLabelWidth: 80,// ),primaryXAxis: DateTimeAxis(
                                // Interval type will be months
                                intervalType: DateTimeIntervalType.days,interval: 1),primaryYAxis: CategoryAxis(
                              maximumLabelWidth: 80,enableAxisAnimation: true,// borderColor: yellow,series: <CartesianSeries<ChartData,dynamic>>[
                              ColumnSeries<ChartData,dynamic>(
                                color: yellow,dataSource: chartData,xAxisName: "days",yAxisName: "values",// color: yellow,animationDuration: 20,xValueMapper: (ChartData gelu,_) =>
                                    gelu.xValue,yValueMapper: (ChartData gelu,_) =>
                                    gelu.yValue,)
                            ]),],);
              } else {
                return CircularProgressIndicator();
              }
            }));
  }
}

class ChartData {
  ChartData(this.xValue,this.yValue);
  ChartData.fromMap(Map<String,dynamic> dataMap)
      : xValue = dataMap['date'],yValue = dataMap['glucose'];
  Timestamp xValue;
  double yValue;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。