selectDataPoints method Null safety

void selectDataPoints(
  1. int pointIndex,
  2. [int seriesIndex = 0]
)

/Selects or deselects the specified data point in the series.

The following are the arguments to be passed.

  • pointIndex - index of the data point that needs to be selected.
  • seriesIndex - index of the series in which the data point is selected.

Where the pointIndex is a required argument and seriesIndex is an optional argument. By default, 0 will be considered as the series index. Thus it will take effect on the first series if no value is specified.

For Circular, Pyramid and Funnel charts, seriesIndex should always be 0, as it has only one series.

If the specified data point is already selected, it will be deselected, else it will be selected. Selection type and multi-selection functionality is also applicable for this, but it is based on the API values specified in ChartSelectionBehavior.

Note: Even though, the enable property in ChartSelectionBehavior is set to false, this method will work.

Widget build(BuildContext context) {
selection = SelectionBehavior(enable: true);
chart = SfCartesianChart(series:getChartData);
  return Scaffold(
     child: Column(
       children: <Widget>[
      FlatButton(
           child: Text('Select'),
           onPressed: select),
         Container(child: chart)
       ]));
}
 void select() {
    selection.selectionIndex(1, 0);
}

Implementation

void selectDataPoints(int pointIndex, [int seriesIndex = 0]) {
  final dynamic seriesRenderer = _selectionBehaviorRenderer!._selectionDetails
      .stateProperties.chartSeries.visibleSeriesRenderers[seriesIndex];
  assert(
      seriesRenderer is CartesianSeriesRenderer == false ||
          getVisibleDataPointIndex(pointIndex,
                  SeriesHelper.getSeriesRendererDetails(seriesRenderer)) !=
              null,
      'Provided point index is not in the visible range. Provide point index which is in the visible range.');
  _selectionBehaviorRenderer = seriesRenderer is CartesianSeriesRenderer
      ? SeriesHelper.getSeriesRendererDetails(seriesRenderer)
          .selectionBehaviorRenderer
      : _selectionBehaviorRenderer;
  _selectionBehaviorRenderer!._selectionDetails.selectionRenderer
      ?.selectDataPoints(pointIndex, seriesIndex);
}