@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.
- package/README.md +80 -166
- package/dist/lib.d.ts +27 -1
- package/dist/lib.js +39 -1
- package/dist/node/cli.js +109 -11
- package/dist/render/anno.d.ts +50 -0
- package/dist/render/anno.js +589 -0
- package/dist/render/arch.d.ts +106 -0
- package/dist/render/arch.js +1011 -0
- package/dist/render/archmap.d.ts +157 -0
- package/dist/render/archmap.js +683 -0
- package/dist/render/area.js +20 -14
- package/dist/render/bar-grouped.js +8 -5
- package/dist/render/bar-stacked.js +11 -7
- package/dist/render/bar.js +8 -3
- package/dist/render/butterfly.d.ts +3 -0
- package/dist/render/butterfly.js +355 -0
- package/dist/render/calendar.d.ts +6 -0
- package/dist/render/calendar.js +448 -0
- package/dist/render/combo.js +16 -8
- package/dist/render/compass.d.ts +6 -0
- package/dist/render/compass.js +77 -0
- package/dist/render/dispatch.js +26 -1
- package/dist/render/echarts-ssr.d.ts +51 -0
- package/dist/render/echarts-ssr.js +511 -0
- package/dist/render/gantt.d.ts +46 -0
- package/dist/render/gantt.js +415 -0
- package/dist/render/gantt2.d.ts +24 -0
- package/dist/render/gantt2.js +198 -0
- package/dist/render/hbar.js +8 -4
- package/dist/render/icons.d.ts +3 -0
- package/dist/render/icons.js +78 -0
- package/dist/render/kinds.d.ts +11 -0
- package/dist/render/kinds.js +176 -0
- package/dist/render/ladder.d.ts +21 -0
- package/dist/render/ladder.js +230 -0
- package/dist/render/layout.d.ts +46 -0
- package/dist/render/layout.js +57 -0
- package/dist/render/line-multi.js +7 -5
- package/dist/render/line.js +9 -5
- package/dist/render/maps.d.ts +46 -0
- package/dist/render/maps.js +237 -0
- package/dist/render/measure.d.ts +0 -1
- package/dist/render/measure.js +0 -1
- package/dist/render/mermaid.d.ts +30 -0
- package/dist/render/mermaid.js +277 -0
- package/dist/render/mindmap.d.ts +10 -0
- package/dist/render/mindmap.js +388 -0
- package/dist/render/orgicon.d.ts +12 -0
- package/dist/render/orgicon.js +31 -0
- package/dist/render/pie.js +1 -1
- package/dist/render/png.d.ts +21 -0
- package/dist/render/png.js +109 -0
- package/dist/render/quadrant.d.ts +6 -0
- package/dist/render/quadrant.js +201 -0
- package/dist/render/query.d.ts +2 -0
- package/dist/render/query.js +108 -1
- package/dist/render/route.d.ts +86 -0
- package/dist/render/route.js +1077 -0
- package/dist/render/scatter.js +87 -12
- package/dist/render/seq.d.ts +41 -0
- package/dist/render/seq.js +248 -0
- package/dist/render/svg.d.ts +34 -2
- package/dist/render/svg.js +159 -20
- package/dist/render/table.d.ts +30 -0
- package/dist/render/table.js +134 -0
- package/dist/render/tokei.d.ts +18 -0
- package/dist/render/tokei.js +230 -0
- package/dist/render/uc.d.ts +58 -0
- package/dist/render/uc.js +319 -0
- package/dist/render/units.d.ts +32 -0
- package/dist/render/units.js +84 -0
- package/dist/render/vendor/au.js +10 -0
- package/dist/render/vendor/br.js +10 -0
- package/dist/render/vendor/china.js +11 -0
- package/dist/render/vendor/de.js +10 -0
- package/dist/render/vendor/eu.js +10 -0
- package/dist/render/vendor/japan.js +10 -0
- package/dist/render/vendor/kr.js +10 -0
- package/dist/render/vendor/mermaid-flow-parser.js +1164 -0
- package/dist/render/vendor/mermaid-gantt-parser.js +810 -0
- package/dist/render/vendor/us-states.js +11 -0
- package/dist/render/vendor/world-geo.d.ts +22 -0
- package/dist/render/vendor/world-geo.js +9 -0
- package/dist/render/vendor/world.d.ts +13 -0
- package/dist/render/vendor/world.js +12 -0
- package/dist/specs/base.d.ts +16 -0
- package/dist/specs/base.js +34 -1
- package/dist/specs/butterfly.d.ts +46 -0
- package/dist/specs/butterfly.js +241 -0
- package/dist/specs/calendar.d.ts +90 -0
- package/dist/specs/calendar.js +296 -0
- package/dist/specs/compass.d.ts +27 -0
- package/dist/specs/compass.js +111 -0
- package/dist/specs/index.d.ts +5 -0
- package/dist/specs/index.js +5 -0
- package/dist/specs/ladder.d.ts +43 -0
- package/dist/specs/ladder.js +261 -0
- package/dist/specs/quadrant.d.ts +41 -0
- package/dist/specs/quadrant.js +200 -0
- package/dist/specs/scatter.d.ts +14 -8
- package/dist/specs/scatter.js +54 -5
- package/dist/specs/table.d.ts +24 -0
- package/dist/specs/table.js +154 -0
- package/dist/store.d.ts +1 -1
- package/dist/store.js +1 -1
- package/dist/styles/butterfly.d.ts +2 -0
- package/dist/styles/butterfly.js +74 -0
- package/dist/styles/calendar.d.ts +2 -0
- package/dist/styles/calendar.js +51 -0
- package/dist/styles/compass.d.ts +2 -0
- package/dist/styles/compass.js +19 -0
- package/dist/styles/fonts-banner.d.ts +5 -0
- package/dist/styles/fonts-banner.js +9 -0
- package/dist/styles/fonts-cjk.d.ts +2 -0
- package/dist/styles/fonts-cjk.js +6 -0
- package/dist/styles/fonts-ttf.d.ts +2 -0
- package/dist/styles/fonts-ttf.js +6 -0
- package/dist/styles/index.js +13 -0
- package/dist/styles/ladder.d.ts +3 -0
- package/dist/styles/ladder.js +66 -0
- package/dist/styles/quadrant.d.ts +2 -0
- package/dist/styles/quadrant.js +19 -0
- package/dist/styles/table.d.ts +2 -0
- package/dist/styles/table.js +12 -0
- package/dist/styles/types.d.ts +1 -0
- package/dist/styles/types.js +1 -0
- package/dist/theme.d.ts +1 -1
- package/dist/theme.js +3 -4
- package/dist/types/dagre.d.ts +39 -0
- package/dist/types/dagre.js +3 -0
- package/package.json +28 -3
|
@@ -0,0 +1,415 @@
|
|
|
1
|
+
// mermaid gantt 渲染器(#20 v1.1):vendored jison 解析 + 自绘时间轴/行条/里程碑。
|
|
2
|
+
// 入口 /draw/gantt?m=<源码>(首行 gantt 关键字可省;/draw/mermaid 的 gantt 分派保留兼容)。
|
|
3
|
+
// 支持:gantt 头、title、dateFormat YYYY-MM-DD(缺省)、section 分组、
|
|
4
|
+
// 任务 flags(done/active/crit/milestone)、显式日期或 after 链、时长 Nd/Nw/Nh、today 线。
|
|
5
|
+
// 不做(明确口径):依赖箭头、excludes 周末剔除、多 dateFormat 模板。
|
|
6
|
+
import { ApiError } from '../specs/index.js';
|
|
7
|
+
import { resolveVersion } from '../version.js';
|
|
8
|
+
import { resolveSkin } from '../styles/index.js';
|
|
9
|
+
import { esc, r2, svgDoc, textW, sVar } from './svg.js';
|
|
10
|
+
import { schemeParam } from './query.js';
|
|
11
|
+
import { mermaidGanttParser } from './vendor/mermaid-gantt-parser.js';
|
|
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');
|
|
13
|
+
const DAY = 86400000;
|
|
14
|
+
const parseISO = (s) => {
|
|
15
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s.trim());
|
|
16
|
+
if (!m)
|
|
17
|
+
throw new ApiError(400, `gantt: cannot parse date "${s}" — v1 supports YYYY-MM-DD. Example: /draw/gantt?${N_EXAMPLE}`);
|
|
18
|
+
return Date.UTC(+m[1], +m[2] - 1, +m[3]);
|
|
19
|
+
};
|
|
20
|
+
const durMs = (s) => {
|
|
21
|
+
const m = /^(\d+(?:\.\d+)?)([hdw])$/.exec(s.trim());
|
|
22
|
+
if (!m)
|
|
23
|
+
throw new ApiError(400, `gantt: cannot parse duration "${s}" — use like 3d/2w/8h`);
|
|
24
|
+
const unit = { h: 3600000, d: DAY, w: 7 * DAY };
|
|
25
|
+
return +m[1] * unit[m[2]];
|
|
26
|
+
};
|
|
27
|
+
const fmtDay = (t) => {
|
|
28
|
+
const d = new Date(t);
|
|
29
|
+
return `${d.getUTCFullYear()}-${String(d.getUTCMonth() + 1).padStart(2, '0')}-${String(d.getUTCDate()).padStart(2, '0')}`;
|
|
30
|
+
};
|
|
31
|
+
/** 解析 mermaid gantt 源码 → GanttDoc(addTask 收到的是 ":flags, id, start, 时长" 原始串,自行拆) */
|
|
32
|
+
export function parseGantt(text) {
|
|
33
|
+
const doc = { sections: [] };
|
|
34
|
+
let cur;
|
|
35
|
+
const yy = {
|
|
36
|
+
setDiagramTitle(t) { doc.title = t.trim(); },
|
|
37
|
+
setAccTitle() { }, setAccDescription() { }, setClickEvent() { }, setLink() { },
|
|
38
|
+
setAxisFormat() { }, setExcludes() { }, setIncludes() { }, setTodayMarker() { },
|
|
39
|
+
setWeekday() { }, setWeekend() { }, setTickInterval() { }, setTopAxis() { },
|
|
40
|
+
enableInclusiveEndDates() { }, setDisplayMode() { }, setShowTaskTooltip() { },
|
|
41
|
+
setDateFormat() { }, // v1 只认 YYYY-MM-DD,声明了别的按缺省处理
|
|
42
|
+
addSection(name) {
|
|
43
|
+
cur = { name: name.trim() || 'Tasks', tasks: [] };
|
|
44
|
+
doc.sections.push(cur);
|
|
45
|
+
},
|
|
46
|
+
addTask(name, raw) {
|
|
47
|
+
if (!cur) {
|
|
48
|
+
cur = { name: 'Tasks', tasks: [] };
|
|
49
|
+
doc.sections.push(cur);
|
|
50
|
+
}
|
|
51
|
+
const meta = String(raw ?? '').replace(/^:/, '').split(',').map(x => x.trim()).filter(Boolean);
|
|
52
|
+
const flags = [];
|
|
53
|
+
const pos = [];
|
|
54
|
+
let after;
|
|
55
|
+
let progress;
|
|
56
|
+
let consumedFlags = true;
|
|
57
|
+
for (const tk of meta) {
|
|
58
|
+
if (consumedFlags && /^(done|active|crit|critical|milestone)$/i.test(tk)) {
|
|
59
|
+
flags.push(tk.toLowerCase() === 'critical' ? 'crit' : tk.toLowerCase());
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
consumedFlags = false;
|
|
63
|
+
const pm = /^(\d+(?:\.\d+)?)%$/.exec(tk);
|
|
64
|
+
if (pm) {
|
|
65
|
+
progress = Math.min(100, Math.max(0, +pm[1]));
|
|
66
|
+
continue;
|
|
67
|
+
} // 语法扩展:可选完成百分比
|
|
68
|
+
if (tk.startsWith('after ')) {
|
|
69
|
+
after = tk.slice(6).trim();
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
pos.push(tk);
|
|
73
|
+
}
|
|
74
|
+
// 位置参数按顺序取:id、开始、结束/时长。用了 after 时开始由依赖决定,
|
|
75
|
+
// 后面的那个 token 就是时长 —— 否则它会落进「开始」槽被丢掉(任务只剩 1 天)。
|
|
76
|
+
const id = pos[0];
|
|
77
|
+
const start = after === undefined ? pos[1] : undefined;
|
|
78
|
+
const endOrDur = after === undefined ? pos[2] : pos[1];
|
|
79
|
+
cur.tasks.push({ name: name.trim(), id, flags, start, after, endOrDur, ...(progress !== undefined ? { progress } : {}) });
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
mermaidGanttParser.yy = yy;
|
|
83
|
+
try {
|
|
84
|
+
mermaidGanttParser.parse(text);
|
|
85
|
+
}
|
|
86
|
+
catch (e) {
|
|
87
|
+
throw new ApiError(400, `gantt syntax error: ${e instanceof Error ? e.message.split('\n')[0] : String(e)}. Example: /draw/gantt?${N_EXAMPLE}`);
|
|
88
|
+
}
|
|
89
|
+
if (!doc.sections.some(s => s.tasks.length)) {
|
|
90
|
+
throw new ApiError(400, `gantt: no tasks found. Example: /draw/gantt?${N_EXAMPLE}`);
|
|
91
|
+
}
|
|
92
|
+
return doc;
|
|
93
|
+
}
|
|
94
|
+
/** 展开任务:日期解析、after 链、里程碑零宽 → 统一 Span 列表 */
|
|
95
|
+
export function resolveSpans(doc) {
|
|
96
|
+
const byId = new Map();
|
|
97
|
+
doc.sections.forEach(s => s.tasks.forEach(t => { if (t.id)
|
|
98
|
+
byId.set(t.id, t); }));
|
|
99
|
+
const resolved = new Map();
|
|
100
|
+
const resolve = (t) => {
|
|
101
|
+
const key = t.id ?? t.name;
|
|
102
|
+
if (resolved.has(key))
|
|
103
|
+
return resolved.get(key);
|
|
104
|
+
let start;
|
|
105
|
+
if (t.after) {
|
|
106
|
+
const dep = byId.get(t.after);
|
|
107
|
+
if (!dep)
|
|
108
|
+
throw new ApiError(400, `gantt: "after ${t.after}" references unknown task id`);
|
|
109
|
+
start = resolve(dep).end;
|
|
110
|
+
}
|
|
111
|
+
else if (t.start) {
|
|
112
|
+
start = parseISO(t.start);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
throw new ApiError(400, `gantt: task "${t.name}" needs a start date or "after <id>"`);
|
|
116
|
+
}
|
|
117
|
+
let end;
|
|
118
|
+
const dur = t.endOrDur ?? '1d';
|
|
119
|
+
if (/^\d{4}-\d{2}-\d{2}$/.test(dur))
|
|
120
|
+
end = parseISO(dur); // 显式结束日
|
|
121
|
+
else
|
|
122
|
+
end = start + Math.max(durMs(dur), 3600000); // 时长
|
|
123
|
+
if (end < start)
|
|
124
|
+
end = start + DAY;
|
|
125
|
+
const r = { start, end: Math.max(end, start + DAY) };
|
|
126
|
+
resolved.set(key, r);
|
|
127
|
+
return r;
|
|
128
|
+
};
|
|
129
|
+
const spans = [];
|
|
130
|
+
doc.sections.forEach((s, si) => {
|
|
131
|
+
for (const t of s.tasks) {
|
|
132
|
+
const { start, end } = resolve(t);
|
|
133
|
+
const flags = new Set(t.flags);
|
|
134
|
+
const isMs = flags.has('milestone');
|
|
135
|
+
const s2 = isMs ? start : start;
|
|
136
|
+
const e2 = isMs ? start : end;
|
|
137
|
+
spans.push({
|
|
138
|
+
name: t.name, start: s2, end: e2, flags, sectionIdx: si,
|
|
139
|
+
tip: isMs ? `${t.name} · ${fmtDay(start)} · milestone` : `${t.name} · ${fmtDay(start)} → ${fmtDay(end)} · ${Math.round((e2 - s2) / DAY)}d${t.progress !== undefined ? ` · ${Math.round(t.progress)}%` : ''}`,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
return spans;
|
|
144
|
+
}
|
|
145
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
146
|
+
/** 渲染(视觉 2.0,对齐行业惯例):双行轴(月/年带+日/周/月刻度)、实线 hairline 网格、
|
|
147
|
+
* section 交替色带、周末底纹、today 实线;状态色 done=实色灰 / crit=淡红+描边 / active=满色+墨描边。
|
|
148
|
+
* opts.scale 显式时间粒度(缺省按跨度自适应);任务可带语法扩展的完成百分比(条内深色进度段) */
|
|
149
|
+
export function renderGantt(doc, opts) {
|
|
150
|
+
const spans = resolveSpans(doc);
|
|
151
|
+
const minT = Math.min(...spans.map(s => s.start));
|
|
152
|
+
const maxT = Math.max(...spans.map(s => s.end));
|
|
153
|
+
const spanDays = Math.max(1, Math.round((maxT - minT) / DAY));
|
|
154
|
+
const PAD = 26;
|
|
155
|
+
const titleH = doc.title ? 40 : 0;
|
|
156
|
+
const upperH = 20; // 上行轴:月/年带
|
|
157
|
+
const lowerH = 24; // 下行轴:日/周/月刻度
|
|
158
|
+
const axisH = upperH + lowerH;
|
|
159
|
+
let sectionH = 26;
|
|
160
|
+
let rowH = 28;
|
|
161
|
+
let barH = 16;
|
|
162
|
+
let labelW = 0;
|
|
163
|
+
doc.sections.forEach(s => { labelW = Math.max(labelW, textW(s.name, 12) + 18); s.tasks.forEach(t => { labelW = Math.max(labelW, textW(t.name, 12) + 18); }); });
|
|
164
|
+
labelW = Math.min(Math.max(labelW, 150), 300);
|
|
165
|
+
const rows = doc.sections.reduce((a, s) => a + 1 + s.tasks.length, 0);
|
|
166
|
+
// 尺寸语义与图表家族一致:显式 w/h = 精确画布(行高自适应分配,7:4 画框),
|
|
167
|
+
// 缺省 = 自然尺寸(needW/needH 兜底,与旧版逐字节一致)
|
|
168
|
+
const needW = labelW + PAD * 2 + Math.max(spanDays * 14, 240);
|
|
169
|
+
const width = opts.width !== undefined ? Math.max(200, Math.min(2400, opts.width)) : Math.min(Math.max(needW, 560), 2400);
|
|
170
|
+
if (width < labelW + PAD * 2 + 80)
|
|
171
|
+
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(titleH + axisH + rows * rowH + PAD * 2, 200), 2000);
|
|
173
|
+
let topOff = 0;
|
|
174
|
+
if (opts.height !== undefined) {
|
|
175
|
+
const bodyH = opts.height - PAD * 2 - titleH - axisH;
|
|
176
|
+
const unit = Math.min(48, Math.max(22, bodyH / rows));
|
|
177
|
+
rowH = Math.floor(unit); // floor 保证内容不超高;余量进 topOff 垂直居中
|
|
178
|
+
sectionH = Math.max(22, Math.round(unit * 0.92));
|
|
179
|
+
barH = Math.min(20, Math.max(14, Math.round(rowH * 0.57)));
|
|
180
|
+
const contentH = rows * rowH;
|
|
181
|
+
topOff = Math.max(0, Math.round((bodyH - contentH) / 2));
|
|
182
|
+
height = Math.max(height, titleH + axisH + topOff + contentH + PAD * 2); // 行过多时允许超高(画框 contain 兜底)
|
|
183
|
+
}
|
|
184
|
+
const x0 = PAD + labelW;
|
|
185
|
+
const x1 = width - PAD;
|
|
186
|
+
const bodyTop = titleH + axisH;
|
|
187
|
+
const bodyBottom = height - PAD;
|
|
188
|
+
const pxPerDay = (x1 - x0) / spanDays;
|
|
189
|
+
const xOf = (t) => x0 + ((t - minT) / DAY) * pxPerDay;
|
|
190
|
+
// 下行刻度粒度:显式 scale 优先,缺省按跨度自适应(≤35d 日、≤130d 周、否则月)
|
|
191
|
+
const mode = opts.scale ?? (spanDays <= 35 ? 'day' : spanDays <= 130 ? 'week' : 'month');
|
|
192
|
+
const daily = mode === 'day';
|
|
193
|
+
const ticks = [];
|
|
194
|
+
const d0 = new Date(minT);
|
|
195
|
+
const day = new Date(Date.UTC(d0.getUTCFullYear(), d0.getUTCMonth(), d0.getUTCDate()));
|
|
196
|
+
if (daily) {
|
|
197
|
+
// 列太窄时按步长抽稀标签(网格线保留)
|
|
198
|
+
const step = pxPerDay >= 8 ? 1 : Math.ceil(8 / pxPerDay);
|
|
199
|
+
for (let i = 0; i <= spanDays + 1; i++) {
|
|
200
|
+
const t = day.getTime() + i * DAY;
|
|
201
|
+
if (t > maxT)
|
|
202
|
+
break;
|
|
203
|
+
if (i % step === 0)
|
|
204
|
+
ticks.push({ t, label: String(new Date(t).getUTCDate()).padStart(2, '0') });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
else if (mode === 'week') {
|
|
208
|
+
while (day.getUTCDay() !== 1)
|
|
209
|
+
day.setUTCDate(day.getUTCDate() + 1);
|
|
210
|
+
for (let t = day.getTime(); t <= maxT; t += 7 * DAY) {
|
|
211
|
+
const d = new Date(t);
|
|
212
|
+
ticks.push({ t, label: `${MONTHS[d.getUTCMonth()]} ${String(d.getUTCDate()).padStart(2, '0')}` });
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
day.setUTCDate(1);
|
|
217
|
+
for (let t = day.getTime(); t <= maxT; t = new Date(t).getUTCMonth() === 11 ? Date.UTC(new Date(t).getUTCFullYear() + 1, 0, 1) : Date.UTC(new Date(t).getUTCFullYear(), new Date(t).getUTCMonth() + 1, 1)) {
|
|
218
|
+
const d = new Date(t);
|
|
219
|
+
ticks.push({ t, label: `${MONTHS[d.getUTCMonth()]} ${d.getUTCFullYear()}` });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
// 上行带:日/周粒度=自然月,月粒度=自然年(首段/跨年带年份)
|
|
223
|
+
const upperSpans = [];
|
|
224
|
+
{
|
|
225
|
+
const byMonth = mode !== 'month';
|
|
226
|
+
let u = byMonth ? Date.UTC(d0.getUTCFullYear(), d0.getUTCMonth(), 1) : Date.UTC(d0.getUTCFullYear(), 0, 1);
|
|
227
|
+
while (u <= maxT) {
|
|
228
|
+
const d = new Date(u);
|
|
229
|
+
const next = byMonth
|
|
230
|
+
? d.getUTCMonth() === 11 ? Date.UTC(d.getUTCFullYear() + 1, 0, 1) : Date.UTC(d.getUTCFullYear(), d.getUTCMonth() + 1, 1)
|
|
231
|
+
: Date.UTC(d.getUTCFullYear() + 1, 0, 1);
|
|
232
|
+
const label = byMonth
|
|
233
|
+
? d.getUTCMonth() === 0 || upperSpans.length === 0 ? `${MONTHS[d.getUTCMonth()]} ${d.getUTCFullYear()}` : MONTHS[d.getUTCMonth()]
|
|
234
|
+
: String(d.getUTCFullYear());
|
|
235
|
+
upperSpans.push({ start: u, end: next, label });
|
|
236
|
+
u = next;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
const parts = [];
|
|
240
|
+
// 表头带(轴两行+标签列同铺浅底,"表格+时间轴"结构感)+ 标签列|时间线竖分隔
|
|
241
|
+
parts.push(`<rect x="${PAD}" y="${titleH}" width="${r2(x1 - PAD)}" height="${axisH}" fill="var(--d-ink)" fill-opacity="0.03"/>`);
|
|
242
|
+
parts.push(`<line x1="${x0}" y1="${titleH}" x2="${x0}" y2="${bodyBottom}" stroke="var(--d-border)" data-x0="${x0}"/>`);
|
|
243
|
+
// 周末底纹(日粒度、每列足够宽才铺,否则成噪点)
|
|
244
|
+
if (pxPerDay >= 8) {
|
|
245
|
+
for (let t = minT; t <= maxT; t += DAY) {
|
|
246
|
+
const wd = new Date(t).getUTCDay();
|
|
247
|
+
if (wd !== 0 && wd !== 6)
|
|
248
|
+
continue;
|
|
249
|
+
const bx = Math.max(xOf(t), x0);
|
|
250
|
+
const bw = Math.min(xOf(t + DAY), x1) - bx;
|
|
251
|
+
if (bw > 0.5)
|
|
252
|
+
parts.push(`<rect x="${r2(bx)}" y="${bodyTop}" width="${r2(bw)}" height="${r2(bodyBottom - bodyTop)}" fill="var(--d-grid)"/>`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
// section 交替色带(隔区铺,含标签列,行追踪)
|
|
256
|
+
let bandY = bodyTop + topOff;
|
|
257
|
+
doc.sections.forEach((sec, si) => {
|
|
258
|
+
const h = sectionH + sec.tasks.length * rowH;
|
|
259
|
+
if (si % 2 === 1)
|
|
260
|
+
parts.push(`<rect x="${PAD}" y="${r2(bandY)}" width="${r2(x1 - PAD)}" height="${r2(h)}" fill="var(--d-ink)" fill-opacity="0.035"/>`);
|
|
261
|
+
bandY += h;
|
|
262
|
+
});
|
|
263
|
+
// 下行刻度 + 细网格(实线 hairline;日粒度下周末数字弱化;无标题时右缘让位 Edit 角标)
|
|
264
|
+
for (const tk of ticks) {
|
|
265
|
+
const x = Math.min(xOf(tk.t), x1);
|
|
266
|
+
parts.push(`<line x1="${r2(x)}" y1="${bodyTop}" x2="${r2(x)}" y2="${bodyBottom}" stroke="var(--d-grid)"/>`);
|
|
267
|
+
if (!doc.title && x > width - 140)
|
|
268
|
+
continue; // 角标胶囊区,只画线不画字
|
|
269
|
+
const weekend = daily && (new Date(tk.t).getUTCDay() === 0 || new Date(tk.t).getUTCDay() === 6);
|
|
270
|
+
parts.push(`<text x="${r2(x)}" y="${titleH + upperH + 16}" text-anchor="middle" font-size="${daily ? 10 : 11}" fill="${weekend ? 'var(--d-faint)' : 'var(--d-muted)'}">${esc(tk.label)}</text>`);
|
|
271
|
+
}
|
|
272
|
+
// 上行带标签 + 加强边界线(画在细网格之上)+ 上下轴分隔线
|
|
273
|
+
for (const us of upperSpans) {
|
|
274
|
+
const bx = Math.min(Math.max(xOf(us.start), x0), x1);
|
|
275
|
+
const cx = (Math.max(xOf(us.start), x0) + Math.min(xOf(us.end), x1)) / 2;
|
|
276
|
+
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="${titleH + 14}" text-anchor="middle" font-size="11" font-weight="500" fill="var(--d-text)">${esc(us.label)}</text>`);
|
|
278
|
+
}
|
|
279
|
+
parts.push(`<line x1="${x0}" y1="${titleH + upperH}" x2="${x1}" y2="${titleH + upperH}" stroke="var(--d-border)"/>`);
|
|
280
|
+
parts.push(`<line x1="${x0}" y1="${bodyBottom}" x2="${x1}" y2="${bodyBottom}" stroke="var(--d-baseline)" stroke-width="1"/>`);
|
|
281
|
+
// 行与时间条(interactive:数据带 data-row=行号/data-node=任务 id/data-after=前置,
|
|
282
|
+
// 行尾铺透明命中 rect——悬停/点击的命中区是整行而非 16px 条,与竞品一致。
|
|
283
|
+
// 标签文本统一收进 #gl 组:explore 页克隆裁剪做 sticky 任务列)
|
|
284
|
+
let y = bodyTop + topOff;
|
|
285
|
+
let ri = 0;
|
|
286
|
+
const labels = [];
|
|
287
|
+
const nSeries = Math.max(1, opts.skin.series.length);
|
|
288
|
+
doc.sections.forEach((sec, si) => {
|
|
289
|
+
labels.push(`<text x="${PAD}" y="${r2(y + 17)}" font-size="12" font-weight="600" fill="var(--d-ink)">${esc(sec.name)}</text>`);
|
|
290
|
+
y += sectionH;
|
|
291
|
+
const color = sVar(si % nSeries);
|
|
292
|
+
for (const t of sec.tasks) {
|
|
293
|
+
const sp = spans.find(s => s.name === t.name && s.sectionIdx === si);
|
|
294
|
+
const rowAttr = opts.interactive ? ` data-row="${ri}"` : '';
|
|
295
|
+
const nodeAttr = opts.interactive
|
|
296
|
+
? ` data-node="${esc(t.id ?? t.name)}"${t.after ? ` data-after="${esc(t.after)}"` : ''}${rowAttr} data-tip="${esc(sp.tip)}"`
|
|
297
|
+
: '';
|
|
298
|
+
labels.push(`<text x="${r2(x0 - 10)}" y="${r2(y + rowH / 2 + 4)}" text-anchor="end" font-size="12" fill="var(--d-text)"${rowAttr}>${esc(t.name)}</text>`);
|
|
299
|
+
const cy = y + rowH / 2;
|
|
300
|
+
if (sp.flags.has('milestone')) {
|
|
301
|
+
parts.push(`<polygon points="${r2(xOf(sp.start))},${r2(cy - 7)} ${r2(xOf(sp.start) + 7)},${r2(cy)} ${r2(xOf(sp.start))},${r2(cy + 7)} ${r2(xOf(sp.start) - 7)},${r2(cy)}" fill="var(--d-ink)"${nodeAttr}/>`);
|
|
302
|
+
// 里程碑日期(右侧放不下换左侧)
|
|
303
|
+
const mx = xOf(sp.start);
|
|
304
|
+
const dTxt = fmtDay(sp.start);
|
|
305
|
+
if (mx + 14 + textW(dTxt, 10) <= x1)
|
|
306
|
+
parts.push(`<text x="${r2(mx + 12)}" y="${r2(cy + 3.5)}" font-size="10" fill="var(--d-muted)">${dTxt}</text>`);
|
|
307
|
+
else
|
|
308
|
+
parts.push(`<text x="${r2(mx - 12)}" y="${r2(cy + 3.5)}" text-anchor="end" font-size="10" fill="var(--d-muted)">${dTxt}</text>`);
|
|
309
|
+
}
|
|
310
|
+
else {
|
|
311
|
+
const bx = xOf(sp.start);
|
|
312
|
+
const bw = Math.max(4, xOf(sp.end) - bx);
|
|
313
|
+
const done = sp.flags.has('done');
|
|
314
|
+
const active = sp.flags.has('active');
|
|
315
|
+
const crit = sp.flags.has('crit');
|
|
316
|
+
const fill = done ? 'var(--d-faint)' : crit ? 'var(--d-mark)' : color;
|
|
317
|
+
// 完成进度(语法扩展):底条降不透明度,左侧实色进度段(dhtmlx/Frappe 惯例)
|
|
318
|
+
const p = t.progress;
|
|
319
|
+
const baseOp = crit ? 0.16 : p !== undefined ? 0.35 : 1;
|
|
320
|
+
parts.push(`<rect x="${r2(bx)}" y="${r2(cy - barH / 2)}" width="${r2(bw)}" height="${barH}" rx="4" fill="${fill}"${baseOp !== 1 ? ` fill-opacity="${baseOp}"` : ''} stroke="${crit ? 'var(--d-mark)' : active ? 'var(--d-ink)' : 'none'}" stroke-width="${crit || active ? 1.5 : 0}"${nodeAttr}/>`);
|
|
321
|
+
if (p !== undefined && p > 0) {
|
|
322
|
+
const pw = Math.max(3, bw * p / 100);
|
|
323
|
+
parts.push(`<rect x="${r2(bx)}" y="${r2(cy - barH / 2)}" width="${r2(Math.min(pw, bw))}" height="${barH}" rx="4" fill="${fill}" fill-opacity="${crit ? 0.45 : 1}"${nodeAttr ? ` data-row="${ri}"` : ''}/>`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (opts.interactive)
|
|
327
|
+
parts.push(`<rect class="rowhit" x="${PAD}" y="${r2(y)}" width="${r2(x1 - PAD)}" height="${rowH}" fill="transparent"${rowAttr} data-tip="${esc(sp.tip)}"/>`);
|
|
328
|
+
y += rowH;
|
|
329
|
+
ri += 1;
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
parts.push(`<g id="gl">${labels.join('')}</g>`);
|
|
333
|
+
// today 线(实线,贯穿内容区;data-today 供 explore 页 Today 按钮定位滚动)
|
|
334
|
+
const today = Date.now();
|
|
335
|
+
if (today >= minT && today <= maxT) {
|
|
336
|
+
const tx = xOf(today);
|
|
337
|
+
parts.push(`<line x1="${r2(tx)}" y1="${bodyTop}" x2="${r2(tx)}" y2="${bodyBottom}" stroke="var(--d-mark)" stroke-width="1.5" data-today=""/>`);
|
|
338
|
+
}
|
|
339
|
+
// 状态图例(只画图里出现过的 flag;惯例放底部一行)
|
|
340
|
+
{
|
|
341
|
+
const flagSet = new Set();
|
|
342
|
+
spans.forEach(s => s.flags.forEach(f => flagSet.add(f)));
|
|
343
|
+
const legendItems = [['done', 'Done'], ['active', 'Active'], ['crit', 'Critical'], ['milestone', 'Milestone']].filter(([k]) => flagSet.has(k));
|
|
344
|
+
if (legendItems.length) {
|
|
345
|
+
let lx = PAD;
|
|
346
|
+
const ly = bodyBottom + 7;
|
|
347
|
+
for (const [k, label] of legendItems) {
|
|
348
|
+
if (k === 'milestone')
|
|
349
|
+
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)"/>`);
|
|
350
|
+
else if (k === 'done')
|
|
351
|
+
parts.push(`<rect x="${r2(lx)}" y="${r2(ly)}" width="10" height="10" rx="2" fill="var(--d-faint)"/>`);
|
|
352
|
+
else if (k === 'active')
|
|
353
|
+
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"/>`);
|
|
354
|
+
else
|
|
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
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const titleSvg = doc.title
|
|
363
|
+
? `<text x="${PAD}" y="${PAD + 14}" font-size="18" font-weight="500" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(doc.title)}</text>`
|
|
364
|
+
: '';
|
|
365
|
+
// interactive:内嵌解析后的任务元数据(explore 详情卡数据源:起止/时长/状态/进度/依赖)
|
|
366
|
+
let gtasksSvg = '';
|
|
367
|
+
if (opts.interactive) {
|
|
368
|
+
const next = new Map();
|
|
369
|
+
doc.sections.forEach(s => s.tasks.forEach(t => {
|
|
370
|
+
if (!t.after)
|
|
371
|
+
return;
|
|
372
|
+
const a = next.get(t.after) ?? [];
|
|
373
|
+
a.push(t.id ?? t.name);
|
|
374
|
+
next.set(t.after, a);
|
|
375
|
+
}));
|
|
376
|
+
const tasks = doc.sections.flatMap(sec => sec.tasks.map(t => {
|
|
377
|
+
const sp = spans.find(s => s.name === t.name && s.sectionIdx === doc.sections.indexOf(sec));
|
|
378
|
+
return {
|
|
379
|
+
id: t.id ?? t.name, name: t.name, sec: sec.name,
|
|
380
|
+
start: fmtDay(sp.start), end: fmtDay(sp.end), days: Math.round((sp.end - sp.start) / DAY),
|
|
381
|
+
st: [...sp.flags], ...(t.progress !== undefined ? { p: t.progress } : {}),
|
|
382
|
+
...(t.after ? { after: t.after } : {}), ...(next.get(t.id ?? t.name)?.length ? { next: next.get(t.id ?? t.name) } : {}),
|
|
383
|
+
};
|
|
384
|
+
}));
|
|
385
|
+
gtasksSvg = `<metadata id="gtasks">${esc(JSON.stringify(tasks))}</metadata>`;
|
|
386
|
+
}
|
|
387
|
+
// interactive:包 #am 根容器(复用 explore 页 tooltip/缩放脚本)+ 行高亮/依赖链 CSS
|
|
388
|
+
const interStyle = opts.interactive
|
|
389
|
+
? `<style>#am [data-node]{cursor:pointer;transition:filter .12s ease,opacity .15s ease}` +
|
|
390
|
+
`#am [data-node]:hover{filter:brightness(1.15)}` +
|
|
391
|
+
`#am rect.rowhit{cursor:default}` +
|
|
392
|
+
`#am text.rh{fill:var(--d-ink);font-weight:600}` +
|
|
393
|
+
`#am rect.rh{filter:brightness(1.12)}` +
|
|
394
|
+
`#am rect.rowhit.rh{filter:none;fill:var(--d-ink);fill-opacity:.05}` +
|
|
395
|
+
`#am.hl [data-node],#am.hl text[data-row]{opacity:.3}` +
|
|
396
|
+
`#am.hl [data-node].on,#am.hl text[data-row].on{opacity:1}</style>`
|
|
397
|
+
: '';
|
|
398
|
+
const svg = svgDoc(width, height, 'gantt', opts.v.version, doc.title ?? `gantt, ${spans.length} tasks`, { type: 'gantt', doc }, opts.skin, interStyle + gtasksSvg + `<g id="am" data-scale="${mode}">${titleSvg + parts.join('')}</g>`, opts.tagQuery);
|
|
399
|
+
return { svg, version: opts.v.version, cacheControl: opts.v.cacheControl };
|
|
400
|
+
}
|
|
401
|
+
/** /draw/gantt?m=... 入口(m 可省首行 gantt 关键字;/draw/mermaid 的 gantt 分派亦走此) */
|
|
402
|
+
export function renderGanttFromUrl(url, interactive = false) {
|
|
403
|
+
const params = url.searchParams;
|
|
404
|
+
const m = params.get('m') ?? '';
|
|
405
|
+
const doc = parseGantt(/^\s*gantt\b/i.test(m) ? m : `gantt\n${m}`);
|
|
406
|
+
if (!doc.title)
|
|
407
|
+
doc.title = params.get('title')?.trim() || undefined; // 与 flowchart 入口一致:?title= 回退
|
|
408
|
+
const v = resolveVersion(params.get('v'));
|
|
409
|
+
const skin = resolveSkin('sankey', params.get('t'), schemeParam(params));
|
|
410
|
+
const width = /(\d+)/.test(params.get('w') ?? '') ? Math.max(200, Math.min(2400, +params.get('w'))) : undefined;
|
|
411
|
+
const height = /(\d+)/.test(params.get('h') ?? '') ? Math.max(140, Math.min(2000, +params.get('h'))) : undefined;
|
|
412
|
+
const sv = params.get('scale');
|
|
413
|
+
const scale = sv === 'day' || sv === 'week' || sv === 'month' ? sv : undefined;
|
|
414
|
+
return renderGantt(doc, { v, skin, interactive, width, height, tagQuery: url.search, ...(scale ? { scale } : {}) });
|
|
415
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type GanttDoc } from './gantt.js';
|
|
2
|
+
import { resolveSkin } from '../styles/index.js';
|
|
3
|
+
import { type DrawResult } from './query.js';
|
|
4
|
+
export interface Gantt2Opts {
|
|
5
|
+
doc: GanttDoc;
|
|
6
|
+
title?: string;
|
|
7
|
+
version: number;
|
|
8
|
+
cacheControl: string;
|
|
9
|
+
skin: ReturnType<typeof resolveSkin>;
|
|
10
|
+
interactive: boolean;
|
|
11
|
+
width?: number;
|
|
12
|
+
height?: number;
|
|
13
|
+
aria?: string;
|
|
14
|
+
/** 原始 URL 查询串:角标直链 explore 落地页用 */
|
|
15
|
+
tagQuery?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 渲染甘特图 v2。
|
|
19
|
+
*/
|
|
20
|
+
export declare function renderGantt2Core(o: Gantt2Opts): DrawResult;
|
|
21
|
+
/**
|
|
22
|
+
* `/draw/gantt2` 入口:GET ?m=<gantt 源码>(和 /draw/gantt 同一套参数)。
|
|
23
|
+
*/
|
|
24
|
+
export declare function renderGantt2FromUrl(url: URL, interactive?: boolean): DrawResult;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
// /draw/gantt2 —— 甘特图 v2:同一份数据(mermaid 甘特源码),换成我们架构图的那张脸。
|
|
2
|
+
//
|
|
3
|
+
// 和 /draw/gantt 的关系:输入完全一样(m=<gantt 源码>),只是长相不同 ——
|
|
4
|
+
// 条按状态着色(和架构图一个调色板)、每行一个类型角标、底下多一行图例。
|
|
5
|
+
// 老路径 /draw/gantt 一行未动。
|
|
6
|
+
import { ApiError } from '../specs/index.js';
|
|
7
|
+
import { parseGantt, resolveSpans } from './gantt.js';
|
|
8
|
+
import { resolveSkin } from '../styles/index.js';
|
|
9
|
+
import { clampInt, schemeParam } from './query.js';
|
|
10
|
+
import { esc, r2, svgDoc, textW } from './svg.js';
|
|
11
|
+
const DAY = 86400000;
|
|
12
|
+
const PAD = 44;
|
|
13
|
+
const AXIS_H = 34;
|
|
14
|
+
const SECTION_H = 30;
|
|
15
|
+
const ROW_H = 40;
|
|
16
|
+
const BAR_H = 22;
|
|
17
|
+
const LEGEND_H = 46;
|
|
18
|
+
const MONTHS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
|
19
|
+
/** 状态 → (展示名, 角标字母, 颜色序号) —— 和架构图共用一套色板 */
|
|
20
|
+
const STATUS = {
|
|
21
|
+
done: { name: 'Done', badge: 'D', color: 0 },
|
|
22
|
+
active: { name: 'Active', badge: 'A', color: 3 },
|
|
23
|
+
crit: { name: 'Critical', badge: 'C', color: 1 },
|
|
24
|
+
milestone: { name: 'Milestone', badge: 'M', color: -1 },
|
|
25
|
+
};
|
|
26
|
+
const STATUS_ORDER = ['done', 'active', 'crit', 'milestone'];
|
|
27
|
+
const sVar = (i) => `var(--d-s${i + 1})`;
|
|
28
|
+
/** 时间轴刻度:跨度大按月,小按周 */
|
|
29
|
+
function makeTicks(minT, maxT) {
|
|
30
|
+
const days = (maxT - minT) / DAY;
|
|
31
|
+
const ticks = [];
|
|
32
|
+
if (days > 120) {
|
|
33
|
+
const d = new Date(minT);
|
|
34
|
+
d.setUTCDate(1);
|
|
35
|
+
d.setUTCHours(0, 0, 0, 0);
|
|
36
|
+
while (d.getTime() <= maxT) {
|
|
37
|
+
ticks.push({ t: d.getTime(), label: `${MONTHS[d.getUTCMonth()]} ${String(d.getUTCFullYear()).slice(2)}` });
|
|
38
|
+
d.setUTCMonth(d.getUTCMonth() + 1);
|
|
39
|
+
}
|
|
40
|
+
return ticks;
|
|
41
|
+
}
|
|
42
|
+
const d = new Date(minT);
|
|
43
|
+
d.setUTCDate(d.getUTCDate() - d.getUTCDay());
|
|
44
|
+
d.setUTCHours(0, 0, 0, 0);
|
|
45
|
+
while (d.getTime() <= maxT) {
|
|
46
|
+
ticks.push({ t: d.getTime(), label: `${d.getUTCMonth() + 1}/${d.getUTCDate()}` });
|
|
47
|
+
d.setUTCDate(d.getUTCDate() + 7);
|
|
48
|
+
}
|
|
49
|
+
return ticks;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* 渲染甘特图 v2。
|
|
53
|
+
*/
|
|
54
|
+
export function renderGantt2Core(o) {
|
|
55
|
+
const doc = o.doc;
|
|
56
|
+
const spans = resolveSpans(doc);
|
|
57
|
+
if (!spans.length)
|
|
58
|
+
throw new ApiError(400, 'gantt: no tasks found');
|
|
59
|
+
const minT = Math.min(...spans.map((s) => s.start));
|
|
60
|
+
const maxT = Math.max(...spans.map((s) => Math.max(s.end, s.start)));
|
|
61
|
+
const ticks = makeTicks(minT, maxT);
|
|
62
|
+
// 左边一列放任务名(和架构图一样,标签是自己的东西)
|
|
63
|
+
const labelW = Math.max(120, ...doc.sections.flatMap((s) => s.tasks.map((t) => textW(t.name, 13) + 34)));
|
|
64
|
+
const x0 = PAD + labelW;
|
|
65
|
+
const width = Math.max(560, o.width ?? 0, x0 + 120);
|
|
66
|
+
const rightPad = 24;
|
|
67
|
+
const span = Math.max(DAY, maxT - minT);
|
|
68
|
+
const plotW = Math.max(160, width - x0 - rightPad);
|
|
69
|
+
const xOf = (t) => x0 + ((t - minT) / span) * plotW;
|
|
70
|
+
const rowsTotal = doc.sections.reduce((n, s) => n + s.tasks.length, 0);
|
|
71
|
+
const axisTop = o.title ? PAD + 34 : PAD;
|
|
72
|
+
const bodyTop = axisTop + AXIS_H;
|
|
73
|
+
const bodyH = doc.sections.length * SECTION_H + rowsTotal * ROW_H;
|
|
74
|
+
const height = Math.max(o.height ?? 0, bodyTop + bodyH + LEGEND_H + PAD);
|
|
75
|
+
const parts = [];
|
|
76
|
+
// 分区色带(隔区铺)
|
|
77
|
+
let bandY = bodyTop;
|
|
78
|
+
doc.sections.forEach((sec, si) => {
|
|
79
|
+
const h = SECTION_H + sec.tasks.length * ROW_H;
|
|
80
|
+
if (si % 2 === 1) {
|
|
81
|
+
parts.push(`<rect x="${PAD}" y="${r2(bandY)}" width="${r2(width - PAD - rightPad)}" height="${r2(h)}" rx="10" fill="var(--d-ink)" fill-opacity="0.035"/>`);
|
|
82
|
+
}
|
|
83
|
+
bandY += h;
|
|
84
|
+
});
|
|
85
|
+
// 细网格 + 刻度
|
|
86
|
+
for (const tk of ticks) {
|
|
87
|
+
const x = xOf(tk.t);
|
|
88
|
+
parts.push(`<line x1="${r2(x)}" y1="${r2(bodyTop)}" x2="${r2(x)}" y2="${r2(bodyTop + bodyH)}" stroke="var(--d-grid)"/>`);
|
|
89
|
+
parts.push(`<text x="${r2(x)}" y="${r2(bodyTop - 10)}" text-anchor="middle" font-size="11" fill="var(--d-muted)">${esc(tk.label)}</text>`);
|
|
90
|
+
}
|
|
91
|
+
parts.push(`<line x1="${r2(x0)}" y1="${r2(bodyTop - 26)}" x2="${r2(x0 + plotW)}" y2="${r2(bodyTop - 26)}" stroke="var(--d-border)"/>`);
|
|
92
|
+
parts.push(`<line x1="${r2(x0)}" y1="${r2(bodyTop)}" x2="${r2(x0)}" y2="${r2(bodyTop + bodyH)}" stroke="var(--d-border)"/>`);
|
|
93
|
+
// 行:分区名 + 任务条 + 类型角标
|
|
94
|
+
let y = bodyTop;
|
|
95
|
+
let ri = 0;
|
|
96
|
+
const counts = new Map();
|
|
97
|
+
doc.sections.forEach((sec) => {
|
|
98
|
+
parts.push(`<text x="${PAD}" y="${r2(y + 20)}" font-size="12" font-weight="600" fill="var(--d-ink)">${esc(sec.name)}</text>`);
|
|
99
|
+
y += SECTION_H;
|
|
100
|
+
for (const t of sec.tasks) {
|
|
101
|
+
const sp = spans.find((s) => s.name === t.name);
|
|
102
|
+
const st = statusOf(sp);
|
|
103
|
+
const meta = STATUS[st] ?? { name: 'Task', badge: '?', color: -1 };
|
|
104
|
+
counts.set(st, (counts.get(st) ?? 0) + 1);
|
|
105
|
+
const cy = y + ROW_H / 2;
|
|
106
|
+
const rowAttr = o.interactive ? ` data-row="${ri}"` : '';
|
|
107
|
+
const nodeAttr = o.interactive
|
|
108
|
+
? ` data-node="${esc(t.id ?? t.name)}"${t.after ? ` data-after="${esc(t.after)}"` : ''}${rowAttr} data-tip="${esc(sp.tip)}"`
|
|
109
|
+
: '';
|
|
110
|
+
const color = meta.color >= 0 ? sVar(meta.color) : 'var(--d-ink)';
|
|
111
|
+
// 角标:行首的小方块,和架构图的节点角标一个样式
|
|
112
|
+
parts.push(`<rect x="${r2(PAD + 2)}" y="${r2(cy - 9)}" width="18" height="18" rx="5" fill="${color}" fill-opacity="${meta.color >= 0 ? 0.2 : 0.14}" stroke="${color}" stroke-opacity="0.55" stroke-width="1"/>`, `<text x="${r2(PAD + 11)}" y="${r2(cy + 3.5)}" text-anchor="middle" font-size="10" font-weight="700" fill="${color}">${esc(meta.badge)}</text>`);
|
|
113
|
+
// 任务名
|
|
114
|
+
parts.push(`<text x="${r2(PAD + 28)}" y="${r2(cy + 4.5)}" font-size="13" fill="var(--d-ink)">${esc(t.name)}</text>`);
|
|
115
|
+
// 条
|
|
116
|
+
if (st === 'milestone') {
|
|
117
|
+
const mx = xOf(sp.start);
|
|
118
|
+
parts.push(`<polygon points="${r2(mx)},${r2(cy - 8)} ${r2(mx + 8)},${r2(cy)} ${r2(mx)},${r2(cy + 8)} ${r2(mx - 8)},${r2(cy)}" fill="${color}"${nodeAttr}/>`);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const bx = xOf(sp.start);
|
|
122
|
+
const bw = Math.max(3, xOf(sp.end) - bx);
|
|
123
|
+
parts.push(`<rect x="${r2(bx)}" y="${r2(cy - BAR_H / 2)}" width="${r2(bw)}" height="${BAR_H}" rx="6"` +
|
|
124
|
+
` fill="${color}" fill-opacity="${st === 'done' ? 0.32 : 0.92}"${nodeAttr}/>`);
|
|
125
|
+
// 进度(可选):条上叠一段更实的
|
|
126
|
+
if (t.progress !== undefined && t.progress > 0) {
|
|
127
|
+
parts.push(`<rect x="${r2(bx)}" y="${r2(cy - BAR_H / 2)}" width="${r2(bw * t.progress / 100)}" height="${BAR_H}" rx="6" fill="${color}"/>`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
// 依赖箭头
|
|
131
|
+
if (t.after) {
|
|
132
|
+
const from = spans.find((s) => s.name === doc.sections.flatMap((x) => x.tasks).find((tt) => (tt.id ?? tt.name) === t.after)?.name);
|
|
133
|
+
if (from) {
|
|
134
|
+
const fx = xOf(Math.max(from.end, from.start));
|
|
135
|
+
const tx = xOf(sp.start);
|
|
136
|
+
parts.push(`<path d="M${r2(fx)} ${r2(cy - 6)}L${r2(tx - 6)} ${r2(cy - 6)}" fill="none" stroke="var(--d-border)" stroke-width="1.4" marker-end="url(#g2-arrow)"/>`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
y += ROW_H;
|
|
140
|
+
ri++;
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
// 图例(架构图同款:色块 + 名字 + 个数)
|
|
144
|
+
const legendTypes = STATUS_ORDER.filter((st) => counts.has(st));
|
|
145
|
+
if (legendTypes.length) {
|
|
146
|
+
let lx = PAD;
|
|
147
|
+
const ly = bodyTop + bodyH + 26;
|
|
148
|
+
const legend = [];
|
|
149
|
+
for (const st of legendTypes) {
|
|
150
|
+
const meta = STATUS[st];
|
|
151
|
+
const color = meta.color >= 0 ? sVar(meta.color) : 'var(--d-ink)';
|
|
152
|
+
const nw = textW(meta.name, 11.5);
|
|
153
|
+
const cw = textW(String(counts.get(st)), 11.5);
|
|
154
|
+
legend.push(`<rect x="${r2(lx)}" y="${r2(ly - 10)}" width="14" height="14" rx="4" fill="${color}" fill-opacity="${meta.color >= 0 ? 0.2 : 0.14}" stroke="${color}" stroke-opacity="0.55" stroke-width="1"/>`, `<text x="${r2(lx + 20)}" y="${r2(ly + 1)}" font-size="11.5" fill="var(--d-muted)">${esc(meta.name)}</text>`, `<text x="${r2(lx + 26 + nw)}" y="${r2(ly + 1)}" font-size="11.5" font-weight="700" fill="var(--d-ink)">${counts.get(st)}</text>`);
|
|
155
|
+
lx += 20 + nw + 8 + cw + 26;
|
|
156
|
+
}
|
|
157
|
+
parts.push(`<g data-legend="status">${legend.join('')}</g>`);
|
|
158
|
+
}
|
|
159
|
+
const titleSvg = o.title
|
|
160
|
+
? `<text x="${PAD}" y="${PAD + 16}" font-size="19" font-weight="600" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(o.title)}</text>`
|
|
161
|
+
: '';
|
|
162
|
+
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
|
+
const spec = { type: 'gantt2', title: o.title, sections: doc.sections };
|
|
164
|
+
const svg = svgDoc(width, height, 'gantt2', o.version, o.aria ?? o.title ?? `gantt chart, ${spans.length} tasks`, spec, o.skin, titleSvg + defs + parts.join(''), o.tagQuery, 'gantt2');
|
|
165
|
+
return { svg, version: o.version, cacheControl: o.cacheControl };
|
|
166
|
+
}
|
|
167
|
+
function statusOf(sp) {
|
|
168
|
+
if (sp.flags.has('milestone'))
|
|
169
|
+
return 'milestone';
|
|
170
|
+
if (sp.flags.has('crit'))
|
|
171
|
+
return 'crit';
|
|
172
|
+
if (sp.flags.has('active'))
|
|
173
|
+
return 'active';
|
|
174
|
+
if (sp.flags.has('done'))
|
|
175
|
+
return 'done';
|
|
176
|
+
return 'active';
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* `/draw/gantt2` 入口:GET ?m=<gantt 源码>(和 /draw/gantt 同一套参数)。
|
|
180
|
+
*/
|
|
181
|
+
export function renderGantt2FromUrl(url, interactive = false) {
|
|
182
|
+
const params = url.searchParams;
|
|
183
|
+
const m = params.get('m') ?? '';
|
|
184
|
+
const doc = parseGantt(/^\s*gantt\b/i.test(m) ? m : `gantt\n${m}`);
|
|
185
|
+
const v = parseInt(params.get('v') ?? '1', 10) || 1;
|
|
186
|
+
const skin = resolveSkin('sankey', params.get('t'), schemeParam(params));
|
|
187
|
+
return renderGantt2Core({
|
|
188
|
+
doc,
|
|
189
|
+
title: params.get('title')?.trim() || doc.title,
|
|
190
|
+
version: v,
|
|
191
|
+
cacheControl: 'public, max-age=300',
|
|
192
|
+
skin,
|
|
193
|
+
interactive,
|
|
194
|
+
width: clampInt(params, 'w', 0, 200, 2400),
|
|
195
|
+
height: clampInt(params, 'h', 0, 140, 2400),
|
|
196
|
+
tagQuery: url.search,
|
|
197
|
+
});
|
|
198
|
+
}
|