@x-cmd/draw 0.2.5 → 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 (131) hide show
  1. package/README.md +80 -166
  2. package/dist/lib.d.ts +27 -1
  3. package/dist/lib.js +39 -1
  4. package/dist/node/cli.js +109 -11
  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 +157 -0
  10. package/dist/render/archmap.js +683 -0
  11. package/dist/render/area.js +20 -14
  12. package/dist/render/bar-grouped.js +8 -5
  13. package/dist/render/bar-stacked.js +11 -7
  14. package/dist/render/bar.js +8 -3
  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 +16 -8
  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 +51 -0
  24. package/dist/render/echarts-ssr.js +511 -0
  25. package/dist/render/gantt.d.ts +46 -0
  26. package/dist/render/gantt.js +415 -0
  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 +7 -5
  39. package/dist/render/line.js +9 -5
  40. package/dist/render/maps.d.ts +46 -0
  41. package/dist/render/maps.js +237 -0
  42. package/dist/render/measure.d.ts +0 -1
  43. package/dist/render/measure.js +0 -1
  44. package/dist/render/mermaid.d.ts +30 -0
  45. package/dist/render/mermaid.js +277 -0
  46. package/dist/render/mindmap.d.ts +10 -0
  47. package/dist/render/mindmap.js +388 -0
  48. package/dist/render/orgicon.d.ts +12 -0
  49. package/dist/render/orgicon.js +31 -0
  50. package/dist/render/pie.js +1 -1
  51. package/dist/render/png.d.ts +21 -0
  52. package/dist/render/png.js +109 -0
  53. package/dist/render/quadrant.d.ts +6 -0
  54. package/dist/render/quadrant.js +201 -0
  55. package/dist/render/query.d.ts +2 -0
  56. package/dist/render/query.js +108 -1
  57. package/dist/render/route.d.ts +86 -0
  58. package/dist/render/route.js +1077 -0
  59. package/dist/render/scatter.js +87 -12
  60. package/dist/render/seq.d.ts +41 -0
  61. package/dist/render/seq.js +248 -0
  62. package/dist/render/svg.d.ts +34 -2
  63. package/dist/render/svg.js +159 -20
  64. package/dist/render/table.d.ts +30 -0
  65. package/dist/render/table.js +134 -0
  66. package/dist/render/tokei.d.ts +18 -0
  67. package/dist/render/tokei.js +230 -0
  68. package/dist/render/uc.d.ts +58 -0
  69. package/dist/render/uc.js +319 -0
  70. package/dist/render/units.d.ts +32 -0
  71. package/dist/render/units.js +84 -0
  72. package/dist/render/vendor/au.js +10 -0
  73. package/dist/render/vendor/br.js +10 -0
  74. package/dist/render/vendor/china.js +11 -0
  75. package/dist/render/vendor/de.js +10 -0
  76. package/dist/render/vendor/eu.js +10 -0
  77. package/dist/render/vendor/japan.js +10 -0
  78. package/dist/render/vendor/kr.js +10 -0
  79. package/dist/render/vendor/mermaid-flow-parser.js +1164 -0
  80. package/dist/render/vendor/mermaid-gantt-parser.js +810 -0
  81. package/dist/render/vendor/us-states.js +11 -0
  82. package/dist/render/vendor/world-geo.d.ts +22 -0
  83. package/dist/render/vendor/world-geo.js +9 -0
  84. package/dist/render/vendor/world.d.ts +13 -0
  85. package/dist/render/vendor/world.js +12 -0
  86. package/dist/specs/base.d.ts +16 -0
  87. package/dist/specs/base.js +34 -1
  88. package/dist/specs/butterfly.d.ts +46 -0
  89. package/dist/specs/butterfly.js +241 -0
  90. package/dist/specs/calendar.d.ts +90 -0
  91. package/dist/specs/calendar.js +296 -0
  92. package/dist/specs/compass.d.ts +27 -0
  93. package/dist/specs/compass.js +111 -0
  94. package/dist/specs/index.d.ts +5 -0
  95. package/dist/specs/index.js +5 -0
  96. package/dist/specs/ladder.d.ts +43 -0
  97. package/dist/specs/ladder.js +261 -0
  98. package/dist/specs/quadrant.d.ts +41 -0
  99. package/dist/specs/quadrant.js +200 -0
  100. package/dist/specs/scatter.d.ts +14 -8
  101. package/dist/specs/scatter.js +54 -5
  102. package/dist/specs/table.d.ts +24 -0
  103. package/dist/specs/table.js +154 -0
  104. package/dist/store.d.ts +1 -1
  105. package/dist/store.js +1 -1
  106. package/dist/styles/butterfly.d.ts +2 -0
  107. package/dist/styles/butterfly.js +74 -0
  108. package/dist/styles/calendar.d.ts +2 -0
  109. package/dist/styles/calendar.js +51 -0
  110. package/dist/styles/compass.d.ts +2 -0
  111. package/dist/styles/compass.js +19 -0
  112. package/dist/styles/fonts-banner.d.ts +5 -0
  113. package/dist/styles/fonts-banner.js +9 -0
  114. package/dist/styles/fonts-cjk.d.ts +2 -0
  115. package/dist/styles/fonts-cjk.js +6 -0
  116. package/dist/styles/fonts-ttf.d.ts +2 -0
  117. package/dist/styles/fonts-ttf.js +6 -0
  118. package/dist/styles/index.js +13 -0
  119. package/dist/styles/ladder.d.ts +3 -0
  120. package/dist/styles/ladder.js +66 -0
  121. package/dist/styles/quadrant.d.ts +2 -0
  122. package/dist/styles/quadrant.js +19 -0
  123. package/dist/styles/table.d.ts +2 -0
  124. package/dist/styles/table.js +12 -0
  125. package/dist/styles/types.d.ts +1 -0
  126. package/dist/styles/types.js +1 -0
  127. package/dist/theme.d.ts +1 -1
  128. package/dist/theme.js +3 -4
  129. package/dist/types/dagre.d.ts +39 -0
  130. package/dist/types/dagre.js +3 -0
  131. package/package.json +28 -3
