setDataPoints method

void setDataPoints(
  1. String key,
  2. List<BoxDataPoint> points
)

Sets the data points stored in this graph, and redraw the graph. See also redraw.

Because the graph does not really store any kind of ordering for X values, ordering is received from iterating the data point X values, as they are stored in LinkedHashMap. The chart stores X values in this map, and as new X values are encountered from new data points and graphs, they are added to the map. LinkedHashMap stores the ordering following the order in which X values are added to the map. If you want to guarantee the ordering in any way, set labelsX in the order you want.

Implementation

void setDataPoints(String key, List<BoxDataPoint> points) {
  if (_userGraphKeys.isNotEmpty && !_userGraphKeys.contains(key)) return;
  _graphKeys.add(key);

  var labelsXSet = labelsX.isNotEmpty;

  // Initialized _dataPoints in the same ordering as labelsX.
  if (labelsXSet) {
    // Add empty data point in some X point that is defined in labels
    for (var element in labelsX) {
      if (_dataPoints[element] == null) {
        _dataPoints[element] = {key: null};
      }
    }
  }

  for (var dp in points) {
    // If the labelsX has predefined X labels, ignore those points that are outside of defined labels.
    if (labelsXSet && !labelsX.contains(labelFormatX(dp.x))) continue;

    if (_dataPoints[labelFormatX(dp.x)] == null) {
      _dataPoints[labelFormatX(dp.x)] = {};
    }

    _dataPoints[labelFormatX(dp.x)]![key] = dp;
  }

  _setMinMaxY();
  clearDrawing();
  redraw();
}