populateDataSource method

void populateDataSource(
  1. List<num> seriesXValues,
  2. List<num> seriesYValues
)

Implementation

void populateDataSource(List<num> seriesXValues, List<num> seriesYValues) {
  final bool trendIsVisible = !isToggled &&
      (type == TrendlineType.polynomial
          ? (polynomialOrder >= 2 && polynomialOrder <= 6)
          : !(type == TrendlineType.movingAverage) ||
              (period >= 2 && period <= seriesXValues.length - 1));

  if (series == null ||
      !series!.controller.isVisible ||
      !trendIsVisible ||
      seriesXValues.isEmpty ||
      seriesYValues.isEmpty) {
    return;
  }

  final List<num> yValues = <num>[];
  final List<num> slopeInterceptXValues = <num>[];
  _SlopeIntercept slopeInterceptData = _SlopeIntercept();
  List<num?>? polynomialSlopes;

  late Function(int, num) slopeValue;
  late Function(num, double) forecastValue;
  late Function(num, [bool]) polynomialForeCastValue;

  late List<num> sortedXValues;
  if (series!.canFindLinearVisibleIndexes) {
    sortedXValues = seriesXValues;
  } else {
    final List<num> xValuesCopy = <num>[...seriesXValues];
    xValuesCopy.sort();
    sortedXValues = xValuesCopy;
  }

  _resetTrendlineDataHolders();
  if (xAxis is RenderDateTimeAxis) {
    slopeValue = _slopeDateTimeXValue;
    forecastValue = _forecastDateTimeValue;
    polynomialForeCastValue = _polynomialForeCastDateTimeAxisValue;
  } else if (xAxis is RenderLogarithmicAxis) {
    slopeValue = _slopeLogXValue;
    forecastValue = _forecastValue;
    polynomialForeCastValue = _polynomialForeCastValue;
  } else if (xAxis is RenderCategoryAxis ||
      xAxis is RenderDateTimeCategoryAxis) {
    slopeValue = _slopeXValue;
    forecastValue = _forecastValue;
    polynomialForeCastValue = _polynomialForeCastValue;
  } else {
    slopeValue = _slopeXValue;
    forecastValue = _forecastValue;
    polynomialForeCastValue = _polynomialForeCastValue;
  }

  switch (type) {
    case TrendlineType.linear:
      _calculateLinearPoints(
          seriesXValues,
          seriesYValues,
          sortedXValues,
          _slopeIntercept,
          slopeInterceptData,
          slopeValue,
          forecastValue,
          slopeInterceptXValues,
          yValues);
      break;

    case TrendlineType.exponential:
      _calculateExponentialPoints(
          seriesXValues,
          seriesYValues,
          sortedXValues,
          _slopeIntercept,
          slopeInterceptData,
          slopeValue,
          forecastValue,
          slopeInterceptXValues,
          yValues);
      break;

    case TrendlineType.power:
      _calculatePowerPoints(
          seriesXValues,
          seriesYValues,
          sortedXValues,
          _slopeIntercept,
          slopeInterceptData,
          slopeValue,
          forecastValue,
          slopeInterceptXValues,
          yValues);
      break;

    case TrendlineType.logarithmic:
      _calculateLogarithmicPoints(
          seriesXValues,
          seriesYValues,
          sortedXValues,
          _slopeIntercept,
          slopeInterceptData,
          slopeValue,
          forecastValue,
          slopeInterceptXValues,
          yValues);
      break;

    case TrendlineType.polynomial:
      _slope = _calculatePolynomialPoints(sortedXValues, seriesYValues,
          polynomialSlopes, slopeValue, polynomialForeCastValue);
      break;

    case TrendlineType.movingAverage:
      _calculateMovingAveragePoints(sortedXValues, seriesYValues);
      _slope = null;
      break;
  }

  // Calculate slope and intercept values after calculated trendline points.
  if (!(type == TrendlineType.movingAverage ||
      type == TrendlineType.polynomial)) {
    slopeInterceptData = _computeSlopeInterceptValues(slopeInterceptXValues,
        yValues, seriesXValues.length, _slopeIntercept);
    _slope = <double>[slopeInterceptData.slope!];
  }

  if (onRenderDetailsUpdate != null) {
    _rSquaredValue = _computeRSquaredValue(seriesXValues, seriesYValues,
        _slope, _slopeIntercept.intercept, slopeValue);
  }

  // Calculate segment index based on the trendXValues.
  trendSegmentIndexes.clear();
  final int xLength = trendlineXValues.length;
  for (int i = 0; i < xLength; i++) {
    trendSegmentIndexes.add(i);
  }
}