decorate method

  1. @override
void decorate(
  1. Iterable<ImmutableBarRendererElement<D>> barElements,
  2. ChartCanvas canvas,
  3. GraphicsFactory graphicsFactory, {
  4. required Rectangle<num> drawBounds,
  5. required double animationPercent,
  6. required bool renderingVertically,
  7. bool rtl = false,
})
override

Implementation

@override
void decorate(
  Iterable<ImmutableBarRendererElement<D>> barElements,
  ChartCanvas canvas,
  GraphicsFactory graphicsFactory, {
  required Rectangle<num> drawBounds,
  required double animationPercent,
  required bool renderingVertically,
  bool rtl = false,
}) {
  // Only decorate the bars when animation is at 100%.
  if (animationPercent != 1.0) {
    return;
  }

  for (var element in barElements) {
    final bounds = element.bounds!;
    final datumIndex = element.index;

    final series = element.series!;

    final measureLowerBoundFn = series.measureLowerBoundFn;
    final measureUpperBoundFn = series.measureUpperBoundFn;

    if (measureLowerBoundFn != null && measureUpperBoundFn != null) {
      final measureOffsetFn = series.measureOffsetFn!;
      final measureAxis =
          series.getAttr(measureAxisKey) as ImmutableAxis<num>;

      if (renderingVertically) {
        final startY = measureAxis.getLocation(
            (measureLowerBoundFn(datumIndex) ?? 0) +
                measureOffsetFn(datumIndex)!)!;
        final endY = measureAxis.getLocation(
            (measureUpperBoundFn(datumIndex) ?? 0) +
                measureOffsetFn(datumIndex)!)!;

        if (startY != endY) {
          final barWidth = bounds.right - bounds.left;
          final x = (bounds.left + bounds.right) / 2;
          final rectWidth =
              min(strokeWidthPx + 2 * outlineWidthPx, barWidth.toDouble());
          final strokeWidth = rectWidth - 2 * outlineWidthPx;
          final rectEndpointLength =
              min(endpointLengthPx + 2 * outlineWidthPx, barWidth.toDouble());
          final endpointLength = rectEndpointLength - 2 * outlineWidthPx;

          if (outlineWidthPx > 0) {
            // Draw rectangle rendering the outline for the vertical line.
            canvas.drawRect(
                Rectangle.fromPoints(Point(x - rectWidth / 2, startY),
                    Point(x + rectWidth / 2, endY)),
                fill: outlineColor,
                strokeWidthPx: outlineWidthPx);

            // Draw rectangle rendering the outline for the horizontal
            // endpoint representing the lower bound.
            canvas.drawRect(
                Rectangle(x - rectEndpointLength / 2, startY - rectWidth / 2,
                    rectEndpointLength, rectWidth),
                fill: outlineColor,
                strokeWidthPx: outlineWidthPx);

            // Draw rectangle rendering the outline for the horizontal
            // endpoint representing the upper bound.
            canvas.drawRect(
                Rectangle(x - rectEndpointLength / 2, endY - rectWidth / 2,
                    rectEndpointLength, rectWidth),
                fill: outlineColor,
                strokeWidthPx: outlineWidthPx);
          }

          // Draw vertical whisker line.
          canvas.drawLine(
              points: [Point(x, startY), Point(x, endY)],
              stroke: strokeColor,
              strokeWidthPx: strokeWidth);

          // Draw horizontal whisker line for the lower bound.
          canvas.drawLine(points: [
            Point(x - endpointLength / 2, startY),
            Point(x + endpointLength / 2, startY)
          ], stroke: strokeColor, strokeWidthPx: strokeWidth);

          // Draw horizontal whisker line for the upper bound.
          canvas.drawLine(points: [
            Point(x - endpointLength / 2, endY),
            Point(x + endpointLength / 2, endY)
          ], stroke: strokeColor, strokeWidthPx: strokeWidth);
        }
      } else {
        final startX = measureAxis.getLocation(
            (measureLowerBoundFn(datumIndex) ?? 0) +
                measureOffsetFn(datumIndex)!)!;
        final endX = measureAxis.getLocation(
            (measureUpperBoundFn(datumIndex) ?? 0) +
                measureOffsetFn(datumIndex)!)!;

        if (startX != endX) {
          final barWidth = bounds.bottom - bounds.top;
          final y = (bounds.top + bounds.bottom) / 2;
          final rectWidth =
              min(strokeWidthPx + 2 * outlineWidthPx, barWidth.toDouble());
          final strokeWidth = rectWidth - 2 * outlineWidthPx;
          final rectEndpointLength =
              min(endpointLengthPx + 2 * outlineWidthPx, barWidth.toDouble());
          final endpointLength = rectEndpointLength - 2 * outlineWidthPx;

          if (outlineWidthPx > 0) {
            // Draw rectangle rendering the outline for the horizontal line.
            canvas.drawRect(
                Rectangle.fromPoints(Point(startX, y - rectWidth / 2),
                    Point(endX, y + rectWidth / 2)),
                fill: outlineColor,
                strokeWidthPx: outlineWidthPx);

            // Draw rectangle rendering the outline for the vertical
            // endpoint representing the lower bound.
            canvas.drawRect(
                Rectangle(startX - rectWidth / 2, y - rectEndpointLength / 2,
                    rectWidth, rectEndpointLength),
                fill: outlineColor,
                strokeWidthPx: outlineWidthPx);

            // Draw rectangle rendering the outline for the vertical
            // endpoint representing the upper bound.
            canvas.drawRect(
                Rectangle(endX - rectWidth / 2, y - rectEndpointLength / 2,
                    rectWidth, rectEndpointLength),
                fill: outlineColor,
                strokeWidthPx: outlineWidthPx);
          }

          // Draw horizontal whisker line.
          canvas.drawLine(
              points: [Point(startX, y), Point(endX, y)],
              stroke: strokeColor,
              strokeWidthPx: strokeWidth);

          // Draw vertical whisker line for the lower bound.
          canvas.drawLine(points: [
            Point(startX, y - endpointLength / 2),
            Point(startX, y + endpointLength / 2)
          ], stroke: strokeColor, strokeWidthPx: strokeWidth);

          // Draw vertical whisker line for the upper bound.
          canvas.drawLine(points: [
            Point(endX, y - endpointLength / 2),
            Point(endX, y + endpointLength / 2)
          ], stroke: strokeColor, strokeWidthPx: strokeWidth);
        }
      }
    }
  }
}