From 441bdf7e71b1c87863ff16d5627af6d6ba233295 Mon Sep 17 00:00:00 2001 From: dekun Date: Tue, 2 Jun 2026 12:10:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=A1=8C=E6=83=85=E5=8C=BA?= =?UTF-8?q?=20barSpacing=20API=20=E5=85=BC=E5=AE=B9=EF=BC=88Lightweight=20?= =?UTF-8?q?Charts=20v4=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- manual_trading_hub/static/chart.js | 79 +++++++++++++++++++--------- manual_trading_hub/static/index.html | 4 +- 2 files changed, 57 insertions(+), 26 deletions(-) diff --git a/manual_trading_hub/static/chart.js b/manual_trading_hub/static/chart.js index 18e1e73..bb4d7cc 100644 --- a/manual_trading_hub/static/chart.js +++ b/manual_trading_hub/static/chart.js @@ -196,12 +196,38 @@ if (elBarCountdown) elBarCountdown.textContent = "距收盘 " + cd; } + function getBarSpacing(ts) { + if (!ts) return 6; + try { + if (typeof ts.barSpacing === "function") { + const v = ts.barSpacing(); + if (Number.isFinite(v) && v > 0) return v; + } + } catch (e) {} + try { + if (typeof ts.options === "function") { + const opts = ts.options(); + const v = opts && opts.barSpacing; + if (Number.isFinite(v) && v > 0) return v; + } + } catch (e) {} + try { + const x0 = ts.logicalToCoordinate(0); + const x1 = ts.logicalToCoordinate(1); + if (x0 != null && x1 != null) { + const d = Math.abs(x1 - x0); + if (Number.isFinite(d) && d > 0) return d; + } + } catch (e) {} + return 6; + } + function priceTagX() { if (!chart || !lastCandles.length) return null; const bar = latestCandle(); if (!bar) return null; const ts = chart.timeScale(); - const spacing = ts.barSpacing(); + const spacing = getBarSpacing(ts); const hostW = chartHost.clientWidth || 0; let x = null; try { @@ -227,34 +253,39 @@ function updatePriceTag() { if (!elPriceTag || !candleSeries || !chart) return; - tickLiveClock(); - const bar = latestCandle(); - if (!bar || bar.close == null) { - elPriceTag.classList.add("hidden"); - elPriceTag.setAttribute("aria-hidden", "true"); - return; - } - let y = null; try { - y = candleSeries.priceToCoordinate(Number(bar.close)); + tickLiveClock(); + const bar = latestCandle(); + if (!bar || bar.close == null) { + elPriceTag.classList.add("hidden"); + elPriceTag.setAttribute("aria-hidden", "true"); + return; + } + let y = null; + try { + y = candleSeries.priceToCoordinate(Number(bar.close)); + } catch (e) { + y = null; + } + const x = priceTagX(); + const hostH = chartHost.clientHeight || 0; + const hostW = chartHost.clientWidth || 0; + if (y == null || x == null || y < 8 || y > hostH - 8 || x < 8 || x > hostW - 8) { + elPriceTag.classList.add("hidden"); + elPriceTag.setAttribute("aria-hidden", "true"); + return; + } + const up = Number(bar.close) >= Number(bar.open); + elPriceTag.classList.remove("hidden", "is-up", "is-down"); + elPriceTag.classList.add(up ? "is-up" : "is-down"); + elPriceTag.setAttribute("aria-hidden", "false"); + elPriceTag.style.left = x + "px"; + elPriceTag.style.top = y + "px"; + if (elPriceTagValue) elPriceTagValue.textContent = fmtPrice(bar.close); } catch (e) { - y = null; - } - const x = priceTagX(); - const hostH = chartHost.clientHeight || 0; - const hostW = chartHost.clientWidth || 0; - if (y == null || x == null || y < 8 || y > hostH - 8 || x < 8 || x > hostW - 8) { elPriceTag.classList.add("hidden"); elPriceTag.setAttribute("aria-hidden", "true"); - return; } - const up = Number(bar.close) >= Number(bar.open); - elPriceTag.classList.remove("hidden", "is-up", "is-down"); - elPriceTag.classList.add(up ? "is-up" : "is-down"); - elPriceTag.setAttribute("aria-hidden", "false"); - elPriceTag.style.left = x + "px"; - elPriceTag.style.top = y + "px"; - if (elPriceTagValue) elPriceTagValue.textContent = fmtPrice(bar.close); } function startPriceTagTimer() { diff --git a/manual_trading_hub/static/index.html b/manual_trading_hub/static/index.html index fed78ca..c8ccda8 100644 --- a/manual_trading_hub/static/index.html +++ b/manual_trading_hub/static/index.html @@ -190,7 +190,7 @@
- - + +