@x-cmd/draw 0.4.6 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/dist/client.d.ts +42 -0
- package/dist/client.js +267 -0
- package/dist/core.d.ts +86 -0
- package/dist/core.js +132 -0
- package/dist/globe.d.ts +4 -0
- package/dist/globe.js +6 -0
- package/dist/layout/cluster.d.ts +2 -0
- package/dist/layout/cluster.js +228 -0
- package/dist/layout/index.d.ts +2 -0
- package/dist/layout/index.js +6 -0
- package/dist/layout/layered.d.ts +51 -0
- package/dist/layout/layered.js +433 -0
- package/dist/layout/portsize.d.ts +18 -0
- package/dist/layout/portsize.js +34 -0
- package/dist/lib.d.ts +17 -4
- package/dist/lib.js +20 -4
- package/dist/node/cmd.d.ts +1 -0
- package/dist/node/cmd.js +3 -0
- package/dist/render/abc-parse.d.ts +75 -0
- package/dist/render/abc-parse.js +345 -0
- package/dist/render/abcjs.d.ts +10 -0
- package/dist/render/abcjs.js +90 -0
- package/dist/render/anno.d.ts +48 -2
- package/dist/render/anno.js +171 -17
- package/dist/render/ansi.d.ts +15 -0
- package/dist/render/ansi.js +142 -0
- package/dist/render/arch.js +8 -3
- package/dist/render/arch3d.d.ts +31 -0
- package/dist/render/arch3d.js +294 -0
- package/dist/render/archmap.js +2 -2
- package/dist/render/area.js +42 -11
- package/dist/render/bar-grid.js +1 -1
- package/dist/render/bar-grouped.js +25 -7
- package/dist/render/bar-stacked.js +22 -6
- package/dist/render/bar.js +27 -6
- package/dist/render/block.d.ts +56 -0
- package/dist/render/block.js +361 -0
- package/dist/render/butterfly.js +4 -1
- package/dist/render/calendar.js +49 -14
- package/dist/render/center.d.ts +26 -0
- package/dist/render/center.js +108 -0
- package/dist/render/chem.d.ts +22 -0
- package/dist/render/chem.js +205 -0
- package/dist/render/chord.d.ts +17 -0
- package/dist/render/chord.js +141 -0
- package/dist/render/combo.js +7 -7
- package/dist/render/compass.js +59 -13
- package/dist/render/echarts-ssr.d.ts +36 -2
- package/dist/render/echarts-ssr.js +77 -18
- package/dist/render/gauge.js +17 -9
- package/dist/render/gitgraph.d.ts +53 -0
- package/dist/render/gitgraph.js +324 -0
- package/dist/render/globe-areas.d.ts +21 -0
- package/dist/render/globe-areas.js +40 -0
- package/dist/render/globe-earth.d.ts +78 -0
- package/dist/render/globe-earth.js +464 -0
- package/dist/render/globe-keys.d.ts +34 -0
- package/dist/render/globe-keys.js +71 -0
- package/dist/render/globe-proj.d.ts +102 -0
- package/dist/render/globe-proj.js +332 -0
- package/dist/render/globe-regions.d.ts +23 -0
- package/dist/render/globe-regions.js +60 -0
- package/dist/render/globe.d.ts +15 -0
- package/dist/render/globe.js +306 -0
- package/dist/render/globe3d.d.ts +113 -0
- package/dist/render/globe3d.js +285 -0
- package/dist/render/hbar-spec.js +23 -10
- package/dist/render/hbar.js +30 -11
- package/dist/render/jianpu.d.ts +10 -0
- package/dist/render/jianpu.js +190 -0
- package/dist/render/journey.d.ts +54 -0
- package/dist/render/journey.js +277 -0
- package/dist/render/kanban.d.ts +50 -0
- package/dist/render/kanban.js +240 -0
- package/dist/render/kinds.js +19 -0
- package/dist/render/line-multi.js +27 -8
- package/dist/render/line.js +32 -14
- package/dist/render/maps.js +4 -2
- package/dist/render/mermaid/block.d.ts +2 -0
- package/dist/render/mermaid/block.js +256 -0
- package/dist/render/mermaid/c4.d.ts +3 -0
- package/dist/render/mermaid/c4.js +165 -0
- package/dist/render/mermaid/class.d.ts +2 -0
- package/dist/render/mermaid/class.js +185 -0
- package/dist/render/mermaid/common.d.ts +34 -0
- package/dist/render/mermaid/common.js +81 -0
- package/dist/render/mermaid/erd.d.ts +2 -0
- package/dist/render/mermaid/erd.js +103 -0
- package/dist/render/mermaid/gitgraph.d.ts +2 -0
- package/dist/render/mermaid/gitgraph.js +139 -0
- package/dist/render/mermaid/journey.d.ts +7 -0
- package/dist/render/mermaid/journey.js +68 -0
- package/dist/render/mermaid/kanban.d.ts +2 -0
- package/dist/render/mermaid/kanban.js +86 -0
- package/dist/render/mermaid/mindmap.d.ts +7 -0
- package/dist/render/mermaid/mindmap.js +74 -0
- package/dist/render/mermaid/packet.d.ts +2 -0
- package/dist/render/mermaid/packet.js +35 -0
- package/dist/render/mermaid/pie.d.ts +7 -0
- package/dist/render/mermaid/pie.js +58 -0
- package/dist/render/mermaid/quadrant.d.ts +8 -0
- package/dist/render/mermaid/quadrant.js +137 -0
- package/dist/render/mermaid/radar.d.ts +8 -0
- package/dist/render/mermaid/radar.js +69 -0
- package/dist/render/mermaid/requirement.d.ts +2 -0
- package/dist/render/mermaid/requirement.js +100 -0
- package/dist/render/mermaid/sankey.d.ts +7 -0
- package/dist/render/mermaid/sankey.js +54 -0
- package/dist/render/mermaid/seq.d.ts +2 -0
- package/dist/render/mermaid/seq.js +147 -0
- package/dist/render/mermaid/state.d.ts +2 -0
- package/dist/render/mermaid/state.js +168 -0
- package/dist/render/mermaid/timeline.d.ts +7 -0
- package/dist/render/mermaid/timeline.js +63 -0
- package/dist/render/mermaid/xychart.d.ts +7 -0
- package/dist/render/mermaid/xychart.js +94 -0
- package/dist/render/mermaid.d.ts +22 -4
- package/dist/render/mermaid.js +173 -7
- package/dist/render/mindmap.js +12 -2
- package/dist/render/nomnoml.d.ts +8 -0
- package/dist/render/nomnoml.js +47 -0
- package/dist/render/packet.d.ts +39 -0
- package/dist/render/packet.js +232 -0
- package/dist/render/params-dispatch.d.ts +20 -0
- package/dist/render/params-dispatch.js +192 -0
- package/dist/render/pie.js +29 -22
- package/dist/render/quadrant.js +38 -9
- package/dist/render/query.d.ts +11 -1
- package/dist/render/query.js +92 -16
- package/dist/render/radar.js +7 -6
- package/dist/render/requirement.d.ts +61 -0
- package/dist/render/requirement.js +355 -0
- package/dist/render/sankey.js +4 -4
- package/dist/render/scatter.js +25 -9
- package/dist/render/scene3d.d.ts +188 -0
- package/dist/render/scene3d.js +433 -0
- package/dist/render/seq.js +13 -8
- package/dist/render/simplify.d.ts +12 -0
- package/dist/render/simplify.js +105 -0
- package/dist/render/source-theme.d.ts +104 -0
- package/dist/render/source-theme.js +261 -0
- package/dist/render/svg.d.ts +81 -1
- package/dist/render/svg.js +259 -2
- package/dist/render/svgdom.d.ts +74 -0
- package/dist/render/svgdom.js +198 -0
- package/dist/render/timeline.d.ts +54 -0
- package/dist/render/timeline.js +290 -0
- package/dist/render/tty.d.ts +17 -0
- package/dist/render/tty.js +173 -0
- package/dist/render/ttycmd.d.ts +7 -0
- package/dist/render/ttycmd.js +152 -0
- package/dist/render/vendor/au.js +1 -1
- package/dist/render/vendor/br.js +1 -1
- package/dist/render/vendor/china-counties.d.ts +32 -0
- package/dist/render/vendor/china-counties.js +8 -0
- package/dist/render/vendor/china-provinces.d.ts +16 -0
- package/dist/render/vendor/china-provinces.js +8 -0
- package/dist/render/vendor/china.js +1 -1
- package/dist/render/vendor/country-aliases.d.ts +207 -0
- package/dist/render/vendor/country-aliases.js +7 -0
- package/dist/render/vendor/de.js +1 -1
- package/dist/render/vendor/eu.js +1 -1
- package/dist/render/vendor/japan.js +1 -1
- package/dist/render/vendor/kr.js +1 -1
- package/dist/render/vendor/world-geo.js +1 -1
- package/dist/render/vendor/world-ne.d.ts +18 -0
- package/dist/render/vendor/world-ne.js +8 -0
- package/dist/render/vendor/world.js +1 -1
- package/dist/render/vexflow.d.ts +10 -0
- package/dist/render/vexflow.js +189 -0
- package/dist/render/wavedrom.d.ts +14 -0
- package/dist/render/wavedrom.js +113 -0
- package/dist/source.d.ts +20 -0
- package/dist/source.js +27 -0
- package/dist/specs/base.js +9 -4
- package/dist/specs/combo.d.ts +1 -0
- package/dist/specs/combo.js +8 -1
- package/dist/specs/gauge.d.ts +1 -0
- package/dist/specs/gauge.js +14 -2
- package/dist/specs/multi.d.ts +3 -0
- package/dist/specs/multi.js +29 -3
- package/dist/specs/quadrant.js +5 -4
- package/dist/specs/scatter.d.ts +1 -0
- package/dist/specs/scatter.js +7 -0
- package/dist/styles/bar.js +3 -0
- package/dist/styles/baseline.d.ts +47 -0
- package/dist/styles/baseline.js +48 -0
- package/dist/styles/hbar.js +3 -0
- package/dist/styles/index.d.ts +1 -1
- package/dist/styles/index.js +16 -4
- package/dist/styles/line.js +9 -0
- package/dist/styles/pie.js +4 -0
- package/dist/styles/sankey.js +4 -0
- package/dist/styles/skins.d.ts +23 -0
- package/dist/styles/skins.js +349 -0
- package/dist/styles/types.d.ts +29 -1
- package/dist/styles/types.js +30 -6
- package/dist/world-data.d.ts +12 -0
- package/dist/world-data.js +192 -0
- package/package.json +38 -5
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// arch3d —— 结构图的三维版(等轴测 + 能转)。
|
|
2
|
+
//
|
|
3
|
+
// URL 语言和 arch2 一模一样(n= 节点、e= 边、b= 分组),画出来是同一个图,
|
|
4
|
+
// 只是从「平面的架构图」换成「三维的场景」。
|
|
5
|
+
//
|
|
6
|
+
// 摆位与投影在 scene3d.ts(服务端与浏览器端共用),这里只管:
|
|
7
|
+
// 1. 解析 URL → 场景
|
|
8
|
+
// 2. 场景 → SVG(静态图:默认相机,一眼等轴测)
|
|
9
|
+
//
|
|
10
|
+
// 交互(转)由浏览器端做:它拿同一份场景,换相机再投影一遍 ——
|
|
11
|
+
// 公式只有一处,两边算出来的画面才是一张。
|
|
12
|
+
import { parseNodes, parseEdges, parseBoundaries } from './archmap.js';
|
|
13
|
+
import { esc } from './svg.js';
|
|
14
|
+
import { iconOf } from './icons.js';
|
|
15
|
+
import { buildScene, projectScene, DEFAULT_CAMERA } from './scene3d.js';
|
|
16
|
+
/** 语义类型 → 色板序号(跟库那套一致:ext 中性,其余各有各的色) */
|
|
17
|
+
const TYPE_COLOR = {
|
|
18
|
+
fe: 0, be: 1, db: 2, cloud: 3, sec: 4, bus: 5, svc: 6,
|
|
19
|
+
};
|
|
20
|
+
const TIER_NAME_EN = ['SOURCE', 'MID', 'SINK'];
|
|
21
|
+
/** 板砖的尺寸:扁正的方板 */
|
|
22
|
+
const TILE_W = 46; // 边长(正方形)
|
|
23
|
+
const TILE_H = 10; // 厚度(薄板)
|
|
24
|
+
/**
|
|
25
|
+
* 节点在场景里的板砖:**扁正**的一块方板(不是长条)——
|
|
26
|
+
* 图标贴在板上,标签在板下,长条会把图标撑得很远。
|
|
27
|
+
*/
|
|
28
|
+
function tileOf(n) {
|
|
29
|
+
void n;
|
|
30
|
+
return { w: TILE_W, d: TILE_W, h: TILE_H };
|
|
31
|
+
}
|
|
32
|
+
/** 类型色(CSS 变量名) */
|
|
33
|
+
function colorOf(type) {
|
|
34
|
+
const i = TYPE_COLOR[type];
|
|
35
|
+
return i === undefined ? 'var(--d-muted)' : `var(--d-s${i + 1})`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 解析 URL → 场景。
|
|
39
|
+
*/
|
|
40
|
+
/** 分组的辅助线怎么画:plain(默认,只留牌子)/ tint / dash —— 那些线框是多余的,要做再要 */
|
|
41
|
+
export function bstyleOf(url) {
|
|
42
|
+
const v = (url.searchParams.get('bstyle') || '').trim().toLowerCase();
|
|
43
|
+
return v === 'dash' || v === 'tint' ? v : 'plain';
|
|
44
|
+
}
|
|
45
|
+
/** 各层的平面要不要画(默认不要):planes=1 才有 */
|
|
46
|
+
export function planesOf(url) {
|
|
47
|
+
const v = (url.searchParams.get('planes') || '').trim().toLowerCase();
|
|
48
|
+
return v === '1' || v === 'true' || v === 'on';
|
|
49
|
+
}
|
|
50
|
+
/** 辅助线(地板网格 + 分组的线框)要不要画(默认不要):aux=1 才有 */
|
|
51
|
+
export function auxOf(url) {
|
|
52
|
+
const v = (url.searchParams.get('aux') || '').trim().toLowerCase();
|
|
53
|
+
return v === '1' || v === 'true' || v === 'on';
|
|
54
|
+
}
|
|
55
|
+
export function sceneFromUrl(url) {
|
|
56
|
+
const nodes = parseNodes(url.searchParams.getAll('n'));
|
|
57
|
+
const ids = new Set(nodes.map((n) => n.id));
|
|
58
|
+
const edges = parseEdges(url.searchParams.getAll('e'), ids);
|
|
59
|
+
const groups = parseBoundaries(url.searchParams.getAll('b'), ids);
|
|
60
|
+
return sceneOf(nodes, edges, groups);
|
|
61
|
+
}
|
|
62
|
+
/** 节点/边/分组 → 场景 */
|
|
63
|
+
export function sceneOf(nodes, edges, groups) {
|
|
64
|
+
return buildScene({
|
|
65
|
+
nodes: nodes.map((n) => {
|
|
66
|
+
const t = tileOf(n);
|
|
67
|
+
// 图标:库里的图标库按名字取(公司/机构那族在私有仓,取不到就算了)
|
|
68
|
+
const icon = n.icon ? iconOf(n.icon) : undefined;
|
|
69
|
+
return { id: n.id, label: n.label, sub: n.sub, color: colorOf(n.type), icon, ...t };
|
|
70
|
+
}),
|
|
71
|
+
edges: edges.map((e) => ({ from: e.from, to: e.to, label: e.label })),
|
|
72
|
+
groups: groups.map((g) => ({ label: g.label, ids: g.ids })),
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/** 场景 → SVG(默认相机) */
|
|
76
|
+
export function renderArch3d(scene, opts = {}) {
|
|
77
|
+
const cam = { ...DEFAULT_CAMERA, ...(opts.camera ?? {}) };
|
|
78
|
+
const bstyle = opts.bstyle ?? 'plain';
|
|
79
|
+
const planes = opts.planes ?? false;
|
|
80
|
+
const aux = opts.aux ?? false;
|
|
81
|
+
const proj = projectScene(scene, cam);
|
|
82
|
+
const pad = 40;
|
|
83
|
+
const vb = {
|
|
84
|
+
x: proj.bbox.x - pad,
|
|
85
|
+
y: proj.bbox.y - pad,
|
|
86
|
+
w: proj.bbox.w + pad * 2,
|
|
87
|
+
h: proj.bbox.h + pad * 2,
|
|
88
|
+
};
|
|
89
|
+
const width = opts.width ?? Math.round(vb.w);
|
|
90
|
+
const height = Math.round((vb.h / vb.w) * width);
|
|
91
|
+
const body = sceneSvg(proj, scene, opts.interactive ?? false, bstyle, planes, aux);
|
|
92
|
+
return (`<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" ` +
|
|
93
|
+
`viewBox="${r2(vb.x)} ${r2(vb.y)} ${r2(vb.w)} ${r2(vb.h)}" ` +
|
|
94
|
+
`font-family="ui-sans-serif,-apple-system,BlinkMacSystemFont,'Segoe UI','PingFang SC','Hiragino Sans GB','Microsoft YaHei',sans-serif" ` +
|
|
95
|
+
`role="img" aria-label="3D architecture diagram">` +
|
|
96
|
+
style() +
|
|
97
|
+
`<g id="am">${body}</g>` +
|
|
98
|
+
`</svg>`);
|
|
99
|
+
}
|
|
100
|
+
/** 场景里所有节点的提示文案(悬停用) */
|
|
101
|
+
function tipOf(scene, id) {
|
|
102
|
+
const n = scene.nodes.find((x) => x.id === id);
|
|
103
|
+
if (!n)
|
|
104
|
+
return id;
|
|
105
|
+
const tier = TIER_NAME_EN[n.tier];
|
|
106
|
+
return n.sub ? `${n.label} · ${n.sub} · ${tier}` : `${n.label} · ${tier}`;
|
|
107
|
+
}
|
|
108
|
+
/** SVG 的骨架:地面网格 → 板台 → 分组盒 → 边 → 节点 */
|
|
109
|
+
function sceneSvg(p, scene, interactive, bstyle, planes, aux) {
|
|
110
|
+
const parts = [];
|
|
111
|
+
// 地面网格(辅助线之一 —— 默认不画)
|
|
112
|
+
if (aux && p.grid.length) {
|
|
113
|
+
const d = p.grid.map((l) => `M${r2(l[0][0])} ${r2(l[0][1])}L${r2(l[1][0])} ${r2(l[1][1])}`).join('');
|
|
114
|
+
parts.push(`<path class="a3d-grid" d="${d}" fill="none" stroke="var(--d-grid)" stroke-width="1"/>`);
|
|
115
|
+
}
|
|
116
|
+
// 平面(每一层一块地坪):有厚度的板 —— 顶面 + 两个侧面;开关说不要就不要
|
|
117
|
+
for (const t of planes ? p.tiers : []) {
|
|
118
|
+
const pts = t.pts.map((q) => `${r2(q[0])},${r2(q[1])}`).join(' ');
|
|
119
|
+
const nameEn = TIER_NAME_EN[t.tier];
|
|
120
|
+
const sides = t.sides.map((sp) => `<polygon class="a3d-tier-side" points="${sp.map((q) => `${r2(q[0])},${r2(q[1])}`).join(' ')}" fill="var(--d-s${t.tier === 0 ? 4 : t.tier === 1 ? 5 : 6})" fill-opacity="0.10"/>`).join('');
|
|
121
|
+
parts.push(`<g class="a3d-tier" data-tier="${t.tier}">` +
|
|
122
|
+
sides +
|
|
123
|
+
`<polygon class="a3d-tier-top" points="${pts}" fill="var(--d-s${t.tier === 0 ? 4 : t.tier === 1 ? 5 : 6})" fill-opacity="0.07" stroke="var(--d-grid)" stroke-width="1"/>` +
|
|
124
|
+
`<text class="a3d-tier-name" x="${r2(t.lx)}" y="${r2(t.ly)}" font-size="10" fill="var(--d-faint)" text-anchor="middle">${esc(nameEn)}</text>` +
|
|
125
|
+
`</g>`);
|
|
126
|
+
}
|
|
127
|
+
// 分组的辅助线:默认只留个牌子(那些线框是多余的,要做再要)
|
|
128
|
+
for (const g of p.groups) {
|
|
129
|
+
const d = g.lines.map((l) => `M${r2(l[0][0])} ${r2(l[0][1])}L${r2(l[1][0])} ${r2(l[1][1])}`).join('');
|
|
130
|
+
const ia = interactive ? ` data-boundary="${esc(g.ids.join(' '))}"` : '';
|
|
131
|
+
const box = !aux || bstyle === 'plain' ? '' :
|
|
132
|
+
bstyle === 'tint'
|
|
133
|
+
? `<path class="a3d-group-box" d="${d}" fill="none" stroke="var(--d-faint)" stroke-opacity="0.35" stroke-width="1"/>`
|
|
134
|
+
: `<path class="a3d-group-box" d="${d}" fill="none" stroke="var(--d-faint)" stroke-opacity="0.5" stroke-width="1" stroke-dasharray="4 4"/>`;
|
|
135
|
+
parts.push(`<g class="a3d-group"${ia}>` +
|
|
136
|
+
box +
|
|
137
|
+
`<text x="${r2(g.lx)}" y="${r2(g.ly)}" font-size="11" fill="var(--d-muted)" text-anchor="middle">${esc(g.label)}</text>` +
|
|
138
|
+
`</g>`);
|
|
139
|
+
}
|
|
140
|
+
// 边
|
|
141
|
+
for (const e of p.edges) {
|
|
142
|
+
const ia = interactive ? ` data-from="${esc(e.from)}" data-to="${esc(e.to)}"` : '';
|
|
143
|
+
const d = e.pts.map((q, i) => `${i === 0 ? 'M' : 'L'}${r2(q[0])} ${r2(q[1])}`).join('');
|
|
144
|
+
parts.push(`<g class="a3d-edge"${ia}>` +
|
|
145
|
+
// data-from/data-to 也要落在 path 上 —— explore 那套点击高亮找的是 path[data-from]
|
|
146
|
+
`<path class="a3d-edge-path" d="${d}"${ia} fill="none"/>` +
|
|
147
|
+
(e.label ? `<text class="a3d-edge-label" data-edge-label="${esc(e.from)} ${esc(e.to)}" x="${r2(e.lx)}" y="${r2(e.ly)}" font-size="10" fill="var(--d-muted)" text-anchor="middle">${esc(e.label)}</text>` : '') +
|
|
148
|
+
`</g>`);
|
|
149
|
+
}
|
|
150
|
+
// 节点:顶面 + 四个侧面(看不见的那些不透明度 0)
|
|
151
|
+
// 单独一层(a3d-layer)—— 浏览器端转视角时要按远近重排(远的先画)
|
|
152
|
+
const order = [...p.nodes].sort((a, b) => b.depth - a.depth);
|
|
153
|
+
const nodeParts = [];
|
|
154
|
+
for (const n of order) {
|
|
155
|
+
const s = scene.nodes.find((x) => x.id === n.id);
|
|
156
|
+
const ia = interactive ? ` data-node="${esc(n.id)}" data-tip="${esc(tipOf(scene, n.id))}" tabindex="0"` : '';
|
|
157
|
+
const sides = n.sides.map((sp, i) => `<polygon class="a3d-side" data-side="${i}" points="${sp.pts.map((q) => `${r2(q[0])},${r2(q[1])}`).join(' ')}" fill="${s.color}" fill-opacity="${sp.vis ? 0.28 : 0}"/>`).join('');
|
|
158
|
+
// 底面:从下往上看的时候露出来的那一面(没有它,块就是缺一块的)
|
|
159
|
+
const bottom = `<polygon class="a3d-bottom" points="${n.bottom.map((q) => `${r2(q[0])},${r2(q[1])}`).join(' ')}" fill="${s.color}" fill-opacity="${n.topVis ? 0 : 0.22}"/>`;
|
|
160
|
+
nodeParts.push(`<g class="a3d-node"${ia}>` +
|
|
161
|
+
`<polygon class="a3d-shadow" points="${n.shadow.map((q) => `${r2(q[0])},${r2(q[1])}`).join(' ')}" fill="#000" fill-opacity="0.28"/>` +
|
|
162
|
+
`<polygon class="a3d-top" points="${n.top.map((q) => `${r2(q[0])},${r2(q[1])}`).join(' ')}" fill="${s.color}" fill-opacity="${n.topVis ? 0.55 : 0}" stroke="${s.color}" stroke-opacity="0.9" stroke-width="1"/>` +
|
|
163
|
+
bottom + sides +
|
|
164
|
+
(n.icon
|
|
165
|
+
? `<image class="a3d-icon" href="${s.icon}" x="${r2(n.icon.x - 17)}" y="${r2(n.icon.y - 17)}" width="34" height="34" preserveAspectRatio="xMidYMid meet"/>`
|
|
166
|
+
: '') +
|
|
167
|
+
`<text class="a3d-label" x="${r2(n.lx)}" y="${r2(n.ly)}" font-size="12" fill="var(--d-ink)" text-anchor="middle">${esc(s.label)}</text>` +
|
|
168
|
+
`</g>`);
|
|
169
|
+
}
|
|
170
|
+
parts.push(`<g class="a3d-layer">${nodeParts.join('')}</g>`);
|
|
171
|
+
return parts.join('');
|
|
172
|
+
}
|
|
173
|
+
/** 场景的配色(跟站上那套暗色一致) */
|
|
174
|
+
function style() {
|
|
175
|
+
return (`<style>:root{color-scheme:dark;` +
|
|
176
|
+
`--d-s1:#34D399;--d-s2:#F87171;--d-s3:#A78BFA;--d-s4:#22D3EE;--d-s5:#60A5FA;--d-s6:#FB923C;` +
|
|
177
|
+
`--d-s7:#4ADE80;--d-s8:#FB7185;--d-s9:#F59E0B;--d-s10:#0EA5E9;` +
|
|
178
|
+
`--d-ink:#FAFAFA;--d-text:#D4D4D8;--d-muted:#A1A1AA;--d-faint:#71717A;--d-grid:#29292E;--d-card:#0E0F11;--d-link:#60A5FA;` +
|
|
179
|
+
`#am .a3d-edge-path{stroke:#7DD3FC;stroke-opacity:.8;stroke-width:1.5}` +
|
|
180
|
+
`#am g[data-node]{cursor:pointer;transition:filter .12s ease,opacity .15s ease}` +
|
|
181
|
+
`#am g[data-node]:focus{outline:none}` +
|
|
182
|
+
`#am g[data-node]:hover,#am g[data-node]:focus-visible{filter:brightness(1.25)}` +
|
|
183
|
+
`#am g[data-node].on{filter:brightness(1.25)}` +
|
|
184
|
+
`#am.hl g[data-node]{opacity:.4}#am.hl g[data-node].on{opacity:1}` +
|
|
185
|
+
`#am.hl g[data-boundary]{opacity:.35}#am.hl g[data-boundary].on{opacity:1}` +
|
|
186
|
+
`#am.hl path[data-from]{opacity:.22}#am.hl path[data-from].on{opacity:1}` +
|
|
187
|
+
`#am.hl [data-edge-label]{opacity:.3}#am.hl [data-edge-label].on{opacity:1}` +
|
|
188
|
+
`}</style>`);
|
|
189
|
+
}
|
|
190
|
+
const r2 = (v) => Math.round(v * 100) / 100;
|
|
191
|
+
/* ---------------------------------------------------------------- 二维(平面图)
|
|
192
|
+
|
|
193
|
+
三维那套的「平面版」:每一层摊成一张平面图,并排放 ——
|
|
194
|
+
源层 / 中层 / 汇层,层内的边是直线,层与层之间的边画成弧(跨过面板)。
|
|
195
|
+
|
|
196
|
+
节点在平面上的位置就是它在三维场景里的 (x, y) —— 层内那一套布局,
|
|
197
|
+
跟三维的完全一致(只是没有高度了)。 */
|
|
198
|
+
export function renderArch3dFlat(scene, opts = {}) {
|
|
199
|
+
const r2 = (v) => Math.round(v * 100) / 100;
|
|
200
|
+
const GAP = 60;
|
|
201
|
+
const PANEL_TITLE = ['SOURCE', 'MID', 'SINK'];
|
|
202
|
+
// 每层一块:节点的 (x,y) 范围 + 面板原点
|
|
203
|
+
// 三块面板**共用一条基线**(各自居中)—— 不然面板会互相压
|
|
204
|
+
const panels = [];
|
|
205
|
+
const boxes = [0, 1, 2].map((tier) => {
|
|
206
|
+
const members = scene.nodes.filter((n) => n.tier === tier);
|
|
207
|
+
if (!members.length)
|
|
208
|
+
return null;
|
|
209
|
+
let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
|
|
210
|
+
for (const n of members) {
|
|
211
|
+
minX = Math.min(minX, n.p.x - n.w / 2);
|
|
212
|
+
maxX = Math.max(maxX, n.p.x + n.w / 2);
|
|
213
|
+
minY = Math.min(minY, n.p.y - n.d / 2);
|
|
214
|
+
maxY = Math.max(maxY, n.p.y + n.d / 2);
|
|
215
|
+
}
|
|
216
|
+
return { tier, minX, maxX, minY, maxY };
|
|
217
|
+
});
|
|
218
|
+
const live = boxes.filter(Boolean);
|
|
219
|
+
const baseY = live.length ? Math.min(...live.map((b) => b.minY)) : 0;
|
|
220
|
+
let cursor = 0;
|
|
221
|
+
for (const b of live) {
|
|
222
|
+
const members = scene.nodes.filter((n) => n.tier === b.tier);
|
|
223
|
+
const w = b.maxX - b.minX;
|
|
224
|
+
const h = b.maxY - b.minY;
|
|
225
|
+
panels.push({
|
|
226
|
+
tier: b.tier, x0: cursor, y0: baseY - b.minY, w, h, minX: b.minX,
|
|
227
|
+
ids: new Set(members.map((n) => n.id)),
|
|
228
|
+
});
|
|
229
|
+
cursor += w + GAP;
|
|
230
|
+
}
|
|
231
|
+
if (!panels.length)
|
|
232
|
+
return '';
|
|
233
|
+
const totalW = cursor - GAP;
|
|
234
|
+
const totalH = Math.max(...panels.map((p) => p.h)) + 60;
|
|
235
|
+
const parts = [];
|
|
236
|
+
// 面板(层)的底
|
|
237
|
+
{ // 二维这版就是三张图,面板永远画(不受三维的平面开关管)
|
|
238
|
+
for (const p of panels) {
|
|
239
|
+
parts.push(`<rect class="a3d-panel" x="${r2(p.x0 - 24)}" y="${r2(p.y0 - 44)}" width="${r2(p.w + 48)}" height="${r2(p.h + 68)}" rx="14" fill="var(--d-s${p.tier === 0 ? 4 : p.tier === 1 ? 5 : 6})" fill-opacity="0.05" stroke="var(--d-grid)" stroke-width="1"/>` +
|
|
240
|
+
`<text class="a3d-panel-title" x="${r2(p.x0 - 24)}" y="${r2(p.y0 - 22)}" font-size="11" fill="var(--d-faint)">${esc(PANEL_TITLE[p.tier])}</text>`);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
// 边:层内的直线,层与层之间的弧
|
|
244
|
+
for (const e of scene.edges) {
|
|
245
|
+
const a = scene.nodes.find((n) => n.id === e.from);
|
|
246
|
+
const b = scene.nodes.find((n) => n.id === e.to);
|
|
247
|
+
if (!a || !b)
|
|
248
|
+
continue;
|
|
249
|
+
const pa = panels.find((p) => p.ids.has(a.id));
|
|
250
|
+
const pb = panels.find((p) => p.ids.has(b.id));
|
|
251
|
+
if (!pa || !pb)
|
|
252
|
+
continue;
|
|
253
|
+
const ax = a.p.x - pa.minX + pa.x0;
|
|
254
|
+
const ay = a.p.y + pa.y0;
|
|
255
|
+
const bx = b.p.x - pb.minX + pb.x0;
|
|
256
|
+
const by = b.p.y + pb.y0;
|
|
257
|
+
const ia = opts.interactive ? ` data-from="${esc(e.from)}" data-to="${esc(e.to)}"` : '';
|
|
258
|
+
let d;
|
|
259
|
+
if (pa === pb) {
|
|
260
|
+
d = `M${r2(ax)} ${r2(ay)}L${r2(bx)} ${r2(by)}`;
|
|
261
|
+
}
|
|
262
|
+
else {
|
|
263
|
+
// 跨面板:拱一下,别直着穿过去
|
|
264
|
+
const mx = (ax + bx) / 2;
|
|
265
|
+
const my = Math.min(ay, by) - 40;
|
|
266
|
+
d = `M${r2(ax)} ${r2(ay)}Q${r2(mx)} ${r2(my)} ${r2(bx)} ${r2(by)}`;
|
|
267
|
+
}
|
|
268
|
+
parts.push(`<g class="a3d-edge"${ia}>` +
|
|
269
|
+
`<path class="a3d-edge-path" d="${d}"${ia} fill="none"/>` +
|
|
270
|
+
(e.label ? `<text class="a3d-edge-label" x="${r2((ax + bx) / 2)}" y="${r2((ay + by) / 2 - 6)}" font-size="10" fill="var(--d-muted)" text-anchor="middle">${esc(e.label)}</text>` : '') +
|
|
271
|
+
`</g>`);
|
|
272
|
+
}
|
|
273
|
+
// 节点
|
|
274
|
+
for (const p of panels) {
|
|
275
|
+
for (const n of scene.nodes.filter((x) => p.ids.has(x.id))) {
|
|
276
|
+
const x = n.p.x - p.minX + p.x0;
|
|
277
|
+
const y = n.p.y + p.y0;
|
|
278
|
+
const attr = opts.interactive ? ` data-node="${esc(n.id)}" data-tip="${esc(n.sub ? n.label + ' · ' + n.sub : n.label)}" tabindex="0"` : '';
|
|
279
|
+
parts.push(`<g class="a3d-node"${attr}>` +
|
|
280
|
+
`<rect class="a3d-tile" x="${r2(x - n.w / 2)}" y="${r2(y - n.d / 2)}" width="${r2(n.w)}" height="${r2(n.d)}" rx="8" fill="${n.color}" fill-opacity="0.35" stroke="${n.color}" stroke-opacity="0.9" stroke-width="1"/>` +
|
|
281
|
+
`<text class="a3d-label" x="${r2(x)}" y="${r2(y + 4)}" font-size="11" fill="var(--d-ink)" text-anchor="middle">${esc(n.label)}</text>` +
|
|
282
|
+
`</g>`);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const width = opts.width ?? Math.round(totalW + 80);
|
|
286
|
+
const height = Math.round((totalH / (totalW + 80)) * width);
|
|
287
|
+
return (`<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" ` +
|
|
288
|
+
`viewBox="${r2(-40)} ${r2(baseY - 40)} ${r2(totalW + 80)} ${r2(totalH + 100)}" ` +
|
|
289
|
+
`font-family="ui-sans-serif,-apple-system,BlinkMacSystemFont,'Segoe UI','PingFang SC','Hiragino Sans GB','Microsoft YaHei',sans-serif" ` +
|
|
290
|
+
`role="img" aria-label="3D architecture diagram (flat)">` +
|
|
291
|
+
style() +
|
|
292
|
+
`<g id="am">${parts.join('')}</g>` +
|
|
293
|
+
`</svg>`);
|
|
294
|
+
}
|
package/dist/render/archmap.js
CHANGED
|
@@ -463,7 +463,7 @@ function drawEdgeLabels(routed, avoid, interactive = false) {
|
|
|
463
463
|
const ty = place.horiz ? place.p[1] + (place.mirror ? 14 : -6) : place.p[1] + 3.5;
|
|
464
464
|
const chipX = place.horiz ? tx - w / 2 : place.mirror ? tx - w : tx;
|
|
465
465
|
const chipY = place.horiz ? ty - 12 : ty - 11;
|
|
466
|
-
out.push(`<rect x="${r2(chipX)}" y="${r2(chipY)}" width="${r2(w)}" height="15" rx="4" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1"/>`, `<text${ia} ${attrs} font-size="
|
|
466
|
+
out.push(`<rect x="${r2(chipX)}" y="${r2(chipY)}" width="${r2(w)}" height="15" rx="4" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1"/>`, `<text${ia} ${attrs} font-size="13" fill="var(--d-text)">${esc(text)}</text>`);
|
|
467
467
|
}
|
|
468
468
|
return out.join('');
|
|
469
469
|
}
|
|
@@ -496,7 +496,7 @@ function drawNode(n, interactive = false, degree = 0, style = 'base') {
|
|
|
496
496
|
: '';
|
|
497
497
|
const labelY = n.sub ? boxCy(b) - 3 : boxCy(b) + 4.5;
|
|
498
498
|
const sub = n.sub
|
|
499
|
-
? `<text x="${r2(boxCx(b))}" y="${r2(boxCy(b) + 15)}" text-anchor="middle" font-size="
|
|
499
|
+
? `<text x="${r2(boxCx(b))}" y="${r2(boxCy(b) + 15)}" text-anchor="middle" font-size="13" fill="var(--d-muted)">${esc(n.sub)}</text>`
|
|
500
500
|
: '';
|
|
501
501
|
// interactive(explore 落地页):data-tip 悬停说明 + data-node 供点击高亮连通关系
|
|
502
502
|
const ia = interactive
|
package/dist/render/area.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// 面积图:行为由皮肤规则驱动(规范 面积图 1-3)。
|
|
2
2
|
import { ApiError } from '../specs/index.js';
|
|
3
3
|
import { card, TAB } from '../theme.js';
|
|
4
|
-
import { esc, fmtFull, fmtNum, niceStep, niceTicks,
|
|
4
|
+
import { axisTitleY, editorialTitle, esc, findPeak, fmtFull, fmtNum, LabelPlacer, markPoints, marksLayer, niceStep, niceTicks, pushLineLabelsAllOrNothing, pushPoints, r2, refLabelReserve, refLineLayer, refLinesOf, reserveBands, roundedPath, scaleMeta, smoothPath, snap, subtitleBlock, sVar, svgDoc, titleBlock, topLegend, toSegments, trunc, yAxisGrid } from './svg.js';
|
|
5
5
|
import { xOverhang, yTickInset } from './measure.js';
|
|
6
6
|
import { HANDEE_WOFF2 } from '../styles/handlee.js';
|
|
7
7
|
const SKETCH_FONT = "Handlee,'Comic Sans MS','Chalkboard SE','Comic Neue','Segoe Print',cursive";
|
|
@@ -43,9 +43,9 @@ function areaLayout(spec, o, m = {}) {
|
|
|
43
43
|
headH = t.height + 6 + (spec.subtitle ? 26 : 0);
|
|
44
44
|
}
|
|
45
45
|
else {
|
|
46
|
-
headSvg = titleBlock(spec.title, pad);
|
|
47
|
-
if (spec.title)
|
|
48
|
-
headH = 24;
|
|
46
|
+
headSvg = titleBlock(spec.title, pad) + subtitleBlock(spec.subtitle, pad, !!spec.title);
|
|
47
|
+
if (spec.title || spec.subtitle)
|
|
48
|
+
headH = 24 + (spec.subtitle ? 16 : 0);
|
|
49
49
|
}
|
|
50
50
|
const hasLegendRow = rules.legend.startsWith('top');
|
|
51
51
|
// plain 画布用规范几何,card 变体用内边距布局
|
|
@@ -57,7 +57,8 @@ function areaLayout(spec, o, m = {}) {
|
|
|
57
57
|
headSvg,
|
|
58
58
|
contentTop: geo ? Math.max(geo.top, pad + headH + (hasLegendRow ? 26 : 14)) : pad + headH + (hasLegendRow ? 36 : 20),
|
|
59
59
|
left,
|
|
60
|
-
|
|
60
|
+
// 参考线文案在绘图区右边 —— 先给它腾出位置(否则只能翻到左边跟刻度叠字)
|
|
61
|
+
right: (geo ? geo.right : width - pad) - refLabelReserve(refLinesOf(o)),
|
|
61
62
|
bottom: geo ? geo.bottom : height - pad - 30,
|
|
62
63
|
xLabelY: geo ? geo.xLabelY : height - pad - 30 + (rules.baseline === 'dots' ? 34 : 20),
|
|
63
64
|
legendY: pad + 14, // 图例坐标题基线,与 Edit 角标同排(#27)
|
|
@@ -114,8 +115,20 @@ export function renderArea(spec, o) {
|
|
|
114
115
|
`<stop offset="0" stop-color="var(--d-s1)" stop-opacity="${rules.areaTop}"/>` +
|
|
115
116
|
`<stop offset="1" stop-color="var(--d-s1)" stop-opacity="${rules.areaBottom}"/></linearGradient></defs>`);
|
|
116
117
|
yAxisGrid(parts, ticks, y, left, right, bottom, rules.grid, rules.gridDash, rules.axisInline, rules.baseline, pts.map((p) => p[0]), rules.tickFont);
|
|
117
|
-
if (rules.axisTitle)
|
|
118
|
+
if (rules.axisTitle || spec.unit)
|
|
118
119
|
parts.push(axisTitleY(spec.unit, (contentTop + bottom) / 2, left - (rules.axisInline ? 17 : 26)));
|
|
120
|
+
// 参考线画在面积之下:它是参照,不是数据
|
|
121
|
+
if (refLinesOf(o).length) {
|
|
122
|
+
refLineLayer(parts, {
|
|
123
|
+
refs: refLinesOf(o),
|
|
124
|
+
toPixel: y,
|
|
125
|
+
plot: { start: bottom, end: contentTop },
|
|
126
|
+
span: { from: left, to: right },
|
|
127
|
+
orient: 'h',
|
|
128
|
+
width,
|
|
129
|
+
height,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
119
132
|
parts.push(`<path d="${path}L${r2(pts[n - 1][0])} ${r2(bottom)}L${r2(pts[0][0])} ${r2(bottom)}Z" fill="url(#dg-area)"/>`);
|
|
120
133
|
const dash = rules.lineDash ? ` stroke-dasharray="${rules.lineDash}"` : '';
|
|
121
134
|
parts.push(`<path d="${path}" fill="none" stroke="var(--d-s1)" stroke-width="${rules.lineWidth}" stroke-linecap="round" stroke-linejoin="round"${dash}/>`);
|
|
@@ -124,7 +137,7 @@ export function renderArea(spec, o) {
|
|
|
124
137
|
spec.data.forEach((d, i) => {
|
|
125
138
|
if (i % skip !== 0 && i !== n - 1)
|
|
126
139
|
return;
|
|
127
|
-
parts.push(`<text x="${r2(xs(i))}" y="${r2(xLabelY)}" text-anchor="middle" font-size="${rules.tickFont}" fill="var(--d-muted)">${esc(trunc(d.label, 12))}</text>`);
|
|
140
|
+
parts.push(`<text x="${r2(xs(i))}" y="${r2(xLabelY)}" text-anchor="middle" font-size="${rules.tickFont}" fill="var(--d-muted)">${d.label.length > 12 ? `<title>${esc(d.label)}</title>` : ''}${esc(trunc(d.label, 12))}</text>`);
|
|
128
141
|
});
|
|
129
142
|
// 数值标签:全有或全无;规范变体默认不标
|
|
130
143
|
const num = o.valueLabels ?? rules.num;
|
|
@@ -157,7 +170,9 @@ export function renderArea(spec, o) {
|
|
|
157
170
|
const mpts = markPoints(o.marks, spec.data.map((d) => d.label), xs, y, [contentTop, bottom]);
|
|
158
171
|
if (mpts.length > 0)
|
|
159
172
|
parts.push(marksLayer(mpts, width));
|
|
160
|
-
|
|
173
|
+
// 交互模式:像素↔值的映射(面积图的纵轴不一定从 0 起 —— 用真实的 yMin)
|
|
174
|
+
const dscale = o.interactive ? scaleMeta(yMin, bottom, yMax, contentTop, values) : '';
|
|
175
|
+
return svgDoc(width, height, 'area', version, spec.title ?? 'area chart', spec, skin, bg + headSvg + dscale + parts.join(''));
|
|
161
176
|
}
|
|
162
177
|
// 多系列面积(area-1):三遍绘制(面积 → 虚线描边 → 点)
|
|
163
178
|
export function renderAreaMulti(spec, o) {
|
|
@@ -186,8 +201,20 @@ export function renderAreaMulti(spec, o) {
|
|
|
186
201
|
parts.push(topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i] })), right, legendY, 'right', rules.legendShape));
|
|
187
202
|
}
|
|
188
203
|
yAxisGrid(parts, ticks, y, left, right, bottom, rules.grid, rules.gridDash, rules.axisInline, rules.baseline, spec.labels.map((_, i) => xs(i)), rules.tickFont);
|
|
189
|
-
if (rules.axisTitle)
|
|
204
|
+
if (rules.axisTitle || spec.unit)
|
|
190
205
|
parts.push(axisTitleY(spec.unit, (contentTop + bottom) / 2, left - (rules.axisInline ? 17 : 26)));
|
|
206
|
+
// 参考线画在面积之下:它是参照,不是数据
|
|
207
|
+
if (refLinesOf(o).length) {
|
|
208
|
+
refLineLayer(parts, {
|
|
209
|
+
refs: refLinesOf(o),
|
|
210
|
+
toPixel: y,
|
|
211
|
+
plot: { start: bottom, end: contentTop },
|
|
212
|
+
span: { from: left, to: right },
|
|
213
|
+
orient: 'h',
|
|
214
|
+
width,
|
|
215
|
+
height,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
191
218
|
allPts.forEach((pts, i) => {
|
|
192
219
|
parts.push(`<path d="${pathOf(pts)}L${r2(pts[n - 1][0])} ${r2(bottom)}L${r2(pts[0][0])} ${r2(bottom)}Z" fill="url(#dg-area-${i})"/>`);
|
|
193
220
|
});
|
|
@@ -200,7 +227,7 @@ export function renderAreaMulti(spec, o) {
|
|
|
200
227
|
spec.labels.forEach((l, i) => {
|
|
201
228
|
if (i % skip !== 0 && i !== n - 1)
|
|
202
229
|
return;
|
|
203
|
-
parts.push(`<text x="${r2(xs(i))}" y="${r2(xLabelY)}" text-anchor="middle" font-size="${rules.tickFont}" fill="var(--d-muted)">${esc(trunc(l, 12))}</text>`);
|
|
230
|
+
parts.push(`<text x="${r2(xs(i))}" y="${r2(xLabelY)}" text-anchor="middle" font-size="${rules.tickFont}" fill="var(--d-muted)">${l.length > 12 ? `<title>${esc(l)}</title>` : ''}${esc(trunc(l, 12))}</text>`);
|
|
204
231
|
});
|
|
205
232
|
const num = o.valueLabels ?? rules.num;
|
|
206
233
|
if (num !== 'none') {
|
|
@@ -223,7 +250,11 @@ export function renderAreaMulti(spec, o) {
|
|
|
223
250
|
const mpts = markPoints(o.marks, spec.labels, xs, y, [contentTop, bottom]);
|
|
224
251
|
if (mpts.length > 0)
|
|
225
252
|
parts.push(marksLayer(mpts, width));
|
|
226
|
-
|
|
253
|
+
// 交互模式:像素↔值的映射 + 全部系列的数据值(编辑器吸附用)
|
|
254
|
+
const dscale = o.interactive
|
|
255
|
+
? scaleMeta(0, bottom, yMax, contentTop, spec.series.flatMap((s) => s.values))
|
|
256
|
+
: '';
|
|
257
|
+
return svgDoc(width, height, 'area', version, spec.title ?? 'area chart', spec, skin, bg + headSvg + dscale + parts.join(''));
|
|
227
258
|
}
|
|
228
259
|
// 手绘风面积图(area-2):xkcd 滤镜 + Handlee 手写字体 + 端点标注
|
|
229
260
|
function renderSketchArea(spec, o) {
|
package/dist/render/bar-grid.js
CHANGED
|
@@ -63,7 +63,7 @@ export function renderBarGrid(spec, o) {
|
|
|
63
63
|
spec.data.forEach((d, i) => {
|
|
64
64
|
if (i % tickSkip !== 0)
|
|
65
65
|
return;
|
|
66
|
-
parts.push(`<text x="${r2(startX + i * pitch + cellW / 2)}" y="${r2(Math.round(height * 0.9))}" text-anchor="middle" font-family="${MONO}" font-size="
|
|
66
|
+
parts.push(`<text x="${r2(startX + i * pitch + cellW / 2)}" y="${r2(Math.round(height * 0.9))}" text-anchor="middle" font-family="${MONO}" font-size="13" fill="var(--d-muted)" style="font-variant-numeric:tabular-nums">${esc(trunc(d.label, 12))}</text>`);
|
|
67
67
|
});
|
|
68
68
|
return svgDoc(width, height, 'bar', version, spec.title ?? 'grid bar', spec, skin, parts.join(''));
|
|
69
69
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { card } from '../theme.js';
|
|
2
|
-
import { esc, fmtFull, fmtNum, niceTicks,
|
|
2
|
+
import { colorRef, esc, findPeak, fmtFull, fmtNum, grayVar, LabelPlacer, labelStride, markPoints, marksLayer, niceTicks, onVar, pushBarLabelsAllOrNothing, r2, refLabelReserve, refLineLayer, refLinesOf, reserveBands, scaleMeta, subtitleBlock, sVar, svgDoc, textW, titleBlock, topLegend, trunc, yAxisGrid } from './svg.js';
|
|
3
3
|
import { yTickInset } from './measure.js';
|
|
4
4
|
// 规范 bar-1 实测几何(960×520):left 76、右留 50、顶 94(0.181h)、底 448(0.862h)、X 标签 480
|
|
5
5
|
// 留白 = max(规范比例, 实测刻度列):长刻度/窄画布时左缘撑大,刻度不裁字
|
|
@@ -20,7 +20,8 @@ export function renderBarMulti(spec, o) {
|
|
|
20
20
|
const { version, skin } = o;
|
|
21
21
|
const rules = skin.rules;
|
|
22
22
|
const k = spec.series.length;
|
|
23
|
-
|
|
23
|
+
// 每列取色:highlight 未点名 → 家族灰;显式色(槽位/hex)→ colorRef;缺省 → 位置色
|
|
24
|
+
const colors = spec.series.map((s, i) => o.highlight && !o.highlight.has(s.name) ? grayVar(i) : colorRef(s.color, sVar(i % skin.series.length)));
|
|
24
25
|
const n = spec.labels.length;
|
|
25
26
|
const gMax = Math.max(...spec.series.flatMap((s) => s.values));
|
|
26
27
|
const [peakS, peakP] = findPeak(spec.series);
|
|
@@ -63,10 +64,11 @@ export function renderBarMulti(spec, o) {
|
|
|
63
64
|
}
|
|
64
65
|
const geo = plain ? barGeo(width, height, !!spec.title, { ticks }) : null;
|
|
65
66
|
const contentTop = geo
|
|
66
|
-
? Math.max(geo.top, pad + (spec.title ? 32 : 0) + (spec.unit ? 18 : 0) + 24)
|
|
67
|
-
: pad + (spec.title ? 32 : 0) + (spec.unit ? 18 : 0) + 24;
|
|
67
|
+
? Math.max(geo.top, pad + (spec.title || spec.subtitle ? 32 : 0) + (spec.unit ? 18 : 0) + 24)
|
|
68
|
+
: pad + (spec.title || spec.subtitle ? 32 : 0) + (spec.unit ? 18 : 0) + 24;
|
|
68
69
|
const left = geo ? geo.left : Math.max(pad + 44, yTickInset({ ticks }));
|
|
69
|
-
|
|
70
|
+
// 参考线文案在绘图区右边 —— 先给它腾出位置(否则只能翻到左边跟刻度叠字)
|
|
71
|
+
const right = (geo ? geo.right : width - pad) - refLabelReserve(refLinesOf(o));
|
|
70
72
|
const bottom = geo ? geo.bottom : height - pad - 30;
|
|
71
73
|
const xLabelY = geo ? geo.xLabelY : bottom + 20;
|
|
72
74
|
const legendY = pad + 14; // 图例坐标题基线,与 Edit 角标同排(#27)
|
|
@@ -85,6 +87,18 @@ export function renderBarMulti(spec, o) {
|
|
|
85
87
|
parts.push(topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i], ...(o.interactive ? { index: i } : {}) })), plain ? right : width - pad, legendY, 'right', rules.legendShape));
|
|
86
88
|
}
|
|
87
89
|
yAxisGrid(parts, ticks, y, left, right, bottom, rules.grid, rules.gridDash, false, rules.baseline, [], rules.tickFont, geo ? 18 : 8);
|
|
90
|
+
// 参考线画在柱之下:它是参照,不是数据
|
|
91
|
+
if (refLinesOf(o).length) {
|
|
92
|
+
refLineLayer(parts, {
|
|
93
|
+
refs: refLinesOf(o),
|
|
94
|
+
toPixel: y,
|
|
95
|
+
plot: { start: bottom, end: contentTop },
|
|
96
|
+
span: { from: left, to: right },
|
|
97
|
+
orient: 'h',
|
|
98
|
+
width,
|
|
99
|
+
height,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
88
102
|
const step = (right - left) / n;
|
|
89
103
|
// 规范 bar-1:柱宽 22、系列间距 5,四角圆角 rx4;通用形态按槽宽自适应
|
|
90
104
|
const gap = rules.barCorner > 0 ? 5 : 0;
|
|
@@ -129,10 +143,14 @@ export function renderBarMulti(spec, o) {
|
|
|
129
143
|
spec.labels.forEach((l, pi) => {
|
|
130
144
|
const cx = left + step * pi + step / 2;
|
|
131
145
|
if (pi % xStride === 0)
|
|
132
|
-
parts.push(`<text x="${r2(cx)}" y="${r2(xLabelY)}" text-anchor="middle" font-size="
|
|
146
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(xLabelY)}" text-anchor="middle" font-size="13" fill="var(--d-muted)">${esc(trunc(l, 12))}</text>`);
|
|
133
147
|
});
|
|
134
148
|
const mpts = markPoints(o.marks, spec.labels, (i) => left + step * i + step / 2, y, [contentTop, bottom]);
|
|
135
149
|
if (mpts.length > 0)
|
|
136
150
|
parts.push(marksLayer(mpts, width));
|
|
137
|
-
|
|
151
|
+
// 交互模式:像素↔值的映射 + 全部系列的数据值(编辑器吸附用)
|
|
152
|
+
const dscale = o.interactive
|
|
153
|
+
? scaleMeta(0, bottom, yMax, contentTop, spec.series.flatMap((s) => s.values))
|
|
154
|
+
: '';
|
|
155
|
+
return svgDoc(width, height, 'bar', version, spec.title ?? 'bar chart', spec, skin, bg + titleBlock(spec.title) + subtitleBlock(spec.subtitle) + dscale + parts.join(''));
|
|
138
156
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { TAB, card } from '../theme.js';
|
|
2
|
-
import { esc, fmtFull, fmtNum, niceTicks,
|
|
2
|
+
import { esc, findPeak, fmtFull, fmtNum, LabelPlacer, labelStride, markPoints, marksLayer, niceTicks, onVar, pushBarLabelsAllOrNothing, r2, refLabelReserve, refLineLayer, refLinesOf, reserveBands, scaleMeta, subtitleBlock, sVar, svgDoc, textW, titleBlock, topLegend, trunc, yAxisGrid } from './svg.js';
|
|
3
3
|
import { yTickInset, tickColW } from './measure.js';
|
|
4
4
|
/**
|
|
5
5
|
* Stacked multi-series bars (auto-switches to 100% stacking on extreme magnitudes).
|
|
@@ -16,7 +16,7 @@ export function renderBarStacked(spec, o) {
|
|
|
16
16
|
// 全局最大段(num=peak 时只标它)
|
|
17
17
|
const [peakS, peakP] = findPeak(spec.series);
|
|
18
18
|
const pad = 24;
|
|
19
|
-
const fixedTop = pad + (spec.title ? 32 : 0) + (spec.unit ? 18 : 0) + 24;
|
|
19
|
+
const fixedTop = pad + (spec.title || spec.subtitle ? 32 : 0) + (spec.unit ? 18 : 0) + 24;
|
|
20
20
|
// 刻度上限:5 档刻度更贴近数据(41842 → 50000,而非 4 档的 60000),减少顶部浪费
|
|
21
21
|
const ticks0 = niceTicks(gMax, 5);
|
|
22
22
|
const yMax0 = ticks0[ticks0.length - 1];
|
|
@@ -46,7 +46,8 @@ export function renderBarStacked(spec, o) {
|
|
|
46
46
|
if (need < width)
|
|
47
47
|
width = Math.round(Math.max(320, need));
|
|
48
48
|
}
|
|
49
|
-
|
|
49
|
+
// 参考线文案在绘图区右边 —— 先给它腾出位置(否则只能翻到左边跟刻度叠字)
|
|
50
|
+
const right = width - pad - refLabelReserve(refLinesOf(o));
|
|
50
51
|
const bottom = height - pad - 30;
|
|
51
52
|
const yMax = pctMode ? 100 : tickVals[tickVals.length - 1];
|
|
52
53
|
const scaleOf = (pi, v) => (pctMode ? (v / totals[pi]) * 100 : v);
|
|
@@ -87,13 +88,26 @@ export function renderBarStacked(spec, o) {
|
|
|
87
88
|
const gy = y(t);
|
|
88
89
|
if (t > 0)
|
|
89
90
|
parts.push(`<line x1="${left}" y1="${r2(gy)}" x2="${right}" y2="${r2(gy)}" stroke="var(--d-grid)" stroke-width="1"/>`);
|
|
90
|
-
parts.push(`<text x="${left - 8}" y="${r2(gy + 4)}" text-anchor="end" font-size="
|
|
91
|
+
parts.push(`<text x="${left - 8}" y="${r2(gy + 4)}" text-anchor="end" font-size="13" fill="var(--d-muted)" style="${TAB}">${t}%</text>`);
|
|
91
92
|
}
|
|
92
93
|
parts.push(`<line x1="${left}" y1="${r2(bottom)}" x2="${right}" y2="${r2(bottom)}" stroke="var(--d-baseline)" stroke-width="1"/>`);
|
|
93
94
|
}
|
|
94
95
|
else {
|
|
95
96
|
yAxisGrid(parts, tickVals, y, left, right, bottom, skin.rules.grid, skin.rules.gridDash);
|
|
96
97
|
}
|
|
98
|
+
// 参考线画在柱之下:它是参照,不是数据。
|
|
99
|
+
// 百分比模式下纵轴是 0..100,参考线的值也按百分比读 —— 与坐标轴同一把尺子。
|
|
100
|
+
if (refLinesOf(o).length) {
|
|
101
|
+
refLineLayer(parts, {
|
|
102
|
+
refs: refLinesOf(o),
|
|
103
|
+
toPixel: y,
|
|
104
|
+
plot: { start: bottom, end: contentTop },
|
|
105
|
+
span: { from: left, to: right },
|
|
106
|
+
orient: 'h',
|
|
107
|
+
width,
|
|
108
|
+
height,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
97
111
|
const step = (right - left) / n;
|
|
98
112
|
const barW = Math.min(56, Math.max(6, step * 0.55));
|
|
99
113
|
const num = o.valueLabels ?? skin.rules.num;
|
|
@@ -134,7 +148,7 @@ export function renderBarStacked(spec, o) {
|
|
|
134
148
|
cursor = yTop;
|
|
135
149
|
});
|
|
136
150
|
if (pi % xStride === 0)
|
|
137
|
-
parts.push(`<text x="${r2(x + barW / 2)}" y="${r2(bottom + 20)}" text-anchor="middle" font-size="
|
|
151
|
+
parts.push(`<text x="${r2(x + barW / 2)}" y="${r2(bottom + 20)}" text-anchor="middle" font-size="13" fill="var(--d-muted)">${esc(trunc(label, 12))}</text>`);
|
|
138
152
|
// 百分比模式:柱顶标该组总量(补回绝对量)
|
|
139
153
|
if (pctMode) {
|
|
140
154
|
parts.push(`<text x="${r2(x + barW / 2)}" y="${r2(contentTop - 8)}" text-anchor="middle" font-size="11" font-weight="500" fill="var(--d-ink)" style="${TAB}">${fmtNum(totals[pi])}</text>`);
|
|
@@ -146,5 +160,7 @@ export function renderBarStacked(spec, o) {
|
|
|
146
160
|
const mpts = markPoints(o.marks, spec.labels, (i) => left + step * i + step / 2, y, [contentTop, bottom]);
|
|
147
161
|
if (mpts.length > 0)
|
|
148
162
|
parts.push(marksLayer(mpts, width));
|
|
149
|
-
|
|
163
|
+
// 交互模式:像素↔值的映射(堆叠柱的纵轴读的是各组总量,那才是用户看到的数)
|
|
164
|
+
const dscale = o.interactive ? scaleMeta(0, bottom, yMax, contentTop, totals) : '';
|
|
165
|
+
return svgDoc(width, height, 'bar', version, spec.title ?? 'stacked bar chart', spec, skin, bg + titleBlock(spec.title) + subtitleBlock(spec.subtitle) + dscale + parts.join(''));
|
|
150
166
|
}
|