@@ -1,7 +1,8 @@
1
1
  // SVG 生成通用工具:转义、数字格式化、好看刻度、metadata 嵌入、文档包装与公共图元。
2
2
  import { FONT, TAB } from '../theme.js';
3
3
  import { ticks as d3Ticks, tickStep } from 'd3-array';
4
- import { INTER_FACE_CSS, INTER_WIDTHS } from '../styles/inter.js';
4
+ import { ApiError } from '../specs/base.js';
5
+ import { INTER_WIDTHS } from '../styles/inter.js';
5
6
  export function esc(s) {
6
7
  return s
7
8
  .replace(/&/g, '&')
@@ -73,8 +74,8 @@ export function rangeTicks(min, max, count = 4) {
73
74
  }
74
75
  return out.length > 0 ? out : [min, max];
75
76
  }
76
- // 内嵌结构化 metadata:数据 + 模板 + 版本,AI 可直接读取
77
- function metaTag(template, version, spec) {
77
+ // 内嵌结构化 metadata:数据 + 模板 + 版本,AI 可直接读取(echarts 覆盖层亦复用)
78
+ export function metaTag(template, version, spec) {
78
79
  const json = JSON.stringify({ generator: 'draw', template, version, spec });
79
80
  return `<metadata>${esc(json)}</metadata>`;
80
81
  }
@@ -102,7 +103,7 @@ export function topLegend(items, x, y, anchor = 'right', shape = 'circle') {
102
103
  }
103
104
  }
