drawAboveBar method

  1. @visibleForTesting
void drawAboveBar(
  1. CanvasWrapper canvasWrapper,
  2. Path aboveBarPath,
  3. Path filledBelowBarPath,
  4. PaintHolder<LineChartData> holder,
  5. LineChartBarData barData,
)

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

Implementation

@visibleForTesting
void drawAboveBar(
  CanvasWrapper canvasWrapper,
  Path aboveBarPath,
  Path filledBelowBarPath,
  PaintHolder<LineChartData> holder,
  LineChartBarData barData,
) {
  if (!barData.aboveBarData.show) {
    return;
  }

  final viewSize = canvasWrapper.size;

  final aboveBarLargestRect = Rect.fromLTRB(
    getPixelX(barData.mostLeftSpot.x, viewSize, holder),
    0,
    getPixelX(barData.mostRightSpot.x, viewSize, holder),
    getPixelY(barData.mostBottomSpot.y, viewSize, holder),
  );

  final aboveBar = barData.aboveBarData;
  _barAreaPaint.setColorOrGradient(
    aboveBar.color,
    aboveBar.gradient,
    aboveBarLargestRect,
  );

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

  canvasWrapper.drawPath(aboveBarPath, _barAreaPaint);

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

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

        Offset to;

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

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

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