updateDataSource method

void updateDataSource(
  1. {List<int>? addedDataIndexes,
  2. List<int>? removedDataIndexes,
  3. List<int>? updatedDataIndexes,
  4. int? addedDataIndex,
  5. int? removedDataIndex,
  6. int? updatedDataIndex}
)

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,
    int? removedDataIndex,
    int? updatedDataIndex}) {
  bool needUpdate = false;
  if (removedDataIndexes != null && removedDataIndexes.isNotEmpty) {
    _removeDataPointsList(removedDataIndexes);
  } else if (removedDataIndex != null) {
    _removeDataPoint(removedDataIndex);
  }
  if (addedDataIndexes != null && addedDataIndexes.isNotEmpty) {
    _addOrUpdateDataPoints(addedDataIndexes, false);
  } else if (addedDataIndex != null) {
    _addOrUpdateDataPoint(addedDataIndex, false);
  }
  if (updatedDataIndexes != null && updatedDataIndexes.isNotEmpty) {
    needUpdate = true;
    _addOrUpdateDataPoints(updatedDataIndexes, true);
  } else if (updatedDataIndex != null) {
    needUpdate = true;
    _addOrUpdateDataPoint(updatedDataIndex, true);
  }
  _updateCartesianSeries(
      _needXRecalculation, _needYRecalculation, needUpdate);
}