drawRangePath method Null safety

void drawRangePath(
  1. Canvas canvas,
  2. Size size,
  3. bool isBottom
)

범위(밴드)를 표시하는 그래프를 그립니다.

Implementation

void drawRangePath(Canvas canvas, Size size, bool isBottom) {
  final rangePath = Path();

  // 밴드 상단
  for (var k = 0; k < barItemsDay!.length; k++) {
    final List<BarDataHour?> barDataHour = barItemsDay![k].barDataHour;
    double offsetDx = offset.dx + ((barWidth * hourCntInDay + barSpace * hourCntInDay) * k);

    for (var i = 0; i < barDataHour.length; i++) {
      if (barDataHour[i] == null) continue;
      double barHeight, rangeTopValue;

      if (isBottom) {
        rangeTopValue = isGroupAvg ? barDataHour[i]!.groupRangeTopValueBottom : barDataHour[i]!.totalRangeTopValueBottom;
      } else {
        rangeTopValue = isGroupAvg ? barDataHour[i]!.groupRangeTopValue : barDataHour[i]!.totalRangeTopValue;
      }
      barHeight = rangeTopValue * barHeightRatio;

      current = Offset(
        offsetDx + barStartOffsetX + yLabelWidth + yLabelMargin + (barWidth / 2) + (barWidth + barSpace) * i,
        baseOffsetY + size.height - xLabelDaySize - xLabelDayMargin - xLabelHourSize - xLabelHourMargin - horizontalLineHeight + (isBottom ? barHeight : -barHeight),
      );

      if (k == 0 && i == 0) {
        rangePath.moveTo(current.dx, current.dy);
      } else {
        if ((hourCntInDay != 1 && i == barDataHour.length - 1) || (hourCntInDay == 1 && k == barItemsDay!.length - 1)) {
          next = current;
        } else {
          BarDataHour nextData = hourCntInDay == 1 ? barDataHour[0]! : barDataHour[i + 1]!;
          barHeight = (isGroupAvg ? nextData.groupRangeTopValue : nextData.totalRangeTopValue) * barHeightRatio;
          next = Offset(
            offsetDx + barStartOffsetX + yLabelWidth + yLabelMargin + (barWidth / 2) + (barWidth + barSpace) * (i + 1),
            baseOffsetY + size.height - xLabelDaySize - xLabelDayMargin - xLabelHourSize - xLabelHourMargin - horizontalLineHeight + (isBottom ? barHeight : -barHeight),
          );
        }

        final controlPoint1 = prev + temp;
        temp = ((next - prev) / 2) * smoothness;
        final controlPoint2 = current - temp;

        rangePath.cubicTo(controlPoint1.dx, controlPoint1.dy, controlPoint2.dx, controlPoint2.dy, current.dx, current.dy);
      }
      prev = current;
    }
  }

  // 밴드 하단
  for (var k = barItemsDay!.length - 1; k >= 0; k--) {
    final barDataHour = barItemsDay![k].barDataHour;
    var offsetDx = offset.dx + ((barWidth * hourCntInDay + barSpace * hourCntInDay) * k);

    for (var i = barDataHour.length - 1; i >= 0; i--) {
      if (barDataHour[i] == null) continue;
      var barHeight;
      if (isBottom) {
        barHeight = (isGroupAvg ? barDataHour[i]!.groupRangeBottomValueBottom : barDataHour[i]!.totalRangeBottomValueBottom) * barHeightRatio;
      } else {
        barHeight = (isGroupAvg ? barDataHour[i]!.groupRangeBottomValue : barDataHour[i]!.totalRangeBottomValue) * barHeightRatio;
      }

      current = Offset(
        offsetDx + barStartOffsetX + yLabelWidth + yLabelMargin + (barWidth / 2) + (barWidth + barSpace) * i,
        baseOffsetY + size.height - xLabelDaySize - xLabelDayMargin - xLabelHourSize - xLabelHourMargin - horizontalLineHeight + (isBottom ? barHeight : -barHeight),
      );

      if (i == barDataHour.length - 1) {
        rangePath.lineTo(current.dx, current.dy);
      } else {
        if ((hourCntInDay != 1 && i == 0) || (hourCntInDay == 1 && k == 0)) {
          next = current;
        } else {
          // TODO 보완필요
          if (barDataHour[i] == null) continue;
          BarDataHour nextData = hourCntInDay == 1 ? barDataHour[0]! : barDataHour[i - 1]!;

          double barHeight = (isGroupAvg ? nextData.groupRangeBottomValue : nextData.totalRangeBottomValue) * barHeightRatio;
          next = Offset(
            offsetDx + barStartOffsetX + yLabelWidth + yLabelMargin + (barWidth / 2) + (barWidth + barSpace) * (i - 1),
            baseOffsetY + size.height - xLabelDaySize - xLabelDayMargin - xLabelHourSize - xLabelHourMargin - horizontalLineHeight + (isBottom ? barHeight : -barHeight),
          );
        }

        Offset controlPoint1 = prev + temp;
        temp = ((next - prev) / 2) * smoothness;
        Offset controlPoint2 = current - temp;

        rangePath.cubicTo(controlPoint1.dx, controlPoint1.dy, controlPoint2.dx, controlPoint2.dy, current.dx, current.dy);

        if (k == 0 && i == 0) {
          rangePath.lineTo(current.dx, current.dy);
        }
      }

      prev = current;
    }
  }

  canvas.drawPath(rangePath, rangePaint);
}