@x-cmd/draw 0.3.0 → 0.4.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.
Files changed (115) hide show
  1. package/README.md +79 -297
  2. package/dist/lib.d.ts +23 -3
  3. package/dist/lib.js +28 -4
  4. package/dist/node/cli.js +1 -1
  5. package/dist/render/anno.d.ts +50 -0
  6. package/dist/render/anno.js +589 -0
  7. package/dist/render/arch.d.ts +106 -0
  8. package/dist/render/arch.js +1011 -0
  9. package/dist/render/archmap.d.ts +90 -3
  10. package/dist/render/archmap.js +174 -102
  11. package/dist/render/area.js +9 -7
  12. package/dist/render/bar-grouped.js +3 -2
  13. package/dist/render/bar-stacked.js +3 -2
  14. package/dist/render/bar.js +3 -2
  15. package/dist/render/butterfly.d.ts +3 -0
  16. package/dist/render/butterfly.js +355 -0
  17. package/dist/render/calendar.d.ts +6 -0
  18. package/dist/render/calendar.js +448 -0
  19. package/dist/render/combo.js +3 -2
  20. package/dist/render/compass.d.ts +6 -0
  21. package/dist/render/compass.js +77 -0
  22. package/dist/render/dispatch.js +26 -1
  23. package/dist/render/echarts-ssr.d.ts +30 -0
  24. package/dist/render/echarts-ssr.js +337 -16
  25. package/dist/render/gantt.d.ts +11 -0
  26. package/dist/render/gantt.js +12 -19
  27. package/dist/render/gantt2.d.ts +24 -0
  28. package/dist/render/gantt2.js +198 -0
  29. package/dist/render/hbar.js +8 -4
  30. package/dist/render/icons.d.ts +3 -0
  31. package/dist/render/icons.js +78 -0
  32. package/dist/render/kinds.d.ts +11 -0
  33. package/dist/render/kinds.js +176 -0
  34. package/dist/render/ladder.d.ts +21 -0
  35. package/dist/render/ladder.js +230 -0
  36. package/dist/render/layout.d.ts +46 -0
  37. package/dist/render/layout.js +57 -0
  38. package/dist/render/line-multi.js +2 -2
  39. package/dist/render/line.js +2 -2
  40. package/dist/render/maps.d.ts +34 -0
  41. package/dist/render/maps.js +197 -2
  42. package/dist/render/mermaid.js +25 -6
  43. package/dist/render/mindmap.js +3 -2
  44. package/dist/render/orgicon.d.ts +12 -0
  45. package/dist/render/orgicon.js +31 -0
  46. package/dist/render/png.js +3 -1
  47. package/dist/render/quadrant.d.ts +6 -0
  48. package/dist/render/quadrant.js +201 -0
  49. package/dist/render/query.d.ts +2 -0
  50. package/dist/render/query.js +100 -1
  51. package/dist/render/route.d.ts +86 -0
  52. package/dist/render/route.js +1077 -0
  53. package/dist/render/scatter.js +73 -7
  54. package/dist/render/seq.d.ts +41 -0
  55. package/dist/render/seq.js +248 -0
  56. package/dist/render/svg.d.ts +16 -2
  57. package/dist/render/svg.js +85 -7
  58. package/dist/render/table.d.ts +30 -0
  59. package/dist/render/table.js +134 -0
  60. package/dist/render/tokei.d.ts +18 -0
  61. package/dist/render/tokei.js +230 -0
  62. package/dist/render/uc.d.ts +58 -0
  63. package/dist/render/uc.js +319 -0
  64. package/dist/render/units.d.ts +32 -0
  65. package/dist/render/units.js +84 -0
  66. package/dist/render/vendor/au.js +10 -0
  67. package/dist/render/vendor/br.js +10 -0
  68. package/dist/render/vendor/china.js +11 -0
  69. package/dist/render/vendor/de.js +10 -0
  70. package/dist/render/vendor/eu.js +10 -0
  71. package/dist/render/vendor/japan.js +10 -0
  72. package/dist/render/vendor/kr.js +10 -0
  73. package/dist/render/vendor/us-states.js +4 -6
  74. package/dist/render/vendor/world-geo.d.ts +22 -0
  75. package/dist/render/vendor/world-geo.js +9 -0
  76. package/dist/render/vendor/world.d.ts +13 -0
  77. package/dist/render/vendor/world.js +12 -0
  78. package/dist/specs/base.d.ts +5 -0
  79. package/dist/specs/base.js +14 -1
  80. package/dist/specs/butterfly.d.ts +46 -0
  81. package/dist/specs/butterfly.js +241 -0
  82. package/dist/specs/calendar.d.ts +90 -0
  83. package/dist/specs/calendar.js +296 -0
  84. package/dist/specs/compass.d.ts +27 -0
  85. package/dist/specs/compass.js +111 -0
  86. package/dist/specs/index.d.ts +5 -0
  87. package/dist/specs/index.js +5 -0
  88. package/dist/specs/ladder.d.ts +43 -0
  89. package/dist/specs/ladder.js +261 -0
  90. package/dist/specs/quadrant.d.ts +41 -0
  91. package/dist/specs/quadrant.js +200 -0
  92. package/dist/specs/scatter.d.ts +14 -8
  93. package/dist/specs/scatter.js +54 -5
  94. package/dist/specs/table.d.ts +24 -0
  95. package/dist/specs/table.js +154 -0
  96. package/dist/store.d.ts +1 -1
  97. package/dist/store.js +1 -1
  98. package/dist/styles/butterfly.d.ts +2 -0
  99. package/dist/styles/butterfly.js +74 -0
  100. package/dist/styles/calendar.d.ts +2 -0
  101. package/dist/styles/calendar.js +51 -0
  102. package/dist/styles/compass.d.ts +2 -0
  103. package/dist/styles/compass.js +19 -0
  104. package/dist/styles/index.js +13 -0
  105. package/dist/styles/ladder.d.ts +3 -0
  106. package/dist/styles/ladder.js +66 -0
  107. package/dist/styles/quadrant.d.ts +2 -0
  108. package/dist/styles/quadrant.js +19 -0
  109. package/dist/styles/table.d.ts +2 -0
  110. package/dist/styles/table.js +12 -0
  111. package/dist/styles/types.d.ts +1 -0
  112. package/dist/styles/types.js +1 -0
  113. package/dist/types/dagre.d.ts +39 -0
  114. package/dist/types/dagre.js +3 -0
  115. package/package.json +17 -1
@@ -5,12 +5,95 @@
5
5
  //
6
6
  // 名字即 option 里的地图名:series.type=map 的 map 字段、geo 组件的 map 字段都填它。
7
7
  // {"type":"map","map":"USA","data":[{"name":"California","value":1}]}
8
+ // WORLD 是世界地图(自然地球投影,等积)—— 投影好的底图随包分发(投影是构建期的事)。
8
9
  // 未收录的地图名不注册,echarts 自己报 "Map xxx not exists"(错误消息更贴切)。
9
10
  const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
10
11
  /** 地图名 → GeoJSON 载入器(动态 import:未用到就不下载) */
11
12
  const MAPS = {
12
13
  USA: () => import('./vendor/us-states.js'),
14
+ CN: () => import('./vendor/china.js'),
15
+ EU: () => import('./vendor/eu.js'),
16
+ JP: () => import('./vendor/japan.js'),
17
+ BR: () => import('./vendor/br.js'),
18
+ AU: () => import('./vendor/au.js'),
19
+ DE: () => import('./vendor/de.js'),
20
+ KR: () => import('./vendor/kr.js'),
21
+ WORLD: () => import('./vendor/world.js'),
13
22
  };
23
+ /**
24
+ * 投影产物是**屏幕坐标(y 向下)**,echarts 却按「北为正」读地图坐标
25
+ * (实测:数据里 y=100 的点会画在屏幕上方)。两边差一个翻转 —— 在这里翻一次,
26
+ * 数据文件保持投影原样(不用在每份数据里写死负号),注册与浏览器端拿到的是同一份。
27
+ */
28
+ function flipY(geo) {
29
+ const walk = (v) => {
30
+ if (!Array.isArray(v))
31
+ return v;
32
+ if (typeof v[0] === 'number')
33
+ return [v[0], -v[1]];
34
+ return v.map(walk);
35
+ };
36
+ const g = geo;
37
+ if (!g || !Array.isArray(g.features))
38
+ return geo;
39
+ return {
40
+ ...g,
41
+ features: g.features.map((f) => ({
42
+ ...f,
43
+ geometry: { ...f.geometry, coordinates: walk(f.geometry?.coordinates) },
44
+ })),
45
+ };
46
+ }
47
+ /** 地图 GeoJSON(按地图名取,已翻成 echarts 认的方向):浏览器端注册地图要用同一份 */
48
+ export async function loadMapGeoJson(name) {
49
+ const load = MAPS[name];
50
+ if (!load)
51
+ throw new Error(`unknown map: ${name}`);
52
+ const mod = await load();
53
+ return flipY(mod.usStatesGeoJson ?? mod.chinaGeoJson ?? mod.euGeoJson ?? mod.japanGeoJson
54
+ ?? mod.brGeoJson ?? mod.auGeoJson ?? mod.deGeoJson ?? mod.krGeoJson ?? mod.worldGeoJson);
55
+ }
56
+ /**
57
+ * US 各州的 GeoJSON(Albers USA 投影,50 州 + DC)—— 随包分发,随取随用。
58
+ * 浏览器端的 echarts 也要注册同一份地图数据,自己那一份没有地图 —— 用这个。
59
+ * 按需载入(静态 import 会把 68KB 拖进冷启动路径)。
60
+ */
61
+ export async function loadUsStatesGeoJson() {
62
+ const mod = await import('./vendor/us-states.js');
63
+ return mod.usStatesGeoJson;
64
+ }
65
+ /** 已注册地图的纵横比(投影平面坐标的 bbox 宽高比):布局缺省按它算尺寸 */
66
+ const aspects = new Map();
67
+ /** GeoJSON 平面坐标的 bbox 纵横比(地图的真实形状比) */
68
+ function aspectOf(geo) {
69
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity;
70
+ const walk = (v) => {
71
+ if (!Array.isArray(v))
72
+ return;
73
+ if (typeof v[0] === 'number') {
74
+ const x = v[0], y = v[1];
75
+ if (x < x0)
76
+ x0 = x;
77
+ if (x > x1)
78
+ x1 = x;
79
+ if (y < y0)
80
+ y0 = y;
81
+ if (y > y1)
82
+ y1 = y;
83
+ return;
84
+ }
85
+ v.forEach(walk);
86
+ };
87
+ const feats = geo.features ?? [];
88
+ for (const f of feats)
89
+ walk(f.geometry?.coordinates);
90
+ const h = y1 - y0;
91
+ return h > 0 ? (x1 - x0) / h : 1;
92
+ }
93
+ /** 已注册地图的纵横比(未注册返回 undefined) */
94
+ export function mapAspect(name) {
95
+ return aspects.get(name);
96
+ }
14
97
  /** option 里引用到的地图名:series.type=map 的 map 字段 + geo 组件的 map 字段 */
