selectDataPoints method
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]) {
if (_selectionBehaviorRenderer != null) {
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);
}
}