繁体   English   中英

如何使饼图具有交互性? 没有得到charts_flutter的饼图onChangedListener回调

[英]How to make pie chart interactive? Not getting charts_flutter's pie chart onChangedListener callback

我正在使用charts_flutter的自动label饼图。 我需要在点击/选择段时获取所选饼图段的数据值。 但我不会在 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);
    }
  }

首先,您必须将defaultInteractions设置为true并将SelectionModelType更改为info

@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之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM