updateDataSource method

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

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) {
   CircularSeriesController seriesController;
   return Column(
     children: <Widget>[
     Container(
       child: SfCircularChart(
           series: <CircularSeries<SalesData, num>>[
               PieSeries<SalesData, num>(
                  dataSource: chartData,
                   onRendererCreated:
                    (CircularSeriesController controller) {
                      seriesController = controller;
                   },
               ),
             ],
       )
  ),
  Container(
     child: RaisedButton(
          onPressed: () {
          chartData.removeAt(0);
          chartData.add(ChartData(3,23));
          seriesController.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 RealTimeUpdateMixin<T, D> renderer = seriesRenderer;

  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,
  );
}