修复行情区 barSpacing API 兼容(Lightweight Charts v4)
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -190,7 +190,7 @@
|
||||
|
||||
<div id="toast"></div>
|
||||
<script src="https://unpkg.com/lightweight-charts@4.2.0/dist/lightweight-charts.standalone.production.js"></script>
|
||||
<script src="/assets/chart.js?v=20260528-hub-market9"></script>
|
||||
<script src="/assets/app.js?v=20260528-hub-market9"></script>
|
||||
<script src="/assets/chart.js?v=20260528-hub-market10"></script>
|
||||
<script src="/assets/app.js?v=20260528-hub-market10"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user