Respect manual chart zoom when auto-follow is off.

Tie the market auto toggle to both price scale and viewport: tail refresh updates data silently while preserving zoom/pan when auto is disabled.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-08 08:30:44 +08:00
parent c8ffc764e1
commit bae78d8368
2 changed files with 11 additions and 5 deletions
+10 -4
View File
@@ -1,5 +1,5 @@
/** /**
* 中控行情区:K 线 + 成交量;Hub 后台轮询 + SSE 推送;价格轴「自动」。 * 中控行情区:K 线 + 成交量;Hub 后台轮询 + SSE 推送;「自动」控制价格轴与视口跟随
*/ */
(function () { (function () {
const AUTO_REFRESH_MS = 5000; const AUTO_REFRESH_MS = 5000;
@@ -2255,22 +2255,27 @@
applyChartPriceFormat(); applyChartPriceFormat();
} }
} }
const autoFollow = priceAutoScale;
const shouldPreserve = const shouldPreserve =
savedRange && savedRange &&
isVisibleRangeValidForCandles(savedRange, candleCountBefore) && isVisibleRangeValidForCandles(savedRange, candleCountBefore) &&
(chartRangeUserLocked || !wasViewingTail); (!autoFollow || chartRangeUserLocked || !wasViewingTail);
applyCandlesToChart( applyCandlesToChart(
mergeCandles(lastCandles, alignCandlesToTick(data.candles), { prepend: false }), mergeCandles(lastCandles, alignCandlesToTick(data.candles), { prepend: false }),
0, 0,
{ {
preserveRange: !!shouldPreserve, preserveRange: !!shouldPreserve,
skipAutoScale: chartRangeUserLocked, skipAutoScale: !autoFollow,
} }
); );
if (epochAtStart !== chartViewEpoch) return; if (epochAtStart !== chartViewEpoch) return;
const n = lastCandles.length; const n = lastCandles.length;
const minorTailUpdate = Math.abs(n - candleCountBefore) <= CHART_TAIL_REFRESH_LIMIT + 5; const minorTailUpdate = Math.abs(n - candleCountBefore) <= CHART_TAIL_REFRESH_LIMIT + 5;
if (wasViewingTail && !chartRangeUserLocked) { if (!autoFollow) {
if (savedRange && isVisibleRangeValidForCandles(savedRange, n)) {
restoreVisibleLogicalRange(savedRange, n);
}
} else if (wasViewingTail && !chartRangeUserLocked) {
restoreVisibleLogicalRange(tailVisibleLogicalRange(n), n); restoreVisibleLogicalRange(tailVisibleLogicalRange(n), n);
} else if (shouldPreserve && minorTailUpdate) { } else if (shouldPreserve && minorTailUpdate) {
if (!restoreVisibleLogicalRange(savedRange, n) && !chartRangeUserLocked) { if (!restoreVisibleLogicalRange(savedRange, n) && !chartRangeUserLocked) {
@@ -2659,6 +2664,7 @@
elPriceAuto.addEventListener("click", function () { elPriceAuto.addEventListener("click", function () {
priceAutoScale = !priceAutoScale; priceAutoScale = !priceAutoScale;
applyPriceAutoScale(); applyPriceAutoScale();
if (priceAutoScale) applyDefaultVisibleRange();
}); });
} }
if (elPosClear) { if (elPosClear) {
+1 -1
View File
@@ -179,7 +179,7 @@
</div> </div>
<div id="market-price-tag-time" class="market-price-tag-time"></div> <div id="market-price-tag-time" class="market-price-tag-time"></div>
</div> </div>
<button type="button" id="market-price-auto" class="market-price-auto is-on" title="价格轴自动缩放">自动</button> <button type="button" id="market-price-auto" class="market-price-auto is-on" title="开启:价格轴与视口随刷新自动调整;关闭:后台静默更新,保留当前缩放">自动</button>
</div> </div>
</div> </div>
</div> </div>