[英]Pie chart charts_flutter change on selection
I created pie-chart like this with the charts_flutter.我用 charts_flutter 创建了这样的饼图。 The text widget changes on chart selection with the
selectionModels->changedListener
.使用
selectionModels->changedListener
更改图表选择时的文本小部件。
So I have a question, is there a way to change the color or the arcWidth of the selected item, that a user can see, which item he selected.所以我有一个问题,有没有办法改变所选项目的颜色或弧宽,用户可以看到,他选择了哪个项目。 Or there exists another way how to show a user selected item?
或者存在另一种方式如何显示用户选择的项目?
Here is the code这是代码
class OutcomeChart extends StatefulWidget {
@override
_OutcomeChartState createState() => _OutcomeChartState();
Widget _buildLabel({String label, double money}) {
return Center(
child: SizedBox(
width: 150,
child: Text(
'$label\n$money',
overflow: TextOverflow.ellipsis,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 18),
),
),
);
}
}
class _OutcomeChartState extends State<OutcomeChart> {
String _categoryName = '';
double _money = 0;
List<charts.Series<Map, dynamic>> _createSampleData(List<Map> data) {
return [
new charts.Series<Map, dynamic>(
id: 'Sales',
domainFn: (Map item, _) => item['category_id'],
measureFn: (Map item, _) => item['money'],
data: data,
)
];
}
void _onSelectionChanged(charts.SelectionModel model) {
if (model.hasDatumSelection) {
final selectedDatum = model.selectedDatum.first;
setState(() {
_categoryName = selectedDatum.datum['category_name'];
_money = selectedDatum.datum['money'].toDouble();
});
}
}
@override
Widget build(BuildContext context) {
return Card(
child: SizedBox(
height: 450,
child: Stack(
children: [
BlocBuilder<HomePageBloc, HomePageState>(
builder: (context, state) {
var data = state is HomePageDataLoaded
? state.outcome
: <Map<dynamic, dynamic>>[];
var widgets = <Widget>[
charts.PieChart(
_createSampleData(data),
defaultInteractions: true,
animate: false,
behaviors: [
new charts.SelectNearest(),
charts.DomainHighlighter(),
],
selectionModels: [
charts.SelectionModelConfig(
type: charts.SelectionModelType.info,
changedListener: _onSelectionChanged,
)
],
defaultRenderer: charts.ArcRendererConfig(
arcWidth: 60,
),
)
];
if (0 == _money) {
var sum = 0;
data
.map((e) => e['money'])
.forEach((element) => sum += element);
widgets.add(widget._buildLabel(money: sum.toDouble(), label: 'All'));
} else {
widgets.add(
widget._buildLabel(money: _money, label: _categoryName));
}
return Stack(children: widgets);
},
),
],
),
),
);
}
}
You can use a behavior, like this:您可以使用一种行为,如下所示:
behaviors: [
new charts.InitialSelection(selectedDataConfig: [
new charts.SeriesDatumConfig<String>('Purchases', 'Eating Out'),
]),
new charts.DomainHighlighter(),
],
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.