updateDataSource method
void
updateDataSource({})
Used to process only the newly added, updated and removed data points in a series, instead of processing all the data points.
To re-render the chart with modified data points, setState() will be called. This will render the process and render the chart from scratch. Thus, the app’s performance will be degraded on continuous update. To overcome this problem, updateDataSource method can be called by passing updated data points indexes. Chart will process only that point and skip various steps like bounds calculation, old data points processing, etc. Thus, this will improve the app’s performance.
The following are the arguments of this method.
- addedDataIndexes –
List<int>
type – Indexes of newly added data points in the existing series. - removedDataIndexes –
List<int>
type – Indexes of removed data points in the existing series. - updatedDataIndexes –
List<int>
type – Indexes of updated data points in the existing series. - addedDataIndex –
int
type – Index of newly added data point in the existing series. - removedDataIndex –
int
type – Index of removed data point in the existing series. - updatedDataIndex –
int
type – Index of updated data point in the existing series.
Returns void
.
Widget build(BuildContext context) {
ChartSeriesController? _chartSeriesController;
return Column(
children: <Widget>[
SfCartesianChart(
series: <LineSeries<SalesData, num>>[
LineSeries<SalesData, num>(
onRendererCreated: (ChartSeriesController controller) {
_chartSeriesController = controller;
},
),
]
),
TextButton(
child: Text("Update data source"),
onPressed: () {
chartData.removeAt(0);
chartData.add(ChartData(3,23));
_chartSeriesController?.updateDataSource(
addedDataIndexes: <int>[chartData.length -1],
removedDataIndexes: <int>[0],
);
}
)
]
);
}
Implementation
void updateDataSource({
List<int>? addedDataIndexes,
List<int>? removedDataIndexes,
List<int>? updatedDataIndexes,
int addedDataIndex = -1,
int removedDataIndex = -1,
int updatedDataIndex = -1,
}) {
final CartesianRealTimeUpdateMixin<T, D>? renderer =
seriesRenderer as CartesianRealTimeUpdateMixin<T, D>?;
if (renderer != null) {
List<int>? effectiveRemovedIndexes;
List<int>? effectiveAddedIndexes;
List<int>? effectiveReplacedIndexes;
if (removedDataIndexes != null) {
effectiveRemovedIndexes = List<int>.from(removedDataIndexes);
}
if (addedDataIndexes != null) {
effectiveAddedIndexes = List<int>.from(addedDataIndexes);
}
if (updatedDataIndexes != null) {
effectiveReplacedIndexes = List<int>.from(updatedDataIndexes);
}
if (removedDataIndex != -1) {
effectiveRemovedIndexes ??= <int>[];
effectiveRemovedIndexes.add(removedDataIndex);
}
if (addedDataIndex != -1) {
effectiveAddedIndexes ??= <int>[];
effectiveAddedIndexes.add(addedDataIndex);
}
if (updatedDataIndex != -1) {
effectiveReplacedIndexes ??= <int>[];
effectiveReplacedIndexes.add(updatedDataIndex);
}
renderer.updateDataPoints(
effectiveRemovedIndexes,
effectiveAddedIndexes,
effectiveReplacedIndexes,
);
}
}