PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, April 23, 2022

[FIXED] How to make pie chart interactive? Not getting charts_flutter's pie chart onChangedListener callback

 April 23, 2022     charts, flutter, pie-chart     No comments   

Issue

I am using charts_flutter's auto label pie chart. I need to get the selected pie chart segment's data value on tapping/selecting the segment. But I am not to getting callback on selectionModel's changedListener.

var donutChart = new charts.PieChart(
      series,
      animate: true,
      animationDuration: Duration(milliseconds: 500),
      defaultInteractions: false,
      selectionModels: [
        new charts.SelectionModelConfig(
          type: charts.SelectionModelType.action,
          changedListener: _onSelectionChanged,
          updatedListener: _onSelectionUpdated,
        ),
      ],
      defaultRenderer: new charts.ArcRendererConfig(
        arcWidth: 65,
        arcRendererDecorators: [
          new charts.ArcLabelDecorator(),
        ],
      ),
    );

  _onSelectionChanged(charts.SelectionModel model) {
    print('In _onSelectionChanged');
    final selectedDatum = model.selectedDatum;
    print(selectedDatum.length);
    if (selectedDatum.first.datum) {
      print(model.selectedSeries[0].measureFn(model.selectedDatum[0].index));
      chartAmountText = selectedDatum[0].datum.totalSpend.toString().split('.');
    }
  }

 _onSelectionUpdated(charts.SelectionModel model) {
    print('In _onSelectionUpdated');
    if (selectedDatum.length > 0) {
      print(selectedDatum[0].datum.category);
    }
  }

Solution

First you must set defaultInteractions to true and change the SelectionModelType to info:

@override
  Widget build(BuildContext context) {
    return new charts.PieChart(
      seriesList,
      animate: animate,
      defaultInteractions: true,
      selectionModels: [
          new charts.SelectionModelConfig(
            type: charts.SelectionModelType.info,
            changedListener: _onSelectionChanged,
            updatedListener: _onSelectionUpdated
          ),
        ],
    
    );
  }

then in _onSelectionChanged remove this condition if (selectedDatum.first.datum) since that is not a bool. And in _onSelectionUpdated add final selectedDatum = model.selectedDatum; before the if.



Answered By - Develocode 777
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing