flutter - 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,
),
),
)
解决方案
创建一个单独的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(),
),
],
推荐阅读
- python - 使用 ruamel.yaml 加载 YAML 时复制别名以分隔 Python 对象
- javascript - Javascript 没有添加 innerText 值
- c - 即使没有按下键,C kbhit() 也会返回 1
- express - 测试项目(mocha+axios+chai)独立,如何统计服务端代码覆盖率
- react-native - @react-navigation/native useNavigation() 笑话
- python - 为什么 Selenium 无法在某些站点上找到元素?
- java - 应用程序在未使用已废弃的许可时崩溃 (KITKAT)
- pycharm - 如何在用户虚拟机上安装 Pycharm 及其默认设置
- c# - 将太多参数从 C# 传递到 Oracle 存储过程
- c++ - 如果尝试在链表程序中添加多个,为什么这段代码会忽略该条目?