drawGrid method

void drawGrid({
  1. required Canvas canvas,
  2. required Rect bounds,
  3. required PriceRange priceRange,
  4. required CoordinateConverter converter,
  5. required ChartViewport viewport,
  6. required int totalCandles,
  7. int verticalDivisions = 6,
  8. double minHorizontalSpacing = 80.0,
})

Draws the background grid for the specified pane.

Implementation

void drawGrid({
  required Canvas canvas,
  required Rect bounds,
  required PriceRange priceRange,
  required CoordinateConverter converter,
  required ChartViewport viewport,
  required int totalCandles,
  int verticalDivisions = 6,
  double minHorizontalSpacing = 80.0,
}) {
  // 1. Horizontal Price Grid Lines
  if (priceRange.span > 0 && bounds.height > 0) {
    final step = _calculateNicePriceStep(priceRange.span, verticalDivisions);
    final firstLinePrice = (priceRange.min / step).ceil() * step;

    for (double p = firstLinePrice; p <= priceRange.max; p += step) {
      final y = CoordinateConverter.priceToY(p, bounds, priceRange);
      if (y >= bounds.top && y <= bounds.bottom) {
        canvas.drawLine(
          Offset(bounds.left, y),
          Offset(bounds.right, y),
          gridPaint,
        );
      }
    }
  }

  // 2. Vertical Time Grid Lines
  if (totalCandles > 0 && bounds.width > 0) {
    final candleTotalWidth = viewport.candleTotalWidth;
    if (candleTotalWidth > 0) {
      final candlesPerGrid = math.max(
        1,
        (minHorizontalSpacing / candleTotalWidth).round(),
      );
      final visible = viewport.calculateVisibleIndices(totalCandles);

      final firstIndex = (visible.start ~/ candlesPerGrid) * candlesPerGrid;
      for (int i = firstIndex; i <= visible.end; i += candlesPerGrid) {
        if (i < 0 || i >= totalCandles) continue;
        final x = converter.indexToX(i);
        if (x >= bounds.left && x <= bounds.right) {
          canvas.drawLine(
            Offset(x, bounds.top),
            Offset(x, bounds.bottom),
            gridPaint,
          );
        }
      }
    }
  }
}