receiveSyncedCrosshair method
Handles crosshair synchronization from another chart in the ChartSyncGroup.
Implementation
void receiveSyncedCrosshair(DateTime? timestamp, double? yPriceRatio) {
if (!_showCrosshair || timestamp == null || candles.isEmpty) {
_crosshairPosition = null;
_hoveredCandle = null;
notifyListeners();
return;
}
final timestamps = candles.map((c) => c.timestamp).toList();
final matchIdx =
ChartSyncGroup.findClosestCandleIndex(timestamps, timestamp);
if (matchIdx < 0 || matchIdx >= candles.length) {
_crosshairPosition = null;
_hoveredCandle = null;
notifyListeners();
return;
}
_isReceivingSync = true;
try {
final converter = CoordinateConverter(
viewport: _viewport,
totalCandles: candles.length,
);
final targetX = converter.indexToX(matchIdx);
final targetY = (yPriceRatio != null && _viewport.viewportHeight > 0)
? (yPriceRatio * _viewport.viewportHeight)
.clamp(0.0, _viewport.viewportHeight)
: (_viewport.viewportHeight * 0.5);
_crosshairPosition = Offset(targetX, targetY);
_hoveredCandle = candles[matchIdx];
notifyListeners();
} finally {
_isReceivingSync = false;
}
}