[英]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.