drawVerticalAxesLabels method

  1. @override
void drawVerticalAxesLabels(
  1. Canvas canvas,
  2. ChartAxisRenderer axisRenderer,
  3. SfCartesianChart chart, [
  4. String? renderType,
  5. double? animationFactor,
  6. ChartAxisRenderer? oldAxisRenderer,
  7. bool? needAnimate,
])

To draw the axis labels of vertical axes

Implementation

@override
void drawVerticalAxesLabels(
    Canvas canvas, ChartAxisRenderer axisRenderer, SfCartesianChart chart,
    [String? renderType,
    double? animationFactor,
    ChartAxisRenderer? oldAxisRenderer,
    bool? needAnimate]) {
  final ChartAxis axis = axisRenderer._axis;
  if (axis.labelPosition.toString().split('.')[1] == renderType) {
    final Rect axisBounds = axisRenderer._bounds;
    final List<AxisLabel> visibleLabels = axisRenderer._visibleLabels;
    TextStyle textStyle;
    late double tempInterval, pointX, pointY, previousEnd;
    for (int i = 0; i < visibleLabels.length; i++) {
      final String labelText =
          axisRenderer.getAxisLabel(axis, visibleLabels[i].renderText!, i);
      final int angle =
          axisRenderer.getAxisLabelAngle(axisRenderer, labelText, i);
      assert(angle - angle.floor() == 0,
          'The angle value of the vertical axis must be the whole number.');
      textStyle = visibleLabels[i].labelStyle;
      textStyle = _getTextStyle(
          textStyle: textStyle,
          fontColor:
              textStyle.color ?? _chartState._chartTheme.axisLabelColor);
      tempInterval = visibleLabels[i].value.toDouble();
      final Size textSize = measureText(labelText, textStyle, 0);
      pointY = (_valueToCoefficient(tempInterval, axisRenderer) *
              axisBounds.height) +
          axisBounds.top;
      pointY = ((axisBounds.top + axisBounds.height) -
              ((axisBounds.top - pointY).abs())) -
          textSize.height / 2;
      pointX = _getPointX(axisRenderer, textSize, axisBounds);
      final _ChartLocation location = _getRotatedTextLocation(
          pointX, pointY, labelText, textStyle, angle, axis);
      if (axis.labelAlignment == LabelAlignment.center) {
        pointX = location.x;
        pointY = location.y;
      } else if (axis.labelAlignment == LabelAlignment.end) {
        pointX = location.x;
        pointY = location.y - textSize.height / 2;
      } else if (axis.labelAlignment == LabelAlignment.start) {
        pointX = location.x;
        pointY = location.y + textSize.height / 2;
      }
      if (axis.labelIntersectAction == AxisLabelIntersectAction.hide &&
          i != 0 &&
          (!axis.isInversed
              ? pointY + (textSize.height / 2) > previousEnd
              : pointY - (textSize.height / 2) < previousEnd)) {
        continue;
      }
      previousEnd = !axis.isInversed
          ? pointY - textSize.height / 2
          : pointY + textSize.height / 2;

      ///  Edge label placement for y-Axis
      if (axis.edgeLabelPlacement == EdgeLabelPlacement.shift) {
        if (axis.labelAlignment == LabelAlignment.center) {
          if (i == 0 && axisBounds.bottom <= pointY + textSize.height / 2) {
            pointY = axisBounds.top + axisBounds.height - textSize.height;
          } else if (i == axisRenderer._visibleLabels.length - 1 &&
              axisBounds.top >= pointY + textSize.height / 2) {
            pointY = axisBounds.top;
          }
        } else if (axis.labelAlignment == LabelAlignment.start) {
          if (i == 0 && axisBounds.bottom <= pointY + textSize.height / 2) {
            pointY = axisBounds.top + axisBounds.height - textSize.height;
          }
        } else if (axis.labelAlignment == LabelAlignment.end) {
          if (i == axisRenderer._visibleLabels.length - 1 &&
              axisBounds.top >= pointY + textSize.height / 2) {
            pointY = axisBounds.top + textSize.height / 2;
          }
        }
      } else if (axis.edgeLabelPlacement == EdgeLabelPlacement.hide) {
        if (axis.labelAlignment == LabelAlignment.center) {
          if (i == 0 || i == axisRenderer._visibleLabels.length - 1) {
            continue;
          }
        } else if ((axis.labelAlignment == LabelAlignment.end) &&
            (i == axisRenderer._visibleLabels.length - 1 ||
                (i == 0 && axis.isInversed))) {
          continue;
        } else if ((axis.labelAlignment == LabelAlignment.start) &&
            (i == 0 ||
                (i == axisRenderer._visibleLabels.length - 1 &&
                    axis.isInversed))) {
          continue;
        }
      }
      axisRenderer._visibleLabels[i]._labelRegion =
          Rect.fromLTWH(pointX, pointY, textSize.width, textSize.height);

      if (needAnimate!) {
        final double? oldLocation = _getPrevLocation(
            axisRenderer, oldAxisRenderer!, tempInterval, textSize);
        pointY = oldLocation != null
            ? (oldLocation - (oldLocation - pointY) * animationFactor!)
            : pointY;
      }

      final Offset point = Offset(pointX, pointY);
      if (axisBounds.top - textSize.height <= pointY &&
          axisBounds.bottom + textSize.height >= pointY) {
        _drawText(
            canvas, labelText, point, textStyle, axisRenderer._labelRotation);
      }
    }
  }
}