generateNormalBarPath method

  1. @visibleForTesting
Path generateNormalBarPath(
  1. Size viewSize,
  2. LineChartBarData barData,
  3. List<AFlSpot> barSpots,
  4. PaintHolder<LineChartData> holder, {
  5. Path? appendToPath,
})

firstly we generate the bar line that we should draw, then we reuse it to fill below bar space. there is two type of barPath that generate here, first one is the sharp corners line on spot connections second one is curved corners line on spot connections, and we use isCurved to find out how we should generate it, If you want to concatenate paths together for creating an area between multiple bars for example, you can pass the appendToPath

Implementation

@visibleForTesting
Path generateNormalBarPath(
  Size viewSize,
  LineChartBarData barData,
  List<AFlSpot> barSpots,
  PaintHolder<LineChartData> holder, {
  Path? appendToPath,
}) {
  final path = appendToPath ?? Path();
  final size = barSpots.length;

  var temp = Offset.zero;

  final x = getPixelX(barSpots[0].x, viewSize, holder);
  final y = getPixelY(barSpots[0].y, viewSize, holder);
  if (appendToPath == null) {
    path.moveTo(x, y);
  } else {
    path.lineTo(x, y);
  }
  for (var i = 1; i < size; i++) {
    /// CurrentSpot
    final current = Offset(
      getPixelX(barSpots[i].x, viewSize, holder),
      getPixelY(barSpots[i].y, viewSize, holder),
    );

    /// previous spot
    final previous = Offset(
      getPixelX(barSpots[i - 1].x, viewSize, holder),
      getPixelY(barSpots[i - 1].y, viewSize, holder),
    );

    /// next point
    final next = Offset(
      getPixelX(barSpots[i + 1 < size ? i + 1 : i].x, viewSize, holder),
      getPixelY(barSpots[i + 1 < size ? i + 1 : i].y, viewSize, holder),
    );

    final controlPoint1 = previous + temp;

    /// if the isCurved is false, we set 0 for smoothness,
    /// it means we should not have any smoothness then we face with
    /// the sharped corners line
    final smoothness = barData.isCurved ? barData.curveSmoothness : 0.0;
    temp = ((next - previous) / 2) * smoothness;

    if (barData.preventCurveOverShooting) {
      if ((next - current).dy <= barData.preventCurveOvershootingThreshold ||
          (current - previous).dy <=
              barData.preventCurveOvershootingThreshold) {
        temp = Offset(temp.dx, 0);
      }

      if ((next - current).dx <= barData.preventCurveOvershootingThreshold ||
          (current - previous).dx <=
              barData.preventCurveOvershootingThreshold) {
        temp = Offset(0, temp.dy);
      }
    }

    final controlPoint2 = current - temp;

    path.cubicTo(
      controlPoint1.dx,
      controlPoint1.dy,
      controlPoint2.dx,
      controlPoint2.dy,
      current.dx,
      current.dy,
    );
  }

  return path;
}