388 lines
14 KiB
JavaScript
388 lines
14 KiB
JavaScript
// 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 = `<div>#</div><div>内容</div><div>次数</div>`;
|
||
root.appendChild(head);
|
||
|
||
if (list.length === 0) {
|
||
const row = document.createElement("div");
|
||
row.className = "tableRow";
|
||
row.innerHTML = `<div class="mono">-</div><div class="hint">暂无数据</div><div class="mono">-</div>`;
|
||
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 = `<div class="mono">#${idx + 1}</div><div>${escapeHtml(key)}</div><div class="mono">${cnt}</div>`;
|
||
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 = `<div>时间</div><div>内容</div><div>价值</div>`;
|
||
root.appendChild(head);
|
||
|
||
if (list.length === 0) {
|
||
const row = document.createElement("div");
|
||
row.className = "tableRow";
|
||
row.innerHTML = `<div class="mono">-</div><div class="hint">暂无中奖记录</div><div class="mono">-</div>`;
|
||
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 = `<div class="mono">${escapeHtml(fmtTime(ts))}</div><div>${escapeHtml(content)}</div><div class="mono">${escapeHtml(value)}</div>`;
|
||
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 = `
|
||
<filter id="${filterId}" x="-20%" y="-20%" width="140%" height="140%">
|
||
<feGaussianBlur stdDeviation="1.6" result="blur"/>
|
||
<feMerge>
|
||
<feMergeNode in="blur"/>
|
||
<feMergeNode in="SourceGraphic"/>
|
||
</feMerge>
|
||
</filter>
|
||
`;
|
||
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(`<span><i class="timelineDotMark" style="background:${TIMELINE_STROKES[k]?.stroke || 'var(--primary)'};opacity:${TIMELINE_STROKES[k]?.opacity ?? 1}"></i>${SERIES_LABEL[k] || k}</span><b>${v}</b>`);
|
||
}
|
||
if (total === 0) lines.push(`<span class="hint">无事件</span>`);
|
||
tip.innerHTML = `<div class="timelineTipTime">${fmtTimeOf(i)}</div><div class="timelineTipBody">${lines.join("")}</div>`;
|
||
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 = `<stop offset="0%" stop-color="currentColor" stop-opacity="0.28"/><stop offset="100%" stop-color="currentColor" stop-opacity="0"/>`;
|
||
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");
|
||
}
|