饼图_选择时 Flutter 变化

7uhlpewt  于 2023-05-29  发布在  Flutter
关注(0)|答案(2)|浏览(134)

我用charts_flutter创建了这样的饼图。使用selectionModels->changedListener选择图表时,文本小部件会发生变化。
所以我有一个问题,有没有一种方法可以改变颜色或arcWidth的选定项目,用户可以看到,他选择的项目。或者存在另一种方式来显示用户选择的项目?

下面是代码

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

kmb7vmvb1#

我没有找到如何使用charts_flutter库来实现它,所以我将其更改为fl_chart。在这里你可以找到一些例子。

vaqhlq81

vaqhlq812#

你可以使用一个行为,像这样:

behaviors: [
    new charts.InitialSelection(selectedDataConfig: [
      new charts.SeriesDatumConfig<String>('Purchases', 'Eating Out'),
    ]),
    new charts.DomainHighlighter(),
  ],

相关问题