drawAxisLabels function

void drawAxisLabels(
  1. Screen screen,
  2. Rectangle area, {
  3. List<String>? xLabels,
  4. List<String>? yLabels,
  5. UvStyle style = const UvStyle(),
})

Draws axis labels along the X and/or Y edges of area.

Implementation

void drawAxisLabels(
  Screen screen,
  Rectangle area, {
  List<String>? xLabels,
  List<String>? yLabels,
  UvStyle style = const UvStyle(),
}) {
  final width = area.width;
  final height = area.height;
  if (width <= 0 || height <= 0) return;

  if (xLabels != null && xLabels.isNotEmpty) {
    final step = xLabels.length <= 1 ? 0 : width / (xLabels.length - 1);
    for (var i = 0; i < xLabels.length; i++) {
      final label = xLabels[i];
      var x = area.minX + (i * step).round();
      x -= (label.length / 2).floor();
      x = math.max(area.minX, math.min(x, area.maxX - 1));
      putText(screen, area, x, area.maxY - 1, label, style);
    }
  }

  if (yLabels != null && yLabels.isNotEmpty) {
    final step = yLabels.length <= 1 ? 0 : height / (yLabels.length - 1);
    for (var i = 0; i < yLabels.length; i++) {
      final label = yLabels[i];
      var y = area.maxY - 1 - (i * step).round();
      y = math.max(area.minY, math.min(y, area.maxY - 1));
      putText(screen, area, area.minX, y, label, style);
    }
  }
}