getMovingAveragePoints method

List<CartesianChartPoint> getMovingAveragePoints(
  1. List<CartesianChartPoint> points,
  2. List xValues,
  3. List<num?> yValues,
  4. CartesianSeriesRenderer seriesRenderer,
)

Defines moving average points

Implementation

List<CartesianChartPoint<dynamic>> getMovingAveragePoints(
    List<CartesianChartPoint<dynamic>> points,
    List<dynamic> xValues,
    List<num?> yValues,
    CartesianSeriesRenderer seriesRenderer) {
  final List<CartesianChartPoint<dynamic>> pts =
      <CartesianChartPoint<dynamic>>[];
  int periods = _trendline.period >= points.length
      ? points.length - 1
      : _trendline.period;
  periods = max(2, periods);
  int? y;
  dynamic x;
  int count;
  int nullCount;
  for (int index = 0; index < points.length - 1; index++) {
    y = count = nullCount = 0;
    for (int j = index; count < periods; j++) {
      count++;
      if (j >= yValues.length || yValues[j] == null) {
        nullCount++;
      }
      y = y! + (j >= yValues.length ? 0 : yValues[j]!).toInt();
    }
    y = ((periods - nullCount) <= 0) ? null : (y! ~/ (periods - nullCount));
    if (y != null && !y.isNaN && index + periods < xValues.length + 1) {
      x = xValues[periods - 1 + index];
      pts.add(getDataPoint(
          x, y, points[periods - 1 + index], seriesRenderer, pts.length));
    }
  }
  return pts;
}