google / charts

https://pub.dev/packages/charts_flutter
Apache License 2.0
2.8k stars 1.2k forks source link

[Question] How to set selection programatically in charts_flutter #208

Open angel1st opened 5 years ago

angel1st commented 5 years ago

I would like to set selection programatically but not only in its initial state. I am aware about user managed selection sample, however it shows how to reset the selection, but it is not clear to me how shall I amend the following line of code _myState.selectionModels[charts.SelectionModelType.info] = new charts.UserManagedSelectionModel(); in case I want to select lest say ('Sales', '2015') instead of simply resetting the selection.

northelks commented 5 years ago

@angel1st looks like it cannot be done programatically https://github.com/google/charts/blob/master/charts_common/lib/src/chart/common/selection_model/selection_model.dart#L199

Only via subscribe to listeners.

Due to the way widgets are constructed in Flutter, there currently isn't a way for users to programmatically specify the selection...

But if you have some another solution for it that would be interesting to know.

GabRoyer commented 3 years ago

See this test for an example of how to do it : https://github.com/google/charts/blob/master/charts_flutter/test/user_managed_state_test.dart

The relevant parts are

     final onTapSelection =
        new charts.UserManagedSelectionModel<String>.fromConfig(
            selectedDataConfig: [
          new charts.SeriesDatumConfig<String>('Sales', '2016')
        ]);

[...]

  void handleOnTap() {
    setState(() {
      myState.selectionModels[charts.SelectionModelType.info] = onTapSelection;
    });
  }