如何使饼图互动?没有在onChangedListener回调中获取charts_flutter的饼图

萨拉达·阿迪卡(Sarada Adhikarla)

我正在使用Charts_flutter的自动标签饼图。我需要在点击/选择分段时获取选定的饼图分段的数据值。但是我不是要在selectionModel的changedListener上获得回调。

var donutChart = new charts.PieChart(
      series,
      animate: true,
      animationDuration: Duration(milliseconds: 500),
      defaultInteractions: false,
      selectionModels: [
        new charts.SelectionModelConfig(
          type: charts.SelectionModelType.action,
          changedListener: _onSelectionChanged,
          updatedListener: _onSelectionUpdated,
        ),
      ],
      defaultRenderer: new charts.ArcRendererConfig(
        arcWidth: 65,
        arcRendererDecorators: [
          new charts.ArcLabelDecorator(),
        ],
      ),
    );

  _onSelectionChanged(charts.SelectionModel model) {
    print('In _onSelectionChanged');
    final selectedDatum = model.selectedDatum;
    print(selectedDatum.length);
    if (selectedDatum.first.datum) {
      print(model.selectedSeries[0].measureFn(model.selectedDatum[0].index));
      chartAmountText = selectedDatum[0].datum.totalSpend.toString().split('.');
    }
  }

 _onSelectionUpdated(charts.SelectionModel model) {
    print('In _onSelectionUpdated');
    if (selectedDatum.length > 0) {
      print(selectedDatum[0].datum.category);
    }
  }
Develocode 777

首先,您必须设置defaultInteractionstrue并将其更改SelectionModelTypeinfo

@override
  Widget build(BuildContext context) {
    return new charts.PieChart(
      seriesList,
      animate: animate,
      defaultInteractions: true,
      selectionModels: [
          new charts.SelectionModelConfig(
            type: charts.SelectionModelType.info,
            changedListener: _onSelectionChanged,
            updatedListener: _onSelectionUpdated
          ),
        ],
    
    );
  }

然后在_onSelectionChanged删除此条件,if (selectedDatum.first.datum)因为这不是一个bool并在之前_onSelectionUpdated添加final selectedDatum = model.selectedDatum;if

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章