refactor: improve measurement label anchoring and time duration format
This commit is contained in:
@ -182,7 +182,30 @@ export class DrawingManager {
|
||||
const rect = this.container.getBoundingClientRect();
|
||||
const x = e.clientX - rect.left;
|
||||
const y = e.clientY - rect.top;
|
||||
const time = this.chart.timeScale().coordinateToTime(x);
|
||||
|
||||
const timeScale = this.chart.timeScale();
|
||||
const candleData = this.dashboard.allData.get(this.dashboard.currentInterval) || [];
|
||||
let time = timeScale.coordinateToTime(x);
|
||||
|
||||
if (time === null && candleData.length > 0) {
|
||||
// Coordinate is likely in the future whitespace
|
||||
const logical = timeScale.coordinateToLogical(x);
|
||||
if (logical !== null) {
|
||||
const lastIdx = candleData.length - 1;
|
||||
const lastCandle = candleData[lastIdx];
|
||||
// Logical index of the last candle is roughly 'lastIdx'
|
||||
// (though logical range might have offsets, this is a good approximation for snapping)
|
||||
const offset = logical - lastIdx;
|
||||
if (offset > 0) {
|
||||
const intervalSec = this.getIntervalSeconds();
|
||||
time = lastCandle.time + Math.round(offset) * intervalSec;
|
||||
} else {
|
||||
// It's in the past but coordinateToTime failed (rare)
|
||||
time = candleData[0].time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const price = this.series.coordinateToPrice(y);
|
||||
return { x, y, time, price };
|
||||
}
|
||||
@ -432,13 +455,36 @@ export class DrawingManager {
|
||||
if (this.requestUpdate) this.requestUpdate();
|
||||
}
|
||||
|
||||
getIntervalSeconds() {
|
||||
const interval = this.dashboard.currentInterval;
|
||||
const unit = interval.slice(-1);
|
||||
const value = parseInt(interval.slice(0, -1));
|
||||
|
||||
switch (unit) {
|
||||
case 'm': return value * 60;
|
||||
case 'h': return value * 3600;
|
||||
case 'd': return value * 86400;
|
||||
case 'w': return value * 604800;
|
||||
case 'M': return value * 2592000;
|
||||
default: return 60;
|
||||
}
|
||||
}
|
||||
|
||||
snapToNearestTime(time) {
|
||||
if (time === null) return null;
|
||||
const candleData = this.dashboard.allData.get(this.dashboard.currentInterval) || [];
|
||||
if (candleData.length === 0) return time;
|
||||
|
||||
const lastCandle = candleData[candleData.length - 1];
|
||||
if (time > lastCandle.time) {
|
||||
// Snapping for the future area
|
||||
const intervalSec = this.getIntervalSeconds();
|
||||
const diff = time - lastCandle.time;
|
||||
const numIntervals = Math.round(diff / intervalSec);
|
||||
return lastCandle.time + (numIntervals * intervalSec);
|
||||
}
|
||||
|
||||
// Find nearest timestamp in current data
|
||||
// Since data is sorted, we can be efficient, but for now a simple find is safest
|
||||
let nearest = candleData[0].time;
|
||||
let minDiff = Math.abs(time - nearest);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user