104
105
  else {
105
- let cx = x;
106
+ let cx = x - TAG_RESERVE; // 右对齐图例向左让出标签宽度(#27)
106
107
  for (let i = items.length - 1; i >= 0; i--) {
107
108
  const it = items[i];
108
109
  const w = textW(it.name, 12);
@@ -116,8 +117,59 @@ export function topLegend(items, x, y, anchor = 'right', shape = 'circle') {
116
117
  return parts.join('');
117
118
  }
118
119
  // 系列色 / 系列上文字色的 var 引用(渲染器一律使用)
120
+ // Edit @X-CMD 角标宽度预留(#27):右对齐图例向左让出此宽度,标签独占右上角
121
+ export const TAG_RESERVE = 150;
122
+ // explore 的 ?d= 直收 spec JSON 的图种(角标链接目标,qr 的 d= 是纯文本另算)
123
+ const EXPLORE_D_KINDS = new Set(['pie', 'bar', 'hbar', 'line', 'area', 'radar', 'gauge', 'scatter', 'combo', 'sankey', 'butterfly']);
124
+ export const EDIT_TAG_TEXT = 'Edit @X-CMD';
125
+ /** 剥离角标的正则源(onepage/explore 内联/gantt 前端换图共用;rect 段可选兼容旧纯文本形) */
126
+ export const EDIT_TAG_STRIP_SRC = '<a [^>]*>(?:<rect[^>]*>(?:</rect>)?)?<text[^>]*>Edit @X-CMD<\\/text><\\/a>';
127
+ /** Edit @X-CMD 角标:蓝色链接胶囊(描边+淡底+同色文字),一眼可点;色值走 --d-link 明暗自适应。
128
+ * baselineY 为文字基线(与标题同基线),胶囊以其光学中心对齐 */
129
+ export function editTag(href, width, baselineY, fs) {
130
+ const tw = textW(EDIT_TAG_TEXT, fs);
131
+ const padX = Math.max(6, Math.round(fs * 0.55));
132
+ const h = Math.round(fs * 1.85);
133
+ const w = Math.round(tw + padX * 2 + 2);
134
+ const x = width - 8 - w;
135
+ const cy = baselineY - Math.round(fs * 0.32);
136
+ return `<a href="${esc(href)}" target="_blank" rel="noopener"><rect x="${r2(x)}" y="${r2(cy - h / 2)}" width="${w}" height="${h}" rx="${r2(h / 2)}" fill="var(--d-link)" fill-opacity="0.09" stroke="var(--d-link)" stroke-opacity="0.5"/><text x="${r2(x + w / 2)}" y="${baselineY}" text-anchor="middle" font-size="${fs}" font-weight="500" letter-spacing="0.2" fill="var(--d-link)">${EDIT_TAG_TEXT}</text></a>`;
137
+ }
138
+ // ---- 标注层(marker layer,10-30 会议"杀手锏")----
139
+ // mark=<锚x>;<锚y>;<文字>:类目轴图锚x=标签、锚y=数值;散点双数值;可重复。
140
+ // 标注独立成 <g id="marks"> 顶层图层:圈+文字,难免压数据 → explore 页用 markhide=1 整层不画。
141
+ /** 像素锚点已换算好的标注层;文字带底色描边保证压线可读,右缘放不下翻到圈左侧 */
142
+ export function marksLayer(pts, width) {
143
+ const parts = ['<g id="marks">'];
144
+ for (const p of pts) {
145
+ parts.push(`<circle cx="${r2(p.x)}" cy="${r2(p.y)}" r="11" fill="none" stroke="var(--d-mark)" stroke-width="2.5"/>`);
146
+ const rightFits = p.x + 15 + textW(p.text, 13) <= width - 8;
147
+ parts.push(rightFits
148
+ ? `<text x="${r2(p.x + 15)}" y="${r2(p.y - 9)}" font-size="13" font-weight="600" fill="var(--d-mark)">${esc(p.text)}</text>`
149
+ : `<text x="${r2(p.x - 15)}" y="${r2(p.y - 9)}" text-anchor="end" font-size="13" font-weight="600" fill="var(--d-mark)">${esc(p.text)}</text>`);
150
+ }
151
+ parts.push('</g>');
152
+ return parts.join('');
153
+ }
119
154
  export const sVar = (i) => `var(--d-s${i + 1})`;
120
155
  export const onVar = (i) => `var(--d-on-s${i + 1})`;
156
+ // 标注层用色(强调红,明暗各一):圈+文字同色,固定色不参与皮肤轮换
157
+ const MARK_COLOR = { light: '#D92D20', dark: '#FF7A6B' };
158
+ // 链接色(Edit @X-CMD 角标等可点元素,明暗各一):blue-600/blue-400
159
+ const LINK_COLOR = { light: '#2563EB', dark: '#60A5FA' };
160
+ /** 类目轴图标注锚点 → 像素:ax 按 labels 找列(找不到抛 400),y 数值映射并夹在绘图区内 */
161
+ export function markPoints(marks, labels, xOf, yOf, clamp) {
162
+ const out = [];
163
+ if (!marks || marks.length === 0)
164
+ return out;
165
+ for (const m of marks) {
166
+ const idx = labels.indexOf(String(m.ax));
167
+ if (idx < 0)
168
+ throw new ApiError(400, `"mark" anchor ${JSON.stringify(String(m.ax))} is not one of the labels`);
169
+ out.push({ x: xOf(idx), y: Math.min(Math.max(yOf(m.ay), clamp[0]), clamp[1]), text: m.text });
170
+ }
171
+ return out;
172
+ }
121
173
  const lumaHex = (hex) => {
122
174
  const h = hex.replace('#', '');
123
175
  return 0.299 * parseInt(h.slice(0, 2), 16) + 0.587 * parseInt(h.slice(2, 4), 16) + 0.114 * parseInt(h.slice(4, 6), 16);
@@ -126,25 +178,57 @@ const onDuo = (d) => ({
126
178
  light: lumaHex(d.light) > 150 ? '#18181b' : '#ffffff',
127
179
  dark: lumaHex(d.dark) > 150 ? '#18181b' : '#ffffff',
128
180
  });
129
- // 皮肤 → 内嵌样式块(auto=双套+media query,dark/light=单套);含 Inter 子集 @font-face
130
- function skinStyle(skin) {
181
+ // 皮肤 → 内嵌样式块(auto=双套+media query,dark/light=单套);含 Inter 子集 @font-face。
182
+ // echarts 覆盖层(echarts-ssr.ts)复用:option 里的 var(--d-*) 颜色由此解析
183
+ export function skinStyle(skin) {
131
184
  const n = skin.neutrals;
132
185
  const varsOf = (side) => {
133
186
  const items = [];
134
187
  skin.series.forEach((d, i) => items.push(`--d-s${i + 1}:${d[side]}`, `--d-on-s${i + 1}:${onDuo(d)[side]}`));
135
- items.push(`--d-ramp-a:${skin.ramp[0][side]}`, `--d-ramp-b:${skin.ramp[1][side]}`, `--d-ink:${n.ink[side]}`, `--d-text:${n.text[side]}`, `--d-muted:${n.muted[side]}`, `--d-faint:${n.faint[side]}`, `--d-grid:${n.grid[side]}`, `--d-baseline:${n.baseline[side]}`, `--d-border:${n.border[side]}`, `--d-card:${n.card[side]}`, `--d-halo:${n.halo[side]}`);
188
+ items.push(`--d-ramp-a:${skin.ramp[0][side]}`, `--d-ramp-b:${skin.ramp[1][side]}`, `--d-ink:${n.ink[side]}`, `--d-text:${n.text[side]}`, `--d-muted:${n.muted[side]}`, `--d-faint:${n.faint[side]}`, `--d-grid:${n.grid[side]}`, `--d-baseline:${n.baseline[side]}`, `--d-border:${n.border[side]}`, `--d-card:${n.card[side]}`, `--d-halo:${n.halo[side]}`, `--d-mark:${MARK_COLOR[side]}`, `--d-link:${LINK_COLOR[side]}`);
136
189
  return `:root{color-scheme:${side};${items.join(';')}}`;
137
190
  };
138
191
  if (skin.scheme === 'auto') {
139
- return `<style>${INTER_FACE_CSS}${varsOf('light')}@media (prefers-color-scheme: dark){${varsOf('dark')}}</style>`;
192
+ return `<style>${varsOf('light')}@media (prefers-color-scheme: dark){${varsOf('dark')}}</style>`;
140
193
  }
141
- return `<style>${INTER_FACE_CSS}${varsOf(skin.scheme)}</style>`;
194
+ return `<style>${varsOf(skin.scheme)}</style>`;
142
195
  }
143
196
  // 统一 SVG 文档包装:外壳 + 皮肤变量 + metadata(渲染器不再各自拼 <svg>)
144
- export function svgDoc(width, height, template, version, aria, spec, skin, children) {
197
+ // tagQuery(#44):原始 URL 查询串 —— archmap/mindmap/mermaid/gantt 等路径式图种
198
+ // 的角标直链到 /draw/explore/<template>?<原参数>(w/h/scheme/format 剥掉),不再是泛链首页
199
+ export function svgDoc(width, height, template, version, aria, spec, skin, children, tagQuery, tagKind) {
200
+ // Edit @X-CMD 角标(#27):右上角,与标题同一基线(y=38/54,普通 baseline),蓝色链接胶囊
201
+ // 可点击(#13 水印=广告):链到对应 explore 页;图表家族带 ?d= 全量数据,点击即得可编辑副本
202
+ const tagFs = Math.max(10, Math.min(18, Math.round(width / 55)));
203
+ const tagY = skin.rules.title === 'editorial' ? 54 : 38;
204
+ let tagHref = 'https://x-cmd.com/draw';
205
+ if (EXPLORE_D_KINDS.has(template)) {
206
+ tagHref = `https://x-cmd.com/draw/explore?type=${template}&d=${encodeURIComponent(JSON.stringify(spec))}`;
207
+ }
208
+ else if (template === 'qr') {
209
+ tagHref = `https://x-cmd.com/draw/explore?type=qr&d=${encodeURIComponent(String(spec?.text ?? ''))}`;
210
+ }
211
+ else if (template === 'table') {
212
+ // 表格的入口直接是 /table —— 那个 app 才是能改表格的地方,
213
+ // draw 底下没有 table 的 explore 页,也不需要有一个。
214
+ tagHref = `https://x-cmd.com/table?d=${encodeURIComponent(JSON.stringify(spec))}`;
215
+ }
216
+ else if (tagQuery) {
217
+ const q = new URLSearchParams(tagQuery.replace(/^\?/, ''));
218
+ for (const k of ['w', 'h', 'scheme', 'format'])
219
+ q.delete(k); // 尺寸/渲染控制参数不带进落地页
220
+ // type= 是「走 ?type= 透传」的标记;路径已经说明了是什么图,别再带进落地页
221
+ // (否则 ?type=archmap 透传出来的角标,和 /draw/archmap 直出的角标不是一条链接)
222
+ if (q.get('type') === (tagKind ?? template))
223
+ q.delete('type');
224
+ const qs = q.toString();
225
+ if (qs)
226
+ tagHref = `https://x-cmd.com/draw/explore/${tagKind ?? template}?${qs}`;
227
+ }
228
+ const tag = editTag(tagHref, width, tagY, tagFs);
145
229
  return (`<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" ` +
146
230
  `font-family="${FONT}" role="img" aria-label="${esc(aria)}">` +
147
- skinStyle(skin) + metaTag(template, version, spec) + children + `</svg>`);
231
+ skinStyle(skin) + metaTag(template, version, spec) + children + tag + `</svg>`);
148
232
  }
149
233
  // 标题块:plain = 18px/500(Evil 系);editorial = 30px/600 自然语言大标题(OpenData 系)
150
234
  // editorial 超宽换行至多 2 行(Latin 按词、CJK 逐字),装不下省略号截断
@@ -258,6 +342,43 @@ export function smoothPath(pts) {
258
342
  }
259
343
  return d;
260
344
  }
345
+ /**
346
+ * 折线的角点改成圆角(x-cmd-hub/draw#116)。
347
+ *
348
+ * 尖角在数据图上很扎眼:一段折线的每个拐角都是一个小小的尖刺。
349
+ * 做法:每个拐角切掉一小段,拿二次贝塞尔补过去 —— 半径 r,
350
+ * 相邻两段短于 2r 就自动收小(免得弧画到线外面去)。
351
+ */
352
+ export function roundedPath(pts, r = 8) {
353
+ if (pts.length < 3)
354
+ return `M${pts.map((p) => `${r2(p[0])} ${r2(p[1])}`).join('L')}`;
355
+ let d = `M${r2(pts[0][0])} ${r2(pts[0][1])}`;
356
+ for (let i = 1; i < pts.length - 1; i++) {
357
+ const [px, py] = pts[i - 1];
358
+ const [cx, cy] = pts[i];
359
+ const [nx, ny] = pts[i + 1];
360
+ const d1 = Math.hypot(cx - px, cy - py);
361
+ const d2 = Math.hypot(nx - cx, ny - cy);
362
+ if (d1 < 0.01 || d2 < 0.01)
363
+ continue;
364
+ // 直着走(没拐弯)就别切角了 —— 免得路径里塞一堆没用的弧
365
+ const cross = (cx - px) * (ny - cy) - (cy - py) * (nx - cx);
366
+ const dot = (cx - px) * (nx - cx) + (cy - py) * (ny - cy);
367
+ if (Math.abs(cross) < 1e-9 && dot > 0) {
368
+ d += `L${r2(cx)} ${r2(cy)}`;
369
+ continue;
370
+ }
371
+ const rr = Math.min(r, d1 / 2, d2 / 2);
372
+ const ax = cx - ((cx - px) / d1) * rr;
373
+ const ay = cy - ((cy - py) / d1) * rr;
374
+ const bx = cx + ((nx - cx) / d2) * rr;
375
+ const by = cy + ((ny - cy) / d2) * rr;
376
+ d += `L${r2(ax)} ${r2(ay)}Q${r2(cx)} ${r2(cy)} ${r2(bx)} ${r2(by)}`;
377
+ }
378
+ const last = pts[pts.length - 1];
379
+ d += `L${r2(last[0])} ${r2(last[1])}`;
380
+ return d;
381
+ }
261
382
  // 按底色亮度选字色:亮底→墨色,深底→白色
262
383
  export function onColor(hex) {
263
384
  const h = hex.replace('#', '');
@@ -267,7 +388,7 @@ export function onColor(hex) {
267
388
  return 0.299 * r + 0.587 * g + 0.114 * b > 150 ? '#18181b' : '#ffffff';
268
389
  }
269
390
  // 标签白描边属性(压在任意底色上都可读;仅 pushLineLabel 内部使用)
270
- const HALO = 'paint-order="stroke" stroke="var(--d-halo)" stroke-width="3" stroke-linejoin="round"';
391
+ const HALO = ''; // 描边已按 2026-11-03 决定移除
271
392
  // 标签碰撞检测:重叠即舍弃,保证任何情况下数字不重叠
272
393
  export class LabelPlacer {
273
394
  boxes = [];
@@ -308,15 +429,28 @@ export function reserveEdges(placer, width, height) {
308
429
  placer.reserve(-2, -2, 2, height + 4); // 左
309
430
  placer.reserve(width, -2, 2, height + 4); // 右
310
431
  }
311
- // 数据点(hollow=空心描边 / filled=实心+白描边),折线/面积系列共用
312
- export function pushPoints(parts, pts, color, rules) {
313
- if (rules.points === 'none')
432
+ // 数据点(hollow=空心描边 / filled=实心+白描边),折线/面积系列共用。
433
+ // tips 提供时逐点挂 data-tip(explore 页悬停高亮+数值的载体);
434
+ // points:'none' 的皮肤输出隐形命中圆(fill=transparent+pointer-events=all),
435
+ // 保证无点皮肤同样逐点可悬停
436
+ export function pushPoints(parts, pts, color, rules, tips, series) {
437
+ const ds = series === undefined ? '' : ` data-series="${series}"`;
438
+ if (rules.points === 'none') {
439
+ if (!tips)
440
+ return;
441
+ for (let i = 0; i < pts.length; i++) {
442
+ const [px, py] = pts[i];
443
+ parts.push(`<circle cx="${r2(px)}" cy="${r2(py)}" r="8" fill="transparent" pointer-events="all" data-tip="${esc(tips[i])}"${ds}/>`);
444
+ }
314
445
  return;
315
- for (const [px, py] of pts) {
446
+ }
447
+ for (let i = 0; i < pts.length; i++) {
448
+ const [px, py] = pts[i];
316
449
  const attrs = rules.points === 'hollow'
317
450
  ? `fill="var(--d-halo)" stroke="${color}" stroke-width="2.5"`
318
451
  : `fill="${color}" stroke="var(--d-halo)" stroke-width="3"`;
319
- parts.push(`<circle cx="${r2(px)}" cy="${r2(py)}" r="${rules.pointR}" ${attrs}/>`);
452
+ const tip = tips?.[i];
453
+ parts.push(`<circle cx="${r2(px)}" cy="${r2(py)}" r="${rules.pointR}" ${attrs}${tip !== undefined ? ` data-tip="${esc(tip)}"` : ''}${ds}/>`);
320
454
  }
321
455
  }
322
456
  // 全有或全无的柱内数值标注:入柱(字号可缩到 7px)→ 回退柱顶/柱右侧 → 整层不标
@@ -352,8 +486,13 @@ export function pushBarLabelsAllOrNothing(parts, items, sharedPlacer, segments =
352
486
  continue;
353
487
  if (!placer.fits(bx, ty - fs, tw + 2, fs + 2))
354
488
  continue;
355
- const halo = l.haloColor ? `paint-order="stroke" stroke="${l.haloColor}" stroke-width="3" stroke-linejoin="round" ` : '';
356
- out.push(`<text x="${r2(cx)}" y="${r2(ty)}" text-anchor="middle" font-size="${r2(fs)}" ${l.peak ? 'font-weight="600" ' : ''}fill="${l.fill}" ${halo}style="${TAB}"${ds(l.series)}>${l.text}</text>`);
489
+ // 柱内标签可能被折线穿过:用「柱子同色」的一小块垫在字下面把线遮掉
490
+ // (不描边 —— 11-03 起全局不再给文字描边;垫色是背景,不是字形描边)
491
+ const mask = l.haloColor
492
+ ? `<rect x="${r2(bx)}" y="${r2(ty - fs)}" width="${r2(tw + 2)}" height="${r2(fs + 2)}" fill="${l.haloColor}"/>`
493
+ : '';
494
+ out.push(mask +
495
+ `<text x="${r2(cx)}" y="${r2(ty)}" text-anchor="middle" font-size="${r2(fs)}" ${l.peak ? 'font-weight="600" ' : ''}fill="${l.fill}"${l.haloColor ? ' data-mask="1"' : ''} style="${TAB}"${ds(l.series)}>${l.text}</text>`);
357
496
  placed = true;
358
497
  break;
359
498
  }
@@ -394,7 +533,7 @@ export function pushBarLabelsAllOrNothing(parts, items, sharedPlacer, segments =
394
533
  const bx = cx - fTw / 2 - 1;
395
534
  if (!placer.fits(bx, ty - l.fs, fTw + 2, l.fs + 2))
396
535
  return false;
397
- out.push(`<text x="${r2(cx)}" y="${r2(ty)}" text-anchor="middle" font-size="${l.fs}" ${l.peak ? 'font-weight="600" ' : ''}fill="var(--d-ink)" paint-order="stroke" stroke="var(--d-halo)" stroke-width="3" stroke-linejoin="round" style="${TAB}"${ds(l.series)}>${l.text}</text>`);
536
+ out.push(`<text x="${r2(cx)}" y="${r2(ty)}" text-anchor="middle" font-size="${l.fs}" ${l.peak ? 'font-weight="600" ' : ''}fill="var(--d-ink)" style="${TAB}"${ds(l.series)}>${l.text}</text>`);
398
537
  }
399
538
  }
400
539
  parts.push(...out);
@@ -0,0 +1,30 @@
1
+ import type { TableSpec } from '../specs/table.js';
2
+ import { type RenderOpts } from './svg.js';
3
+ /** 一列。 */
4
+ export interface TableCol {
5
+ name: string;
6
+ align: 'left' | 'right';
7
+ /** 列宽(px) */
8
+ w: number;
9
+ }
10
+ /** 渲染用的网格。 */
11
+ export interface TableGrid {
12
+ title?: string;
13
+ cols: TableCol[];
14
+ rows: string[][];
15
+ /** 自然宽(px) */
16
+ width: number;
17
+ /** 自然高(px) */
18
+ height: number;
19
+ }
20
+ /**
21
+ * 规格 → 可渲染的网格。
22
+ */
23
+ export declare function tableToGrid(spec: TableSpec): TableGrid;
24
+ /**
25
+ * 渲染表格。
26
+ *
27
+ * @param spec 规格(嵌进 <metadata> —— I5:输出里带着它渲染自的那份数据)
28
+ * @param o 渲染选项
29
+ */
30
+ export declare function renderTable(spec: TableSpec, o: RenderOpts): string;
@@ -0,0 +1,134 @@
1
+ // 表格渲染器:扁平的 {cols, rows} → SVG。
2
+ //
3
+ // 排版用 draw 自己的那一套(svgDoc / textW / skin),所以同一页里表格与其它图
4
+ // 长得是一家人 —— 这是 x-cmd/draw#24 明确要求的:「表头和单元格的对齐要跟 draw
5
+ // 的其它 kind 用同一套排版办法,不然同一张表在两个地方会长得不一样」。
6
+ //
7
+ // 规格自包含(见 src/specs/table.ts):多维表格仓(x-cmd/table)是另一个服务,
8
+ // 两边共享的是 cols=/rows= 这个约定,不是代码。
9
+ import { ApiError } from '../specs/index.js';
10
+ import { esc, svgDoc, textW } from './svg.js';
11
+ /** 单元格左右内边距。 */
12
+ const PAD_X = 10;
13
+ /** 行高。 */
14
+ const ROW_H = 28;
15
+ /** 表头字号。 */
16
+ const HEAD_FS = 13;
17
+ /** 数据字号。 */
18
+ const BODY_FS = 14;
19
+ /** 画布外边距。 */
20
+ const PAD = 24;
21
+ /**
22
+ * 顶边那一条留给标题与 @X-CMD 角标(角标的基线固定在 38)。
23
+ * 表头从它下面开始 —— 不然窄表格的最后一列表头会被角标压住。
24
+ */
25
+ const TOP_LINE_Y = 38;
26
+ const CONTENT_TOP = TOP_LINE_Y + 20;
27
+ /** 列宽的上下限。 */
28
+ const MIN_COL_W = 72;
29
+ const MAX_COL_W = 320;
30
+ /** 整列都是数字 → 右对齐(与 Airtable 一致)。 */
31
+ function columnNumeric(rows, index) {
32
+ let seen = false;
33
+ for (const row of rows) {
34
+ const v = row[index];
35
+ if (v === undefined || v === '')
36
+ continue;
37
+ if (!Number.isFinite(Number(v)))
38
+ return false;
39
+ seen = true;
40
+ }
41
+ return seen;
42
+ }
43
+ /**
44
+ * 规格 → 可渲染的网格。
45
+ */
46
+ export function tableToGrid(spec) {
47
+ if (!spec.cols.length && !spec.rows.length) {
48
+ throw new ApiError(400, 'A table needs data. Try ?cols=Service,Errors&rows=api:12;worker:9');
49
+ }
50
+ const width = Math.max(spec.cols.length, ...spec.rows.map((r) => r.length), 0);
51
+ const names = [];
52
+ for (let i = 0; i < width; i++)
53
+ names.push(spec.cols[i] ?? String(i + 1));
54
+ const cols = names.map((name, i) => {
55
+ let w = textW(name, HEAD_FS);
56
+ for (const row of spec.rows) {
57
+ const cw = textW(row[i] ?? '', BODY_FS);
58
+ if (cw > w)
59
+ w = cw;
60
+ }
61
+ return {
62
+ name,
63
+ align: columnNumeric(spec.rows, i) ? 'right' : 'left',
64
+ w: Math.min(MAX_COL_W, Math.max(MIN_COL_W, Math.ceil(w) + PAD_X * 2)),
65
+ };
66
+ });
67
+ const rows = spec.rows.map((r) => {
68
+ const out = [];
69
+ for (let i = 0; i < width; i++)
70
+ out.push(r[i] ?? '');
71
+ return out;
72
+ });
73
+ const grid = {
74
+ cols,
75
+ rows,
76
+ width: Math.ceil(cols.reduce((a, c) => a + c.w, 0) + PAD * 2),
77
+ height: Math.ceil(CONTENT_TOP + 34 + rows.length * ROW_H + PAD),
78
+ };
79
+ if (spec.title)
80
+ grid.title = spec.title;
81
+ return grid;
82
+ }
83
+ /**
84
+ * 渲染表格。
85
+ *
86
+ * @param spec 规格(嵌进 <metadata> —— I5:输出里带着它渲染自的那份数据)
87
+ * @param o 渲染选项
88
+ */
89
+ export function renderTable(spec, o) {
90
+ const grid = tableToGrid(spec);
91
+ const width = o.width || grid.width;
92
+ const height = o.height || grid.height;
93
+ // 画布比自然宽度窄时,列宽等比压缩(文字放不下就溢出,宁可溢出也不要糊在一起)
94
+ const natural = grid.width - PAD * 2;
95
+ const scale = natural > 0 ? Math.min(1, (width - PAD * 2) / natural) : 1;
96
+ const widths = grid.cols.map((c) => c.w * scale);
97
+ const parts = [];
98
+ // 标题与角标共用顶边那一条(角标的基线固定在 38)
99
+ if (grid.title) {
100
+ parts.push(`<text x="${PAD}" y="${TOP_LINE_Y}" font-size="18" font-weight="500" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(grid.title)}</text>`);
101
+ }
102
+ // 表头
103
+ let y = CONTENT_TOP;
104
+ let x = PAD;
105
+ const headY = y + 22;
106
+ for (let i = 0; i < grid.cols.length; i++) {
107
+ const c = grid.cols[i];
108
+ const anchor = c.align === 'right' ? 'end' : 'start';
109
+ const tx = c.align === 'right' ? x + widths[i] - PAD_X : x + PAD_X;
110
+ parts.push(`<text x="${tx}" y="${headY}" font-size="${HEAD_FS}" font-weight="600" fill="var(--d-muted)" text-anchor="${anchor}">${esc(c.name)}</text>`);
111
+ x += widths[i];
112
+ }
113
+ parts.push(`<line x1="${PAD}" y1="${headY + 8}" x2="${width - PAD}" y2="${headY + 8}" stroke="var(--d-border)" stroke-width="1"/>`);
114
+ y = headY + 8;
115
+ // 数据行
116
+ for (let r = 0; r < grid.rows.length; r++) {
117
+ const row = grid.rows[r];
118
+ const rowY = y + r * ROW_H;
119
+ if (r % 2 === 1) {
120
+ parts.push(`<rect x="${PAD}" y="${rowY}" width="${width - PAD * 2}" height="${ROW_H}" fill="var(--d-grid)" opacity="0.35"/>`);
121
+ }
122
+ let cx = PAD;
123
+ for (let i = 0; i < grid.cols.length; i++) {
124
+ const c = grid.cols[i];
125
+ const anchor = c.align === 'right' ? 'end' : 'start';
126
+ const tx = c.align === 'right' ? cx + widths[i] - PAD_X : cx + PAD_X;
127
+ parts.push(`<text x="${tx}" y="${rowY + ROW_H / 2 + 5}" font-size="${BODY_FS}" fill="var(--d-ink)" text-anchor="${anchor}">${esc(row[i] ?? '')}</text>`);
128
+ cx += widths[i];
129
+ }
130
+ parts.push(`<line x1="${PAD}" y1="${rowY + ROW_H}" x2="${width - PAD}" y2="${rowY + ROW_H}" stroke="var(--d-grid)" stroke-width="1"/>`);
131
+ }
132
+ const aria = grid.title ? `${grid.title} — table` : 'table';
133
+ return svgDoc(width, height, 'table', o.version, aria, spec, o.skin, parts.join(''));
134
+ }
@@ -0,0 +1,18 @@
1
+ import type { MultiSpec } from '../specs/index.js';
2
+ import { type DrawResult } from './query.js';
3
+ /**
4
+ * 解析 tokei 的 JSON,转成堆叠柱要用的多系列 spec。
5
+ *
6
+ * 接受 tokei 的完整输出(含每个文件的 reports / children)——这里只取每门语言的
7
+ * 三项合计;也接受裁剪过的 {"TypeScript":{"code":…,"comments":…,"blanks":…}}。
8
+ */
9
+ export declare function parseTokei(raw: string): MultiSpec;
10
+ /** tokei 的数据:三项计数 + 文件数(有的话) */
11
+ export interface TokeiData {
12
+ spec: MultiSpec;
13
+ /** 每种语言的文件数;输入里没有文件信息时是 null */
14
+ files: number[] | null;
15
+ }
16
+ /** 解析 tokei 的 JSON(整份或裁剪过的),顺带把文件数取出来 */
17
+ export declare function parseTokeiData(raw: string): TokeiData;
18
+ export declare function renderTokeiFromUrl(url: URL, body?: string): DrawResult;