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>[];
  final _SlopeIntercept slopeIntercept = _SlopeIntercept();
  _SlopeIntercept slopeInterceptData = _SlopeIntercept();
  List<num?>? polynomialSlopes;
  List<double>? polynomialSlopesData;
  List<double>? slope;

  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();
  yValues.clear();
  slopeInterceptXValues.clear();
  trendlineXValues.clear();
  trendlineYValues.clear();

  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:
      polynomialSlopesData = _calculatePolynomialPoints(
          sortedXValues,
          seriesYValues,
          polynomialSlopes,
          slopeValue,
          polynomialForeCastValue);
      slope = polynomialSlopesData;
      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!];
  }

  // Trigger the onRenderDetailsUpdate event.
  if (onRenderDetailsUpdate != null) {
    final TrendlineRenderParams args = TrendlineRenderParams(
      slopeIntercept.intercept,
      series!.index,
      name,
      series!.name,
      _points,
      slope,
      _computeRSquaredValue(seriesXValues, seriesYValues, slope,
          slopeIntercept.intercept, slopeValue),
    );
    onRenderDetailsUpdate!(args);
  }

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