我正在尝试使用 Y 轴(垂直轴)具有自定义标签的 TimeSeriesChart。
我可以验证字符串 (' final List<dynamic> string = data.data['options'];
') 是否包含我期望的值。我什至可以像字符串 [0] 一样引用它们,但是当我这样做时 strings[value.toInt()]
它抛出 RangeError
.
请看这段代码。
在我初始化“最终标签”的地方,我有两行代码定义了 BasicNumericTickFormatterSpec
.
第一行是我想要做的。我想使用 value
在 strings[]
中查找字符串.
我能做到((num value) => 'MyValue: ${string[0]}')
效果很好。
我能做到((num value) => 'MyValue: ${string}')
只是为了输出每个“步骤”的所有值。
所以,基本上我可以验证 string
实际上保存了我期望的值,并且我可以访问这些值,但是如果我尝试查找一个值,((num value) => 'MyValue: ${string[value.toInt()]}')
然后它会因这个 RangeError 而中断。
Widget _buildChart(BuildContext context, DocumentSnapshot data) {
final List<dynamic> string = data.data['options'];
final labels =
charts.BasicNumericTickFormatterSpec((num value) => 'MyValue: ${string[value.toInt()]}');
// charts.BasicNumericTickFormatterSpec((num value) => 'MyValue: ${string[0]}')
var chart = charts.TimeSeriesChart(seriesList,
// Sets up a currency formatter for the measure axis.
primaryMeasureAxis: new charts.NumericAxisSpec(tickFormatterSpec: labels),
animate: animate,
// Optionally pass in a [DateTimeFactory] used by the chart. The factory
// should create the same type of [DateTime] as the data provided. If none
// specified, the default creates local date time.
dateTimeFactory: const charts.LocalDateTimeFactory());
return new Padding(
padding: new EdgeInsets.all(10.0),
child: new SizedBox(
height: 200.0,
child: chart,
));
}
我期望我放入 ((num value) => '$strings[value.toInt()])
的每个“值”将从 strings[] 返回一个字符串。
相反,我得到:
flutter: The following RangeError was thrown during performLayout(): flutter: RangeError (index): Invalid value: Not in range 0..1, inclusive: 2
最佳答案
尝试扩展您的 tick 格式化程序闭包,以便获得更多诊断信息:
final labels = charts.BasicNumericTickFormatterSpec((num value) {
var index = value.floor();
print('--- tick for $value in $string with $index ---');
return (index < string.length)
? 'MyValue: ${string[index]}'
: 'overflow ${string.length} $index';
});
关于flutter - 使用 Flutter 图表添加自定义 primaryMeasureAxis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56338022/