feat(dashboard): four-skill level trend chart (inline SVG, from training log)

This commit is contained in:
Lang
2026-10-14 13:48:22 +08:00
parent 327530ec0e
commit e3513b5c2e
+47
View File
@@ -277,6 +277,9 @@ HTML = r"""<!doctype html>
.card .k{color:var(--muted);font-size:13px}
.card .v{font-size:28px;font-weight:700;margin-top:4px}
.card .c{color:var(--muted);font-size:12px;margin-top:6px}
.chart{background:var(--panel);border:1px solid var(--line);border-radius:12px;padding:14px 12px 6px}
.legend{display:flex;gap:16px;flex-wrap:wrap;font-size:13px;margin:0 6px 6px;color:var(--muted)}
.legend i{display:inline-block;width:10px;height:10px;border-radius:2px;margin-right:5px;vertical-align:middle}
table{width:100%;border-collapse:collapse;background:var(--panel);
border:1px solid var(--line);border-radius:10px;overflow:hidden;font-size:13.5px}
th,td{padding:9px 11px;text-align:left;border-bottom:1px solid var(--line);vertical-align:top}
@@ -307,6 +310,9 @@ HTML = r"""<!doctype html>
<h2>四维等级</h2>
<div class="cards" id="skills"></div>
<h2>等级趋势</h2>
<div class="chart" id="chart"></div>
<h2>掌握概览</h2>
<div class="cards" id="overview"></div>
@@ -406,6 +412,47 @@ document.getElementById("history").innerHTML =
(DATA.history.map(h => `<tr><td class="mono">${esc(h.date)}</td>
<td>${esc(h.text)}</td></tr>`).join("") || "") + `</tbody>`;
// 等级趋势图(从训练日志的每次等级画线)
(function () {
const LV = {A1:1, A2:2, B1:3, B2:4, C1:5, C2:6};
const val = s => {
const m = String(s || "").trim().toUpperCase().match(/^([ABC])([12])(\+?)/);
return m ? LV[m[1] + m[2]] + (m[3] ? 0.5 : 0) : null;
};
const SK = [
{k:"listening", l:"听", c:"#4da3ff"}, {k:"speaking", l:"说", c:"#f1c40f"},
{k:"reading", l:"读", c:"#2ecc71"}, {k:"writing", l:"写", c:"#e879f9"}];
const el = document.getElementById("chart");
const pts = DATA.log.slice().sort((a, b) => a.date < b.date ? -1 : 1)
.map(r => ({date: r.date, v: Object.fromEntries(SK.map(s => [s.k, val(r[s.k])]))}));
if (!pts.some(p => SK.some(s => p.v[s.k] != null))) {
el.innerHTML = '<div class="muted">暂无趋势数据</div>'; return;
}
const W = 760, H = 320, L = 46, R = 18, T = 16, B = 40, ymin = 0.7, ymax = 6.3;
const xs = i => L + (pts.length === 1 ? (W - L - R) / 2 : i * (W - L - R) / (pts.length - 1));
const ys = v => T + (ymax - v) * (H - T - B) / (ymax - ymin);
let g = "";
["A1","A2","B1","B2","C1","C2"].forEach((lab, i) => {
const y = ys(i + 1);
g += `<line x1="${L}" y1="${y}" x2="${W - R}" y2="${y}" stroke="var(--line)"/>`;
g += `<text x="${L - 8}" y="${y + 4}" fill="var(--muted)" font-size="11" text-anchor="end">${lab}</text>`;
});
pts.forEach((p, i) => {
g += `<text x="${xs(i)}" y="${H - 14}" fill="var(--muted)" font-size="11" text-anchor="middle">${p.date.slice(5)}</text>`;
});
SK.forEach(s => {
const seq = pts.map((p, i) => ({i, v: p.v[s.k]})).filter(o => o.v != null);
if (!seq.length) return;
if (seq.length > 1)
g += `<polyline fill="none" stroke="${s.c}" stroke-width="2" points="${seq.map(o => xs(o.i) + "," + ys(o.v)).join(" ")}"/>`;
seq.forEach(o => {
g += `<circle cx="${xs(o.i)}" cy="${ys(o.v)}" r="4" fill="${s.c}"><title>${s.l} ${o.v} · ${pts[o.i].date}</title></circle>`;
});
});
el.innerHTML = `<div class="legend">` + SK.map(s => `<span><i style="background:${s.c}"></i>${s.l}</span>`).join("") +
`</div><svg viewBox="0 0 ${W} ${H}" width="100%" preserveAspectRatio="xMidYMid meet">${g}</svg>`;
})();
renderItems();
</script>
</body>