drawAxisTitles static method

void drawAxisTitles(
  1. Canvas canvas,
  2. ChartContext context
)

Draws optional X/Y axis title labels (cartesian charts).

Implementation

static void drawAxisTitles(Canvas canvas, ChartContext context) {
  final xTitle = context.config.xAxisTitle;
  final yTitle = context.config.yAxisTitle;
  if ((xTitle == null || xTitle.isEmpty) &&
      (yTitle == null || yTitle.isEmpty)) {
    return;
  }

  final bounds = context.bounds;
  final style = context.theme.axisTextStyle.copyWith(
    fontWeight: FontWeight.w600,
  );

  if (xTitle != null && xTitle.isNotEmpty) {
    // Sit below the tick-label band when axes are shown, otherwise hug the
    // plot edge with a small gap.
    final titleY = context.config.showAxis
        ? bounds.bottom + 26
        : bounds.bottom + 6;
    _drawLabel(
      canvas,
      xTitle,
      Offset(bounds.left + bounds.width / 2, titleY),
      style,
      align: TextAlign.center,
      maxWidth: bounds.width,
    );
  }

  if (yTitle != null && yTitle.isNotEmpty) {
    // Sit beyond the tick-label band (32px) when axes are shown, otherwise
    // hug the plot edge with a small gap.
    final titleX = context.config.showAxis
        ? bounds.left - 44
        : bounds.left - 8;
    _drawRotatedLabel(
      canvas,
      yTitle,
      Offset(titleX, bounds.top + bounds.height / 2),
      style,
      maxWidth: bounds.height,
    );
  }
}