drawBackground method

void drawBackground(
  1. Canvas canvas,
  2. Size size
)

Draws the background elements of the chart.

Implementation

void drawBackground(Canvas canvas, Size size) {
  final separatedTextPainters = <TextPainter>[];
  final labelTextPainters = <TextPainter>[];
  final separatedLinePaint = Paint()
    ..strokeWidth = separatedLineWidth
    ..strokeCap = separatedLineCap
    ..color = separatedLineColor;

  double separatedTextMaxWidth = 0;
  double bottomLabelAreaHeight = 0;

  if (isVisibleSeparatedText) {
    for (int i = 0; i < separatedLineCount; i++) {
      final percent = i / (separatedLineCount - 1);

      // Draw about the text.
      final value = (maxValue - percent * maxValue).round();
      final text = markType == ChartMarkType.integer
        ? "$value"
        : "$value%";

      final textPainter = TextPainter(
        text: TextSpan(text: text, style: defaultTextStyle.merge(separatedTextStyle)),
        textDirection: TextDirection.ltr,
      );

      textPainter.layout();

      separatedTextPainters.add(textPainter);
      separatedTextMaxWidth = max(separatedTextMaxWidth, textPainter.width);
    }
  }

  if (isVisibleLabel) {
    for (int i = 0; i < states.length; i++) {
      final textPainter = TextPainter(
        text: TextSpan(text: states[i].data.label, style: defaultTextStyle.merge(labelTextStyle)),
        textDirection: TextDirection.ltr
      );

      textPainter.layout();

      labelTextPainters.add(textPainter);
      bottomLabelAreaHeight = max(bottomLabelAreaHeight, textPainter.height);
    }

    bottomLabelAreaHeight += labelTextMargin;
  }

  final innerSide = separatedTextMaxWidth + separatedTextMargin;
  final bodyWidth = size.width - innerSide;
  final bodyHeight = size.height - bottomLabelAreaHeight;

  if (backgroundColor != null) {
    canvas.drawRect(
      separatedTextDirection == ChartSeparatedTextDirection.leading
        ? Rect.fromLTRB(innerSide, 0, size.width, bodyHeight)
        : Rect.fromLTRB(0, 0, size.width - innerSide, bodyHeight),
      Paint()..color = backgroundColor!
    );
  }

  // Darw about the separated text and label and line.
  for (int i = 0; i < separatedLineCount; i++) {
    final percent = i / (separatedLineCount - 1);
    final height = (size.height - bottomLabelAreaHeight) * percent;

    // About separated text.
    if (isVisibleSeparatedText) {
      final textPainter = separatedTextPainters[i];
      final textWidth = textPainter.width;
      final textHeight = height - textPainter.size.height / 2;

      switch (separatedTextAlignment) {
        case ChartSeparatedTextAlignment.center:
          separatedTextDirection == ChartSeparatedTextDirection.leading
            ? textPainter.paint(canvas, Offset((separatedTextMaxWidth - textWidth) / 2, textHeight))
            : textPainter.paint(canvas, Offset(size.width - (separatedTextMaxWidth + textWidth) / 2, textHeight));
          break;

        case ChartSeparatedTextAlignment.leading:
          separatedTextDirection == ChartSeparatedTextDirection.leading
            ? textPainter.paint(canvas, Offset(0, textHeight))
            : textPainter.paint(canvas, Offset(size.width - textWidth, textHeight));
          break;

        case ChartSeparatedTextAlignment.trailing:
          separatedTextDirection == ChartSeparatedTextDirection.leading
            ? textPainter.paint(canvas, Offset(separatedTextMaxWidth - textWidth, textHeight))
            : textPainter.paint(canvas, Offset(size.width - separatedTextMaxWidth, textHeight));
          break;
      }
    }

    // About line.
    if (i == separatedLineCount - 1) {
      separatedLinePaint.color = separatedBorderColor;
      separatedLinePaint.strokeWidth = separatedBorderWidth;
    }

    if (separatedTextDirection == ChartSeparatedTextDirection.leading) {
      canvas.drawLine(Offset(innerSide, height), Offset(size.width, height), separatedLinePaint);
    } else {
      canvas.drawLine(Offset(0, height), Offset(size.width - innerSide, height), separatedLinePaint);
    }
  }

  // Draw the label texts.
  for (int i = 0; i < states.length; i++) {
    final percent = i / (states.length);

    if (isVisibleLabel) {
      final textPainter = labelTextPainters[i];
      final areaLeft = bodyWidth * percent;
      final areaWidth = bodyWidth / states.length;
      final textWidth = textPainter.width;
      final textHeight = size.height - bottomLabelAreaHeight;
      final textMargin = separatedTextDirection == ChartSeparatedTextDirection.leading ? innerSide : 0;

      textPainter.paint(
        canvas,
        Offset(((textMargin + areaLeft) + areaWidth / 2) - textWidth / 2, textHeight + labelTextMargin)
      );
    }
  }

  // Sets the bottom constraint to the label area height.
  constraintBy(ChartPainterConstraint(bottom: bottomLabelAreaHeight));

  // Sets the left or right constraint to the inner side width.
  if (separatedTextDirection == ChartSeparatedTextDirection.leading) {
    constraintBy(ChartPainterConstraint(left: innerSide));
  } else {
    constraintBy(ChartPainterConstraint(right: innerSide));
  }
}