diff --git a/webui/app.js b/webui/app.js index cc87d5a..b5deddf 100644 --- a/webui/app.js +++ b/webui/app.js @@ -718,393 +718,6 @@ function updateDeviceOverview() { setTextIfExists("topAdbState", deviceOverviewState.adb ? UI_COPY.device.adbReady : UI_COPY.device.adbMissing); } -function renderBars(containerId, dataMap, labelMap) { - const root = $(containerId); - if (!root) return; - clearEl(root); - - const m = (dataMap && typeof dataMap === "object") ? dataMap : {}; - const items = Object.keys(m).map(k => ({ key: k, count: Number(m[k]) || 0 })) - .filter(x => x.count > 0); - items.sort((a, b) => b.count - a.count); - - if (items.length === 0) { - const d = document.createElement("div"); - d.className = "hint"; - d.textContent = "暂无数据"; - root.appendChild(d); - return; - } - - const max = Math.max(...items.map(x => x.count)); - for (const it of items) { - const row = document.createElement("div"); - row.className = "barRow"; - - const label = document.createElement("div"); - label.className = "barLabel"; - label.textContent = (labelMap && labelMap[it.key]) ? labelMap[it.key] : it.key; - - const track = document.createElement("div"); - track.className = "barTrack"; - const fill = document.createElement("div"); - fill.className = "barFill"; - fill.style.width = `${max > 0 ? (it.count / max * 100) : 0}%`; - track.appendChild(fill); - - const val = document.createElement("div"); - val.className = "barValue"; - val.textContent = String(it.count); - - row.appendChild(label); - row.appendChild(track); - row.appendChild(val); - root.appendChild(row); - } -} - -function renderTopContent(containerId, items) { - const root = $(containerId); - if (!root) return; - clearEl(root); - - const list = Array.isArray(items) ? items : []; - const head = document.createElement("div"); - head.className = "tableRow tableHead"; - head.innerHTML = `
#
内容
次数
`; - root.appendChild(head); - - if (list.length === 0) { - const row = document.createElement("div"); - row.className = "tableRow"; - row.innerHTML = `
-
暂无数据
-
`; - root.appendChild(row); - return; - } - - list.forEach((it, idx) => { - const row = document.createElement("div"); - row.className = "tableRow"; - const key = String(it.key ?? ""); - const cnt = Number(it.count) || 0; - row.innerHTML = `
#${idx + 1}
${escapeHtml(key)}
${cnt}
`; - root.appendChild(row); - }); -} - -function renderWinsTable(containerId, wins) { - const root = $(containerId); - if (!root) return; - clearEl(root); - - const list = Array.isArray(wins) ? wins : []; - - const head = document.createElement("div"); - head.className = "tableRow tableHead"; - head.innerHTML = `
时间
内容
价值
`; - root.appendChild(head); - - if (list.length === 0) { - const row = document.createElement("div"); - row.className = "tableRow"; - row.innerHTML = `
-
暂无中奖记录
-
`; - root.appendChild(row); - return; - } - - // 后端按时间顺序存储,这里倒序展示(最新在上) - for (let i = list.length - 1; i >= 0; i--) { - const it = list[i] || {}; - const ts = Number(it.ts) || 0; - const content = String(it.content || "-"); - const value = (it.prize_value_yuan === null || typeof it.prize_value_yuan === "undefined") ? "-" : String(it.prize_value_yuan); - - const row = document.createElement("div"); - row.className = "tableRow"; - row.innerHTML = `
${escapeHtml(fmtTime(ts))}
${escapeHtml(content)}
${escapeHtml(value)}
`; - root.appendChild(row); - } -} - -function renderTimeline(containerId, timeline) { - const root = $(containerId); - if (!root) return; - clearEl(root); - - const t = (timeline && typeof timeline === "object") ? timeline : null; - const series = t && t.series && typeof t.series === "object" ? t.series : null; - const bins = Number(t && t.bins) || 0; - - if (!series || bins <= 0) { - const d = document.createElement("div"); - d.className = "hint"; - d.textContent = "暂无数据"; - root.appendChild(d); - return; - } - - let maxY = 0; - for (const k of TIMELINE_SERIES_KEYS) { - const arr = Array.isArray(series[k]) ? series[k] : []; - for (const v of arr) maxY = Math.max(maxY, Number(v) || 0); - } - if (maxY <= 0) { - const d = document.createElement("div"); - d.className = "hint"; - d.textContent = "暂无数据"; - root.appendChild(d); - return; - } - - const W = TIMELINE_LAYOUT.width; - const H = TIMELINE_LAYOUT.height; - const padL = TIMELINE_LAYOUT.paddingLeft; - const padR = TIMELINE_LAYOUT.paddingRight; - const padT = TIMELINE_LAYOUT.paddingTop; - const padB = TIMELINE_LAYOUT.paddingBottom; - - const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); - svg.setAttribute("viewBox", `0 0 ${W} ${H}`); - svg.setAttribute("width", "100%"); - svg.setAttribute("height", String(H)); - svg.classList.add("timelineSvg"); - - // SVG glow filter(feGaussianBlur + feMerge)让折线带霓虹光晕 - const NS_FILTER = "http://www.w3.org/2000/svg"; - const defs = document.createElementNS(NS_FILTER, "defs"); - const filterId = (containerId || "timeline") + "Glow"; - defs.innerHTML = ` - - - - - - - - `; - svg.appendChild(defs); - - const axis = document.createElementNS("http://www.w3.org/2000/svg", "path"); - axis.setAttribute("d", `M ${padL} ${H - padB} L ${W - padR} ${H - padB}`); - axis.setAttribute("stroke", "var(--border)"); - axis.setAttribute("fill", "none"); - svg.appendChild(axis); - - // 水平网格线(4 等分),淡色 dashed - const gridSteps = 4; - for (let i = 1; i <= gridSteps; i++) { - const gy = padT + ((H - padT - padB) * i / gridSteps); - const gline = document.createElementNS("http://www.w3.org/2000/svg", "path"); - gline.setAttribute("d", `M ${padL} ${gy.toFixed(2)} L ${W - padR} ${gy.toFixed(2)}`); - gline.setAttribute("stroke", "var(--border)"); - gline.setAttribute("stroke-opacity", "0.5"); - gline.setAttribute("stroke-dasharray", "2 4"); - gline.setAttribute("fill", "none"); - svg.appendChild(gline); - } - - const mkText = (x, y, txt, anchor = "start") => { - const el = document.createElementNS("http://www.w3.org/2000/svg", "text"); - el.setAttribute("x", String(x)); - el.setAttribute("y", String(y)); - el.setAttribute("fill", "var(--muted)"); - el.setAttribute("font-size", "10"); - el.setAttribute("font-family", "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace"); - el.setAttribute("text-anchor", anchor); - el.textContent = txt; - return el; - }; - - const startTs = Number(t.start_ts) || 0; - const binSec = Number(t.bin_seconds) || 0; - const xAt = (i) => { - if (bins <= 1) return padL; - return padL + (W - padL - padR) * (i / (bins - 1)); - }; - const yAt = (v) => { - const vv = Number(v) || 0; - const h = (H - padT - padB); - return (H - padB) - (vv / maxY) * h; - }; - - // y 轴刻度(0 / mid / max) - const yMid = Math.round(maxY / 2); - svg.appendChild(mkText(padL - 6, padT + 4, String(maxY), "end")); - if (yMid > 0 && yMid < maxY) { - svg.appendChild(mkText(padL - 6, padT + (H - padT - padB) / 2 + 4, String(yMid), "end")); - } - svg.appendChild(mkText(padL - 6, H - padB + 4, "0", "end")); - - // x 轴 3 个时间刻度 - const fmtShort = (ts) => { - if (!ts) return "-"; - try { return new Date(ts * 1000).toLocaleTimeString(); } catch { return "-"; } - }; - svg.appendChild(mkText(padL, H - 8, fmtShort(startTs), "start")); - svg.appendChild(mkText((padL + (W - padR)) / 2, H - 8, fmtShort(startTs + Math.floor(bins / 2) * binSec), "middle")); - svg.appendChild(mkText(W - padR, H - 8, fmtShort(startTs + (bins - 1) * binSec), "end")); - - for (const k of TIMELINE_SERIES_KEYS) { - const arr = Array.isArray(series[k]) ? series[k] : []; - if (arr.length === 0) continue; - let d = ""; - for (let i = 0; i < Math.min(bins, arr.length); i++) { - const x = xAt(i); - const y = yAt(arr[i]); - d += (i === 0) ? `M ${x.toFixed(2)} ${y.toFixed(2)}` : ` L ${x.toFixed(2)} ${y.toFixed(2)}`; - } - const p = document.createElementNS("http://www.w3.org/2000/svg", "path"); - p.setAttribute("d", d); - p.setAttribute("fill", "none"); - p.setAttribute("stroke", TIMELINE_STROKES[k]?.stroke || "var(--primary)"); - p.setAttribute("stroke-width", "1.8"); - p.setAttribute("stroke-opacity", String(TIMELINE_STROKES[k]?.opacity ?? 1)); - if (TIMELINE_STROKES[k]?.dash) p.setAttribute("stroke-dasharray", TIMELINE_STROKES[k].dash); - p.setAttribute("stroke-linecap", "round"); - p.setAttribute("stroke-linejoin", "round"); - p.setAttribute("filter", `url(#${filterId})`); - svg.appendChild(p); - } - - // Crosshair hover layer:垂直线 + 当前 bin 的 dot 圆 + tooltip - const NS = "http://www.w3.org/2000/svg"; - const crossLine = document.createElementNS(NS, "line"); - crossLine.setAttribute("y1", String(padT)); - crossLine.setAttribute("y2", String(H - padB)); - crossLine.setAttribute("stroke", "var(--text)"); - crossLine.setAttribute("stroke-opacity", "0.25"); - crossLine.setAttribute("stroke-dasharray", "2 3"); - crossLine.setAttribute("pointer-events", "none"); - crossLine.setAttribute("visibility", "hidden"); - svg.appendChild(crossLine); - // 容器(外层 root)作为相对定位锚点 - root.style.position = "relative"; - - const tip = document.createElement("div"); - tip.className = "timelineTip"; - tip.hidden = true; - root.appendChild(tip); - - const fmtTimeOf = (i) => { - if (!startTs || !binSec) return ""; - try { return new Date((startTs + i * binSec) * 1000).toLocaleTimeString(); } catch { return ""; } - }; - const SERIES_LABEL = { - fudai_open_detail: "打开详情", - fudai_attend_ok: "参与成功", - fudai_attend_fail: "参与失败", - fudai_result_win: "中奖", - fudai_result_no_win: "未中奖", - fudai_skip: "跳过", - switch_room: "切换直播间", - }; - - const onMove = (ev) => { - const rect = svg.getBoundingClientRect(); - const xRatio = (ev.clientX - rect.left) / rect.width; - if (xRatio < 0 || xRatio > 1) return; - const xCoord = padL + (W - padL - padR) * xRatio; - // 找最近的 bin - let i = Math.round(((xCoord - padL) / Math.max(1, W - padL - padR)) * (bins - 1)); - if (i < 0) i = 0; else if (i >= bins) i = bins - 1; - const xOnAxis = xAt(i); - crossLine.setAttribute("x1", String(xOnAxis)); - crossLine.setAttribute("x2", String(xOnAxis)); - crossLine.setAttribute("visibility", "visible"); - - // 生成 tip 内容 - const lines = []; - let total = 0; - for (const k of TIMELINE_SERIES_KEYS) { - const arr = Array.isArray(series[k]) ? series[k] : []; - const v = Number(arr[i]) || 0; - total += v; - if (v > 0) lines.push(`${SERIES_LABEL[k] || k}${v}`); - } - if (total === 0) lines.push(`无事件`); - tip.innerHTML = `
${fmtTimeOf(i)}
${lines.join("")}
`; - tip.hidden = false; - - // tip 位置:根据当前 svg 实际宽度比例换算到 root 内 px - const actualX = (xOnAxis / W) * rect.width; - let leftPx = actualX + 8; - if (leftPx + 160 > rect.width) leftPx = actualX - 168; - if (leftPx < 0) leftPx = 4; - tip.style.left = `${Math.max(0, leftPx)}px`; - tip.style.top = `${rect.height * 0.06}px`; - }; - const onLeave = () => { - crossLine.setAttribute("visibility", "hidden"); - tip.hidden = true; - }; - svg.addEventListener("mousemove", onMove); - svg.addEventListener("mouseleave", onLeave); - - root.appendChild(svg); - - const legend = document.createElement("div"); - legend.className = "hint"; - legend.textContent = "图例:实线/虚线区分同色不同事件 · 鼠标悬停查看明细"; - root.appendChild(legend); -} - -function renderOverviewSparkline(svgId, timeline) { - const svg = document.getElementById(svgId); - if (!svg) return; - // 把 fudai_open_detail + fudai_attend_ok 两个序列合成一条迷你折线 - const series = (timeline && timeline.series) || {}; - const keys = ["fudai_open_detail", "fudai_attend_ok"]; - const arrs = keys.map((k) => Array.isArray(series[k]) ? series[k] : []); - const len = Math.max(...arrs.map((a) => a.length), 0); - if (len < 2) { - svg.innerHTML = ""; - svg.classList.remove("hasData"); - return; - } - const combined = new Array(len).fill(0); - for (const arr of arrs) { - for (let i = 0; i < arr.length; i++) combined[i] += Number(arr[i]) || 0; - } - const maxY = Math.max(1, ...combined); - // 100x24 viewBox:留 1px 边 - const W = 100, H = 24, padY = 1.5; - const xAt = (i) => (W * i) / (len - 1); - const yAt = (v) => H - padY - ((H - padY * 2) * v) / maxY; - let d = ""; - for (let i = 0; i < len; i++) { - const x = xAt(i).toFixed(2), y = yAt(combined[i]).toFixed(2); - d += (i === 0 ? "M" : " L") + x + " " + y; - } - // 渐变填充区 - const dArea = d + ` L ${W} ${H} L 0 ${H} Z`; - const NS = "http://www.w3.org/2000/svg"; - svg.setAttribute("viewBox", `0 0 ${W} ${H}`); - svg.innerHTML = ""; - // gradient defs - const defs = document.createElementNS(NS, "defs"); - const gid = svgId + "Grad"; - const grad = document.createElementNS(NS, "linearGradient"); - grad.setAttribute("id", gid); - grad.setAttribute("x1", "0"); grad.setAttribute("y1", "0"); - grad.setAttribute("x2", "0"); grad.setAttribute("y2", "1"); - grad.innerHTML = ``; - defs.appendChild(grad); - svg.appendChild(defs); - const area = document.createElementNS(NS, "path"); - area.setAttribute("d", dArea); - area.setAttribute("fill", `url(#${gid})`); - svg.appendChild(area); - const line = document.createElementNS(NS, "path"); - line.setAttribute("d", d); - line.setAttribute("fill", "none"); - line.setAttribute("stroke", "currentColor"); - line.setAttribute("stroke-width", "1.25"); - line.setAttribute("stroke-linecap", "round"); - line.setAttribute("stroke-linejoin", "round"); - svg.appendChild(line); - svg.classList.add("hasData"); -} - async function refreshDevices() { const data = await apiGet("/api/devices"); const sel = $("device"); diff --git a/webui/index.html b/webui/index.html index a28f609..d282bac 100644 --- a/webui/index.html +++ b/webui/index.html @@ -1278,10 +1278,11 @@ - - - - - + + + + + + diff --git a/webui/js/dashboard_renderers.js b/webui/js/dashboard_renderers.js new file mode 100644 index 0000000..75b434c --- /dev/null +++ b/webui/js/dashboard_renderers.js @@ -0,0 +1,387 @@ +// Dashboard chart and table renderers shared by WebUI classic scripts. +function renderBars(containerId, dataMap, labelMap) { + const root = $(containerId); + if (!root) return; + clearEl(root); + + const m = (dataMap && typeof dataMap === "object") ? dataMap : {}; + const items = Object.keys(m).map(k => ({ key: k, count: Number(m[k]) || 0 })) + .filter(x => x.count > 0); + items.sort((a, b) => b.count - a.count); + + if (items.length === 0) { + const d = document.createElement("div"); + d.className = "hint"; + d.textContent = "暂无数据"; + root.appendChild(d); + return; + } + + const max = Math.max(...items.map(x => x.count)); + for (const it of items) { + const row = document.createElement("div"); + row.className = "barRow"; + + const label = document.createElement("div"); + label.className = "barLabel"; + label.textContent = (labelMap && labelMap[it.key]) ? labelMap[it.key] : it.key; + + const track = document.createElement("div"); + track.className = "barTrack"; + const fill = document.createElement("div"); + fill.className = "barFill"; + fill.style.width = `${max > 0 ? (it.count / max * 100) : 0}%`; + track.appendChild(fill); + + const val = document.createElement("div"); + val.className = "barValue"; + val.textContent = String(it.count); + + row.appendChild(label); + row.appendChild(track); + row.appendChild(val); + root.appendChild(row); + } +} + +function renderTopContent(containerId, items) { + const root = $(containerId); + if (!root) return; + clearEl(root); + + const list = Array.isArray(items) ? items : []; + const head = document.createElement("div"); + head.className = "tableRow tableHead"; + head.innerHTML = `
#
内容
次数
`; + root.appendChild(head); + + if (list.length === 0) { + const row = document.createElement("div"); + row.className = "tableRow"; + row.innerHTML = `
-
暂无数据
-
`; + root.appendChild(row); + return; + } + + list.forEach((it, idx) => { + const row = document.createElement("div"); + row.className = "tableRow"; + const key = String(it.key ?? ""); + const cnt = Number(it.count) || 0; + row.innerHTML = `
#${idx + 1}
${escapeHtml(key)}
${cnt}
`; + root.appendChild(row); + }); +} + +function renderWinsTable(containerId, wins) { + const root = $(containerId); + if (!root) return; + clearEl(root); + + const list = Array.isArray(wins) ? wins : []; + + const head = document.createElement("div"); + head.className = "tableRow tableHead"; + head.innerHTML = `
时间
内容
价值
`; + root.appendChild(head); + + if (list.length === 0) { + const row = document.createElement("div"); + row.className = "tableRow"; + row.innerHTML = `
-
暂无中奖记录
-
`; + root.appendChild(row); + return; + } + + // 后端按时间顺序存储,这里倒序展示(最新在上) + for (let i = list.length - 1; i >= 0; i--) { + const it = list[i] || {}; + const ts = Number(it.ts) || 0; + const content = String(it.content || "-"); + const value = (it.prize_value_yuan === null || typeof it.prize_value_yuan === "undefined") ? "-" : String(it.prize_value_yuan); + + const row = document.createElement("div"); + row.className = "tableRow"; + row.innerHTML = `
${escapeHtml(fmtTime(ts))}
${escapeHtml(content)}
${escapeHtml(value)}
`; + root.appendChild(row); + } +} + +function renderTimeline(containerId, timeline) { + const root = $(containerId); + if (!root) return; + clearEl(root); + + const t = (timeline && typeof timeline === "object") ? timeline : null; + const series = t && t.series && typeof t.series === "object" ? t.series : null; + const bins = Number(t && t.bins) || 0; + + if (!series || bins <= 0) { + const d = document.createElement("div"); + d.className = "hint"; + d.textContent = "暂无数据"; + root.appendChild(d); + return; + } + + let maxY = 0; + for (const k of TIMELINE_SERIES_KEYS) { + const arr = Array.isArray(series[k]) ? series[k] : []; + for (const v of arr) maxY = Math.max(maxY, Number(v) || 0); + } + if (maxY <= 0) { + const d = document.createElement("div"); + d.className = "hint"; + d.textContent = "暂无数据"; + root.appendChild(d); + return; + } + + const W = TIMELINE_LAYOUT.width; + const H = TIMELINE_LAYOUT.height; + const padL = TIMELINE_LAYOUT.paddingLeft; + const padR = TIMELINE_LAYOUT.paddingRight; + const padT = TIMELINE_LAYOUT.paddingTop; + const padB = TIMELINE_LAYOUT.paddingBottom; + + const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg"); + svg.setAttribute("viewBox", `0 0 ${W} ${H}`); + svg.setAttribute("width", "100%"); + svg.setAttribute("height", String(H)); + svg.classList.add("timelineSvg"); + + // SVG glow filter(feGaussianBlur + feMerge)让折线带霓虹光晕 + const NS_FILTER = "http://www.w3.org/2000/svg"; + const defs = document.createElementNS(NS_FILTER, "defs"); + const filterId = (containerId || "timeline") + "Glow"; + defs.innerHTML = ` + + + + + + + + `; + svg.appendChild(defs); + + const axis = document.createElementNS("http://www.w3.org/2000/svg", "path"); + axis.setAttribute("d", `M ${padL} ${H - padB} L ${W - padR} ${H - padB}`); + axis.setAttribute("stroke", "var(--border)"); + axis.setAttribute("fill", "none"); + svg.appendChild(axis); + + // 水平网格线(4 等分),淡色 dashed + const gridSteps = 4; + for (let i = 1; i <= gridSteps; i++) { + const gy = padT + ((H - padT - padB) * i / gridSteps); + const gline = document.createElementNS("http://www.w3.org/2000/svg", "path"); + gline.setAttribute("d", `M ${padL} ${gy.toFixed(2)} L ${W - padR} ${gy.toFixed(2)}`); + gline.setAttribute("stroke", "var(--border)"); + gline.setAttribute("stroke-opacity", "0.5"); + gline.setAttribute("stroke-dasharray", "2 4"); + gline.setAttribute("fill", "none"); + svg.appendChild(gline); + } + + const mkText = (x, y, txt, anchor = "start") => { + const el = document.createElementNS("http://www.w3.org/2000/svg", "text"); + el.setAttribute("x", String(x)); + el.setAttribute("y", String(y)); + el.setAttribute("fill", "var(--muted)"); + el.setAttribute("font-size", "10"); + el.setAttribute("font-family", "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace"); + el.setAttribute("text-anchor", anchor); + el.textContent = txt; + return el; + }; + + const startTs = Number(t.start_ts) || 0; + const binSec = Number(t.bin_seconds) || 0; + const xAt = (i) => { + if (bins <= 1) return padL; + return padL + (W - padL - padR) * (i / (bins - 1)); + }; + const yAt = (v) => { + const vv = Number(v) || 0; + const h = (H - padT - padB); + return (H - padB) - (vv / maxY) * h; + }; + + // y 轴刻度(0 / mid / max) + const yMid = Math.round(maxY / 2); + svg.appendChild(mkText(padL - 6, padT + 4, String(maxY), "end")); + if (yMid > 0 && yMid < maxY) { + svg.appendChild(mkText(padL - 6, padT + (H - padT - padB) / 2 + 4, String(yMid), "end")); + } + svg.appendChild(mkText(padL - 6, H - padB + 4, "0", "end")); + + // x 轴 3 个时间刻度 + const fmtShort = (ts) => { + if (!ts) return "-"; + try { return new Date(ts * 1000).toLocaleTimeString(); } catch { return "-"; } + }; + svg.appendChild(mkText(padL, H - 8, fmtShort(startTs), "start")); + svg.appendChild(mkText((padL + (W - padR)) / 2, H - 8, fmtShort(startTs + Math.floor(bins / 2) * binSec), "middle")); + svg.appendChild(mkText(W - padR, H - 8, fmtShort(startTs + (bins - 1) * binSec), "end")); + + for (const k of TIMELINE_SERIES_KEYS) { + const arr = Array.isArray(series[k]) ? series[k] : []; + if (arr.length === 0) continue; + let d = ""; + for (let i = 0; i < Math.min(bins, arr.length); i++) { + const x = xAt(i); + const y = yAt(arr[i]); + d += (i === 0) ? `M ${x.toFixed(2)} ${y.toFixed(2)}` : ` L ${x.toFixed(2)} ${y.toFixed(2)}`; + } + const p = document.createElementNS("http://www.w3.org/2000/svg", "path"); + p.setAttribute("d", d); + p.setAttribute("fill", "none"); + p.setAttribute("stroke", TIMELINE_STROKES[k]?.stroke || "var(--primary)"); + p.setAttribute("stroke-width", "1.8"); + p.setAttribute("stroke-opacity", String(TIMELINE_STROKES[k]?.opacity ?? 1)); + if (TIMELINE_STROKES[k]?.dash) p.setAttribute("stroke-dasharray", TIMELINE_STROKES[k].dash); + p.setAttribute("stroke-linecap", "round"); + p.setAttribute("stroke-linejoin", "round"); + p.setAttribute("filter", `url(#${filterId})`); + svg.appendChild(p); + } + + // Crosshair hover layer:垂直线 + 当前 bin 的 dot 圆 + tooltip + const NS = "http://www.w3.org/2000/svg"; + const crossLine = document.createElementNS(NS, "line"); + crossLine.setAttribute("y1", String(padT)); + crossLine.setAttribute("y2", String(H - padB)); + crossLine.setAttribute("stroke", "var(--text)"); + crossLine.setAttribute("stroke-opacity", "0.25"); + crossLine.setAttribute("stroke-dasharray", "2 3"); + crossLine.setAttribute("pointer-events", "none"); + crossLine.setAttribute("visibility", "hidden"); + svg.appendChild(crossLine); + // 容器(外层 root)作为相对定位锚点 + root.style.position = "relative"; + + const tip = document.createElement("div"); + tip.className = "timelineTip"; + tip.hidden = true; + root.appendChild(tip); + + const fmtTimeOf = (i) => { + if (!startTs || !binSec) return ""; + try { return new Date((startTs + i * binSec) * 1000).toLocaleTimeString(); } catch { return ""; } + }; + const SERIES_LABEL = { + fudai_open_detail: "打开详情", + fudai_attend_ok: "参与成功", + fudai_attend_fail: "参与失败", + fudai_result_win: "中奖", + fudai_result_no_win: "未中奖", + fudai_skip: "跳过", + switch_room: "切换直播间", + }; + + const onMove = (ev) => { + const rect = svg.getBoundingClientRect(); + const xRatio = (ev.clientX - rect.left) / rect.width; + if (xRatio < 0 || xRatio > 1) return; + const xCoord = padL + (W - padL - padR) * xRatio; + // 找最近的 bin + let i = Math.round(((xCoord - padL) / Math.max(1, W - padL - padR)) * (bins - 1)); + if (i < 0) i = 0; else if (i >= bins) i = bins - 1; + const xOnAxis = xAt(i); + crossLine.setAttribute("x1", String(xOnAxis)); + crossLine.setAttribute("x2", String(xOnAxis)); + crossLine.setAttribute("visibility", "visible"); + + // 生成 tip 内容 + const lines = []; + let total = 0; + for (const k of TIMELINE_SERIES_KEYS) { + const arr = Array.isArray(series[k]) ? series[k] : []; + const v = Number(arr[i]) || 0; + total += v; + if (v > 0) lines.push(`${SERIES_LABEL[k] || k}${v}`); + } + if (total === 0) lines.push(`无事件`); + tip.innerHTML = `
${fmtTimeOf(i)}
${lines.join("")}
`; + tip.hidden = false; + + // tip 位置:根据当前 svg 实际宽度比例换算到 root 内 px + const actualX = (xOnAxis / W) * rect.width; + let leftPx = actualX + 8; + if (leftPx + 160 > rect.width) leftPx = actualX - 168; + if (leftPx < 0) leftPx = 4; + tip.style.left = `${Math.max(0, leftPx)}px`; + tip.style.top = `${rect.height * 0.06}px`; + }; + const onLeave = () => { + crossLine.setAttribute("visibility", "hidden"); + tip.hidden = true; + }; + svg.addEventListener("mousemove", onMove); + svg.addEventListener("mouseleave", onLeave); + + root.appendChild(svg); + + const legend = document.createElement("div"); + legend.className = "hint"; + legend.textContent = "图例:实线/虚线区分同色不同事件 · 鼠标悬停查看明细"; + root.appendChild(legend); +} + +function renderOverviewSparkline(svgId, timeline) { + const svg = document.getElementById(svgId); + if (!svg) return; + // 把 fudai_open_detail + fudai_attend_ok 两个序列合成一条迷你折线 + const series = (timeline && timeline.series) || {}; + const keys = ["fudai_open_detail", "fudai_attend_ok"]; + const arrs = keys.map((k) => Array.isArray(series[k]) ? series[k] : []); + const len = Math.max(...arrs.map((a) => a.length), 0); + if (len < 2) { + svg.innerHTML = ""; + svg.classList.remove("hasData"); + return; + } + const combined = new Array(len).fill(0); + for (const arr of arrs) { + for (let i = 0; i < arr.length; i++) combined[i] += Number(arr[i]) || 0; + } + const maxY = Math.max(1, ...combined); + // 100x24 viewBox:留 1px 边 + const W = 100, H = 24, padY = 1.5; + const xAt = (i) => (W * i) / (len - 1); + const yAt = (v) => H - padY - ((H - padY * 2) * v) / maxY; + let d = ""; + for (let i = 0; i < len; i++) { + const x = xAt(i).toFixed(2), y = yAt(combined[i]).toFixed(2); + d += (i === 0 ? "M" : " L") + x + " " + y; + } + // 渐变填充区 + const dArea = d + ` L ${W} ${H} L 0 ${H} Z`; + const NS = "http://www.w3.org/2000/svg"; + svg.setAttribute("viewBox", `0 0 ${W} ${H}`); + svg.innerHTML = ""; + // gradient defs + const defs = document.createElementNS(NS, "defs"); + const gid = svgId + "Grad"; + const grad = document.createElementNS(NS, "linearGradient"); + grad.setAttribute("id", gid); + grad.setAttribute("x1", "0"); grad.setAttribute("y1", "0"); + grad.setAttribute("x2", "0"); grad.setAttribute("y2", "1"); + grad.innerHTML = ``; + defs.appendChild(grad); + svg.appendChild(defs); + const area = document.createElementNS(NS, "path"); + area.setAttribute("d", dArea); + area.setAttribute("fill", `url(#${gid})`); + svg.appendChild(area); + const line = document.createElementNS(NS, "path"); + line.setAttribute("d", d); + line.setAttribute("fill", "none"); + line.setAttribute("stroke", "currentColor"); + line.setAttribute("stroke-width", "1.25"); + line.setAttribute("stroke-linecap", "round"); + line.setAttribute("stroke-linejoin", "round"); + svg.appendChild(line); + svg.classList.add("hasData"); +}