drawLabel method

  1. @protected
void drawLabel(
  1. ChartCanvas canvas,
  2. Tick<D> tick, {
  3. required AxisOrientation orientation,
  4. required Rectangle<int> axisBounds,
  5. required Rectangle<int>? drawAreaBounds,
  6. required bool isFirst,
  7. required bool isLast,
  8. bool collision = false,
})

Implementation

@protected
void drawLabel(
  ChartCanvas canvas,
  Tick<D> tick, {
  required AxisOrientation orientation,
  required Rectangle<int> axisBounds,
  required Rectangle<int>? drawAreaBounds,
  required bool isFirst,
  required bool isLast,
  bool collision = false,
}) {
  final locationPx = tick.locationPx ?? 0;
  final labelOffsetPx = tick.labelOffsetPx ?? 0;
  final isRtl = chartContext.isRtl;
  final labelElements = splitLabel(tick.textElement!);
  final labelHeight = getLabelHeight(labelElements);
  var multiLineLabelOffset = 0;

  for (final line in labelElements) {
    var x = 0;
    var y = 0;

    if (orientation == AxisOrientation.bottom ||
        orientation == AxisOrientation.top) {
      y = orientation == AxisOrientation.bottom
          ? axisBounds.top + labelOffsetFromAxisPx(collision: collision)
          : axisBounds.bottom -
              (labelHeight.toInt() - multiLineLabelOffset) -
              labelOffsetFromAxisPx(collision: collision);

      final direction = _normalizeHorizontalAnchor(
          tickLabelAnchor(collision: collision), isRtl, isFirst, isLast);

      line.textDirection = direction;

      switch (direction) {
        case TextDirection.rtl:
          x = (locationPx +
                  labelOffsetFromTickPx(collision: collision) +
                  labelOffsetPx)
              .toInt();
          break;
        case TextDirection.ltr:
          x = (locationPx -
                  labelOffsetFromTickPx(collision: collision) -
                  labelOffsetPx)
              .toInt();
          break;
        case TextDirection.center:
        default:
          x = (locationPx - labelOffsetPx).toInt();
          break;
      }
    } else {
      if (orientation == AxisOrientation.left) {
        if (tickLabelJustification == TickLabelJustification.inside) {
          x = axisBounds.right - labelOffsetFromAxisPx(collision: collision);
          line.textDirection = TextDirection.rtl;
        } else {
          x = axisBounds.left;
          line.textDirection = TextDirection.ltr;
        }
      } else {
        // orientation == right
        if (tickLabelJustification == TickLabelJustification.inside) {
          x = axisBounds.left + labelOffsetFromAxisPx(collision: collision);
          line.textDirection = TextDirection.ltr;
        } else {
          x = axisBounds.right;
          line.textDirection = TextDirection.rtl;
        }
      }

      switch (normalizeVerticalAnchor(
          tickLabelAnchor(collision: collision), isFirst, isLast)) {
        case _PixelVerticalDirection.over:
          y = (locationPx -
                  (labelHeight - multiLineLabelOffset) -
                  labelOffsetFromTickPx(collision: collision) -
                  labelOffsetPx)
              .toInt();
          break;
        case _PixelVerticalDirection.under:
          y = (locationPx +
                  labelOffsetFromTickPx(collision: collision) +
                  labelOffsetPx)
              .toInt();
          break;
        case _PixelVerticalDirection.center:
        default:
          y = (locationPx - labelHeight / 2 + labelOffsetPx).toInt();
          break;
      }
    }
    canvas.drawText(line, x, y + multiLineLabelOffset,
        rotation: _degToRad(labelRotation(collision: collision).toDouble()));
    multiLineLabelOffset +=
        multiLineLabelPadding + line.measurement.verticalSliceWidth.round();
  }
}