selectDataPoints method

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, series index 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.

late SelectionBehavior selectionBehavior;

void initState() {
  selectionBehavior = SelectionBehavior(
    enable: true
  );
  super.initState();
}

Widget build(BuildContext context) {
  return Column(
    children: [
      TextButton(
        onPressed: () {
          setState(() {
            select();
          });
        },
        child: Text('Select data points')
      ),
      SfCartesianChart(
        series: <BarSeries<SalesData, num>>[
          BarSeries<SalesData, num>(
            selectionBehavior: selectionBehavior
          )
        ]
      )
    ]
  );
}

void select() {
  selectionBehavior.selectDataPoints(3);
}

Implementation

void selectDataPoints(int pointIndex, [int seriesIndex = 0]) {
  RenderChartPlotArea? plotArea;
  if (parentBox is RenderChartPlotArea) {
    plotArea = parentBox! as RenderChartPlotArea;
  } else if (parentBox is RenderBehaviorArea) {
    final RenderBehaviorArea behaviorArea = parentBox! as RenderBehaviorArea;
    plotArea = behaviorArea.plotArea;
  }

  if (plotArea == null) {
    return;
  }

  ChartSeriesRenderer? seriesRenderer;
  RenderBox? child = plotArea.firstChild;
  while (child != null) {
    final ContainerParentDataMixin<RenderBox> childParentData =
        child.parentData! as ContainerParentDataMixin<RenderBox>;
    if (child is ChartSeriesRenderer && child.index == seriesIndex) {
      seriesRenderer = child;
      break;
    }
    child = childParentData.nextSibling;
  }

  if (seriesRenderer != null &&
      seriesRenderer.selectionBehavior != null &&
      seriesRenderer.selectionBehavior!.enable) {
    plotArea.selectionController.updateSelection(
      seriesRenderer,
      seriesIndex,
      pointIndex,
      seriesRenderer.selectionBehavior!.toggleSelection,
      selectionType: plotArea.selectionMode,
    );
  }
}