增加K线

This commit is contained in:
dekun
2026-05-30 10:42:54 +08:00
parent 53959a9008
commit e9f1a6a46f
2 changed files with 25 additions and 64 deletions
+19 -29
View File
@@ -1,4 +1,4 @@
/** 多周期 K 线 · SQLite 后端 + localStorage · 弹窗全屏 Lightweight Charts */
/** 多周期 K 线 · SQLite 后端 + localStorage · 弹窗大图 Lightweight Charts */
const CHART_INTERVALS = ["5m", "15m", "30m", "1h", "4h", "1d", "1w"];
const INTERVAL_LIMITS = {
@@ -30,6 +30,8 @@ const COLORS = {
};
const MINI_SIZE = { w: 380, h: 100 };
/** 弹窗图表最小尺寸;实际按视口放大(带鱼屏可接近全宽) */
const MODAL_CHART_MIN = { w: 1280, h: 560 };
const DEFAULT_MINI_INTERVAL = "1d";
let chartModalSymbol = "";
@@ -47,15 +49,12 @@ function limitForInterval(interval) {
return INTERVAL_LIMITS[interval] || 500;
}
function modalSize() {
const fs = document.fullscreenElement;
if (fs) {
return {
w: Math.max(800, window.innerWidth - 48),
h: Math.max(480, window.innerHeight - 160),
};
}
return { w: 1280, h: 680 };
function modalChartSize() {
const padX = 32;
const chromeY = 150;
const w = Math.max(MODAL_CHART_MIN.w, window.innerWidth - padX * 2);
const h = Math.max(MODAL_CHART_MIN.h, window.innerHeight - chromeY);
return { w, h };
}
function loadKlineFromLS(symbol, interval) {
@@ -171,7 +170,7 @@ function drawCandlestickChart(canvas, candles, options = {}) {
if (!canvas || !candles.length) return;
const large = options.large === true;
const size = large ? modalSize() : MINI_SIZE;
const size = large ? modalChartSize() : MINI_SIZE;
const volRatio = large ? 0.22 : 0.32;
const pad = large
? { t: 16, r: 16, b: 28, l: 56 }
@@ -298,7 +297,7 @@ async function loadMiniChart(box) {
drawCandlestickChart(canvas, candles, { large: false });
box.dataset.loaded = "1";
if (status) status.textContent = `${candles.length}日·${sourceLabel(source)}`;
box.title = `${symbol} 日K ${candles.length}根 (${sourceLabel(source)}),点击全屏`;
box.title = `${symbol} 日K ${candles.length}根 (${sourceLabel(source)}),点击查看大图`;
} catch (e) {
if (status) status.textContent = "—";
box.title = `${symbol}: ${e.message}`;
@@ -328,7 +327,7 @@ function ensureLwcChart(container) {
}
destroyLwcChart();
const { w, h } = modalSize();
const { w, h } = modalChartSize();
container.style.width = `${w}px`;
container.style.height = `${h}px`;
@@ -407,7 +406,7 @@ function updateModalMeta(candles, source, interval) {
title.textContent = `${chartModalSymbol} · ${interval.toUpperCase()} K线`;
}
if (hint) {
hint.textContent = `${candles.length} 根 · ${sourceLabel(source)} · 滚轮缩放 · 拖拽平移 · 十字线 · Esc 退出`;
hint.textContent = `${candles.length} 根 · ${sourceLabel(source)} · 滚轮缩放 · 拖拽平移 · 十字线 · Esc 或点击遮罩关闭`;
}
}
@@ -439,9 +438,6 @@ function closeChartModal() {
modal.classList.add("hidden");
destroyLwcChart();
chartModalSymbol = "";
if (document.fullscreenElement) {
document.exitFullscreen?.().catch(() => {});
}
}
async function openChartModal(symbol) {
@@ -465,12 +461,6 @@ async function openChartModal(symbol) {
if (container) container.innerHTML = "";
await loadModalChart(DEFAULT_MINI_INTERVAL);
const inner = modal.querySelector(".chart-modal-inner");
const req = inner.requestFullscreen || inner.webkitRequestFullscreen;
if (req) {
req.call(inner).catch(() => {});
}
}
function setupChartModal() {
@@ -514,14 +504,14 @@ function setupChartModal() {
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") closeChartModal();
});
document.addEventListener("fullscreenchange", () => {
window.addEventListener("resize", () => {
if (!chartModalSymbol || !lwcChart) return;
const container = document.getElementById("chart-modal-container");
if (!container) return;
const rect = container.getBoundingClientRect();
if (rect.width > 0 && rect.height > 0) {
lwcChart.applyOptions({ width: rect.width, height: rect.height });
}
if (!container?.isConnected) return;
const { w, h } = modalChartSize();
container.style.width = `${w}px`;
container.style.height = `${h}px`;
lwcChart.applyOptions({ width: w, height: h });
});
}