renderYAxis method

void renderYAxis(
  1. CanvasRenderingContext2D ctx,
  2. double minValue,
  3. double maxValue
)

Implementation

void renderYAxis(
    CanvasRenderingContext2D ctx, double minValue, double maxValue) {
  final cWidth = chart.width;
  final cHeight = chart.height;
  ctx
    ..beginPath()
    ..strokeStyle = style.fontColor
    ..moveTo(cWidth - style.rightMargin, style.topMargin)
    ..lineTo(cWidth - style.rightMargin, cHeight - style.bottomMargin)
    ..stroke();

  final usableHeight = cHeight - style.topMargin - style.bottomMargin;
  if (usableHeight < 10) {
    print('Not enough space for Y axis');
    return;
  }
  final diff = maxValue - minValue;
  if (yFrom == 0 && yTo == 0) {
    yFrom = minValue - (diff * 0.1);
    yTo = maxValue + (diff * 0.1);
  }
  if (yFrom < 0 && minValue >= 0) {
    yFrom = 0;
  }
  final preferStepCount = usableHeight ~/ 20;
  var yStepVal = diff / preferStepCount;
  if (yDecimals == null) {
    yDecimals = 0;
    yStepVal.toStringAsFixed(18).split('.')[1].split('').forEach((e) {
      if (e == '0') {
        yDecimals = (yDecimals ?? 0) + 1;
      } else {
        return;
      }
    });
  }
  if (yDecimals! > 6) {
    yDecimals = 0;
  }
  yDecimals = (yDecimals ?? 0) + 1;
  yFrom = roundDouble(yFrom, yDecimals!);
  yStepVal = roundDouble(yStepVal, yDecimals!);
  var currStepY = yFrom;
  var stepCount = 0;
  if (yStepVal <= 0) {
    throw Exception('yStepVal must be greater than 0');
  }
  while (yTo > currStepY) {
    currStepY += yStepVal;
    stepCount++;
  }
  yTo = currStepY;
  final yStepPix = usableHeight / stepCount;
  yScaleValue = (yTo - yFrom) / usableHeight;
  ctx
    ..textAlign = 'left'
    ..textBaseline = 'middle';

  final x = cWidth - style.rightMargin;
  for (var i = 1; i < stepCount; i++) {
    final y = (cHeight - style.bottomMargin) - (i * yStepPix);
    drawYLabel(ctx, x, y.round(), yFrom + (i * yStepVal), yDecimals!);
  }
}