当 defaultRender 设置为非常基本的 ArcRendererConfig() 时,Flutter GoogleChart 饼图不会渲染

2024-03-17

图表按预期显示使用以下饼图小部件。

class ABCPieChart extends StatefulWidget {
  @override
  _ABCPieChartState createState() => _ABCPieChartState();
}

class _TABCPieChartState extends State<ABCPieChart> {
  List<charts.Series<ChartEntity, String>> _entities = List();

  _initData() {
    var values = [
      ChartEntity("Food", 30, Colors.greenAccent),
      ChartEntity("Clothing", 30, Colors.cyan),
      ChartEntity("Fashion", 20, Colors.red),
      ChartEntity("Gadgets", 20, Colors.blue),
    ];

    _entities.add(charts.Series(
        data: values,
        domainFn: (ChartEntity entity, _) => entity.title,
        measureFn: (ChartEntity entity, _) => entity.percentage,
        colorFn: (ChartEntity entity, _) =>
            charts.ColorUtil.fromDartColor(entity.color),
        id: "random chart",
        labelAccessorFn: (ChartEntity entity, _) => "${entity.percentage}"));
  }

  @override
  void initState() {
    super.initState();
    _entities = List<charts.Series<ChartEntity, String>>();
    _initData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 400.0,
      width: 400,
      child: charts.PieChart(
        _entities,
        animate: true,
        animationDuration: Duration(milliseconds: 500),
      ),
    );
  }
}

Without Default renderer But when I try to customize it to a donut shape, Even after adding a very basic defaultRenderer, the chart is not rendered in the screen anymore.

 class ABCPieChart extends StatefulWidget {
  @override
  _ABCPieChartState createState() => _ABCPieChartState();
}

class _TABCPieChartState extends State<ABCPieChart> {
  List<charts.Series<ChartEntity, String>> _entities = List();

  _initData() {
    var values = [
      ChartEntity("Food", 30, Colors.greenAccent),
      ChartEntity("Clothing", 30, Colors.cyan),
      ChartEntity("Fashion", 20, Colors.red),
      ChartEntity("Gadgets", 20, Colors.blue),
    ];

    _entities.add(charts.Series(
        data: values,
        domainFn: (ChartEntity entity, _) => entity.title,
        measureFn: (ChartEntity entity, _) => entity.percentage,
        colorFn: (ChartEntity entity, _) =>
            charts.ColorUtil.fromDartColor(entity.color),
        id: "random chart",
        labelAccessorFn: (ChartEntity entity, _) => "${entity.percentage}"));
  }

  @override
  void initState() {
    super.initState();
    _entities = List<charts.Series<ChartEntity, String>>();
    _initData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 400.0,
      width: 400,
      child: charts.PieChart(
        _entities,
        animate: true,
        animationDuration: Duration(milliseconds: 500),
        defaultRenderer: charts.ArcRendererConfig(),

      ),
    );
  }
}

即使我从谷歌示例中复制并粘贴代码here https://google.github.io/charts/flutter/example/pie_charts/auto_label,它不渲染。 (既不是热重载,也不是热重启,也不是冷重启)

这个问题仅与PieChart and ArcRendererConfig. BarChart with BarRendererConfig工作正常

我认为这可能是设置问题,或者可能错过了一个非常小但至关重要的东西,有人知道可能出了什么问题吗?

我已经在 Github 上交叉发布了这个问题here https://github.com/google/charts/issues/321,希望 GitHub 和 SO 中任何不相交的受众都能看到这个问题(我就是其中之一)。不是为了发送垃圾邮件或骚扰任何人。


您必须添加饼图的类型参数。

更新:- 解释

所以在OP的情况下,而不是child: charts.PieChart( use child: charts.PieChart<String>(.

we use <String>作为类型参数,因为图表的系列是charts.Series<ChartEntity, String>

如果系列是charts.Series<ChartEntity, int>那么我们应该使用<int>作为类型参数

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当 defaultRender 设置为非常基本的 ArcRendererConfig() 时,Flutter GoogleChart 饼图不会渲染 的相关文章

随机推荐