createSegments method

  1. @override
void createSegments ()
override

Creates a collection of segments for the points in the series.

Implementation

@override
void createSegments() {
  final Rect rect = _calculatePlotOffset(_chart._chartAxis._axisClipRect,
      Offset(_xAxis.plotOffset, _yAxis.plotOffset));
  List<num> yCoef = <num>[];
  final List<num> xValues = <num>[];
  final List<num> yValues = <num>[];
  List<double> controlPoints = <double>[];
  double x1, x2, y1, y2;
  double startControlX;
  double startControlY;
  double endControlX;
  double endControlY;
  dynamic splineType;

  if (this is SplineSeries) {
    splineType = this.splineType;
  }

  for (int i = 0; i < _dataPoints.length; i++) {
    if (_dataPoints[i].isDrop != true) {
      xValues.add(_dataPoints[i].xValue);
      yValues.add(_dataPoints[i].yValue);
    }
  }

  if (xValues.isNotEmpty) {
    final List<num> dx = List<num>(xValues.length - 1);

    /// Check the type of spline
    if (splineType == SplineType.monotonic) {
      yCoef =
          _getMonotonicSpline(xValues, yValues, yCoef, xValues.length, dx);
    } else if (splineType == SplineType.cardinal) {
      if (this is SplineSeries) {
        cardinalSplineTension = cardinalSplineTension ?? 0.5;
        yCoef = _getCardinalSpline(
            xValues, yValues, yCoef, xValues.length, cardinalSplineTension);
      }
    } else {
      yCoef =
          _naturalSpline(xValues, yValues, yCoef, xValues.length, splineType);
    }
    for (int pointIndex = 0; pointIndex < xValues.length - 1; pointIndex++) {
      if (xValues[pointIndex] != null &&
          yValues[pointIndex] != null &&
          xValues[pointIndex + 1] != null &&
          yValues[pointIndex + 1] != null) {
        final double x = xValues[pointIndex].toDouble();
        final double y = yValues[pointIndex].toDouble();
        final double nextX = xValues[pointIndex + 1].toDouble();
        final double nextY = yValues[pointIndex + 1].toDouble();
        if (splineType == SplineType.monotonic) {
          controlPoints = _calculateMonotonicControlPoints(
              x,
              y,
              nextX,
              nextY,
              yCoef[pointIndex].toDouble(),
              yCoef[pointIndex + 1].toDouble(),
              dx[pointIndex].toDouble());
        } else if (splineType == SplineType.cardinal) {
          controlPoints = _calculateCardinalControlPoints(x, y, nextX, nextY,
              yCoef[pointIndex].toDouble(), yCoef[pointIndex + 1].toDouble());
        } else {
          controlPoints = _calculateControlPoints(
              xValues,
              yValues,
              yCoef[pointIndex].toDouble(),
              yCoef[pointIndex + 1].toDouble(),
              pointIndex);
        }

        final num currentPointXValue = xValues[pointIndex];
        final num currentPointYValue = yValues[pointIndex];
        final num nextPointXValue = xValues[pointIndex + 1];
        final num nextPointYValue = yValues[pointIndex + 1];

        x1 = _calculatePoint(currentPointXValue, currentPointYValue, _xAxis,
                _yAxis, _chart._requireInvertedAxis, this, rect)
            .x;
        y1 = _calculatePoint(currentPointXValue, currentPointYValue, _xAxis,
                _yAxis, _chart._requireInvertedAxis, this, rect)
            .y;

        x2 = _calculatePoint(nextPointXValue, nextPointYValue, _xAxis, _yAxis,
                _chart._requireInvertedAxis, this, rect)
            .x;
        y2 = _calculatePoint(nextPointXValue, nextPointYValue, _xAxis, _yAxis,
                _chart._requireInvertedAxis, this, rect)
            .y;

        startControlX = _calculatePoint(controlPoints[0], controlPoints[1],
                _xAxis, _yAxis, _chart._requireInvertedAxis, this, rect)
            .x;
        startControlY = _calculatePoint(controlPoints[0], controlPoints[1],
                _xAxis, _yAxis, _chart._requireInvertedAxis, this, rect)
            .y;
        endControlX = _calculatePoint(controlPoints[2], controlPoints[3],
                _xAxis, _yAxis, _chart._requireInvertedAxis, this, rect)
            .x;
        endControlY = _calculatePoint(controlPoints[2], controlPoints[3],
                _xAxis, _yAxis, _chart._requireInvertedAxis, this, rect)
            .y;

        final List<num> values = <num>[];
        values.add(x1);
        values.add(y1);
        values.add(x2);
        values.add(y2);
        values.add(startControlX);
        values.add(startControlY);
        values.add(endControlX);
        values.add(endControlY);
        _createSegment(
            values, _dataPoints[pointIndex], _dataPoints[pointIndex + 1]);
      }
    }
  }
}