簡體   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