drawDataLabelWithBackground method

void drawDataLabelWithBackground(
  1. int index,
  2. Canvas canvas,
  3. String dataLabel,
  4. Offset offset,
  5. int angle,
  6. TextStyle style,
  7. Paint fillPaint,
  8. Paint strokePaint,
)

Implementation

void drawDataLabelWithBackground(
  int index,
  Canvas canvas,
  String dataLabel,
  Offset offset,
  int angle,
  TextStyle style,
  Paint fillPaint,
  Paint strokePaint,
) {
  final EdgeInsets margin = dataLabelSettings.margin;
  if (!dataLabelSettings.showZeroValue && dataLabel == '0') {
    return;
  }
  if (!offset.dx.isNaN && !offset.dy.isNaN) {
    if (dataLabel.isNotEmpty) {
      // TODO(VijayakumarM): Check and optimize.
      if (fillPaint.color != Colors.transparent ||
          (strokePaint.color != const Color.fromARGB(0, 25, 5, 5) &&
              strokePaint.strokeWidth > 0)) {
        final TextPainter textPainter = TextPainter(
          text: TextSpan(text: dataLabel, style: style),
          textDirection: TextDirection.ltr,
        )..layout();
        final Rect dataLabelRect = Rect.fromLTWH(
          offset.dx,
          offset.dy,
          textPainter.width + margin.horizontal,
          textPainter.height + margin.vertical,
        );
        RRect labelRect = RRect.fromRectAndRadius(
            dataLabelRect, Radius.circular(dataLabelSettings.borderRadius));
        // To check and update the label rect and offset by rotated
        // label rect height overlaps with plotArea or not.
        if (angle != 0) {
          final Rect rotatedBounds =
              calculateRotatedBounds(dataLabelRect, dataLabelSettings.angle);
          final double heightFromCenter = rotatedBounds.height / 2;
          if (paintBounds.bottom < labelRect.center.dy + heightFromCenter) {
            labelRect =
                _rotatedRRect(labelRect, labelRect.bottom - heightFromCenter);
            offset = Offset(labelRect.left, labelRect.top);
          }
          if (paintBounds.top > labelRect.center.dy - heightFromCenter) {
            labelRect =
                _rotatedRRect(labelRect, labelRect.top + heightFromCenter);
            offset = Offset(labelRect.left, labelRect.top);
          }
        }

        canvas.save();
        canvas.translate(labelRect.center.dx, labelRect.center.dy);
        canvas.rotate((angle * pi) / 180);
        canvas.translate(-labelRect.center.dx, -labelRect.center.dy);
        if (strokePaint.color != Colors.transparent &&
            strokePaint.strokeWidth > 0) {
          canvas.drawRRect(labelRect, strokePaint);
        }

        if (fillPaint.color != Colors.transparent) {
          canvas.drawRRect(labelRect, fillPaint);
        }
        canvas.restore();
      }
    }
  }
  drawDataLabel(
    index,
    canvas,
    dataLabel,
    offset.dx + margin.left,
    offset.dy + margin.top,
    angle,
    style,
  );
}