setCrosshairPosition method
Updates crosshair position and extracts hovered candle under pointer.
Implementation
void setCrosshairPosition(Offset? position) {
if (!_showCrosshair) {
_crosshairPosition = null;
if (_syncGroup != null && !_isReceivingSync) {
_syncGroup!.clearCrosshair(source: this);
}
return;
}
_crosshairPosition = position;
if (position != null && candles.isNotEmpty) {
final converter = CoordinateConverter(
viewport: _viewport,
totalCandles: candles.length,
);
final index = converter.xToIndex(position.dx);
if (index >= 0 && index < candles.length) {
_hoveredCandle = candles[index];
}
} else {
_hoveredCandle = null;
}
if (_syncGroup != null && !_isReceivingSync) {
final hoveredTime = _hoveredCandle?.timestamp;
final yRatio = (position != null && _viewport.viewportHeight > 0)
? (position.dy / _viewport.viewportHeight).clamp(0.0, 1.0)
: null;
_syncGroup!.broadcastCrosshair(
source: this,
timestamp: hoveredTime,
yPriceRatio: yRatio,
);
}
notifyListeners();
}