erluxman
erluxman

Reputation: 19415

Flutter GoogleChart Pie chart does not render when defaultRender is set to very basic ArcRendererConfig()

Chart Displays as expected with the following PieChart widget.

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(),

      ),
    );
  }
}

After adding a defaultRenderer

Even when I copy and paste the code from google sample here, It does not render. (Neither in hotreload, nor in Hotrestart, nor Coldrestart)

This issue is only withPieChart and ArcRendererConfig. BarChart with BarRendererConfigworks fine

I think it might be setup issue or might have missed a very small but crucial stuff, does anyone have any idea what might have been wrong?

I have cross-posted this question in Github here, with a hope that this question will be seen by any disjoint set of audience in GitHub and SO(I am one of them). Not for spamming or annoying anybody.

Upvotes: 2

Views: 2159

Answers (4)

Jonah Sebright
Jonah Sebright

Reputation: 86

You have to add the type parameter of the PieChart.

Update:- Explanation

So in OP's case, instead of child: charts.PieChart( use child: charts.PieChart<String>(.

we use <String> as type param because the series for chart is charts.Series<ChartEntity, String>

if series is charts.Series<ChartEntity, int> then we should use <int> as type parameter

Upvotes: 2

Aatika Khan
Aatika Khan

Reputation: 5

simply remove the defaultRenderer and it will work

Upvotes: -1

Pedro Molina
Pedro Molina

Reputation: 1484

Here a solution possibly

I got with (is a barchart in my case):

 defaultRenderer: new charts.BarRendererConfig(
    fillPattern: charts.FillPatternType.solid,
    //customRendererId: "id",
    barRendererDecorator: charts.BarLabelDecorator(
        labelPosition: charts.BarLabelPosition.auto,
        labelAnchor: charts.BarLabelAnchor.middle),
    cornerStrategy: const charts.ConstCornerStrategy(10),
  ),

Upvotes: 0

DONOVAN
DONOVAN

Reputation: 1

In my case the problem was the same, but resolved by putting an id in defaultRenderer, e.g.:

customRendererId: 'novoId'

Upvotes: 0

Related Questions