auto-detect data interval and match default period

This commit is contained in:
2026-08-13 21:43:14 +08:00
parent 0db1241b47
commit 15c27c0132
+21
View File
@@ -564,6 +564,22 @@ function bindToolbar() {
setActiveIndicatorButtons()
}
function detectDefaultPeriod(data) {
if (data.len < 2) return '1m'
let minGap = Infinity
for (let i = 1; i < Math.min(data.len, 100); i++) {
const gap = data.ts[i] - data.ts[i - 1]
if (gap > 0 && gap < minGap) minGap = gap
}
const gapMin = minGap / MINUTE
const candidates = PERIODS.map(p => ({ key: p.key, span: getSpanMinutes(p.period) }))
let best = candidates[0]
for (const c of candidates) {
if (c.span <= gapMin + 0.01) best = c
}
return best.key
}
function mountChart(source) {
const chartDom = document.getElementById('chart')
if (chartDom === null) return
@@ -574,6 +590,11 @@ function mountChart(source) {
rawLoaded = true
if (loadingEl) loadingEl.style.display = 'none'
console.log(`Loaded ${data.len} bars, ${new Date(data.ts[0])} → ${new Date(data.ts[data.len - 1])}`)
const defaultPeriod = detectDefaultPeriod(data)
if (defaultPeriod !== activePeriodKey) {
activePeriodKey = defaultPeriod
setActivePeriodButton()
}
if (chart) dispose(chartDom)
chart = init(chartDom, {
styles: {