15
98
  export function mapNamesOf(option) {
16
99
  const names = new Set();
@@ -27,6 +110,112 @@ export function mapNamesOf(option) {
27
110
  }
28
111
  return [...names];
29
112
  }
113
+ /**
114
+ * 世界地图的**经纬度**版本(球面用)—— 动态载入(静态 import 会拖进冷启动路径)。
115
+ * 坐标粗一点(球上本来就小)。
116
+ */
117
+ export async function loadWorldSphericalGeoJson() {
118
+ const mod = await import('./vendor/world-geo.js');
119
+ return mod.worldSphericalGeoJson;
120
+ }
121
+ // ── 世界地图「绕一圈」────────────────────────────────────────────────────────
122
+ //
123
+ // 视图挪到某个经度时(series.center),底图得绕着地球转过去 —— 不然那一侧是空的:
124
+ // 世界地图的数据只覆盖 -180°..180°,挪到东边时,画布右边露出的是画布底色(看着像地图缺一块)。
125
+ //
126
+ // 做法:底图左右各加一份(整体平移一个世界宽)—— 数据还是同一份,只是多几份拷贝,
127
+ // 于是「往东挪」和「往西挪」都有东西接着。布局要跟着放大(份数倍),不然地图被压成三分之一。
128
+ //
129
+ // 三个函数都**自包含**(不引模块外任何东西):浏览器端(explore 页重画)直接跑同一份源码,
130
+ // 两边不会走偏 —— 与 headLayout 同一套路。
131
+ /** 绕圈后的份数(底图 + 左右各一份) */
132
+ export const WORLD_WRAP_COPIES = 3;
133
+ /**
134
+ * option 里有没有「挪到某个经度」的世界地图(要绕圈的场合)。
135
+ * 自包含:浏览器端直接跑同一份源码。
136
+ */
137
+ export function worldWrapNeeded(option) {
138
+ const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
139
+ const o = (option ?? {});
140
+ const series = Array.isArray(o.series) ? o.series : isObj(o.series) ? [o.series] : [];
141
+ for (const s of series) {
142
+ if (!isObj(s))
143
+ continue;
144
+ if (s.type !== 'map' || s.map !== 'WORLD')
145
+ continue;
146
+ if (s.center !== undefined && s.center !== null)
147
+ return true;
148
+ }
149
+ return false;
150
+ }
151
+ /**
152
+ * 底图左右各加一份(整体平移一个世界宽)。
153
+ * 自包含:浏览器端直接跑同一份源码。
154
+ */
155
+ export function worldWrapGeo(geo) {
156
+ const g = geo;
157
+ const feats = g?.features ?? [];
158
+ if (!feats.length)
159
+ return geo;
160
+ // 世界宽 = 底图外接框的宽(世界地图的数据正好覆盖 -180°..180°)
161
+ let x0 = Infinity, x1 = -Infinity;
162
+ const walk = (v) => {
163
+ if (!Array.isArray(v))
164
+ return;
165
+ if (typeof v[0] === 'number') {
166
+ const x = v[0];
167
+ if (x < x0)
168
+ x0 = x;
169
+ if (x > x1)
170
+ x1 = x;
171
+ return;
172
+ }
173
+ v.forEach(walk);
174
+ };
175
+ for (const f of feats)
176
+ walk(f.geometry?.coordinates);
177
+ const w = x1 - x0;
178
+ if (!(w > 0))
179
+ return geo;
180
+ const shifted = (dx) => {
181
+ const sh = (v) => {
182
+ if (!Array.isArray(v))
183
+ return v;
184
+ if (typeof v[0] === 'number')
185
+ return [v[0] + dx, v[1]];
186
+ return v.map(sh);
187
+ };
188
+ return feats.map((f) => ({ ...f, geometry: { ...f.geometry, coordinates: sh(f.geometry?.coordinates) } }));
189
+ };
190
+ return { ...g, features: [...feats, ...shifted(w), ...shifted(-w)] };
191
+ }
192
+ /**
193
+ * 绕圈后布局要按份数放大 —— 数据多了左右两份,地图还是原来那么大,
194
+ * 不然会被压成三分之一(看着像缩小了一圈)。
195
+ * 自包含:浏览器端直接跑同一份源码。
196
+ */
197
+ export function worldWrapLayout(option) {
198
+ const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
199
+ const series = Array.isArray(option.series) ? option.series : isObj(option.series) ? [option.series] : [];
200
+ for (const s of series) {
201
+ if (!isObj(s))
202
+ continue;
203
+ if (s.type !== 'map' || s.map !== 'WORLD')
204
+ continue;
205
+ if (s.center === undefined || s.center === null)
206
+ continue;
207
+ const n = 3; // WORLD_WRAP_COPIES —— 抄一份在这儿,免得这个函数引模块外的东西
208
+ const size = s.layoutSize;
209
+ if (typeof size === 'string' && size.trim().endsWith('%')) {
210
+ const v = parseFloat(size);
211
+ if (Number.isFinite(v))
212
+ s.layoutSize = `${(v * n).toFixed(2)}%`;
213
+ }
214
+ else if (typeof size === 'number' && Number.isFinite(size)) {
215
+ s.layoutSize = size * n;
216
+ }
217
+ }
218
+ }
30
219
  /**
31
220
  * Register the map data an ECharts option refers to. Must run before `setOption`:
32
221
  * echarts resolves `series.map` / `geo.map` against its registered map table.
@@ -36,7 +225,13 @@ export async function registerMaps(echarts, option) {
36
225
  const load = MAPS[name];
37
226
  if (!load)
38
227
  continue;
39
- const mod = await load();
40
- echarts.registerMap(name, mod.usStatesGeoJson);
228
+ const base = await loadMapGeoJson(name);
229
+ // 纵横比按**底图**算(不是绕圈后的):布局按底图定尺,再由 worldWrapLayout 乘份数
230
+ const aspect = aspectOf(base);
231
+ // 世界地图挪到某个经度 ⇒ 底图绕一圈(否则那一侧是空的)
232
+ const geo = name === 'WORLD' && worldWrapNeeded(option) ? worldWrapGeo(base) : base;
233
+ echarts.registerMap(name, geo);
234
+ if (!aspects.has(name))
235
+ aspects.set(name, aspect);
41
236
  }
42
237
  }
@@ -1,8 +1,8 @@
1
- // mermaid 语法糖 → archmap(#20 方案 A′):只吃 flowchart(graph/flowchart)子集。
2
- // 解析 = vendored jison 产物(官方 flow.jison 编译);布局 = dagre(纯 JS);
3
- // 渲染 = renderArchmapCore(三风格/交互/PNG/缓存全继承)。
4
- // 时序图/甘特等其余图型不在 v1 范围,给出明确 400。
5
- import dagre from 'dagre';
1
+ // dagre 无官方类型:类型用本仓自绘的最小面(普通模块,非 ambient 声明 ——
2
+ // ambient 属全局类型改动,JSR 不许发布)
3
+ // @ts-types="../types/dagre.ts"
4
+ import dagreRaw from 'dagre';
5
+ const dagre = dagreRaw;
6
6
  import { ApiError } from '../specs/index.js';
7
7
  import { resolveVersion } from '../version.js';
8
8
  import { resolveSkin } from '../styles/index.js';
@@ -56,6 +56,15 @@ export function parseFlow(text) {
56
56
  length: 1,
57
57
  };
58
58
  };
59
+ // 连线两端可能从没作为独立节点声明过(subgraph 里的写法)——补一个占位节点,
60
+ // 免得布局阶段查不到盒子。
61
+ const ensureVertex = (id) => {
62
+ if (byId.has(id))
63
+ return;
64
+ const n = { id, label: id, shape: 'rect' };
65
+ byId.set(id, n);
66
+ doc.nodes.push(n);
67
+ };
59
68
  const yy = {
60
69
  // mermaid FlowDB 约定:lexer 通过 yy.lex.firstGraph 区分首行方向
61
70
  lex: { firstGraph() { const r = first; first = false; return r; } },
@@ -81,6 +90,10 @@ export function parseFlow(text) {
81
90
  const label = asText(link && link.text);
82
91
  for (const s of sources)
83
92
  for (const t of targets) {
93
+ // subgraph 里写的连线不会先触发 addVertex —— 两端自己补上,
94
+ // 否则布局阶段找不到节点就炸了(500)。
95
+ ensureVertex(s);
96
+ ensureVertex(t);
84
97
  const info = pending[0] ?? { stroke: 'normal' };
85
98
  doc.edges.push({ from: s, to: t, label, dotted: info.stroke === 'dotted', thick: info.stroke === 'thick' });
86
99
  }
@@ -88,7 +101,12 @@ export function parseFlow(text) {
88
101
  },
89
102
  addSubGraph(id, list) {
90
103
  const title = asText(id) || 'group';
91
- const nodes = (list ?? []).map((x) => asText(x)).filter(Boolean);
104
+ // 成员可能是嵌套数组,也可能是「GW CDN」这样一整行文本 —— 摊平后按空白切开,
105
+ // 再只留下确实是节点的 id(否则边界框找不到节点,渲染时 500)。
106
+ const nodes = (Array.isArray(list) ? list : [list])
107
+ .flatMap((x) => asText(x).split(/\s+/))
108
+ .filter(Boolean)
109
+ .filter((nid) => byId.has(nid));
92
110
  if (nodes.length)
93
111
  doc.groups.push({ title, nodes });
94
112
  },
@@ -253,6 +271,7 @@ export function renderMermaidFromUrl(url, interactive = false) {
253
271
  aria: title ?? `mermaid flowchart, ${doc.nodes.length} nodes`,
254
272
  edgeMode: 'straight',
255
273
  straightDir: doc.direction === 'LR' || doc.direction === 'RL' ? 'LR' : 'TB',
274
+ tagQuery: url.search, tagKind: 'mermaid', // 角标直链 explore(#44)
256
275
  };
257
276
  return renderArchmapCore(opts);
258
277
  }
@@ -361,7 +361,8 @@ export function renderMindmapFromUrl(url, interactive = false) {
361
361
  : style === 'bubble'
362
362
  ? drawBubble(root, interactive)
363
363
  : drawOrganic(root, interactive);
364
- const titleH = title ? 40 : 0;
364
+ // 无标题也留 30px 角标带(#44):Edit 胶囊占右上 y≈25–45,不留带会压住顶部节点
365
+ const titleH = title ? 40 : 30;
365
366
  const width = Math.max(W, clampInt(params, 'w', 0, 200, 2400));
366
367
  const height = Math.max(H + titleH, clampInt(params, 'h', 0, 140, 2000));
367
368
  const titleSvg = title
@@ -382,6 +383,6 @@ export function renderMindmapFromUrl(url, interactive = false) {
382
383
  const inner = iStyle + `<g id="mm">` + body + `</g>`;
383
384
  const children = titleH ? titleSvg + `<g transform="translate(0 ${titleH})">` + inner + `</g>` : inner;
384
385
  const spec = { type: 'mindmap', style, ...(title ? { title } : {}), paths };
385
- const svg = svgDoc(width, height, 'mindmap', v.version, title ?? `mindmap: ${root.label}`, spec, skin, children);
386
+ const svg = svgDoc(width, height, 'mindmap', v.version, title ?? `mindmap: ${root.label}`, spec, skin, children, url.search);
386
387
  return { svg, version: v.version, cacheControl: v.cacheControl };
387
388
  }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * 机构剪影的路径(24×24 网格)。
3
+ * 墙体带门廊缺口;四扇窗是 evenodd 的洞。
4
+ */
5
+ export declare const GROUP_ICON_D: string;
6
+ /** 视图框边长(路径按这个网格画)。 */
7
+ export declare const GROUP_ICON_VIEW = 24;
8
+ /**
9
+ * 画组图标:在 (x, y) 处画一个 size×size 的方框。
10
+ * @param fill 填充色(通常是节点的语义色)
11
+ */
12
+ export declare function groupIcon(x: number, y: number, size: number, fill: string): string;
@@ -0,0 +1,31 @@
1
+ // 组图标:「公司/机构」的通用字形 —— 自绘,不对应任何一家公司。
2
+ //
3
+ // 为什么自绘:公开层要画它。`org:` 前缀的图标在内部图库里(私有仓),公开请求查不到,
4
+ // 于是画这个。**公开层的输出里一个第三方商标图形都不能有** —— 所以它是我们自己的美术,
5
+ // 不是任何品牌的 logo,也不进任何"图标素材库"。
6
+ //
7
+ // 画法:24×24 的**平顶办公楼**剪影(墙体 + 四扇窗 + 门廊缺口)。平顶才像"机构/公司",
8
+ // 坡屋顶会读成"房子" —— 在域名后缀的图里恰恰是最不该出现的联想。
9
+ // 单色填充,颜色跟节点的语义色走(var(--d-sN);中性类型则是 var(--d-muted)),
10
+ // 明暗两套皮肤都合适,也跟图里的类型配色一致 —— 它看着像个正经元素,不像"图片挂了"。
11
+ //
12
+ // 授权层(登录后)命中内部图库时,由调用方把节点换成真 logo;组图标只在没有真 logo 时出现。
13
+ import { r2 } from './svg.js';
14
+ /**
15
+ * 机构剪影的路径(24×24 网格)。
16
+ * 墙体带门廊缺口;四扇窗是 evenodd 的洞。
17
+ */
18
+ export const GROUP_ICON_D = 'M5 4h14v18h-5v-5h-4v5H5V4Z' +
19
+ 'M7.5 7h3v2.5h-3ZM13.5 7h3v2.5h-3Z' +
20
+ 'M7.5 11.5h3v2.5h-3ZM13.5 11.5h3v2.5h-3Z';
21
+ /** 视图框边长(路径按这个网格画)。 */
22
+ export const GROUP_ICON_VIEW = 24;
23
+ /**
24
+ * 画组图标:在 (x, y) 处画一个 size×size 的方框。
25
+ * @param fill 填充色(通常是节点的语义色)
26
+ */
27
+ export function groupIcon(x, y, size, fill) {
28
+ const s = size / GROUP_ICON_VIEW;
29
+ return `<g transform="translate(${r2(x)} ${r2(y)}) scale(${r2(s)})">` +
30
+ `<path d="${GROUP_ICON_D}" fill="${fill}" fill-rule="evenodd"/></g>`;
31
+ }
@@ -10,7 +10,9 @@
10
10
  // Workers 由入口(src/index.ts)静态 import 拿 WebAssembly.Module 后注入 —— 两种
