| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /* ══════════════════════════════════════════════════════════════════
- 铭道 · 通用时间序列图(mts-* = Mingdao Time Series)
- v1.0 · 2026-08-14 · 从顾问端健康K线(mdk-*)泛化 · 净资产趋势/CFHI 等共用
- 用法:MingdaoTS.create({ mount, ranges, defaultRange, footFn, ... })
- 特性:周期档位 · 三态渲染(full/partial/empty)· 胶囊标注(起始/最高/最低/当前 · 同点自动合并)
- 整齐 Y 轴步长(2/4/5/10)· 家族/用户维度 points 映射 · compact 移动模式
- 纯 SVG 字符串渲染 · 零依赖 · CSS 自注入 · Phase 2 迁 RN 只换本渲染层
- ══════════════════════════════════════════════════════════════════ */
- window.MingdaoTS = (function () {
- var CSS = ''
- + '.mts-bar{display:flex;flex-direction:row;flex-wrap:wrap;gap:4px;justify-content:flex-end}'
- + '.mts-rbtn{font-family:"Noto Sans SC",sans-serif;font-size:.6rem;line-height:1;color:var(--text-muted,var(--ink-mute,#8A938E));background:transparent;border:1px solid rgba(44,110,73,.22);border-radius:99px;padding:5px 11px;cursor:pointer;transition:all .18s;white-space:nowrap}'
- + '.mts-rbtn:hover{color:var(--primary,#2C6E49);border-color:rgba(44,110,73,.5);background:rgba(44,110,73,.05)}'
- + '.mts-rbtn.active{color:#fff;background:var(--primary,#2C6E49);border-color:var(--primary,#2C6E49);font-weight:600}'
- + '.mts-rbtn.no-data{opacity:.62}'
- + '.mts-rbtn.no-data::after{content:"·";margin-left:4px;color:var(--amber,#C77A2C);font-weight:700}'
- + '.mts-empty{display:flex;flex-direction:column;align-items:center;justify-content:center;gap:8px;min-height:240px;padding:22px 18px;text-align:center;background:repeating-linear-gradient(135deg,rgba(44,110,73,.028) 0 10px,transparent 10px 20px);border:1px dashed rgba(44,110,73,.26);border-radius:10px}'
- + '.mts-empty-ic{font-size:1.4rem;opacity:.5}'
- + '.mts-empty-t{font-family:"Noto Serif SC",serif;font-size:.78rem;font-weight:600;color:var(--text,var(--ink,#22312B))}'
- + '.mts-empty-d{font-family:"Noto Sans SC",sans-serif;font-size:.6rem;color:var(--text-muted,var(--ink-mute,#8A938E));line-height:1.8;max-width:540px}';
- function injectCSS() {
- if (document.getElementById('mts-css')) return;
- var st = document.createElement('style');
- st.id = 'mts-css';
- st.textContent = CSS;
- document.head.appendChild(st);
- }
- /* 整齐步长(2/4/5/10) */
- function cleanAxis(vMin, vMax) {
- var cands = [2, 4, 5, 10];
- for (var i = 0; i < cands.length; i++) {
- var st = cands[i];
- var l = Math.floor((vMin - 1.5) / st) * st;
- var h = Math.ceil((vMax + 1.5) / st) * st;
- var nb = (h - l) / st;
- if (nb >= 3 && nb <= 5) return { lo: l, hi: h, step: st, bands: nb };
- }
- var l5 = Math.floor((vMin - 1.5) / 5) * 5;
- return { lo: l5, hi: l5 + 20, step: 5, bands: 4 };
- }
- function textWidth(s, fs) {
- var w = 0;
- for (var c = 0; c < s.length; c++) {
- var ch = s[c];
- w += ch === ' ' ? .30 : ch === '·' ? .50 : (ch.charCodeAt(0) < 128 ? .56 : 1.0);
- }
- return w * fs;
- }
- function capsule(cx, cy, text, fs, fill, anchor, VB_W) {
- var w = textWidth(text, fs) + 24, h = fs + 12;
- var x = anchor === 'start' ? cx : anchor === 'end' ? cx - w : cx - w / 2;
- x = Math.max(2, Math.min(x, VB_W - w - 2));
- var y = cy - h / 2, tip = Math.min(Math.max(cx, x + 10), x + w - 10);
- return '<g><rect x="' + x.toFixed(1) + '" y="' + y.toFixed(1) + '" width="' + w.toFixed(1) + '" height="' + h + '" rx="' + (h / 2) + '" fill="' + fill + '"/>'
- + '<path d="M' + (tip - 5) + ',' + (y + h).toFixed(1) + ' L' + (tip + 5) + ',' + (y + h).toFixed(1) + ' L' + tip.toFixed(1) + ',' + (y + h + 6.5).toFixed(1) + ' Z" fill="' + fill + '"/>'
- + '<text x="' + (x + w / 2).toFixed(1) + '" y="' + (cy + fs * .36).toFixed(1) + '" text-anchor="middle" font-family="\'Playfair Display\',\'Noto Serif SC\',serif" font-size="' + fs + '" font-weight="700" fill="#fff" letter-spacing=".5">' + text + '</text></g>';
- }
- function resolvePoints(points, userKey) {
- if (!points) return null;
- if (points instanceof Array) return points;
- return points[userKey] || points['demo-user'] || null;
- }
- /* cfg: { mount, ranges, defaultRange, userKey|userFn, footFn(rangeKey,userKey,stats), emptyFn(R),
- compact, hero:{id, valueFn(userKey)} } */
- function create(cfg) {
- injectCSS();
- var mount = cfg.mount;
- var uid = 'mts' + Math.floor(Math.random() * 1e6);
- var cur = null;
- function userKey() { return cfg.userFn ? cfg.userFn() : (cfg.userKey || 'demo-user'); }
- var btns = Object.keys(cfg.ranges).map(function (rk) {
- return '<button type="button" class="mts-rbtn" data-range="' + rk + '">' + cfg.ranges[rk].label + '</button>';
- }).join('');
- // title === false → 只渲染档位行(宿主卡片自带标题时)
- mount.innerHTML = (cfg.title === false
- ? '<div style="display:flex;flex-direction:row;justify-content:flex-end;margin-bottom:8px"><div class="mts-bar" id="' + uid + '-bar">' + btns + '</div></div>'
- : '<div style="display:flex;flex-direction:row;align-items:center;gap:8px;flex-wrap:wrap;margin-bottom:8px">'
- + '<div style="font-family:\'Noto Serif SC\',serif;font-size:.6rem;font-weight:700;color:var(--text,var(--ink,#22312B))">' + (cfg.title || '📈 走势') + '</div>'
- + '<div class="mts-bar" id="' + uid + '-bar" style="margin-left:auto">' + btns + '</div></div>')
- + '<div id="' + uid + '-chart"></div>'
- + '<div id="' + uid + '-foot" style="font-size:.56rem;color:var(--text-muted,var(--ink-mute,#8A938E));line-height:1.7;padding-top:6px;border-top:1px dashed rgba(44,110,73,.14)"></div>';
- mount.querySelector('#' + uid + '-bar').addEventListener('click', function (e) {
- var btn = e.target.closest('.mts-rbtn');
- if (btn) render(btn.getAttribute('data-range'));
- });
- function render(rangeKey) {
- var R = cfg.ranges[rangeKey];
- if (!R) return;
- cur = rangeKey;
- var chart = mount.querySelector('#' + uid + '-chart');
- var foot = mount.querySelector('#' + uid + '-foot');
- // 按钮态
- mount.querySelectorAll('.mts-rbtn').forEach(function (b) {
- var rk = b.getAttribute('data-range');
- b.classList.toggle('active', rk === rangeKey);
- var rp = resolvePoints(cfg.ranges[rk].points, userKey());
- b.classList.toggle('no-data', !(rp && rp.some(function (v) { return v !== null; })));
- });
- var pts = resolvePoints(R.points, userKey());
- var hasAny = !!(pts && pts.some(function (v) { return v !== null; }));
- if (!hasAny) {
- chart.innerHTML = cfg.emptyFn ? cfg.emptyFn(R)
- : '<div class="mts-empty"><div class="mts-empty-ic">⏳</div><div class="mts-empty-t">' + R.label + ' · 数据待接入</div>'
- + '<div class="mts-empty-d">该区间数据须由后台按数据契约回填后自动点亮 · 缺失时点回 null(不可用 0 代替)。<br>接口规格见 docs/数据契约 · 健康K线 CFHI 时间序列 v1.0.md</div></div>';
- foot.innerHTML = cfg.footEmptyFn ? cfg.footEmptyFn(R) : '';
- return;
- }
- var n = pts.length;
- var firstIdx = pts.findIndex(function (v) { return v !== null; });
- var lastIdx = n - 1 - pts.slice().reverse().findIndex(function (v) { return v !== null; });
- var real = [];
- for (var i = firstIdx; i <= lastIdx; i++) if (pts[i] !== null) real.push({ i: i, v: pts[i] });
- var vals = real.map(function (o) { return o.v; });
- var vMin = Math.min.apply(null, vals), vMax = Math.max.apply(null, vals);
- var vStart = vals[0], vNow = vals[vals.length - 1];
- var iMax = real[vals.indexOf(vMax)].i, iMin = real[vals.indexOf(vMin)].i;
- var compact = !!cfg.compact;
- var VB_W = compact ? 420 : 1000, VB_H = compact ? 260 : 320; // 08-14 加高 + compact 改 420 窄坐标系(1单位≈1px · 否则 420px 容器下整图缩小 2.6 倍)
- var X0 = compact ? 34 : 52, X1 = compact ? 400 : 962, Y_TOP = compact ? 38 : 44, Y_BOT = compact ? 204 : 252;
- var ax = cleanAxis(vMin, vMax);
- var X = function (i) { return X0 + (X1 - X0) * (n === 1 ? 0 : i / (n - 1)); };
- var Y = function (v) { return Y_BOT - (v - ax.lo) / (ax.hi - ax.lo) * (Y_BOT - Y_TOP); };
- var colLine = cfg.color || '#2C6E49';
- var s = '<svg viewBox="0 0 ' + VB_W + ' ' + VB_H + '" style="width:100%;height:auto;display:block">';
- s += '<defs><linearGradient id="' + uid + 'g" x1="0" y1="0" x2="0" y2="1">'
- + '<stop offset="0" stop-color="' + colLine + '" stop-opacity=".22"/><stop offset="1" stop-color="' + colLine + '" stop-opacity="0"/></linearGradient>'
- + '<pattern id="' + uid + 'h" width="8" height="8" patternTransform="rotate(45)" patternUnits="userSpaceOnUse">'
- + '<rect width="8" height="8" fill="rgba(154,148,132,.05)"/><line x1="0" y1="0" x2="0" y2="8" stroke="rgba(154,148,132,.3)" stroke-width="1.4"/></pattern></defs>';
- for (var g = 0; g <= ax.bands; g++) {
- var gv = ax.lo + ax.step * g;
- s += '<line x1="' + X0 + '" y1="' + Y(gv).toFixed(1) + '" x2="' + X1 + '" y2="' + Y(gv).toFixed(1) + '" stroke="rgba(44,110,73,.10)" stroke-width="1" stroke-dasharray="4 4"/>';
- s += '<text x="' + (X0 - 9) + '" y="' + (Y(gv) + 4.5).toFixed(1) + '" text-anchor="end" font-family="\'Playfair Display\',serif" font-size="13" fill="#9A9484">' + Math.round(gv) + '</text>';
- }
- if (firstIdx > 0) {
- var mw = X(firstIdx) - X0;
- s += '<rect x="' + X0 + '" y="' + Y_TOP + '" width="' + mw.toFixed(1) + '" height="' + (Y_BOT - Y_TOP) + '" fill="url(#' + uid + 'h)"/>';
- s += '<text x="' + (X0 + mw / 2).toFixed(1) + '" y="' + ((Y_TOP + Y_BOT) / 2).toFixed(1) + '" text-anchor="middle" font-family="\'Noto Sans SC\',sans-serif" font-size="13" fill="#9A9484">该区间暂无数据</text>';
- s += '<line x1="' + X(firstIdx).toFixed(1) + '" y1="' + Y_TOP + '" x2="' + X(firstIdx).toFixed(1) + '" y2="' + Y_BOT + '" stroke="rgba(154,148,132,.5)" stroke-width="1.2" stroke-dasharray="3 3"/>';
- }
- var d = real.map(function (o, k) { return (k ? 'L' : 'M') + X(o.i).toFixed(1) + ',' + Y(o.v).toFixed(1); }).join(' ');
- s += '<path d="' + d + ' L' + X(lastIdx).toFixed(1) + ',' + Y_BOT + ' L' + X(firstIdx).toFixed(1) + ',' + Y_BOT + ' Z" fill="url(#' + uid + 'g)"/>';
- s += '<path d="' + d + '" fill="none" stroke="' + colLine + '" stroke-width="2.6" stroke-linejoin="round"/>';
- if (real.length <= 40) real.forEach(function (o) {
- if (o.i !== iMax && o.i !== iMin && o.i !== lastIdx)
- s += '<circle cx="' + X(o.i).toFixed(1) + '" cy="' + Y(o.v).toFixed(1) + '" r="2.2" fill="' + colLine + '" opacity=".38"/>';
- });
- // 胶囊:同点自动合并(起始=最低 / 最高=当前)
- var GOLD = '#B8954A', RED = '#B5433A';
- var fsMax = compact ? 13 : 14, fsMin = compact ? 12.5 : 13.5, fsNow = compact ? 14 : 15.5;
- var sameSM = (iMin === firstIdx && vMin === vStart);
- var sameMN = (vMax === vNow); // 平台尾段首现最高≠末点也算同值 → 合并(与顾问端 mdk 一致)
- // 最高
- if (!sameMN) {
- s += '<line x1="' + X(iMax).toFixed(1) + '" y1="' + Y(vMax).toFixed(1) + '" x2="' + X(iMax).toFixed(1) + '" y2="' + Y_BOT + '" stroke="rgba(184,149,74,.42)" stroke-width="1.2" stroke-dasharray="3 4"/>';
- s += '<circle cx="' + X(iMax).toFixed(1) + '" cy="' + Y(vMax).toFixed(1) + '" r="5.5" fill="' + GOLD + '" stroke="#fff" stroke-width="2.2"/>';
- s += capsule(X(iMax), Y(vMax) - (compact ? 24 : 26), '最高 ' + vMax, fsMax, GOLD, 'middle', VB_W);
- }
- // 起始/最低
- s += '<line x1="' + X(iMin).toFixed(1) + '" y1="' + Y(vMin).toFixed(1) + '" x2="' + X(iMin).toFixed(1) + '" y2="' + Y_BOT + '" stroke="rgba(181,67,58,.35)" stroke-width="1.2" stroke-dasharray="3 4"/>';
- s += '<circle cx="' + X(iMin).toFixed(1) + '" cy="' + Y(vMin).toFixed(1) + '" r="5.5" fill="' + RED + '" stroke="#fff" stroke-width="2.2"/>';
- s += capsule(X(iMin), Y(vMin) - (compact ? 24 : 26), (sameSM ? '起始 · 最低 ' : '最低 ') + vMin, fsMin, RED, 'start', VB_W);
- if (!sameSM) {
- s += '<circle cx="' + X(firstIdx).toFixed(1) + '" cy="' + Y(vStart).toFixed(1) + '" r="4.5" fill="#8A8A7A" stroke="#fff" stroke-width="2"/>';
- s += capsule(X(firstIdx), Y(vStart) - (compact ? 22 : 24), '起始 ' + vStart, compact ? 12 : 13, '#8A8A7A', 'start', VB_W);
- }
- // 当前
- s += '<circle cx="' + X(lastIdx).toFixed(1) + '" cy="' + Y(vNow).toFixed(1) + '" r="10" fill="' + colLine + '" opacity=".2">'
- + '<animate attributeName="r" values="7;15;7" dur="2.2s" repeatCount="indefinite"/>'
- + '<animate attributeName="opacity" values=".3;.05;.3" dur="2.2s" repeatCount="indefinite"/></circle>';
- s += '<circle cx="' + X(lastIdx).toFixed(1) + '" cy="' + Y(vNow).toFixed(1) + '" r="6" fill="' + colLine + '" stroke="#fff" stroke-width="2.4"/>';
- s += capsule(X(lastIdx), Y(vNow) - (compact ? 26 : 28), (sameMN ? '当前 · 最高 ' : '当前 ') + vNow, fsNow, colLine, 'end', VB_W);
- // x 轴
- s += '<line x1="' + X0 + '" y1="' + Y_BOT + '" x2="' + X1 + '" y2="' + Y_BOT + '" stroke="rgba(44,110,73,.2)" stroke-width="1.2"/>';
- var axl = R.axis || [];
- axl.forEach(function (t, k) {
- var px = X0 + (X1 - X0) * k / (axl.length - 1);
- var an = k === 0 ? 'start' : k === axl.length - 1 ? 'end' : 'middle';
- s += '<text x="' + px.toFixed(1) + '" y="' + (Y_BOT + 20) + '" text-anchor="' + an + '" font-size="12" fill="#9A9484">' + t + '</text>';
- });
- s += '<text x="' + X0 + '" y="' + (Y_BOT + (compact ? 40 : 43)) + '" text-anchor="start" font-family="\'Playfair Display\',serif" font-size="13" fill="#6B6B5E">区间 ' + vMin + ' – ' + vMax + ' 振幅 ' + (+(vMax - vMin).toFixed(1)) + ' 分 净变 <tspan fill="' + colLine + '" font-weight="700">' + (vNow - vStart >= 0 ? '+' : '') + (+(vNow - vStart).toFixed(1)) + '</tspan></text>';
- s += '<text x="' + X1 + '" y="' + (Y_BOT + (compact ? 40 : 43)) + '" text-anchor="end" font-family="\'Playfair Display\',serif" font-size="12" letter-spacing="2" fill="#C9A84C">' + (cfg.brandTag || 'MINGDAO') + ' · ' + rangeKey + '</text>';
- s += '</svg>';
- chart.innerHTML = s;
- if (foot) foot.innerHTML = cfg.footFn ? cfg.footFn(rangeKey, userKey(), {
- start: vStart, min: vMin, max: vMax, now: vNow, n: n, real: real.length,
- partial: firstIdx > 0 || lastIdx < n - 1
- }) : '';
- }
- render(cfg.defaultRange || Object.keys(cfg.ranges)[0]);
- return { render: render, current: function () { return cur; } };
- }
- return { create: create };
- })();
|