首页 > 解决方案 > Flutter BezierChart 数据问题

问题描述

我正在尝试在颤振中创建一个图表,其坐标或点会根据数组动态变化,但目前,当我使用 bezierChart 包执行此操作时,我收到以下问题,指出“元素类型'Set>'可以' t 分配给列表类型 'DataPoint'。” 此错误直接源自 for 循环行内部数据,您可以从我下面链接的代码片段中看到。有没有办法动态地将数据点传递给这个数据部分,这样我就可以拥有 X 数量的数据点,而不是拥有预定数量的数据点。

Container(
                  height: MediaQuery.of(context).size.height / 2,
                  width: MediaQuery.of(context).size.width * 0.9,
                  child: BezierChart(
                  bezierChartScale: BezierChartScale.MONTHLY,
                  fromDate: fromDate,
                  toDate: toDate,
                  selectedDate: toDate,
                  series: [
                    BezierLine(
                      label: "Weight Monthly Comparison",
                      onMissingValue: (dateTime) {
                        if (dateTime.month.isEven) {
                          return 0.0;
                        }
                        return 0.0;
                      },
                      data: [
                        //ERROR OCCURS INSIDE HERE
                          for (var i = 0; i < progressionList[index].weight.length; i++) 
                          {
                            DataPoint<DateTime>(value: (progressionList[index].weight[0]).toDouble(), xAxis: progressionList[index].timeupdate[0]),
                          }
                        //DataPoint<DateTime>(value: (progressionList[index].weight[0]).toDouble(), xAxis: progressionList[index].timeupdate[0]),
                        // DataPoint<DateTime>(value: (progressionList[index].weight[0]).toDouble(), xAxis: date1),
                        // DataPoint<DateTime>(value: (progressionList[index].weight[0]).toDouble(), xAxis: date2),
                        // DataPoint<DateTime>(value: (progressionList[index].weight[0]).toDouble(), xAxis: date3),
                        // DataPoint<DateTime>(value: (progressionList[index].weight[0]).toDouble(), xAxis: date4),
                        // DataPoint<DateTime>(value: (progressionList[index].weight[0]).toDouble(), xAxis: date5),
                        // DataPoint<DateTime>(value: (progressionList[index].weight[0]).toDouble(), xAxis: date6),
                      ],
                    ),
                  ],
                  config: BezierChartConfig(
                    verticalIndicatorStrokeWidth: 3.0,
                    verticalIndicatorColor: Colors.black26,
                    showVerticalIndicator: true,
                    verticalIndicatorFixedPosition: false,
                    backgroundColor: Colors.red,
                    footerHeight: 30.0,
                  ),
                ),
              )

标签: flutterdynamic

解决方案


创建一个单独的List方法来循环和返回整个列表。

最简单的例子之一,

List<DataPoint> makeDataPoints() {
 List<DataPoint> dataPointList = [];
 for (int i = 1; i < 10; i++) {
   dataPointList.add(DataPoint<DateTime>(
      value: i, xAxis: DateTime(2020, 10, 01, i, 00)));
 }
return dataPointList;
}

对循环条件和主体进行所有必要的修改,并从通常的数据点位置调用此方法,

数据:makeDataPoints()

series: [
  BezierLine(
    label: "Line Name",
    data: makeDataPoints(),
  ),
],

推荐阅读