drawBelowBar method

  1. @visibleForTesting
void drawBelowBar(
  1. CanvasWrapper canvasWrapper,
  2. Path belowBarPath,
  3. Path filledAboveBarPath,
  4. PaintHolder<LineChartData> holder,
  5. LineChartBarData barData,
)

firstly we draw belowBarPath, then if cutOffY value is provided in BarAreaData, belowBarPath maybe draw over the main bar line, then to fix the problem we use filledAboveBarPath to clear the above section from this draw.

Implementation

@visibleForTesting
void drawBelowBar(
  CanvasWrapper canvasWrapper,
  Path belowBarPath,
  Path filledAboveBarPath,
  PaintHolder<LineChartData> holder,
  LineChartBarData barData,
) {
  if (!barData.belowBarData.show) {
    return;
  }

  final viewSize = canvasWrapper.size;

  final belowBarLargestRect = Rect.fromLTRB(
    getPixelX(barData.mostLeftSpot.x, viewSize, holder),
    getPixelY(barData.mostTopSpot.y, viewSize, holder),
    getPixelX(barData.mostRightSpot.x, viewSize, holder),
    viewSize.height,
  );

  final belowBar = barData.belowBarData;
  _barAreaPaint.setColorOrGradient(
    belowBar.color,
    belowBar.gradient,
    belowBarLargestRect,
  );

  if (barData.belowBarData.applyCutOffY) {
    canvasWrapper.saveLayer(
      Rect.fromLTWH(0, 0, viewSize.width, viewSize.height),
      Paint(),
    );
  }

  canvasWrapper.drawPath(belowBarPath, _barAreaPaint);

  // clear the above area that get out of the bar line
  if (barData.belowBarData.applyCutOffY) {
    canvasWrapper
      ..drawPath(filledAboveBarPath, _clearBarAreaPaint)
      ..restore();
  }

  /// draw below spots line
  if (barData.belowBarData.spotsLine.show) {
    for (final spot in barData.spots) {
      if (barData.belowBarData.spotsLine.checkToShowSpotLine(spot)) {
        final from = Offset(
          getPixelX(spot.x, viewSize, holder),
          getPixelY(spot.y, viewSize, holder),
        );

        Offset to;

        // Check applyCutOffY
        if (barData.belowBarData.spotsLine.applyCutOffY &&
            barData.belowBarData.applyCutOffY) {
          to = Offset(
            getPixelX(spot.x, viewSize, holder),
            getPixelY(barData.belowBarData.cutOffY, viewSize, holder),
          );
        } else {
          to = Offset(
            getPixelX(spot.x, viewSize, holder),
            viewSize.height,
          );
        }

        _barAreaLinesPaint
          ..color = barData.belowBarData.spotsLine.flLineStyle.color
          ..strokeWidth =
              barData.belowBarData.spotsLine.flLineStyle.strokeWidth
          ..transparentIfWidthIsZero();

        canvasWrapper.drawDashedLine(
          from,
          to,
          _barAreaLinesPaint,
          barData.belowBarData.spotsLine.flLineStyle.dashArray,
        );
      }
    }
  }
}