drawTimeAxis method
void
drawTimeAxis({
- required Canvas canvas,
- required Rect timeBounds,
- required List<
Candle> candles, - required VisibleIndices visible,
- required CoordinateConverter converter,
- required ChartViewport viewport,
- required Timeframe timeframe,
- double minLabelSpacing = 85.0,
- double priceAxisWidth = 65.0,
Draws the time scale along the bottom.
Implementation
void drawTimeAxis({
required Canvas canvas,
required Rect timeBounds,
required List<Candle> candles,
required VisibleIndices visible,
required CoordinateConverter converter,
required ChartViewport viewport,
required Timeframe timeframe,
double minLabelSpacing = 85.0,
double priceAxisWidth = 65.0,
}) {
// Draw horizontal divider line
canvas.drawLine(
Offset(timeBounds.left, timeBounds.top),
Offset(
timeBounds.right + priceAxisWidth,
timeBounds.top,
), // extend into price axis area
Paint()
..color = theme.axisLineColor
..strokeWidth = 1.0,
);
if (candles.isEmpty || visible.count <= 0) return;
final candleTotalWidth = viewport.candleTotalWidth;
if (candleTotalWidth <= 0) return;
final candlesPerLabel = math.max(
1,
(minLabelSpacing / candleTotalWidth).round(),
);
final firstIndex = (visible.start ~/ candlesPerLabel) * candlesPerLabel;
for (int i = firstIndex; i <= visible.end; i += candlesPerLabel) {
if (i < 0 || i >= candles.length) continue;
final c = candles[i];
final x = converter.indexToX(i);
if (x >= timeBounds.left && x <= timeBounds.right - 30.0) {
final label = _formatTimestamp(c.timestamp, timeframe);
final textSpan = TextSpan(text: label, style: theme.axisTextStyle);
final tp = TextPainter(text: textSpan, textDirection: TextDirection.ltr)
..layout();
tp.paint(canvas, Offset(x - (tp.width / 2.0), timeBounds.top + 6.0));
}
}
}