Files
ipass-plugin-cache/www/assets/chart.js
T
ssw 43718bd0c4 iPass cache gateway 源码
容量管理: zones.rs 直读 nginx file_cache 共享内存记账(双轴: 磁盘 + keys_zone 索引内存), ?scan=1 扫盘上限 20 万文件; POST /api/admin/cache/purge 整区清理(只删文件不删目录, 根治 [crit] mkdir failed); POST /api/admin/cache/invalidate 按制品精准失效(预览->确认两步式, glob/子串, 删除上限 5000/扫描上限 20 万); GET /api/admin/cache 新增 capacity 块; 前端容量面板 + 按制品失效工具条; 测试 test-cache-capacity.sh 48/48(容量 27 + 精准失效 21, 含 EXPECT_MIN 护栏与前端静态契约); docs 容量管理章节
2026-09-21 09:25:11 +08:00

142 lines
6.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* ============================================================================
图表:纯 SVG / HTML,**零外部依赖**
为什么不用 Chart.js / ECharts:管理台是网关容器里的一把静态文件,
引 CDN 等于把面板可用性押在公网可达性上(内网部署直接白屏),
引本地副本又要把几百 KB 第三方代码塞进镜像。趋势图只有折线和条形两种,
自己画不到 200 行,且颜色/字体能直接吃页面的 CSS 变量。
========================================================================== */
window.IP = window.IP || {};
IP.chart = (function () {
"use strict";
var VB_W = 720; // viewBox 宽度;实际宽度由 CSS 拉满容器
var COLOR = ["#2563eb", "#0d9488", "#7c3aed", "#ea580c", "#0891b2", "#db2777"];
/// 轴上限取整到 1/2/2.5/5/10 × 10^n,避免 y 轴刻度出现 8734 这种数
function niceMax(v) {
if (!(v > 0)) return 1;
var exp = Math.pow(10, Math.floor(Math.log10(v)));
var f = v / exp;
var n = f <= 1 ? 1 : f <= 2 ? 2 : f <= 2.5 ? 2.5 : f <= 5 ? 5 : 10;
return n * exp;
}
/// 大数简写:12345 → 1.2万。图表刻度里写全串数字会挤成一团
function fmtNum(n) {
n = Number(n) || 0;
if (n >= 100000000) return (n / 100000000).toFixed(1).replace(/\.0$/, "") + "亿";
if (n >= 10000) return (n / 10000).toFixed(n >= 1000000 ? 0 : 1).replace(/\.0$/, "") + "万";
return String(n);
}
function empty(el, text) {
if (el) el.innerHTML = '<div class="chart-empty">' + IP.esc(text || "暂无数据") + "</div>";
}
/**
* 折线(面积)图。
* @param el 容器元素
* @param opts.labels x 轴标签(如 ["09-07", ...])
* @param opts.series [{ name, values, color, fill }]
* @param opts.height 图形高度(px,默认 220)
*/
function line(el, opts) {
if (!el) return;
opts = opts || {};
var labels = opts.labels || [];
var series = (opts.series || []).filter(function (s) { return s && s.values && s.values.length; });
if (!labels.length || !series.length) return empty(el, opts.emptyText || "该区间内还没有数据");
var H = opts.height || 220;
var L = 48, R = 14, T = 16, B = 26;
var n = labels.length;
var max = 0;
series.forEach(function (s) {
s.values.forEach(function (v) { if (v > max) max = v; });
});
max = niceMax(max);
var iw = VB_W - L - R, ih = H - T - B;
var X = function (i) { return n === 1 ? L + iw / 2 : L + (iw * i) / (n - 1); };
var Y = function (v) { return T + ih * (1 - v / max); };
var parts = [];
// 网格 + y 轴刻度(5 条,含 0 与顶)
for (var g = 0; g <= 4; g++) {
var yv = (max * g) / 4, y = Y(yv);
parts.push('<line class="grid" x1="' + L + '" y1="' + y.toFixed(1) + '" x2="' + (VB_W - R) + '" y2="' + y.toFixed(1) + '"/>');
parts.push('<text class="tick" x="' + (L - 8) + '" y="' + (y + 4).toFixed(1) + '" text-anchor="end">' + fmtNum(yv) + "</text>");
}
// x 轴
parts.push('<line class="axis" x1="' + L + '" y1="' + (T + ih) + '" x2="' + (VB_W - R) + '" y2="' + (T + ih) + '"/>');
// x 轴标签:点多时抽稀,最多 8 个,否则 14 天标签会互相叠
var step = Math.ceil(n / 8);
for (var i = 0; i < n; i++) {
if (i % step !== 0 && i !== n - 1) continue;
parts.push('<text class="tick" x="' + X(i).toFixed(1) + '" y="' + (T + ih + 17) + '" text-anchor="middle">' +
IP.esc(labels[i]) + "</text>");
}
series.forEach(function (s, si) {
var color = s.color || COLOR[si % COLOR.length];
var pts = s.values.map(function (v, i) { return X(i).toFixed(1) + "," + Y(v).toFixed(1); }).join(" ");
if (s.fill !== false && series.length === 1) {
parts.push('<polygon fill="' + color + '" fill-opacity=".10" stroke="none" points="' +
X(0).toFixed(1) + "," + (T + ih) + " " + pts + " " + X(n - 1).toFixed(1) + "," + (T + ih) + '"/>');
}
parts.push('<polyline fill="none" stroke="' + color + '" stroke-width="2" stroke-linejoin="round" ' +
'stroke-linecap="round" points="' + pts + '"/>');
// 点:带 <title>,鼠标停上去显示「日期 · 序列名:值」
s.values.forEach(function (v, i) {
parts.push('<circle class="dot" cx="' + X(i).toFixed(1) + '" cy="' + Y(v).toFixed(1) + '" r="2.8" fill="' + color + '">' +
"<title>" + IP.esc(labels[i] + " · " + s.name + ":" + v) + "</title></circle>");
});
});
var legend = series.map(function (s, si) {
var color = s.color || COLOR[si % COLOR.length];
return '<span><i style="background:' + color + '"></i>' + IP.esc(s.name) + "</span>";
}).join("");
el.innerHTML =
(series.length > 1 ? '<div class="chart-legend">' + legend + "</div>" : "") +
// 不设 preserveAspectRatio="none":那会按容器横向拉伸,圆点变椭圆、文字变形。
// 保持默认等比缩放(CSS 里 height:auto),图表随宽度整体放大,文字始终正。
'<svg viewBox="0 0 ' + VB_W + " " + H + '" role="img">' +
parts.join("") + "</svg>";
}
/**
* 横向条形图(HTML 实现:文字对齐与换行交给浏览器,比 SVG text 稳)。
* @param opts.items [{ label, value, sub, color }] 已按业务排序
* @param opts.valueText 自定义右侧数值文本
*/
function bars(el, opts) {
if (!el) return;
opts = opts || {};
var items = opts.items || [];
if (!items.length) return empty(el, opts.emptyText || "该区间内还没有数据");
var max = 0;
items.forEach(function (it) { if (it.value > max) max = it.value; });
if (max <= 0) max = 1;
el.innerHTML = items.map(function (it, i) {
var pct = Math.max(2, Math.round((it.value / max) * 100));
var color = it.color || COLOR[i % COLOR.length];
var right = opts.valueText ? opts.valueText(it) : "<b>" + fmtNum(it.value) + "</b>" +
(it.sub ? ' <span class="muted">' + IP.esc(it.sub) + "</span>" : "");
return '<div class="bar-row">' +
'<div class="lb" title="' + IP.esc(it.label) + '">' + IP.esc(it.label) + "</div>" +
'<div class="bar-track"><div class="bar-fill" style="width:' + pct + "%;background:" + color + '"></div></div>' +
'<div class="vv">' + right + "</div>" +
"</div>";
}).join("");
}
return { line: line, bars: bars, fmtNum: fmtNum, niceMax: niceMax, COLOR: COLOR };
})();