feat: 图表悬停显示数值
This commit is contained in:
1 parent
1a1a95bb9b
commit
c2daff02a3
3 files changed
+66
-6
No files matched your search
@@ -1,5 +1,12 @@
|
||||
# 更新日志
|
||||
|
||||
## 2026-10-26 → ✨ 图表鼠标悬停显示数值
|
||||
|
||||
- ✨ 三张趋势图(流量/CPU/内存)支持悬停:显示该时间点标签与各序列具体数值(下行/上行/CPU/内存)
|
||||
- ✨ 悬停时图表内出现竖向指示线,提示框跟随鼠标并自动避让窗口边缘
|
||||
- 🔧 提示框为单例浮层(`#chartTip`),不随 3 秒刷新重建;图表元数据按渲染批次注册,避免残留
|
||||
- ✅ 实测:流量图「13:00 下行 0 B / 上行 0 B」、CPU 图「07:00 CPU 0.00%」、内存图「18:00 3.34 GB」,移出后自动隐藏
|
||||
|
||||
## 2026-10-26 → ✨ 新增「运行时长 Top 5」,四个榜单并列一行
|
||||
|
||||
- ✨ Top 5 变四列:CPU / 内存 / 网络 / **运行时长**(按连续运行时长降序,显示 `1天5小时` 与 `1/1` 运行容器数,青色进度条)
|
||||
|
||||
+52
-6
@@ -168,6 +168,9 @@ function avatar(name) {
|
||||
}
|
||||
|
||||
/* ---------------- 趋势图表 ---------------- */
|
||||
const CHART_META = new Map();
|
||||
let chartSeq = 0;
|
||||
|
||||
function lineChart(points, series, unit, opts) {
|
||||
opts = opts || {};
|
||||
const W = 360, H = 118, PL = 4, PR = 4, PT = 12, PB = 17;
|
||||
@@ -181,21 +184,62 @@ function lineChart(points, series, unit, opts) {
|
||||
const ticks = points.map((p, i) => ({ i, l: p.label })).filter(({ i }) => i % gap === 0);
|
||||
const gridY = [0, 0.5, 1].map(f => PT + ih * f);
|
||||
const fmtMax = opts.fmt || ((v) => String(Math.round(v)));
|
||||
return `<svg class="tf-chart" viewBox="0 0 ${W} ${H}" preserveAspectRatio="none">
|
||||
<defs>${series.map((sr, si) => `<linearGradient id="g${si}${sr.key}" x1="0" y1="0" x2="0" y2="1">
|
||||
const cid = 'cv' + (++chartSeq);
|
||||
CHART_META.set(cid, { points, series, unit });
|
||||
return `<svg class="tf-chart" data-cid="${cid}" viewBox="0 0 ${W} ${H}" preserveAspectRatio="none">
|
||||
<defs>${series.map((sr, si) => `<linearGradient id="g${si}${cid}" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="${sr.color}" stop-opacity=".26"/><stop offset="100%" stop-color="${sr.color}" stop-opacity="0"/>
|
||||
</linearGradient>`).join('')}</defs>
|
||||
${gridY.map(y => `<line x1="${PL}" y1="${y.toFixed(1)}" x2="${PL + iw}" y2="${y.toFixed(1)}" stroke="#eef2f7" stroke-width="1"/>`).join('')}
|
||||
${series.map((sr, si) => {
|
||||
const pts = points.map((p, i) => `${xAt(i).toFixed(1)},${yAt(p[sr.key] || 0).toFixed(1)}`).join(' ');
|
||||
const area = sr.area ? `<polygon points="${PL},${PT + ih} ${pts} ${PL + iw},${PT + ih}" fill="url(#g${si}${sr.key})"/>` : '';
|
||||
const area = sr.area ? `<polygon points="${PL},${PT + ih} ${pts} ${PL + iw},${PT + ih}" fill="url(#g${si}${cid})"/>` : '';
|
||||
return `${area}<polyline points="${pts}" fill="none" stroke="${sr.color}" stroke-width="1.7" ${sr.dash ? 'stroke-dasharray="3 3"' : ''} stroke-linejoin="round"/>`;
|
||||
}).join('')}
|
||||
${ticks.map(({ i, l }) => `<text x="${xAt(i).toFixed(1)}" y="${H - 5}" font-size="8.5" fill="#93a0b4" text-anchor="middle">${esc(l)}</text>`).join('')}
|
||||
<text x="${PL}" y="${PT - 3}" font-size="8.5" fill="#93a0b4">峰值 ${fmtMax(max)}</text>
|
||||
<line class="ch-guide" x1="0" y1="${PT}" x2="0" y2="${PT + ih}" stroke="#c8d3e2" stroke-width="1" stroke-dasharray="2 2" opacity="0"/>
|
||||
</svg>`;
|
||||
}
|
||||
|
||||
function bindChartTips() {
|
||||
let tip = document.getElementById('chartTip');
|
||||
if (!tip) {
|
||||
tip = document.createElement('div');
|
||||
tip.id = 'chartTip';
|
||||
tip.className = 'chart-tip';
|
||||
document.body.appendChild(tip);
|
||||
}
|
||||
const hide = () => { tip.style.display = 'none'; document.querySelectorAll('.ch-guide').forEach(g => g.setAttribute('opacity', '0')); };
|
||||
$$('#view .tf-chart').forEach(svg => {
|
||||
const meta = CHART_META.get(svg.dataset.cid);
|
||||
if (!meta) return;
|
||||
svg.addEventListener('mousemove', (e) => {
|
||||
const r = svg.getBoundingClientRect();
|
||||
const n = meta.points.length;
|
||||
if (!n) return;
|
||||
const ratio = Math.min(1, Math.max(0, (e.clientX - r.left) / r.width));
|
||||
const idx = Math.round(ratio * (n - 1));
|
||||
const pt = meta.points[idx];
|
||||
if (!pt) return;
|
||||
const vbW = 360, PL = 4, PR = 4, PT = 12, PB = 17;
|
||||
const xv = PL + ((vbW - PL - PR) * idx) / Math.max(1, n - 1);
|
||||
const guide = svg.querySelector('.ch-guide');
|
||||
if (guide) { guide.setAttribute('x1', xv); guide.setAttribute('x2', xv); guide.setAttribute('opacity', '1'); }
|
||||
tip.innerHTML = `<div class="ct-t">${esc(pt.label)}</div>${meta.series.map(sr =>
|
||||
`<div class="ct-row"><span class="ct-dot" style="background:${sr.color}"></span>${esc(sr.name || sr.key)}<b>${sr.fmt ? sr.fmt(pt[sr.key] || 0) : fmtBytes(pt[sr.key] || 0)}</b></div>`).join('')}`;
|
||||
tip.style.display = 'block';
|
||||
const tw = tip.offsetWidth, th = tip.offsetHeight;
|
||||
let left = e.clientX + 12, top = e.clientY - th - 10;
|
||||
if (left + tw > window.innerWidth - 8) left = e.clientX - tw - 12;
|
||||
if (top < 8) top = e.clientY + 14;
|
||||
tip.style.left = left + 'px';
|
||||
tip.style.top = top + 'px';
|
||||
});
|
||||
svg.addEventListener('mouseleave', hide);
|
||||
});
|
||||
}
|
||||
|
||||
async function renderApps(silent) {
|
||||
const q = state.q ? `&q=${encodeURIComponent(state.q)}` : '';
|
||||
const [data, h, ev, ins, full, tf] = await Promise.all([
|
||||
@@ -329,6 +373,7 @@ async function renderApps(silent) {
|
||||
<div class="app-grid">${cards || '<div class="empty">没有匹配的应用</div>'}</div>`;
|
||||
$('#backOverview').onclick = () => go('apps');
|
||||
} else {
|
||||
CHART_META.clear(); chartSeq = 0;
|
||||
const tfPts = (tf && tf.points) || [];
|
||||
const tfTotal = (tf && tf.total) || { rx: 0, tx: 0 };
|
||||
const tfNow = (tf && tf.now) || { rx: 0, tx: 0, cpu: 0, mem: 0 };
|
||||
@@ -349,17 +394,17 @@ async function renderApps(silent) {
|
||||
<div class="trend-grid">
|
||||
<div class="trend-card">
|
||||
<div class="tc-head"><span class="tc-title">流量</span><span class="tc-now">↓ ${fmtRate(tfNow.rx)} · ↑ ${fmtRate(tfNow.tx)}</span></div>
|
||||
${tfPts.length ? lineChart(tfPts, [{ key: 'rx', color: '#1a73e8', area: true }, { key: 'tx', color: '#f79009', dash: true }], tf.unit, { fmt: fmtBytes }) : noData}
|
||||
${tfPts.length ? lineChart(tfPts, [{ key: 'rx', name: '下行', color: '#1a73e8', area: true, fmt: fmtBytes }, { key: 'tx', name: '上行', color: '#f79009', dash: true, fmt: fmtBytes }], tf.unit, { fmt: fmtBytes }) : noData}
|
||||
<div class="tc-foot">总 ${fmtBytes(tfTotal.rx + tfTotal.tx)} · 下 ${fmtBytes(tfTotal.rx)} · 上 ${fmtBytes(tfTotal.tx)}</div>
|
||||
</div>
|
||||
<div class="trend-card">
|
||||
<div class="tc-head"><span class="tc-title">CPU</span><span class="tc-now">${fmtPct(tfNow.cpu)}</span></div>
|
||||
${tfPts.length ? lineChart(tfPts, [{ key: 'cpu', color: '#12b76a', area: true }], tf.unit, { max: Math.min(100, Math.max(2, (tfPeak.cpu || 0) * 1.25)), fmt: (v) => v.toFixed(1) + '%' }) : noData}
|
||||
${tfPts.length ? lineChart(tfPts, [{ key: 'cpu', name: 'CPU', color: '#12b76a', area: true, fmt: (v) => v.toFixed(2) + '%' }], tf.unit, { max: Math.min(100, Math.max(2, (tfPeak.cpu || 0) * 1.25)), fmt: (v) => v.toFixed(1) + '%' }) : noData}
|
||||
<div class="tc-foot">均值 ${(tfAvg.cpu || 0).toFixed(1)}% · 峰值 ${(tfPeak.cpu || 0).toFixed(1)}% · ${tf && tf.cores ? tf.cores + ' 核' : ''}</div>
|
||||
</div>
|
||||
<div class="trend-card">
|
||||
<div class="tc-head"><span class="tc-title">内存</span><span class="tc-now">${fmtBytes(tfNow.mem)}</span></div>
|
||||
${tfPts.length ? lineChart(tfPts, [{ key: 'mem', color: '#7a5af8', area: true }], tf.unit, { fmt: fmtBytes }) : noData}
|
||||
${tfPts.length ? lineChart(tfPts, [{ key: 'mem', name: '内存', color: '#7a5af8', area: true, fmt: fmtBytes }], tf.unit, { fmt: fmtBytes }) : noData}
|
||||
<div class="tc-foot">均值 ${fmtBytes(tfAvg.mem || 0)} · 峰值 ${fmtBytes(tfPeak.mem || 0)}</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -395,6 +440,7 @@ async function renderApps(silent) {
|
||||
if (!tfDate.value) return;
|
||||
state.traffic.range = 'day'; state.traffic.date = tfDate.value; render();
|
||||
};
|
||||
bindChartTips();
|
||||
$$('#view .top-row').forEach(r => r.onclick = () => openApp(r.dataset.top));
|
||||
const moreBtn = $('#insightMore');
|
||||
if (moreBtn) moreBtn.onclick = () => { state.insightOpen = !state.insightOpen; render(); };
|
||||
|
||||
@@ -316,3 +316,10 @@ table.tb th.num{text-align:right}
|
||||
.tf-chart{width:100%;height:112px;display:block}
|
||||
|
||||
.top-note{font-size:11px;color:#9aa4b5;margin:-6px 0 12px 2px}
|
||||
|
||||
/* 图表悬停提示 */
|
||||
.chart-tip{position:fixed;z-index:9999;display:none;pointer-events:none;background:rgba(32,40,54,.94);color:#fff;border-radius:8px;padding:7px 9px;font-size:11.5px;line-height:1.5;box-shadow:0 6px 18px rgba(0,0,0,.18);min-width:110px}
|
||||
.chart-tip .ct-t{font-size:11px;color:#c9d3e2;margin-bottom:3px}
|
||||
.chart-tip .ct-row{display:flex;align-items:center;gap:6px;white-space:nowrap}
|
||||
.chart-tip .ct-row b{margin-left:auto;font-variant-numeric:tabular-nums}
|
||||
.chart-tip .ct-dot{width:6px;height:6px;border-radius:50%;display:inline-block}
|
||||
Reference in new issue
Block a user