setData method

  1. @override
void setData(
  1. int index,
  2. ChartSegment segment
)
override

Implementation

@override
void setData(int index, ChartSegment segment) {
  super.setData(index, segment);
  final List<num?> yCoefficients = List<num?>.filled(dataCount, 0);
  _yMax = double.negativeInfinity;

  num x1 = xValues[index];
  num y1 = nonEmptyYValues[index];
  num x2 = double.nan;
  num y2 = double.nan;

  int nextIndex = index + 1;
  if (nextIndex < dataCount) {
    x2 = xValues[nextIndex];
    y2 = nonEmptyYValues[nextIndex];
  }

  num controlX1 = 0;
  num controlY1 = 0;
  num controlX2 = 0;
  num controlY2 = 0;

  switch (splineType) {
    case SplineType.natural:
    case SplineType.clamped:
      _computeNaturalSpline(nonEmptyYValues, yCoefficients, splineType);

      const num oneThird = 1 / 3.0;
      final num deltaXSquared = pow(x2.toDouble() - x1.toDouble(), 2);
      final num yCoef1 = yCoefficients[index]!;
      num yCoef2 = yCoef1;
      if (nextIndex < dataCount) {
        yCoef2 = yCoefficients[nextIndex]!;
      }

      final num dx1 = (2 * x1) + x2;
      final num dx2 = x1 + (2 * x2);
      final num dy1 = (2 * y1) + y2;
      final num dy2 = y1 + (2 * y2);

      controlX1 = dx1 * oneThird;
      controlY1 = oneThird *
          (dy1 - (oneThird * deltaXSquared * (yCoef1 + (0.5 * yCoef2))));
      controlX2 = dx2 * oneThird;
      controlY2 = oneThird *
          (dy2 - (oneThird * deltaXSquared * ((0.5 * yCoef1) + yCoef2)));
      _yMax = max(_yMax, max(controlY1, max(controlY2, max(y1, y2))));
      break;

    case SplineType.monotonic:
      final List<num?> dx = List<num?>.filled(dataCount, 0);
      _computeMonotonicSpline(nonEmptyYValues, yCoefficients, dx, dataCount);

      final num yCoef1 = yCoefficients[index]!;
      num yCoef2 = yCoef1;
      if (nextIndex < dataCount) {
        yCoef2 = yCoefficients[nextIndex]!;
      }

      final num value = dx[index]! / 3;
      controlX1 = x1 + value;
      controlY1 = y1 + (yCoef1 * value);
      controlX2 = x2 - value;
      controlY2 = y2 - (yCoef2 * value);
      _yMax = max(_yMax, max(controlY1, max(controlY2, max(y1, y2))));
      break;

    case SplineType.cardinal:
      _computeCardinalSpline(yCoefficients, cardinalSplineTension);

      final num coefficientY = yCoefficients[index]!;
      num coefficientY1 = coefficientY;
      num yCoefficient = coefficientY;
      num y1Coefficient = coefficientY1;
      if (nextIndex < dataCount) {
        coefficientY1 = yCoefficients[nextIndex]!;
      }

      if (xAxis is RenderDateTimeAxis) {
        yCoefficient = coefficientY / _cardinalSplineDateTimeInterval();
        y1Coefficient = coefficientY1 / _cardinalSplineDateTimeInterval();
      }

      controlX1 = x1 + (coefficientY / 3);
      controlY1 = y1 + (yCoefficient / 3);
      controlX2 = x2 - (coefficientY1 / 3);
      controlY2 = y2 - (y1Coefficient / 3);
      _yMax = max(_yMax, max(controlY1, max(controlY2, max(y1, y2))));
      break;
  }

  x1 = xValues[index];
  y1 = yValues[index];
  nextIndex = nextIndexConsideringEmptyPointMode(
      index, emptyPointSettings.mode, yValues, dataCount);
  if (nextIndex != -1) {
    x2 = xValues[nextIndex];
    y2 = yValues[nextIndex];
  }

  segment as SplineSegment<T, D>
    ..series = this
    ..currentSegmentIndex = index
    .._x1 = x1
    .._y1 = y1
    .._controlX1 = controlX1
    .._controlY1 = controlY1
    .._controlX2 = controlX2
    .._controlY2 = controlY2
    .._x2 = x2
    .._y2 = y2
    ..isEmpty = isEmpty(index);
}