@x-cmd/draw 0.5.1 → 0.5.3
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/dist/draw-client.iife.js +28 -28
- package/dist/render/gantt.js +38 -32
- package/dist/render/gantt2.js +10 -7
- package/dist/render/hbar-spec.js +25 -15
- package/dist/render/source-theme.d.ts +3 -1
- package/dist/render/source-theme.js +4 -2
- package/dist/render/svg.d.ts +10 -0
- package/dist/render/svg.js +12 -0
- package/dist/render/wavedrom.js +143 -6
- package/package.json +1 -1
package/dist/render/gantt.js
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
import { ApiError } from '../specs/index.js';
|
|
7
7
|
import { resolveVersion } from '../version.js';
|
|
8
8
|
import { resolveSkin } from '../styles/index.js';
|
|
9
|
-
import { esc, r2, svgDoc, textW, sVar } from './svg.js';
|
|
9
|
+
import { esc, legendRowY, r2, svgDoc, textW, sVar } from './svg.js';
|
|
10
10
|
import { schemeParam } from './query.js';
|
|
11
11
|
import { mermaidGanttParser } from './vendor/mermaid-gantt-parser.js';
|
|
12
12
|
const N_EXAMPLE = 'm=' + encodeURIComponent('gantt\n dateFormat YYYY-MM-DD\n section Dev\n Design :done, a1, 2026-11-01, 3d\n Build :a2, after a1, 5d');
|
|
@@ -152,7 +152,18 @@ export function renderGantt(doc, opts) {
|
|
|
152
152
|
const maxT = Math.max(...spans.map(s => s.end));
|
|
153
153
|
const spanDays = Math.max(1, Math.round((maxT - minT) / DAY));
|
|
154
154
|
const PAD = 26;
|
|
155
|
+
// 图例:house 规矩 —— 坐**标题下一行、左缘**(#186);原来吊在底部一条带,离条老远。
|
|
156
|
+
// (这张图有标题 ⇒ 图例在标题下面一行;没标题 ⇒ 坐标题本该坐的那一行。)
|
|
157
|
+
const flagSet = new Set();
|
|
158
|
+
spans.forEach((s) => s.flags.forEach((f) => flagSet.add(f)));
|
|
159
|
+
const legendItems = [['done', 'Done'], ['active', 'Active'], ['crit', 'Critical'], ['milestone', 'Milestone']]
|
|
160
|
+
.filter(([k]) => flagSet.has(k));
|
|
161
|
+
const titleY = doc.title ? PAD + 14 : undefined;
|
|
162
|
+
const legendY = legendRowY(titleY, PAD);
|
|
155
163
|
const titleH = doc.title ? 40 : 0;
|
|
164
|
+
// 图例行:标题下面那一行(有图例才占位);轴头条从图例下面开始
|
|
165
|
+
const legendH = legendItems.length ? 26 : 0;
|
|
166
|
+
const headH = legendItems.length ? Math.max(titleH + legendH, legendY + 12) : titleH;
|
|
156
167
|
const upperH = 20; // 上行轴:月/年带
|
|
157
168
|
const lowerH = 24; // 下行轴:日/周/月刻度
|
|
158
169
|
const axisH = upperH + lowerH;
|
|
@@ -169,21 +180,21 @@ export function renderGantt(doc, opts) {
|
|
|
169
180
|
const width = opts.width !== undefined ? Math.max(200, Math.min(2400, opts.width)) : Math.min(Math.max(needW, 560), 2400);
|
|
170
181
|
if (width < labelW + PAD * 2 + 80)
|
|
171
182
|
labelW = Math.max(70, width - PAD * 2 - 80); // 窄画布压缩标签列,保时间轴最小可用
|
|
172
|
-
let height = opts.height !== undefined ? Math.max(140, Math.min(2000, opts.height)) : Math.min(Math.max(
|
|
183
|
+
let height = opts.height !== undefined ? Math.max(140, Math.min(2000, opts.height)) : Math.min(Math.max(headH + axisH + rows * rowH + PAD * 2, 200), 2000);
|
|
173
184
|
let topOff = 0;
|
|
174
185
|
if (opts.height !== undefined) {
|
|
175
|
-
const bodyH = opts.height - PAD * 2 -
|
|
186
|
+
const bodyH = opts.height - PAD * 2 - headH - axisH;
|
|
176
187
|
const unit = Math.min(48, Math.max(22, bodyH / rows));
|
|
177
188
|
rowH = Math.floor(unit); // floor 保证内容不超高;余量进 topOff 垂直居中
|
|
178
189
|
sectionH = Math.max(22, Math.round(unit * 0.92));
|
|
179
190
|
barH = Math.min(20, Math.max(14, Math.round(rowH * 0.57)));
|
|
180
191
|
const contentH = rows * rowH;
|
|
181
192
|
topOff = Math.max(0, Math.round((bodyH - contentH) / 2));
|
|
182
|
-
height = Math.max(height,
|
|
193
|
+
height = Math.max(height, headH + axisH + topOff + contentH + PAD * 2); // 行过多时允许超高(画框 contain 兜底)
|
|
183
194
|
}
|
|
184
195
|
const x0 = PAD + labelW;
|
|
185
196
|
const x1 = width - PAD;
|
|
186
|
-
const bodyTop =
|
|
197
|
+
const bodyTop = headH + axisH;
|
|
187
198
|
const bodyBottom = height - PAD;
|
|
188
199
|
const pxPerDay = (x1 - x0) / spanDays;
|
|
189
200
|
const xOf = (t) => x0 + ((t - minT) / DAY) * pxPerDay;
|
|
@@ -238,8 +249,8 @@ export function renderGantt(doc, opts) {
|
|
|
238
249
|
}
|
|
239
250
|
const parts = [];
|
|
240
251
|
// 表头带(轴两行+标签列同铺浅底,"表格+时间轴"结构感)+ 标签列|时间线竖分隔
|
|
241
|
-
parts.push(`<rect x="${PAD}" y="${
|
|
242
|
-
parts.push(`<line x1="${x0}" y1="${
|
|
252
|
+
parts.push(`<rect x="${PAD}" y="${headH}" width="${r2(x1 - PAD)}" height="${axisH}" fill="var(--d-ink)" fill-opacity="0.03"/>`);
|
|
253
|
+
parts.push(`<line x1="${x0}" y1="${headH}" x2="${x0}" y2="${bodyBottom}" stroke="var(--d-border)" data-x0="${x0}"/>`);
|
|
243
254
|
// 周末底纹(日粒度、每列足够宽才铺,否则成噪点)
|
|
244
255
|
if (pxPerDay >= 8) {
|
|
245
256
|
for (let t = minT; t <= maxT; t += DAY) {
|
|
@@ -267,16 +278,16 @@ export function renderGantt(doc, opts) {
|
|
|
267
278
|
if (!doc.title && x > width - 140)
|
|
268
279
|
continue; // 角标胶囊区,只画线不画字
|
|
269
280
|
const weekend = daily && (new Date(tk.t).getUTCDay() === 0 || new Date(tk.t).getUTCDay() === 6);
|
|
270
|
-
parts.push(`<text x="${r2(x)}" y="${
|
|
281
|
+
parts.push(`<text x="${r2(x)}" y="${headH + upperH + 16}" text-anchor="middle" font-size="${daily ? 10 : 11}" fill="${weekend ? 'var(--d-faint)' : 'var(--d-muted)'}">${esc(tk.label)}</text>`);
|
|
271
282
|
}
|
|
272
283
|
// 上行带标签 + 加强边界线(画在细网格之上)+ 上下轴分隔线
|
|
273
284
|
for (const us of upperSpans) {
|
|
274
285
|
const bx = Math.min(Math.max(xOf(us.start), x0), x1);
|
|
275
286
|
const cx = (Math.max(xOf(us.start), x0) + Math.min(xOf(us.end), x1)) / 2;
|
|
276
287
|
parts.push(`<line x1="${r2(bx)}" y1="${bodyTop}" x2="${r2(bx)}" y2="${bodyBottom}" stroke="var(--d-border)"/>`);
|
|
277
|
-
parts.push(`<text x="${r2(cx)}" y="${
|
|
288
|
+
parts.push(`<text x="${r2(cx)}" y="${headH + 14}" text-anchor="middle" font-size="11" font-weight="500" fill="var(--d-text)">${esc(us.label)}</text>`);
|
|
278
289
|
}
|
|
279
|
-
parts.push(`<line x1="${x0}" y1="${
|
|
290
|
+
parts.push(`<line x1="${x0}" y1="${headH + upperH}" x2="${x1}" y2="${headH + upperH}" stroke="var(--d-border)"/>`);
|
|
280
291
|
parts.push(`<line x1="${x0}" y1="${bodyBottom}" x2="${x1}" y2="${bodyBottom}" stroke="var(--d-baseline)" stroke-width="1"/>`);
|
|
281
292
|
// 行与时间条(interactive:数据带 data-row=行号/data-node=任务 id/data-after=前置,
|
|
282
293
|
// 行尾铺透明命中 rect——悬停/点击的命中区是整行而非 16px 条,与竞品一致。
|
|
@@ -336,31 +347,26 @@ export function renderGantt(doc, opts) {
|
|
|
336
347
|
const tx = xOf(today);
|
|
337
348
|
parts.push(`<line x1="${r2(tx)}" y1="${bodyTop}" x2="${r2(tx)}" y2="${bodyBottom}" stroke="var(--d-mark)" stroke-width="1.5" data-today=""/>`);
|
|
338
349
|
}
|
|
339
|
-
// 状态图例(只画图里出现过的 flag
|
|
340
|
-
{
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
parts.push(`<rect x="${r2(lx)}" y="${r2(ly)}" width="10" height="10" rx="2" fill="var(--d-mark)" fill-opacity="0.16" stroke="var(--d-mark)" stroke-width="1.2"/>`);
|
|
356
|
-
lx += 14;
|
|
357
|
-
parts.push(`<text x="${r2(lx)}" y="${r2(ly + 9)}" font-size="10" fill="var(--d-muted)">${label}</text>`);
|
|
358
|
-
lx += textW(label, 10) + 18;
|
|
359
|
-
}
|
|
350
|
+
// 状态图例(只画图里出现过的 flag)—— 坐标题下一行的左缘(#186)
|
|
351
|
+
if (legendItems.length) {
|
|
352
|
+
let lx = PAD;
|
|
353
|
+
const ly = legendY - 9; // 色块顶
|
|
354
|
+
for (const [k, label] of legendItems) {
|
|
355
|
+
if (k === 'milestone')
|
|
356
|
+
parts.push(`<polygon points="${r2(lx + 5)},${r2(ly)} ${r2(lx + 10)},${r2(ly + 5)} ${r2(lx + 5)},${r2(ly + 10)} ${r2(lx)},${r2(ly + 5)}" fill="var(--d-ink)"/>`);
|
|
357
|
+
else if (k === 'done')
|
|
358
|
+
parts.push(`<rect x="${r2(lx)}" y="${r2(ly)}" width="10" height="10" rx="2" fill="var(--d-faint)"/>`);
|
|
359
|
+
else if (k === 'active')
|
|
360
|
+
parts.push(`<rect x="${r2(lx)}" y="${r2(ly)}" width="10" height="10" rx="2" fill="${sVar(0)}" stroke="var(--d-ink)" stroke-width="1"/>`);
|
|
361
|
+
else
|
|
362
|
+
parts.push(`<rect x="${r2(lx)}" y="${r2(ly)}" width="10" height="10" rx="2" fill="var(--d-mark)" fill-opacity="0.16" stroke="var(--d-mark)" stroke-width="1.2"/>`);
|
|
363
|
+
lx += 14;
|
|
364
|
+
parts.push(`<text x="${r2(lx)}" y="${r2(legendY)}" font-size="10" fill="var(--d-muted)">${label}</text>`);
|
|
365
|
+
lx += textW(label, 10) + 18;
|
|
360
366
|
}
|
|
361
367
|
}
|
|
362
368
|
const titleSvg = doc.title
|
|
363
|
-
? `<text x="${PAD}" y="${
|
|
369
|
+
? `<text x="${PAD}" y="${r2(titleY)}" font-size="18" font-weight="500" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(doc.title)}</text>`
|
|
364
370
|
: '';
|
|
365
371
|
// interactive:内嵌解析后的任务元数据(explore 详情卡数据源:起止/时长/状态/进度/依赖)
|
|
366
372
|
let gtasksSvg = '';
|
package/dist/render/gantt2.js
CHANGED
|
@@ -7,14 +7,13 @@ import { ApiError } from '../specs/index.js';
|
|
|
7
7
|
import { parseGantt, resolveSpans } from './gantt.js';
|
|
8
8
|
import { resolveSkin } from '../styles/index.js';
|
|
9
9
|
import { clampInt, schemeParam } from './query.js';
|
|
10
|
-
import { esc, r2, svgDoc, textW } from './svg.js';
|
|
10
|
+
import { esc, legendRowY, r2, svgDoc, textW } from './svg.js';
|
|
11
11
|
const DAY = 86400000;
|
|
12
12
|
const PAD = 44;
|
|
13
13
|
const AXIS_H = 34;
|
|
14
14
|
const SECTION_H = 30;
|
|
15
15
|
const ROW_H = 40;
|
|
16
16
|
const BAR_H = 22;
|
|
17
|
-
const LEGEND_H = 46;
|
|
18
17
|
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
19
18
|
/** 状态 → (展示名, 角标字母, 颜色序号) —— 和架构图共用一套色板 */
|
|
20
19
|
const STATUS = {
|
|
@@ -68,10 +67,14 @@ export function renderGantt2Core(o) {
|
|
|
68
67
|
const plotW = Math.max(160, width - x0 - rightPad);
|
|
69
68
|
const xOf = (t) => x0 + ((t - minT) / span) * plotW;
|
|
70
69
|
const rowsTotal = doc.sections.reduce((n, s) => n + s.tasks.length, 0);
|
|
71
|
-
|
|
70
|
+
// 图例:house 规矩 —— 坐**标题下一行、左缘**(#186);原来吊在底部一条带,离条老远。
|
|
71
|
+
// (这张图有标题 ⇒ 图例在标题下面一行;没标题 ⇒ 坐标题本该坐的那一行。)
|
|
72
|
+
const titleY = o.title ? PAD + 16 : undefined;
|
|
73
|
+
const legendY = legendRowY(titleY, PAD);
|
|
74
|
+
const axisTop = legendY + 14;
|
|
72
75
|
const bodyTop = axisTop + AXIS_H;
|
|
73
76
|
const bodyH = doc.sections.length * SECTION_H + rowsTotal * ROW_H;
|
|
74
|
-
const height = Math.max(o.height ?? 0, bodyTop + bodyH +
|
|
77
|
+
const height = Math.max(o.height ?? 0, bodyTop + bodyH + PAD);
|
|
75
78
|
const parts = [];
|
|
76
79
|
// 分区色带(隔区铺)
|
|
77
80
|
let bandY = bodyTop;
|
|
@@ -140,11 +143,11 @@ export function renderGantt2Core(o) {
|
|
|
140
143
|
ri++;
|
|
141
144
|
}
|
|
142
145
|
});
|
|
143
|
-
// 图例(架构图同款:色块 + 名字 + 个数)
|
|
146
|
+
// 图例(架构图同款:色块 + 名字 + 个数)—— 坐标题下一行的左缘(#186)
|
|
144
147
|
const legendTypes = STATUS_ORDER.filter((st) => counts.has(st));
|
|
145
148
|
if (legendTypes.length) {
|
|
146
149
|
let lx = PAD;
|
|
147
|
-
const ly =
|
|
150
|
+
const ly = legendY;
|
|
148
151
|
const legend = [];
|
|
149
152
|
for (const st of legendTypes) {
|
|
150
153
|
const meta = STATUS[st];
|
|
@@ -157,7 +160,7 @@ export function renderGantt2Core(o) {
|
|
|
157
160
|
parts.push(`<g data-legend="status">${legend.join('')}</g>`);
|
|
158
161
|
}
|
|
159
162
|
const titleSvg = o.title
|
|
160
|
-
? `<text x="${PAD}" y="${
|
|
163
|
+
? `<text x="${PAD}" y="${r2(titleY)}" font-size="19" font-weight="600" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(o.title)}</text>`
|
|
161
164
|
: '';
|
|
162
165
|
const defs = `<defs><marker id="g2-arrow" viewBox="0 0 10 10" refX="8.5" refY="5" markerWidth="6" markerHeight="6" orient="auto-start-reverse"><path d="M0 0L10 5L0 10z" fill="var(--d-border)"/></marker></defs>`;
|
|
163
166
|
const spec = { type: 'gantt2', title: o.title, sections: doc.sections };
|
package/dist/render/hbar-spec.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// diverging 蝴蝶图(左新建右关闭)/ lift 实色基础分+斜纹增益。
|
|
3
3
|
import { ApiError } from '../specs/index.js';
|
|
4
4
|
import { TAB, card } from '../theme.js';
|
|
5
|
-
import { esc, fmtFull, fmtNum, niceTicks, onVar, r2, refLineLayer, sVar, textW, trunc, svgDoc } from './svg.js';
|
|
5
|
+
import { esc, fmtFull, fmtNum, legendRowY, niceTicks, onVar, r2, refLineLayer, sVar, textW, trunc, svgDoc } from './svg.js';
|
|
6
6
|
import { fitCut } from './measure.js';
|
|
7
7
|
import { renderHBar } from './hbar.js';
|
|
8
8
|
const tipOf = (o, text) => (o.interactive ? ` data-tip="${esc(text)}"` : '');
|
|
@@ -195,16 +195,19 @@ export function renderHbarDiverging(spec, o) {
|
|
|
195
195
|
// 行名在 0.0707w..0.2707w 之间(其后是左值):放不下按预算截短(fs16)
|
|
196
196
|
const lblCut = fitCut(spec.labels, 12, width * 0.2707 - width * 0.0707 - 10, 16);
|
|
197
197
|
const parts = [card(width, height, true)];
|
|
198
|
-
//
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
198
|
+
// 图例:house 规矩 —— **左缘**、坐顶那一行(#186)。
|
|
199
|
+
// (原来在中间偏右:离左边的行名列老远,看着像另一张图的图例;
|
|
200
|
+
// 也独占一条带,标题跟图例离得远。)
|
|
201
|
+
// 这张图没有标题 ⇒ 图例坐标题本该坐的那一行(legendRowY(undefined))。
|
|
202
|
+
const lgY = legendRowY(undefined);
|
|
203
|
+
const lgX = width * 0.0707; // 与行名列左缘对齐
|
|
204
|
+
const lgGap = Math.max(58, textW(trunc(left.name, 12), 13) + 34);
|
|
202
205
|
const lg0 = o.interactive ? ' data-legend="0"' : '';
|
|
203
206
|
const lg1 = o.interactive ? ' data-legend="1"' : '';
|
|
204
|
-
parts.push(`<circle cx="${r2(
|
|
205
|
-
parts.push(`<text x="${r2(
|
|
206
|
-
parts.push(`<circle cx="${r2(
|
|
207
|
-
parts.push(`<text x="${r2(
|
|
207
|
+
parts.push(`<circle cx="${r2(lgX)}" cy="${r2(lgY - 4)}" r="5" fill="var(--d-s1)"${lg0}/>`);
|
|
208
|
+
parts.push(`<text x="${r2(lgX + 16)}" y="${r2(lgY)}" font-size="13" fill="var(--d-muted)"${lg0}>${esc(trunc(left.name, 12))}</text>`);
|
|
209
|
+
parts.push(`<circle cx="${r2(lgX + lgGap)}" cy="${r2(lgY - 4)}" r="5" fill="var(--d-s2)"${lg1}/>`);
|
|
210
|
+
parts.push(`<text x="${r2(lgX + lgGap + 16)}" y="${r2(lgY)}" font-size="13" fill="var(--d-muted)"${lg1}>${esc(trunc(right.name, 12))}</text>`);
|
|
208
211
|
spec.labels.forEach((l, i) => {
|
|
209
212
|
const y = top + i * pitch;
|
|
210
213
|
const lw = (half * left.values[i]) / max;
|
|
@@ -230,6 +233,9 @@ export function renderHbarLift(spec, o) {
|
|
|
230
233
|
// 整图自适应:行少时贴内容收缩(规范行距 43/条高 22/底带 96),宽度等比跟随
|
|
231
234
|
const shrunk = o.autoHeight && n <= 10; // 规范行数(10)也走绝对几何:比例压缩会给 39.8≠43
|
|
232
235
|
if (shrunk) {
|
|
236
|
+
// 画布尺寸照旧(规范 960×560):图例搬去顶上一行后底部会空出一段 ——
|
|
237
|
+
// 但画布一缩,宽度跟着缩 ⇒ 行名列跟着窄 ⇒ 名字被截断(house 规矩:文字别截断)✗
|
|
238
|
+
// ⇒ 画布不动,空出来的那段由展示层贴紧内容时裁掉。
|
|
233
239
|
height = Math.round(Math.max(200, 55 + (n - 1) * 43 + 22 + 96)); // n=10 验算:560 ✓(规范行距43/条高22)
|
|
234
240
|
if (o.autoWidth)
|
|
235
241
|
width = Math.round(Math.max(400, o.width * (height / o.height)));
|
|
@@ -276,11 +282,15 @@ export function renderHbarLift(spec, o) {
|
|
|
276
282
|
}
|
|
277
283
|
parts.push(`<text x="${r2(x0 + tw + 8)}" y="${r2(y + barH * 0.73)}" font-size="13" fill="var(--d-muted)" style="${TAB}">${fmtFull(total)} +${fmtFull(lift.values[i])}</text>`);
|
|
278
284
|
});
|
|
279
|
-
//
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
+
// 图例:house 规矩 —— 坐**顶**那一行、左缘(#186)。
|
|
286
|
+
// (原来吊在底部一条带:离条老远,像脚注;实色/斜纹各是什么,读者得往回翻。)
|
|
287
|
+
// 这张图没有标题 ⇒ 图例坐标题本该坐的那一行。
|
|
288
|
+
const lgY = legendRowY(undefined);
|
|
289
|
+
const lgX = width * 0.0292; // 与名次列左缘对齐
|
|
290
|
+
const lgGap = Math.max(52, textW(trunc(base.name, 12), 11) + 30);
|
|
291
|
+
parts.push(`<rect x="${r2(lgX)}" y="${r2(lgY - 9)}" width="10" height="10" rx="2" fill="${baseColor}"/>`);
|
|
292
|
+
parts.push(`<text x="${r2(lgX + 16)}" y="${r2(lgY)}" font-size="11" fill="var(--d-muted)">${esc(trunc(base.name, 12))}</text>`);
|
|
293
|
+
parts.push(`<rect x="${r2(lgX + lgGap)}" y="${r2(lgY - 9)}" width="10" height="10" rx="2" fill="url(#dgl-hatch)" stroke="${baseColor}"/>`);
|
|
294
|
+
parts.push(`<text x="${r2(lgX + lgGap + 16)}" y="${r2(lgY)}" font-size="11" fill="var(--d-muted)">${esc(trunc(lift.name, 12))}</text>`);
|
|
285
295
|
return svgDoc(width, height, 'hbar', version, spec.title ?? 'ranking', spec, skin, parts.join(''));
|
|
286
296
|
}
|
|
@@ -26,8 +26,10 @@ export declare const SERIF_STACK = "'Times New Roman',Times,Georgia,serif";
|
|
|
26
26
|
* 垫一层底色 —— 图是独立的一张图,得有底,不然在白底页面上看不见深色的线。
|
|
27
27
|
*
|
|
28
28
|
* 底色垫在**最下面**:插到 <svg> 开头(第一个子元素之前)。
|
|
29
|
+
*
|
|
30
|
+
* @param y 画布左上角的 y(viewBox 的原点不是 0 时,底要从那儿开始铺)
|
|
29
31
|
*/
|
|
30
|
-
export declare function withBackground(svg: string, theme: SvgTheme, w: number, h: number): string;
|
|
32
|
+
export declare function withBackground(svg: string, theme: SvgTheme, w: number, h: number, y?: number): string;
|
|
31
33
|
/**
|
|
32
34
|
* 库里写死的颜色换成我们这套。
|
|
33
35
|
*
|
|
@@ -36,9 +36,11 @@ const r2 = (v) => Math.round(v * 100) / 100;
|
|
|
36
36
|
* 垫一层底色 —— 图是独立的一张图,得有底,不然在白底页面上看不见深色的线。
|
|
37
37
|
*
|
|
38
38
|
* 底色垫在**最下面**:插到 <svg> 开头(第一个子元素之前)。
|
|
39
|
+
*
|
|
40
|
+
* @param y 画布左上角的 y(viewBox 的原点不是 0 时,底要从那儿开始铺)
|
|
39
41
|
*/
|
|
40
|
-
export function withBackground(svg, theme, w, h) {
|
|
41
|
-
const rect = `<rect x="0" y="
|
|
42
|
+
export function withBackground(svg, theme, w, h, y = 0) {
|
|
43
|
+
const rect = `<rect x="0" y="${r2(y)}" width="${r2(w)}" height="${r2(h)}" fill="${theme.bg}"/>`;
|
|
42
44
|
const at = svg.indexOf('>');
|
|
43
45
|
if (at < 0)
|
|
44
46
|
return svg;
|
package/dist/render/svg.d.ts
CHANGED
|
@@ -20,6 +20,16 @@ export declare function metaTag(template: string, version: number, spec: unknown
|
|
|
20
20
|
* @param values 图上画出来的数据值(编辑器拿它做吸附候选)
|
|
21
21
|
*/
|
|
22
22
|
export declare function scaleMeta(v0: number, p0: number, v1: number, p1: number, values: number[]): string;
|
|
23
|
+
/**
|
|
24
|
+
* 图例坐哪一行(#186 house 规矩):**标题基线的下一行**,一律**左缘**起排。
|
|
25
|
+
* 没有标题 ⇒ 坐画布上边距那一行(标题本该坐的位置)。
|
|
26
|
+
*
|
|
27
|
+
* 规矩的由来:图例独占一条带(顶部居中一条 / 底部一条)⇒ 标题与图例离得老远,
|
|
28
|
+
* 读者得来回找;贴上标题 ⇒ 一眼看完「这张图讲什么 + 那几色是什么」。
|
|
29
|
+
*
|
|
30
|
+
* @param titleBaseline 标题基线 y;没有标题传 undefined
|
|
31
|
+
*/
|
|
32
|
+
export declare function legendRowY(titleBaseline: number | undefined, pad?: number): number;
|
|
23
33
|
export declare function topLegend(items: readonly {
|
|
24
34
|
name: string;
|
|
25
35
|
color: string;
|
package/dist/render/svg.js
CHANGED
|
@@ -93,6 +93,18 @@ export function scaleMeta(v0, p0, v1, p1, values) {
|
|
|
93
93
|
const json = JSON.stringify({ y: { v0, p0, v1, p1 }, values });
|
|
94
94
|
return `<metadata id="dscale">${esc(json)}</metadata>`;
|
|
95
95
|
}
|
|
96
|
+
/**
|
|
97
|
+
* 图例坐哪一行(#186 house 规矩):**标题基线的下一行**,一律**左缘**起排。
|
|
98
|
+
* 没有标题 ⇒ 坐画布上边距那一行(标题本该坐的位置)。
|
|
99
|
+
*
|
|
100
|
+
* 规矩的由来:图例独占一条带(顶部居中一条 / 底部一条)⇒ 标题与图例离得老远,
|
|
101
|
+
* 读者得来回找;贴上标题 ⇒ 一眼看完「这张图讲什么 + 那几色是什么」。
|
|
102
|
+
*
|
|
103
|
+
* @param titleBaseline 标题基线 y;没有标题传 undefined
|
|
104
|
+
*/
|
|
105
|
+
export function legendRowY(titleBaseline, pad = 24) {
|
|
106
|
+
return titleBaseline === undefined ? pad + 14 : titleBaseline + 22;
|
|
107
|
+
}
|
|
96
108
|
// 顶部图例:anchor='left' 从左往右(shadcn 圆点) | 'right' 从右往左(Evil 方块)
|
|
97
109
|
// item.index is only passed by interactive embedders: the legend item gets a
|
|
98
110
|
// data-legend attribute plus an invisible hit rect (for click-to-toggle series).
|
package/dist/render/wavedrom.js
CHANGED
|
@@ -74,10 +74,10 @@ export async function renderWavedrom(src, opts = {}) {
|
|
|
74
74
|
// 结构色换掉(波形的数据色不动):#000 是笔画/文字,#fff 是波形块的底
|
|
75
75
|
out = out.replace(/<style([^>]*)>([\s\S]*?)<\/style>/g, (_m, attrs, css) => {
|
|
76
76
|
let themed = css
|
|
77
|
-
.replace(/stroke
|
|
78
|
-
.replace(/color
|
|
79
|
-
.replace(/fill
|
|
80
|
-
.replace(/fill
|
|
77
|
+
.replace(/stroke:\s*#000\b/g, `stroke:${theme.ink}`)
|
|
78
|
+
.replace(/color:\s*#000\b/g, `color:${theme.ink}`)
|
|
79
|
+
.replace(/fill:\s*#000\b/g, `fill:${theme.ink}`)
|
|
80
|
+
.replace(/fill:\s*#fff;/g, `fill:${theme.panel};`);
|
|
81
81
|
// 文字没写 fill(默认黑)—— 给一条
|
|
82
82
|
themed = themed.replace(/text\{/, `text{fill:${theme.ink};`);
|
|
83
83
|
// 皮肤那个深蓝(信号名用它)在暗色底上太沉 —— 换成站上的强调色
|
|
@@ -88,6 +88,18 @@ export async function renderWavedrom(src, opts = {}) {
|
|
|
88
88
|
// wavedrom 的画法全写在 CSS 类里(.s1/.s2/…)—— 样式块会被出图那道工序剥掉,
|
|
89
89
|
// 所以先内联到元素上:图就自足了(没有 CSS),浏览器和 PNG 一个样。
|
|
90
90
|
out = inlineCss(out);
|
|
91
|
+
// 库还有一批评色**不写在样式表里**,是写死在属性/内联样式里的(#188):
|
|
92
|
+
// - 分段模拟波的折线:style="fill:none;stroke:#000;…"(render-piece-wise.js 里写死)
|
|
93
|
+
// - 位域图的格线:<g stroke="black" …>
|
|
94
|
+
// 这些是**结构色**,得跟主题走 —— 不然暗底上是纯黑,等于没画。
|
|
95
|
+
out = out
|
|
96
|
+
.replace(/stroke:\s*#000\b/gi, `stroke:${theme.ink}`)
|
|
97
|
+
.replace(/\bstroke="black"/gi, `stroke="${theme.line}"`)
|
|
98
|
+
.replace(/\bfill="black"/gi, `fill="${theme.ink}"`)
|
|
99
|
+
.replace(/\bcolor="black"/gi, `color="${theme.ink}"`);
|
|
100
|
+
// 没写填色的文字(位域图的位号/字段名):库靠样式表里的 text{} 填黑 ——
|
|
101
|
+
// 服务端没有那张样式表(样式表是浏览器那边注入的)⇒ 字是 SVG 默认的黑 ⇒ 暗底上等于没有。
|
|
102
|
+
out = out.replace(/<text\b([^>]*)>/g, (m, attrs) => /\bfill\s*[:=]/.test(attrs) ? m : `<text${attrs} fill="${theme.ink}">`);
|
|
91
103
|
// 数据块(head/body/tail)的颜色:皮肤的一个颜色类会**同时**用在
|
|
92
104
|
// 「波形的 0 块」和「数据块」上(.s7 是白底)—— 波形 0 块在暗色底上要换成卡片色,
|
|
93
105
|
// 但数据块得留住自己的颜色(官方演示里 head/body/tail 是彩色的)。
|
|
@@ -105,9 +117,134 @@ export async function renderWavedrom(src, opts = {}) {
|
|
|
105
117
|
if (scheme === 'dark')
|
|
106
118
|
out = out.replace(/#0041c4/g, theme.accent);
|
|
107
119
|
out = withFont(out);
|
|
108
|
-
|
|
109
|
-
|
|
120
|
+
// 分段模拟波(pw):画布要撑到能装下整条路径 —— 见 fitPieceWise()
|
|
121
|
+
const fitted = fitPieceWise(out, size);
|
|
122
|
+
out = fitted.svg;
|
|
123
|
+
out = withBackground(out, theme, fitted.w, fitted.h);
|
|
124
|
+
out = setSize(out, fitted.w, fitted.h);
|
|
110
125
|
out = ensureSvgNs(out);
|
|
111
126
|
out = ensureViewBox(out);
|
|
127
|
+
// viewBox 跟着画布走(内容整体往下挪了 dy ⇒ 画布要长出那一截)
|
|
128
|
+
out = out.replace(/viewBox="[^"]*"/, `viewBox="0 0 ${fitted.w} ${fitted.h}"`);
|
|
129
|
+
if (fitted.dy)
|
|
130
|
+
out = shiftContent(out, fitted.dy);
|
|
112
131
|
return out;
|
|
113
132
|
}
|
|
133
|
+
/** 每条路径命令吃几个数 */
|
|
134
|
+
const CMD_ARGS = { m: 2, l: 2, t: 2, h: 1, v: 1, q: 4, c: 6, s: 4, a: 7 };
|
|
135
|
+
/**
|
|
136
|
+
* 路径的坐标范围(相对它的父组)。
|
|
137
|
+
*
|
|
138
|
+
* 只求「画到哪里」,不求画得长什么样 ⇒ 曲线拿控制点当极值(略大,宁多勿少)。
|
|
139
|
+
*/
|
|
140
|
+
function pathExtent(d) {
|
|
141
|
+
const toks = d.match(/[a-zA-Z]|-?\d*\.?\d+(?:e[-+]?\d+)?/g) ?? [];
|
|
142
|
+
let x = 0;
|
|
143
|
+
let y = 0;
|
|
144
|
+
let x0 = 0;
|
|
145
|
+
let y0 = 0;
|
|
146
|
+
let x1 = 0;
|
|
147
|
+
let y1 = 0;
|
|
148
|
+
let seen = false;
|
|
149
|
+
const grow = (px, py) => {
|
|
150
|
+
if (!seen) {
|
|
151
|
+
x0 = x1 = px;
|
|
152
|
+
y0 = y1 = py;
|
|
153
|
+
seen = true;
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
x0 = Math.min(x0, px);
|
|
157
|
+
x1 = Math.max(x1, px);
|
|
158
|
+
y0 = Math.min(y0, py);
|
|
159
|
+
y1 = Math.max(y1, py);
|
|
160
|
+
};
|
|
161
|
+
let cmd = '';
|
|
162
|
+
let i = 0;
|
|
163
|
+
while (i < toks.length) {
|
|
164
|
+
if (/[a-zA-Z]/.test(toks[i])) {
|
|
165
|
+
cmd = toks[i].toLowerCase();
|
|
166
|
+
i++;
|
|
167
|
+
}
|
|
168
|
+
const n = CMD_ARGS[cmd];
|
|
169
|
+
if (n === undefined)
|
|
170
|
+
break;
|
|
171
|
+
const args = toks.slice(i, i + n).map(Number);
|
|
172
|
+
if (args.length < n || args.some(Number.isNaN))
|
|
173
|
+
break;
|
|
174
|
+
i += n;
|
|
175
|
+
if (cmd === 'm') {
|
|
176
|
+
x = args[0];
|
|
177
|
+
y = args[1];
|
|
178
|
+
grow(x, y);
|
|
179
|
+
}
|
|
180
|
+
else if (cmd === 'l' || cmd === 't') {
|
|
181
|
+
x += args[0];
|
|
182
|
+
y += args[1];
|
|
183
|
+
grow(x, y);
|
|
184
|
+
}
|
|
185
|
+
else if (cmd === 'h') {
|
|
186
|
+
x += args[0];
|
|
187
|
+
grow(x, y);
|
|
188
|
+
}
|
|
189
|
+
else if (cmd === 'v') {
|
|
190
|
+
y += args[0];
|
|
191
|
+
grow(x, y);
|
|
192
|
+
}
|
|
193
|
+
else if (cmd === 'q' || cmd === 's') {
|
|
194
|
+
grow(x + args[0], y + args[1]);
|
|
195
|
+
x += args[2];
|
|
196
|
+
y += args[3];
|
|
197
|
+
grow(x, y);
|
|
198
|
+
}
|
|
199
|
+
else if (cmd === 'c') {
|
|
200
|
+
grow(x + args[0], y + args[1]);
|
|
201
|
+
grow(x + args[2], y + args[3]);
|
|
202
|
+
x += args[4];
|
|
203
|
+
y += args[5];
|
|
204
|
+
grow(x, y);
|
|
205
|
+
}
|
|
206
|
+
else if (cmd === 'a') {
|
|
207
|
+
x += args[5];
|
|
208
|
+
y += args[6];
|
|
209
|
+
grow(x, y);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return seen ? { x0, y0, x1, y1 } : null;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* 分段模拟波(pw):把画布撑到能装下整条路径。
|
|
216
|
+
*
|
|
217
|
+
* 库把 d 路径按「两格宽、一格高」缩放后画在 lane 的起点,但 lane 的宽度是按
|
|
218
|
+
* wave 的**字符个数**算的 —— pw 没有字符 ⇒ lane 只有一格宽 ⇒ 路径大半落在
|
|
219
|
+
* 画布外(库自己的短处:render-piece-wise.js 只管画,不管留位)。
|
|
220
|
+
*
|
|
221
|
+
* 我们不改库的算法,只把画布撑开:路径画到哪,画布就到哪。
|
|
222
|
+
*/
|
|
223
|
+
function fitPieceWise(svg, size) {
|
|
224
|
+
// pw 的路径长这样:<g transform="translate(0,Y)"><path style="fill:none;stroke:…;stroke-width:1px;" d="…"/>
|
|
225
|
+
const m = /<g transform="translate\(0,(-?[\d.]+)\)">\s*<path[^>]*?stroke-width:1px[^>]*?d="([^"]+)"/.exec(svg);
|
|
226
|
+
if (!m)
|
|
227
|
+
return { svg, w: size.w, h: size.h, dy: 0 };
|
|
228
|
+
const ext = pathExtent(m[2]);
|
|
229
|
+
if (!ext)
|
|
230
|
+
return { svg, w: size.w, h: size.h, dy: 0 };
|
|
231
|
+
// lane 组自己的位移(标签列占多宽,全看它)
|
|
232
|
+
const lane = /<g transform="translate\((-?[\d.]+),(-?[\d.]+)\)" id="lanes/.exec(svg);
|
|
233
|
+
const lx = lane ? Number(lane[1]) : 0;
|
|
234
|
+
const ly = lane ? Number(lane[2]) : 0;
|
|
235
|
+
const gy = Number(m[1]);
|
|
236
|
+
const ax1 = lx + ext.x1;
|
|
237
|
+
const ay0 = ly + gy + ext.y0;
|
|
238
|
+
const ay1 = ly + gy + ext.y1;
|
|
239
|
+
// 路径可能跑到 y=0 之上 ⇒ 内容整体往下挪 dy,画布跟着长出那一截
|
|
240
|
+
const dy = Math.max(0, -ay0);
|
|
241
|
+
const w = Math.max(size.w, Math.ceil(ax1 + 6));
|
|
242
|
+
const h = Math.max(size.h, Math.ceil(ay1 + 4 + dy));
|
|
243
|
+
return { svg, w, h, dy };
|
|
244
|
+
}
|
|
245
|
+
/** 画布往上挪了 dy ⇒ 内容整体往下挪,不然上面那截看不见 */
|
|
246
|
+
function shiftContent(svg, dy) {
|
|
247
|
+
return svg
|
|
248
|
+
.replace(/<g id="(waves_[^"]*)">/, `<g transform="translate(0,${dy})" id="$1">`)
|
|
249
|
+
.replace(/<g id="(groups_[^"]*)">/, `<g transform="translate(0,${dy})" id="$1">`);
|
|
250
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-cmd/draw",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "SVG chart rendering as a service, a library, and a CLI — pie/bar/hbar/line/area/radar/gauge/scatter/combo/sankey/qr, same params everywhere",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|