11
11
  // 来源 initWasm 通吃。字体喂仓内 base64 TTF 子集(Inter + 手绘 Handlee;fontdb
12
12
  // 不认 woff2,故用 fonts-ttf.ts 的解压版,家族名同源),不读系统字体(wasm 沙盒内
13
- // 本就无系统字体;子集外字符——如 CJK——PNG 中无回退,见 #24 已知边界)。
13
+ // 本就无系统字体)。中文另有 Noto Sans SC 子集(GB2312 一级 3755 字 + 中文标点,
14
+ // 见 fonts-cjk.ts):SVG 里出现 CJK 才挂载 —— 拉丁图表的 PNG 不付那 2.3MB;
15
+ // 子集外的生僻字仍无字形(#24 已知边界)。
14
16
  import { ApiError } from '../specs/index.js';
15
17
  import { INTER_TTF_B64, HANDEE_TTF_B64 } from '../styles/fonts-ttf.js';
16
18
  import { FONT_FILES_B64 } from '../styles/fonts-banner.js';
@@ -0,0 +1,6 @@
1
+ import type { QuadrantSpec } from '../specs/index.js';
2
+ import { type RenderOpts } from './svg.js';
3
+ /**
4
+ * Quadrant chart: a 2×2 matrix (box) or a two-axis cross, with optional points.
5
+ */
6
+ export declare function renderQuadrant(spec: QuadrantSpec, o: RenderOpts): string;
@@ -0,0 +1,201 @@
1
+ import { card } from '../theme.js';
2
+ import { esc, r2, textW, trunc, svgDoc, titleBlock, sVar } from './svg.js';
3
+ const ARROW = 12; // 箭头长度
4
+ const PAD = 18; // 格内边距
5
+ // 字号偏大:看这图的人年长,字要看得清
6
+ const FS_TITLE = 18; // 格内主标签
7
+ const FS_NOTE = 14; // 格内说明
8
+ const FS_LABEL = 13; // 并列小标签
9
+ const FS_AXIS = 15; // 轴名
10
+ const FS_END = 13; // 轴端胶囊
11
+ const FS_POINT = 14; // 散点名字
12
+ const RADIUS = 14; // 面板圆角
13
+ const NO_BREAK = ',。、;:!?,.;:)]}」』】》'; // 这些不能出现在行首
14
+ /**
15
+ * Quadrant chart: a 2×2 matrix (box) or a two-axis cross, with optional points.
16
+ */
17
+ export function renderQuadrant(spec, o) {
18
+ const { width, height, version, skin } = o;
19
+ const parts = [card(width, height, skin.rules.canvas === 'plain')];
20
+ const groups = spec.groups ?? [];
21
+ const hasCells = groups.some((g) => g && (g.name || g.note || g.labels?.length));
22
+ const frame = spec.frame ?? (hasCells ? 'box' : 'cross');
23
+ const boxMode = frame === 'box';
24
+ // 几何:矩阵区填满可用面积(不强制正方);四周给轴名与端标签留位。
25
+ const titleH = spec.title ? 44 : 12;
26
+ const top = titleH + (spec.title ? 26 : 10); // 有标题时面板往下让开(左上角是标题)
27
+ const left = 66;
28
+ const right = spec.xlabel ? 34 + Math.ceil(textW(spec.xlabel, FS_AXIS)) : 34;
29
+ const bottom = 60;
30
+ const mw = Math.max(120, width - left - right);
31
+ const mh = Math.max(120, height - top - bottom);
32
+ const mx = left;
33
+ const myFinal = top;
34
+ const midX = mx + mw / 2;
35
+ const midY = myFinal + mh / 2;
36
+ const px = (x) => mx + x * mw;
37
+ const py = (y) => myFinal + (1 - y) * mh;
38
+ // 轴的几何:矩阵式 = 纵轴沿面板左边、横轴沿中线;十字式 = 两条轴在正中交叉
39
+ const axX = boxMode ? mx : midX;
40
+ const axY0 = myFinal + mh;
41
+ const axY1 = boxMode ? myFinal - 16 : myFinal;
42
+ const axX0 = mx;
43
+ const axX1 = boxMode ? mx + mw + 16 : mx + mw;
44
+ const axMidY = midY;
45
+ // 面板:矩阵式画外框 + 四格淡底;十字式只画一块底(不然图飘在空白里)
46
+ parts.push(`<rect x="${r2(mx)}" y="${r2(myFinal)}" width="${r2(mw)}" height="${r2(mh)}" rx="${RADIUS}" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1"/>`);
47
+ if (boxMode) {
48
+ const cw = mw / 2 - 6;
49
+ const ch = mh / 2 - 6;
50
+ for (const [cx, cy] of [[mx + 3, myFinal + 3], [midX + 3, myFinal + 3], [mx + 3, midY + 3], [midX + 3, midY + 3]]) {
51
+ parts.push(`<rect x="${r2(cx)}" y="${r2(cy)}" width="${r2(cw)}" height="${r2(ch)}" rx="9" fill="var(--d-grid)" fill-opacity="0.6"/>`);
52
+ }
53
+ }
54
+ // 两条轴:正方向带箭头
55
+ const arrow = (x1, y1, x2, y2) => {
56
+ const dx = x2 - x1;
57
+ const dy = y2 - y1;
58
+ const len = Math.hypot(dx, dy) || 1;
59
+ const ux = dx / len;
60
+ const uy = dy / len;
61
+ const bx = x2 - ux * ARROW;
62
+ const by = y2 - uy * ARROW;
63
+ const px2 = -uy;
64
+ const py2 = ux;
65
+ return `<line x1="${r2(x1)}" y1="${r2(y1)}" x2="${r2(bx)}" y2="${r2(by)}" stroke="var(--d-baseline)" stroke-width="1.4"/>` +
66
+ `<path d="M${r2(x2)} ${r2(y2)}L${r2(bx + px2 * 4)} ${r2(by + py2 * 4)}L${r2(bx - px2 * 4)} ${r2(by - py2 * 4)}Z" fill="var(--d-baseline)"/>`;
67
+ };
68
+ parts.push(arrow(axX, axY0, axX, axY1)); // 纵轴向上
69
+ parts.push(arrow(axX0, axMidY, axX1, axMidY)); // 横轴向右
70
+ if (boxMode) {
71
+ parts.push(`<line x1="${r2(midX)}" y1="${r2(myFinal + 3)}" x2="${r2(midX)}" y2="${r2(myFinal + mh - 3)}" stroke="var(--d-border)" stroke-width="1"/>`);
72
+ }
73
+ // 格内标签:四格的**标题同一基线**,说明按格宽折行(中文断行有规矩)
74
+ if (boxMode) {
75
+ const blocks = groups.map((g) => {
76
+ const rows = [];
77
+ if (!g)
78
+ return rows;
79
+ if (g.name)
80
+ rows.push({ text: trunc(g.name, 14), fs: FS_TITLE, fill: 'var(--d-ink)', weight: '600' });
81
+ if (g.note) {
82
+ for (const ln of wrap(g.note, mw / 2 - PAD * 2, FS_NOTE).slice(0, 3)) {
83
+ rows.push({ text: ln, fs: FS_NOTE, fill: 'var(--d-muted)', weight: '400' });
84
+ }
85
+ }
86
+ for (const l of g.labels ?? [])
87
+ rows.push({ text: trunc(l, 12), fs: FS_LABEL, fill: 'var(--d-faint)', weight: '400' });
88
+ return rows;
89
+ });
90
+ const GAP = 6;
91
+ const blockH = (rows) => rows.reduce((a, r) => a + r.fs * 1.35, 0) + GAP * Math.max(0, rows.length - 1);
92
+ // 标题对齐:以四格里最高的块为准,各块的标题落在同一条基线上
93
+ const maxH = Math.max(0, ...blocks.map(blockH));
94
+ const cells = [[mx, myFinal], [midX, myFinal], [mx, midY], [midX, midY]];
95
+ blocks.forEach((rows, i) => {
96
+ if (!rows.length)
97
+ return;
98
+ const [cx, cy] = cells[i];
99
+ const cellH = mh / 2;
100
+ // 标题基线 = 格心 - 最高块的一半 + 标题高
101
+ let y = cy + (cellH - maxH) / 2 + rows[0].fs * 0.95;
102
+ rows.forEach((r, ri) => {
103
+ const x = cx + PAD + (ri === 0 && r.fs >= FS_TITLE ? 15 : 0);
104
+ if (ri === 0 && r.fs >= FS_TITLE) {
105
+ parts.push(`<circle cx="${r2(cx + PAD + 5)}" cy="${r2(y - r.fs * 0.32)}" r="5" fill="${sVar(i % skin.series.length)}"/>`);
106
+ }
107
+ parts.push(`<text x="${r2(x)}" y="${r2(y)}" font-size="${r.fs}" font-weight="${r.weight}"${r.fs >= 16 ? ' letter-spacing="-0.01em"' : ''} fill="${r.fill}">${esc(r.text)}</text>`);
108
+ y += r.fs * 1.35 + GAP;
109
+ });
110
+ });
111
+ }
112
+ // 轴端标签:小胶囊(细描边 + 淡底)
113
+ const pill = (text, x, y, anchor) => {
114
+ const w = textW(text, FS_END) + 16;
115
+ const h = FS_END + 7;
116
+ const l = anchor === 'end' ? x - w : anchor === 'middle' ? x - w / 2 : x;
117
+ parts.push(`<rect x="${r2(l)}" y="${r2(y - h + 4)}" width="${r2(w)}" height="${h}" rx="9" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1"/>`);
118
+ const tx = anchor === 'end' ? x - 7 : anchor === 'middle' ? x : x + 7;
119
+ parts.push(`<text x="${r2(tx)}" y="${r2(y - h + 4 + FS_END + 1.5)}" font-size="${FS_END}" text-anchor="${anchor === 'end' ? 'end' : anchor === 'middle' ? 'middle' : 'start'}" fill="var(--d-muted)">${esc(text)}</text>`);
120
+ };
121
+ const xLow = spec.xlow ?? '低';
122
+ const xHigh = spec.xhigh ?? '高';
123
+ const yLow = spec.ylow ?? '低';
124
+ const yHigh = spec.yhigh ?? '高';
125
+ if (boxMode) {
126
+ pill(yHigh, mx - 12, myFinal + 10, 'end');
127
+ pill(yLow, mx - 12, myFinal + mh - 4, 'end');
128
+ pill(xLow, mx, myFinal + mh + 24, 'start');
129
+ pill(xHigh, mx + mw, myFinal + mh + 24, 'end');
130
+ }
131
+ else {
132
+ pill(yHigh, axX, myFinal - 8, 'middle');
133
+ pill(yLow, axX, myFinal + mh + 8, 'middle');
134
+ pill(xLow, mx - 8, midY, 'end');
135
+ pill(xHigh, mx + mw + 8, midY, 'start');
136
+ }
137
+ // 轴名:贴在各自箭头的外侧,不与端点胶囊同行
138
+ if (spec.xlabel) {
139
+ if (boxMode) {
140
+ parts.push(`<text x="${r2(axX1 + 14)}" y="${r2(axMidY + 5)}" font-size="${FS_AXIS}" fill="var(--d-text)">${esc(spec.xlabel)}</text>`);
141
+ }
142
+ else {
143
+ parts.push(`<text x="${r2(axX1)}" y="${r2(axMidY + 30)}" font-size="${FS_AXIS}" text-anchor="end" fill="var(--d-text)">${esc(spec.xlabel)}</text>`);
144
+ }
145
+ }
146
+ if (spec.ylabel) {
147
+ // 纵轴名竖排贴在纵轴左边(参考图「解决度」的写法):横排会跟左上角的标题打架
148
+ const yx = boxMode ? Math.max(16, mx - 32) : Math.max(16, axX - 34);
149
+ parts.push(`<text x="${r2(yx)}" y="${r2(midY)}" font-size="${FS_AXIS}" text-anchor="middle" fill="var(--d-text)" transform="rotate(-90 ${r2(yx)} ${r2(midY)})">${esc(spec.ylabel)}</text>`);
150
+ }
151
+ // 散点:系列色圆点 + 白圈,名字放在**远离纵轴**的一侧(不横穿轴线)
152
+ (spec.items ?? []).forEach((it, i) => {
153
+ const cx = px(it.x);
154
+ const cy = py(it.y);
155
+ const color = sVar(i % skin.series.length);
156
+ parts.push(`<circle cx="${r2(cx)}" cy="${r2(cy)}" r="6.5" fill="${color}" stroke="var(--d-halo)" stroke-width="2"/>`);
157
+ const label = trunc(it.name, 10);
158
+ const tw = textW(label, FS_POINT);
159
+ const leftSide = cx < axX; // 默认往远离纵轴的一侧放
160
+ const fits = leftSide ? cx - 13 - tw > mx : cx + 13 + tw < mx + mw;
161
+ const useLeft = leftSide ? fits : !fits;
162
+ parts.push(`<text x="${r2(useLeft ? cx - 13 : cx + 13)}" y="${r2(cy + 5)}" font-size="${FS_POINT}" text-anchor="${useLeft ? 'end' : 'start'}" fill="var(--d-text)">${esc(label)}</text>`);
163
+ });
164
+ return svgDoc(width, height, 'quadrant', version, spec.title ?? 'quadrant chart', spec, skin, titleBlock(spec.title) + parts.join(''));
165
+ }
166
+ /**
167
+ * 中文断行:按字宽折行;标点不落行首;末行不留孤字。
168
+ */
169
+ function wrap(text, maxW, fs) {
170
+ const chars = [...text];
171
+ const w = (ch) => (ch.charCodeAt(0) > 0x2e80 ? fs : fs * 0.55);
172
+ const lines = [];
173
+ let line = '';
174
+ let lw = 0;
175
+ for (const ch of chars) {
176
+ const cw = w(ch);
177
+ if (lw + cw > maxW && line !== '') {
178
+ // 下一个字符是标点 → 挤回本行(标点不落行首)
179
+ if (NO_BREAK.includes(ch)) {
180
+ line += ch;
181
+ lw += cw;
182
+ continue;
183
+ }
184
+ lines.push(line);
185
+ line = ch;
186
+ lw = cw;
187
+ }
188
+ else {
189
+ line += ch;
190
+ lw += cw;
191
+ }
192
+ }
193
+ if (line !== '')
194
+ lines.push(line);
195
+ // 末行只剩一个字 → 并回上一行(孤字难看)
196
+ if (lines.length > 1 && lines[lines.length - 1].length === 1) {
197
+ lines[lines.length - 2] += lines[lines.length - 1];
198
+ lines.pop();
199
+ }
200
+ return lines;
201
+ }
@@ -10,6 +10,8 @@ export interface DrawResult {
10
10
  }
11
11
  export declare function valueLabelsParam(params: URLSearchParams): 'all' | 'peak' | 'none' | undefined;
12
12
  export declare function clampInt(params: URLSearchParams, key: string, def: number, min: number, max: number): number;
13
+ /** 自动布局的方向:lr = 层往右走(默认),tb = 层往下走;不给 = 用默认 */
14
+ export declare function dirParam(params: URLSearchParams): 'LR' | 'TB' | undefined;
13
15
  export declare function stackParam(params: URLSearchParams): boolean;
14
16
  export declare function schemeParam(params: URLSearchParams): Scheme;
15
17
  export declare function boolParam(params: URLSearchParams, key: string): boolean | undefined;