简体   繁体   English

饼图 charts_flutter 在选择时发生变化

[英]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);
              },
            ),
          ],
        ),
      ),
    );
  }
}

I didn't find how to do it with the charts_flutter library, so I change it to the fl_chart .我没有找到如何使用 charts_flutter 库进行操作,因此我将其更改为fl_chart And here you can find some examples. 在这里你可以找到一些例子。

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.

相关问题 如何从颤振(charts_flutter)在饼图上添加图例? - How to add a legend on pie chart from flutter (charts_flutter)? 如何更改饼图中的图例图标,我使用的是 charts_flutter 0.9.0 - How to change legend icon in pie chart, i am using charts_flutter 0.9.0 如何使饼图具有交互性? 没有得到charts_flutter的饼图onChangedListener回调 - How to make pie chart interactive? Not getting charts_flutter's pie chart onChangedListener callback 如何在charts_flutter插件上更改折线图背景颜色? - How to change line chart background color on charts_flutter plugin? 为什么我不能在 charts_flutter 中为饼图创建一个类字符串作为 domainFn - Why I cant make a class string as domainFn for pie chart in charts_flutter 如何在charts_flutter中创建一个空图表? - How to create an empty chart in charts_flutter? 是否可以使用charts_flutter包旋转饼图? - Is it possible to rotate pie charts with the charts_flutter package? 如何使用 Charts_flutter 库更改折线图中线条的平滑度(曲率)? - How Can I Change the Smoothness(Curvature) of the line in line chart using Charts_flutter library? 如何更改charts_flutter条形图中条形图的宽度? - How to change width of bar in bar charts in charts_flutter? 在图表中格式化时间标签_flutter时间序列图 - Format time labels in charts_flutter time series chart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM