@x-cmd/draw 0.3.0 → 0.4.1

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 (121) hide show
  1. package/README.md +79 -297
  2. package/dist/lib.d.ts +26 -3
  3. package/dist/lib.js +31 -4
  4. package/dist/node/cli.js +1 -1
  5. package/dist/render/anno.d.ts +50 -0
  6. package/dist/render/anno.js +589 -0
  7. package/dist/render/arch.d.ts +106 -0
  8. package/dist/render/arch.js +1011 -0
  9. package/dist/render/archmap.d.ts +90 -3
  10. package/dist/render/archmap.js +174 -102
  11. package/dist/render/area.js +9 -7
  12. package/dist/render/banner.d.ts +8 -0
  13. package/dist/render/banner.js +204 -0
  14. package/dist/render/bar-grouped.js +3 -2
  15. package/dist/render/bar-stacked.js +3 -2
  16. package/dist/render/bar.js +3 -2
  17. package/dist/render/butterfly.d.ts +3 -0
  18. package/dist/render/butterfly.js +355 -0
  19. package/dist/render/calendar.d.ts +6 -0
  20. package/dist/render/calendar.js +448 -0
  21. package/dist/render/combo.js +3 -2
  22. package/dist/render/compass.d.ts +6 -0
  23. package/dist/render/compass.js +77 -0
  24. package/dist/render/dispatch.js +26 -1
  25. package/dist/render/echarts-ssr.d.ts +30 -0
  26. package/dist/render/echarts-ssr.js +337 -16
  27. package/dist/render/gantt.d.ts +11 -0
  28. package/dist/render/gantt.js +12 -19
  29. package/dist/render/gantt2.d.ts +24 -0
  30. package/dist/render/gantt2.js +198 -0
  31. package/dist/render/hbar.js +8 -4
  32. package/dist/render/icons.d.ts +3 -0
  33. package/dist/render/icons.js +78 -0
  34. package/dist/render/kinds.d.ts +11 -0
  35. package/dist/render/kinds.js +176 -0
  36. package/dist/render/ladder.d.ts +21 -0
  37. package/dist/render/ladder.js +230 -0
  38. package/dist/render/layout.d.ts +46 -0
  39. package/dist/render/layout.js +57 -0
  40. package/dist/render/line-multi.js +2 -2
  41. package/dist/render/line.js +2 -2
  42. package/dist/render/maps.d.ts +34 -0
  43. package/dist/render/maps.js +197 -2
  44. package/dist/render/mermaid.js +25 -6
  45. package/dist/render/mindmap.js +3 -2
  46. package/dist/render/onepage.d.ts +62 -0
  47. package/dist/render/onepage.js +670 -0
  48. package/dist/render/orgicon.d.ts +12 -0
  49. package/dist/render/orgicon.js +31 -0
  50. package/dist/render/png.js +3 -1
  51. package/dist/render/quadrant.d.ts +6 -0
  52. package/dist/render/quadrant.js +201 -0
  53. package/dist/render/query.d.ts +2 -0
  54. package/dist/render/query.js +100 -1
  55. package/dist/render/route.d.ts +86 -0
  56. package/dist/render/route.js +1077 -0
  57. package/dist/render/scatter.js +73 -7
  58. package/dist/render/seq.d.ts +41 -0
  59. package/dist/render/seq.js +248 -0
  60. package/dist/render/src-spec.d.ts +5 -0
  61. package/dist/render/src-spec.js +49 -0
  62. package/dist/render/svg.d.ts +16 -2
  63. package/dist/render/svg.js +85 -7
  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/us-states.js +4 -6
  80. package/dist/render/vendor/world-geo.d.ts +22 -0
  81. package/dist/render/vendor/world-geo.js +9 -0
  82. package/dist/render/vendor/world.d.ts +13 -0
  83. package/dist/render/vendor/world.js +12 -0
  84. package/dist/specs/base.d.ts +5 -0
  85. package/dist/specs/base.js +14 -1
  86. package/dist/specs/butterfly.d.ts +46 -0
  87. package/dist/specs/butterfly.js +241 -0
  88. package/dist/specs/calendar.d.ts +90 -0
  89. package/dist/specs/calendar.js +296 -0
  90. package/dist/specs/compass.d.ts +27 -0
  91. package/dist/specs/compass.js +111 -0
  92. package/dist/specs/index.d.ts +5 -0
  93. package/dist/specs/index.js +5 -0
  94. package/dist/specs/ladder.d.ts +43 -0
  95. package/dist/specs/ladder.js +268 -0
  96. package/dist/specs/quadrant.d.ts +41 -0
  97. package/dist/specs/quadrant.js +200 -0
  98. package/dist/specs/scatter.d.ts +14 -8
  99. package/dist/specs/scatter.js +54 -5
  100. package/dist/specs/table.d.ts +24 -0
  101. package/dist/specs/table.js +154 -0
  102. package/dist/store.d.ts +1 -1
  103. package/dist/store.js +1 -1
  104. package/dist/styles/butterfly.d.ts +2 -0
  105. package/dist/styles/butterfly.js +74 -0
  106. package/dist/styles/calendar.d.ts +2 -0
  107. package/dist/styles/calendar.js +51 -0
  108. package/dist/styles/compass.d.ts +2 -0
  109. package/dist/styles/compass.js +19 -0
  110. package/dist/styles/index.js +13 -0
  111. package/dist/styles/ladder.d.ts +3 -0
  112. package/dist/styles/ladder.js +66 -0
  113. package/dist/styles/quadrant.d.ts +2 -0
  114. package/dist/styles/quadrant.js +19 -0
  115. package/dist/styles/table.d.ts +2 -0
  116. package/dist/styles/table.js +12 -0
  117. package/dist/styles/types.d.ts +1 -0
  118. package/dist/styles/types.js +1 -0
  119. package/dist/types/dagre.d.ts +39 -0
  120. package/dist/types/dagre.js +3 -0
  121. package/package.json +17 -1
@@ -0,0 +1,448 @@
1
+ import { dayIso, daysInMonth, firstWeekday, isoDay, isoParts, isoWeekday } from '../specs/calendar.js';
2
+ import { TAB, card } from '../theme.js';
3
+ import { esc, r2, svgDoc, textW, titleBlock, topLegend, trunc } from './svg.js';
4
+ import { footerUnit, headerMeta, tipHeight, tipUnit } from './units.js';
5
+ const PAD = 24;
6
+ const HEAD_FS = 12;
7
+ const NUM_FS = 13;
8
+ const EV_FS = 12;
9
+ /** 事件类别 → 色板序号 + 名字(中英) */
10
+ const KINDS = {
11
+ work: { c: 1, zh: '工作', en: 'Work' },
12
+ meeting: { c: 2, zh: '会议', en: 'Meeting' },
13
+ review: { c: 3, zh: '评审', en: 'Review' },
14
+ release: { c: 4, zh: '发布', en: 'Release' },
15
+ focus: { c: 5, zh: '专注', en: 'Focus' },
16
+ travel: { c: 6, zh: '出行', en: 'Travel' },
17
+ health: { c: 7, zh: '健康', en: 'Health' },
18
+ personal: { c: 8, zh: '个人', en: 'Personal' },
19
+ class: { c: 9, zh: '课程', en: 'Class' },
20
+ oncall: { c: 10, zh: '值班', en: 'On call' },
21
+ };
22
+ const WEEK_CN = ['日', '一', '二', '三', '四', '五', '六'];
23
+ const WEEK_EN = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
24
+ const MONTH_CN = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十', '十一', '十二'];
25
+ const MONTH_EN = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
26
+ const DEFAULT_PERIODS = ['08:00', '09:40', '11:20', '14:00', '15:40', '17:20', '19:00'];
27
+ const colorVar = (ctx, kind) => {
28
+ const k = KINDS[kind];
29
+ return k ? `var(--d-s${k.c})` : 'var(--d-muted)';
30
+ };
31
+ /** 事件按时间排,没时间的按原样 */
32
+ function sortItems(items) {
33
+ return [...items].sort((a, b) => (a.time ?? '99:99').localeCompare(b.time ?? '99:99'));
34
+ }
35
+ /** 按像素宽度截断(挤的格子里文字不能串到隔壁格) */
36
+ function fitText(s, maxW, fs) {
37
+ if (maxW <= 8)
38
+ return trunc(s, 2);
39
+ if (textW(s, fs) <= maxW)
40
+ return s;
41
+ let t = s;
42
+ while (t.length > 1 && textW(t + '…', fs) > maxW)
43
+ t = t.slice(0, -1);
44
+ return t + '…';
45
+ }
46
+ /** 某一天是休息日吗(默认:周末) */
47
+ function isRest(ctx, date) {
48
+ const d = ctx.dayOf(date);
49
+ if (d)
50
+ return d.kind === 'rest' || d.kind === 'holiday';
51
+ const w = isoWeekday(date);
52
+ return w === 0 || w === 6;
53
+ }
54
+ /** 某一天是法定假日吗 */
55
+ function isHoliday(ctx, date) {
56
+ return ctx.dayOf(date)?.kind === 'holiday';
57
+ }
58
+ function buildCtx(spec, o, ws) {
59
+ const byDate = new Map();
60
+ for (const it of spec.items) {
61
+ const arr = byDate.get(it.date) ?? [];
62
+ arr.push(it);
63
+ byDate.set(it.date, arr);
64
+ }
65
+ const days = new Map();
66
+ for (const d of spec.days)
67
+ days.set(d.date, d);
68
+ const today = dayIso(Math.floor(Date.now() / 86400000));
69
+ const ctx = {
70
+ spec,
71
+ skin: o.skin,
72
+ colorOf: (kind) => colorVar(ctx, kind),
73
+ itemsOn: (date) => sortItems(byDate.get(date) ?? []),
74
+ dayOf: (date) => days.get(date),
75
+ today,
76
+ ws,
77
+ overflow: [],
78
+ };
79
+ return ctx;
80
+ }
81
+ /* ---------------------------------------------------------------- 网格 */
82
+ /** 一个月的格子:7 列 × n 行,外加周头 */
83
+ function monthGrid(ctx, y, m, x, y0, w, h, dense = false, headY) {
84
+ const parts = [];
85
+ const cols = 7;
86
+ const cw = w / cols;
87
+ const first = firstWeekday(y, m);
88
+ const dim = daysInMonth(y, m);
89
+ const rows = Math.ceil((first + dim) / 7);
90
+ const ch = h / rows;
91
+ // 周头
92
+ for (let c = 0; c < cols; c++) {
93
+ const wd = (c + ctx.ws) % 7;
94
+ const label = dense ? WEEK_CN[wd] : (ctx.skin.rules.title === 'editorial' ? WEEK_EN[wd] : WEEK_CN[wd]);
95
+ parts.push(`<text x="${r2(x + c * cw + cw / 2)}" y="${r2(headY ?? y0 - 8)}" text-anchor="middle" font-size="${HEAD_FS}" fill="var(--d-muted)">${esc(label)}</text>`);
96
+ }
97
+ // 格子
98
+ for (let r = 0; r < rows; r++) {
99
+ for (let c = 0; c < cols; c++) {
100
+ const idx = r * 7 + c;
101
+ const dayNum = idx - first + 1;
102
+ const cx = x + c * cw;
103
+ const cy = y0 + r * ch;
104
+ const outside = dayNum < 1 || dayNum > dim;
105
+ const date = dayIso(Date.UTC(y, m - 1, dayNum) / 86400000);
106
+ // 线:只画横线(Apple 的月视图是横线分隔,竖线没有)
107
+ parts.push(`<line x1="${r2(cx)}" y1="${r2(cy)}" x2="${r2(cx + cw)}" y2="${r2(cy)}" stroke="var(--d-grid)" stroke-width="1"/>`);
108
+ if (outside) {
109
+ // 上/下个月的日子:淡淡地摆在那儿(Apple 的月视图)
110
+ const nd = Number(date.slice(8));
111
+ parts.push(`<text x="${r2(cx + cw - 8)}" y="${r2(cy + 16)}" text-anchor="end" font-size="${NUM_FS}" fill="var(--d-faint)" style="${TAB}">${nd}</text>`);
112
+ continue;
113
+ }
114
+ const iso = date;
115
+ const holiday = isHoliday(ctx, iso);
116
+ const rest = isRest(ctx, iso);
117
+ const today = iso === ctx.today;
118
+ // 日期号:今天红底白字,假日红字,休息灰字
119
+ const nx = cx + cw - 8;
120
+ const ny = cy + 16;
121
+ if (today) {
122
+ // 圆圈要正好套住数字:数字是右对齐的,圆心得跟着数字的宽度走
123
+ const nw = textW(String(dayNum), NUM_FS);
124
+ parts.push(`<circle cx="${r2(nx - nw / 2)}" cy="${r2(ny - NUM_FS * 0.35)}" r="10" fill="var(--d-mark)"/>`);
125
+ }
126
+ const fill = today ? '#ffffff' : holiday ? 'var(--d-mark)' : rest ? 'var(--d-muted)' : 'var(--d-ink)';
127
+ parts.push(`<text x="${r2(nx)}" y="${r2(ny)}" text-anchor="end" font-size="${NUM_FS}" font-weight="${today || holiday ? 600 : 400}" fill="${fill}" style="${TAB}">${dayNum}</text>`);
128
+ // 假日的名字
129
+ const dl = ctx.dayOf(iso)?.label;
130
+ if (dl) {
131
+ const dfill = holiday ? 'var(--d-mark)' : 'var(--d-text)';
132
+ parts.push(`<text x="${r2(cx + 6)}" y="${r2(cy + 16)}" font-size="${EV_FS}" fill="${dfill}">${esc(fitText(dl, cw - 30, EV_FS))}</text>`);
133
+ }
134
+ // 事件
135
+ const items = ctx.itemsOn(iso);
136
+ const room = cw - 26; // 小圆点 + 边距
137
+ if (dense) {
138
+ // 格子窄:只摆小圆点(哪些天有事),名字留给周历/备注
139
+ items.slice(0, 8).forEach((it, i) => {
140
+ const dx = cx + 8 + (i % 4) * 9;
141
+ const dy = cy + 30 + Math.floor(i / 4) * 11;
142
+ parts.push(`<circle cx="${r2(dx)}" cy="${r2(dy)}" r="3" fill="${ctx.colorOf(it.kind)}"/>`);
143
+ });
144
+ }
145
+ else {
146
+ const maxRows = Math.max(1, Math.floor((ch - 22) / 15));
147
+ items.slice(0, maxRows).forEach((it, i) => {
148
+ const ey = cy + 32 + i * 15;
149
+ parts.push(`<circle cx="${r2(cx + 9)}" cy="${r2(ey - 4)}" r="3" fill="${ctx.colorOf(it.kind)}"/>`);
150
+ // 月历不写时间(苹果的月视图也不写)—— 格子窄,时间会把名字挤没
151
+ const label = it.label;
152
+ parts.push(`<text x="${r2(cx + 16)}" y="${r2(ey)}" font-size="${EV_FS}" fill="var(--d-text)">${esc(fitText(label, room, EV_FS))}</text>`);
153
+ });
154
+ // 下一行:先是「+N」(自己占一行),再排备注 —— 备注跟「+N」挤一行会被盖住
155
+ let row = maxRows;
156
+ if (items.length > maxRows) {
157
+ parts.push(`<text x="${r2(cx + 6)}" y="${r2(cy + 32 + row * 15)}" font-size="11" fill="var(--d-muted)">+${items.length - maxRows}</text>`);
158
+ // 没画出来的别丢 —— 提示单元会把它们列出来
159
+ for (const it of items.slice(maxRows))
160
+ ctx.overflow.push({ date: iso, label: it.label, time: it.time, kind: it.kind });
161
+ row++;
162
+ }
163
+ // 当天的备注
164
+ const notes = ctx.spec.notes.filter((n) => n.date === iso);
165
+ notes.forEach((n, i) => {
166
+ const ny2 = cy + 32 + (row + i) * 15;
167
+ if (ny2 > cy + ch - 4) {
168
+ // 格子装不下:也别压到下一格 —— 交给提示单元
169
+ ctx.overflow.push({ date: iso, label: n.text, kind: 'note' });
170
+ return;
171
+ }
172
+ parts.push(`<text x="${r2(cx + 6)}" y="${r2(ny2)}" font-size="11" fill="var(--d-muted)">${esc(fitText(n.text, cw - 12, 11))}</text>`);
173
+ });
174
+ }
175
+ }
176
+ }
177
+ // 底边
178
+ parts.push(`<line x1="${r2(x)}" y1="${r2(y0 + rows * ch)}" x2="${r2(x + w)}" y2="${r2(y0 + rows * ch)}" stroke="var(--d-grid)" stroke-width="1"/>`);
179
+ return parts.join('');
180
+ }
181
+ /** 月份的大标题(2026年11月) */
182
+ function monthLabel(ctx, y, m) {
183
+ const en = ctx.skin.rules.title === 'editorial';
184
+ return en ? `${MONTH_EN[m - 1]} ${y}` : `${y}年${m}月`;
185
+ }
186
+ /* ---------------------------------------------------------------- 周/课程表 */
187
+ /** 时间网格:列 = 几天,行 = 时段 */
188
+ function timeGrid(ctx, days, periods, x, y0, w, h, dense = false) {
189
+ const parts = [];
190
+ const timeW = 46;
191
+ const cols = days.length;
192
+ const cw = (w - timeW) / cols;
193
+ const rows = periods.length;
194
+ const ch = h / rows;
195
+ // 周头
196
+ days.forEach((d, c) => {
197
+ const wd = isoWeekday(d);
198
+ const label = dense ? WEEK_CN[wd] : (ctx.skin.rules.title === 'editorial' ? WEEK_EN[wd] : WEEK_CN[wd]);
199
+ parts.push(`<text x="${r2(x + timeW + c * cw + cw / 2)}" y="${r2(y0 - 8)}" text-anchor="middle" font-size="${HEAD_FS}" fill="var(--d-muted)">${esc(label)}</text>`);
200
+ const dn = Number(d.slice(8));
201
+ const holiday = isHoliday(ctx, d);
202
+ parts.push(`<text x="${r2(x + timeW + c * cw + cw / 2)}" y="${r2(y0 - 24)}" text-anchor="middle" font-size="${NUM_FS}" font-weight="600" fill="${holiday ? 'var(--d-mark)' : d === ctx.today ? 'var(--d-ink)' : 'var(--d-muted)'}">${dn}</text>`);
203
+ });
204
+ // 行:时间标签 + 横线
205
+ periods.forEach((p, r) => {
206
+ const py = y0 + r * ch;
207
+ parts.push(`<text x="${r2(x + timeW - 8)}" y="${r2(py + ch / 2 + 4)}" text-anchor="end" font-size="11" fill="var(--d-muted)" style="${TAB}">${esc(p)}</text>`);
208
+ parts.push(`<line x1="${r2(x + timeW)}" y1="${r2(py)}" x2="${r2(x + w)}" y2="${r2(py)}" stroke="var(--d-grid)" stroke-width="1"/>`);
209
+ });
210
+ // 竖线(每天之间)
211
+ for (let c = 0; c <= cols; c++) {
212
+ parts.push(`<line x1="${r2(x + timeW + c * cw)}" y1="${r2(y0)}" x2="${r2(x + timeW + c * cw)}" y2="${r2(y0 + rows * ch)}" stroke="var(--d-grid)" stroke-width="1"/>`);
213
+ }
214
+ // 事件:按时间落格
215
+ for (const d of days) {
216
+ const c = days.indexOf(d);
217
+ const items = ctx.itemsOn(d);
218
+ for (const it of items) {
219
+ const t = it.time ?? periods[0];
220
+ // 落在最后一个「开始时间 ≤ 事件时间」的槽里
221
+ let r = -1;
222
+ periods.forEach((pp, i) => { if (pp <= t)
223
+ r = i; });
224
+ if (r < 0)
225
+ r = 0;
226
+ const bx = x + timeW + c * cw + 3;
227
+ const by = y0 + r * ch + 3;
228
+ const bw = cw - 6;
229
+ const bh = Math.max(16, ch - 6);
230
+ parts.push(`<rect x="${r2(bx)}" y="${r2(by)}" width="${r2(bw)}" height="${r2(bh)}" rx="6" fill="${ctx.colorOf(it.kind)}" fill-opacity="0.14"/>`, `<rect x="${r2(bx)}" y="${r2(by)}" width="3" height="${r2(bh)}" rx="1.5" fill="${ctx.colorOf(it.kind)}"/>`, `<text x="${r2(bx + 8)}" y="${r2(by + 14)}" font-size="11" fill="var(--d-ink)">${esc(trunc(it.label, 14))}</text>`);
231
+ }
232
+ }
233
+ return parts.join('');
234
+ }
235
+ /* ---------------------------------------------------------------- 排期表 */
236
+ /** 排期表:行 = 事项(带区间),列 = 天 */
237
+ function planBoard(ctx, start, days, x, y0, w, rowH) {
238
+ const parts = [];
239
+ const labelW = 150;
240
+ const axisW = w - labelW;
241
+ const per = axisW / days;
242
+ const d0 = isoDay(start);
243
+ const rows = ctx.spec.items;
244
+ // 天头(每 7 天一个刻度)
245
+ for (let i = 0; i <= days; i += 7) {
246
+ const gx = x + labelW + i * per;
247
+ parts.push(`<line x1="${r2(gx)}" y1="${r2(y0 - 14)}" x2="${r2(gx)}" y2="${r2(y0 + rows.length * rowH)}" stroke="var(--d-grid)" stroke-width="1"/>`);
248
+ const dd = dayIso(d0 + i);
249
+ if (gx + 40 < x + w) {
250
+ parts.push(`<text x="${r2(gx + 4)}" y="${r2(y0 - 18)}" font-size="11" fill="var(--d-muted)" style="${TAB}">${esc(dd.slice(5))}</text>`);
251
+ }
252
+ }
253
+ rows.forEach((it, r) => {
254
+ const ry = y0 + r * rowH;
255
+ parts.push(`<line x1="${r2(x)}" y1="${r2(ry)}" x2="${r2(x + w)}" y2="${r2(ry)}" stroke="var(--d-grid)" stroke-width="1"/>`);
256
+ parts.push(`<text x="${r2(x + 4)}" y="${r2(ry + rowH / 2 + 4)}" font-size="12" fill="var(--d-ink)">${esc(trunc(it.label, 20))}</text>`);
257
+ const s = isoDay(it.date);
258
+ const e = isoDay(it.until ?? it.date);
259
+ const bx = x + labelW + (s - d0) * per;
260
+ const bw = Math.max(per * 0.6, (e - s + 1) * per - 4);
261
+ parts.push(`<rect x="${r2(bx + 2)}" y="${r2(ry + rowH / 2 - 9)}" width="${r2(bw)}" height="18" rx="5" fill="${ctx.colorOf(it.kind)}" fill-opacity="0.85"/>`);
262
+ });
263
+ const by = y0 + rows.length * rowH;
264
+ parts.push(`<line x1="${r2(x)}" y1="${r2(by)}" x2="${r2(x + w)}" y2="${r2(by)}" stroke="var(--d-grid)" stroke-width="1"/>`);
265
+ // 今天那根线
266
+ const tx = x + labelW + (isoDay(ctx.today) - d0) * per;
267
+ if (tx > x + labelW && tx < x + w) {
268
+ parts.push(`<line x1="${r2(tx)}" y1="${r2(y0 - 14)}" x2="${r2(tx)}" y2="${r2(by)}" stroke="var(--d-mark)" stroke-width="1.5"/>`);
269
+ }
270
+ return parts.join('');
271
+ }
272
+ /* ---------------------------------------------------------------- 主入口 */
273
+ /**
274
+ * Render a calendar (month / week / year / bi-month / quarter / course / plan).
275
+ */
276
+ export function renderCalendar(spec, o) {
277
+ const { width, height, version, skin } = o;
278
+ const ws = spec.weekStart === 1 ? 1 : 0;
279
+ const ctx = buildCtx(spec, o, ws);
280
+ const start = spec.start ?? ctx.today;
281
+ const p = isoParts(start);
282
+ let title = spec.title ?? '';
283
+ // 图例:出现过的类别(坐底部一行 —— 类别多的时候放标题那行会压住标题)
284
+ const kinds = [...new Set(spec.items.map((i) => i.kind))];
285
+ const legendItems = kinds.map((k) => ({
286
+ name: KINDS[k] ? (skin.rules.title === 'editorial' ? KINDS[k].en : KINDS[k].zh) : k,
287
+ color: ctx.colorOf(k),
288
+ }));
289
+ const legendH = legendItems.length ? 26 : 0;
290
+ const bodyTop = 62;
291
+ // 页脚(统计/来源)—— 底部一行;提示单元在它上面
292
+ const footerH = 24;
293
+ const bodyW = width - PAD * 2;
294
+ // 标题:与格子高度无关,先定下来
295
+ if (!title) {
296
+ if (spec.layout === 'month') {
297
+ title = skin.rules.title === 'editorial' ? `${MONTH_EN[p.m - 1]} ${p.y}` : `${p.y}年${p.m}月`;
298
+ }
299
+ else if (spec.layout === 'year') {
300
+ title = `${p.y}`;
301
+ }
302
+ else if (spec.layout === 'plan') {
303
+ title = '排期';
304
+ }
305
+ }
306
+ /** 主体:按给定的高度画一遍(高度会被提示单元挤掉一点,所以可能要画两遍) */
307
+ const drawBody = (bodyH) => {
308
+ const parts = [];
309
+ if (spec.layout === 'month') {
310
+ parts.push(monthGrid(ctx, p.y, p.m, PAD, bodyTop, bodyW, bodyH));
311
+ }
312
+ else if (spec.layout === 'bimonth' || spec.layout === 'quarter') {
313
+ const n = spec.layout === 'bimonth' ? 2 : 3;
314
+ const months = spec.months?.length
315
+ ? spec.months.slice(0, n).map((mm) => isoParts(`${mm}-01`))
316
+ : Array.from({ length: n }, (_, i) => {
317
+ const d = new Date(Date.UTC(p.y, p.m - 1 + i, 1));
318
+ return { y: d.getUTCFullYear(), m: d.getUTCMonth() + 1 };
319
+ });
320
+ const gap = 24;
321
+ const mw = (width - PAD * 2 - gap * (n - 1)) / n;
322
+ const headY = bodyTop + 20; // 周头
323
+ const gridY = bodyTop + 28; // 网格顶
324
+ months.forEach((mm, i) => {
325
+ const mx = PAD + i * (mw + gap);
326
+ parts.push(`<text x="${r2(mx)}" y="${r2(bodyTop)}" font-size="15" font-weight="600" fill="var(--d-ink)">${esc(monthLabel(ctx, mm.y, mm.m))}</text>`);
327
+ const noteH = 56;
328
+ parts.push(monthGrid(ctx, mm.y, mm.m, mx, gridY, mw, bodyH - 28 - noteH, n === 3, headY));
329
+ // 这个月的注意事项(横向:放在下面;纵向:放在右边)
330
+ const mkey = `${mm.y}-${String(mm.m).padStart(2, '0')}`;
331
+ const notes = ctx.spec.notes.filter((nt) => !nt.date || nt.date.startsWith(mkey));
332
+ if (notes.length) {
333
+ const vertical = spec.notesPos === 'right' || spec.notesPos === 'left';
334
+ if (vertical) {
335
+ const nx = spec.notesPos === 'left' ? mx - 120 : mx + mw + 6;
336
+ notes.slice(0, 6).forEach((nt, k) => {
337
+ parts.push(`<circle cx="${r2(nx + 3)}" cy="${r2(bodyTop + 6 + k * 16)}" r="2.5" fill="var(--d-faint)"/>`);
338
+ parts.push(`<text x="${r2(nx + 10)}" y="${r2(bodyTop + 10 + k * 16)}" font-size="11" fill="var(--d-muted)">${esc(trunc(nt.text, 16))}</text>`);
339
+ });
340
+ }
341
+ else {
342
+ notes.slice(0, 3).forEach((nt, k) => {
343
+ parts.push(`<text x="${r2(mx + 4)}" y="${r2(bodyTop + bodyH - noteH + 18 + k * 16)}" font-size="11" fill="var(--d-muted)">${esc(trunc(nt.text, n === 3 ? 22 : 34))}</text>`);
344
+ });
345
+ }
346
+ }
347
+ });
348
+ }
349
+ else if (spec.layout === 'year') {
350
+ title = title || `${p.y}`;
351
+ const cols = 4;
352
+ const rows = 3;
353
+ const gap = 14;
354
+ const mw = (width - PAD * 2 - gap * (cols - 1)) / cols;
355
+ const mh = (bodyH - gap * (rows - 1)) / rows;
356
+ for (let i = 0; i < 12; i++) {
357
+ const r = Math.floor(i / cols);
358
+ const c = i % cols;
359
+ const mx = PAD + c * (mw + gap);
360
+ const my = bodyTop + r * (mh + gap);
361
+ const mm = { y: p.y, m: i + 1 };
362
+ parts.push(`<text x="${r2(mx)}" y="${r2(my + 12)}" font-size="12" font-weight="600" fill="var(--d-ink)">${esc(skin.rules.title === 'editorial' ? MONTH_EN[i] : `${i + 1}月`)}</text>`);
363
+ parts.push(yearMini(ctx, mm.y, mm.m, mx, my + 20, mw, mh - 26));
364
+ }
365
+ }
366
+ else if (spec.layout === 'week' || spec.layout === 'course') {
367
+ const d0 = isoDay(start);
368
+ const wd = isoWeekday(start);
369
+ const back = (wd - ws + 7) % 7;
370
+ const days = Array.from({ length: 7 }, (_, i) => dayIso(d0 - back + i));
371
+ const periods = spec.periods?.length ? spec.periods : DEFAULT_PERIODS;
372
+ if (!title)
373
+ title = spec.layout === 'course' ? '课程表' : `${days[0].slice(5)} – ${days[6].slice(5)}`;
374
+ parts.push(timeGrid(ctx, days, periods, PAD, bodyTop + 18, width - PAD * 2, bodyH - 18));
375
+ }
376
+ else {
377
+ // plan
378
+ const days = 56;
379
+ parts.push(planBoard(ctx, start, days, PAD, bodyTop + 18, width - PAD * 2, Math.max(22, (bodyH - 18) / Math.max(1, spec.items.length))));
380
+ }
381
+ return parts;
382
+ };
383
+ // 先按「没有提示单元」画一遍:看看有没有塞不下的东西
384
+ ctx.overflow.length = 0;
385
+ let parts = drawBody(height - bodyTop - legendH - footerH - 14);
386
+ let tipH = 0;
387
+ if (ctx.overflow.length) {
388
+ // 有塞不下的 → 页脚上方加一块提示,把明细列清楚;主体相应矮一点(重画一遍)
389
+ tipH = tipHeight(ctx.overflow.length, bodyW);
390
+ ctx.overflow.length = 0;
391
+ parts = drawBody(height - bodyTop - legendH - footerH - tipH - 14);
392
+ }
393
+ // 页头:标题 + 右边一句摘要
394
+ const nItems = spec.items.length;
395
+ const nDays = spec.days.length;
396
+ const nNotes = spec.notes.length;
397
+ const meta = [nItems ? `${nItems} 个日程` : '', nNotes ? `${nNotes} 条备注` : ''].filter(Boolean).join(' · ');
398
+ // 右边让开 Edit @X-CMD 角标(它占右上角那一块,最多 ~100px)
399
+ const badgeRoom = Math.min(108, bodyW * 0.32);
400
+ const titleSvg = titleBlock(title) + headerMeta(meta, PAD, 38, bodyW - badgeRoom);
401
+ // 图例:出现过的类别(坐底部一行 —— 类别多的时候放标题那行会压住标题)
402
+ const legendSvg = legendItems.length
403
+ ? `<g transform="translate(0 ${r2(height - footerH - 18)})">${topLegend(legendItems, PAD, 0, 'left', 'circle')}</g>`
404
+ : '';
405
+ // 提示单元:主体里没画下的明细(格子太密时装不下的日程)
406
+ const tipRows = ctx.overflow.map((o) => ({
407
+ text: o.label,
408
+ sub: o.time ? `${o.date.slice(5)} ${o.time}` : o.date.slice(5),
409
+ color: o.kind === 'note' ? 'var(--d-faint)' : ctx.colorOf(o.kind),
410
+ }));
411
+ const tipSvg = tipRows.length
412
+ ? tipUnit(tipRows, PAD, height - footerH - 18 - (legendH ? 26 : 0) - tipHeight(tipRows.length, bodyW) - 6, bodyW, '下面这些没画进格子')
413
+ : '';
414
+ // 页脚:统计 + 来源
415
+ const footerSvg = footerUnit([
416
+ { label: '日程', value: String(nItems) },
417
+ ...(nDays ? [{ label: '标记', value: String(nDays) }] : []),
418
+ ...(nNotes ? [{ label: '备注', value: String(nNotes) }] : []),
419
+ ], spec.source, PAD, height - 10, bodyW);
420
+ const bg = card(width, height, skin.rules.canvas === 'plain');
421
+ const svg = svgDoc(width, height, 'calendar', version, title || 'calendar', spec, skin, bg + titleSvg + legendSvg + tipSvg + footerSvg + parts.join(''));
422
+ return svg;
423
+ }
424
+ /** 年历里的迷你月:只有日期号,有事的日子带一个小点 */
425
+ function yearMini(ctx, y, m, x, y0, w, h) {
426
+ const parts = [];
427
+ const cols = 7;
428
+ const cw = w / cols;
429
+ const first = firstWeekday(y, m);
430
+ const dim = daysInMonth(y, m);
431
+ const rows = Math.ceil((first + dim) / 7);
432
+ const ch = h / rows;
433
+ const fs = Math.max(7, Math.min(10, Math.floor(cw * 0.62)));
434
+ for (let i = 0; i < dim; i++) {
435
+ const r = Math.floor((first + i) / 7);
436
+ const c = (first + i) % 7;
437
+ const iso = dayIso(Date.UTC(y, m - 1, i + 1) / 86400000);
438
+ const rest = isRest(ctx, iso);
439
+ const holiday = isHoliday(ctx, iso);
440
+ const nx = x + c * cw + cw / 2;
441
+ const ny = y0 + r * ch + ch / 2 + 3;
442
+ parts.push(`<text x="${r2(nx)}" y="${r2(ny)}" text-anchor="middle" font-size="${fs}" fill="${holiday ? 'var(--d-mark)' : rest ? 'var(--d-faint)' : 'var(--d-text)'}">${i + 1}</text>`);
443
+ if (ctx.itemsOn(iso).length) {
444
+ parts.push(`<circle cx="${r2(nx)}" cy="${r2(ny + 4)}" r="1.6" fill="var(--d-s2)"/>`);
445
+ }
446
+ }
447
+ return parts.join('');
448
+ }
@@ -42,8 +42,9 @@ export function renderCombo(spec, o) {
42
42
  const y = (v) => bottom - (v / yMax) * (bottom - contentTop);
43
43
  const lineYMax = lineTicks[lineTicks.length - 1];
44
44
  const yLine = (v) => bottom - (v / lineYMax) * (bottom - contentTop);
45
+ // 卡片垫底(标题压在卡片之上)
46
+ const bg = card(width, height);
45
47
  const parts = [
46
- card(width, height),
47
48
  topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i], ...(o.interactive ? { index: i } : {}) })), width - pad, pad + 14),
48
49
  ];
49
50
  for (const t of ticks) {
@@ -190,7 +191,7 @@ export function renderCombo(spec, o) {
190
191
  const mpts = markPoints(o.marks, spec.labels, (i) => left + step * i + step / 2, y, [contentTop, bottom]);
191
192
  if (mpts.length > 0)
192
193
  parts.push(marksLayer(mpts, width));
193
- return svgDoc(width, height, 'combo', version, spec.title ?? 'combo chart', spec, skin, titleBlock(spec.title) + parts.join(''));
194
+ return svgDoc(width, height, 'combo', version, spec.title ?? 'combo chart', spec, skin, bg + titleBlock(spec.title) + parts.join(''));
194
195
  }
195
196
  // 混合图 1 · Evil 柱线组合(规范 combo-1,960×500):
196
197
  // 紫色圆角柱(0-100)+ 绿色折线(独立量程),虚线网格,右上方块图例,无数值标注
@@ -0,0 +1,6 @@
1
+ import type { CompassSpec } from '../specs/index.js';
2
+ import { type RenderOpts } from './svg.js';
3
+ /**
4
+ * Compass chart: four labelled arrows radiating from a center point.
5
+ */
6
+ export declare function renderCompass(spec: CompassSpec, o: RenderOpts): string;
@@ -0,0 +1,77 @@
1
+ import { card } from '../theme.js';
2
+ import { esc, r2, textW, trunc, svgDoc, titleBlock, sVar } from './svg.js';
3
+ const ARROW = 12; // 箭头长度
4
+ const BASE = 5.5; // 箭身根部半宽
5
+ // 字号偏大:看这图的人年长,字要看得清
6
+ const FS_LABEL = 15; // 方向标签
7
+ const FS_NOTE = 13; // 副说明
8
+ /**
9
+ * Compass chart: four labelled arrows radiating from a center point.
10
+ */
11
+ export function renderCompass(spec, o) {
12
+ const { width, height, version, skin } = o;
13
+ const parts = [card(width, height, skin.rules.canvas === 'plain')];
14
+ const titleH = spec.title ? 44 : 12;
15
+ const cx = width / 2;
16
+ const cy = titleH + (height - titleH) / 2;
17
+ // 箭头长度:留出端点标签的位置(标签在端点外,再留一行副说明)
18
+ const arm = Math.max(64, Math.min(width, height) * 0.28);
19
+ const arms = [
20
+ { dir: 'up', a: spec.up ?? {}, ux: 0, uy: -1 },
21
+ { dir: 'right', a: spec.right ?? {}, ux: 1, uy: 0 },
22
+ { dir: 'down', a: spec.down ?? {}, ux: 0, uy: 1 },
23
+ { dir: 'left', a: spec.left ?? {}, ux: -1, uy: 0 },
24
+ ];
25
+ let idx = 0;
26
+ for (const { a, ux, uy } of arms) {
27
+ if (!a.label && !a.note)
28
+ continue;
29
+ const color = sVar(idx % skin.series.length);
30
+ idx++;
31
+ const x2 = cx + ux * arm;
32
+ const y2 = cy + uy * arm;
33
+ const px = -uy;
34
+ const py = ux;
35
+ // 箭身:根部宽、末端收成尖(锥形),比一根细线有分量
36
+ const bx = cx + ux * (arm - ARROW);
37
+ const by = cy + uy * (arm - ARROW);
38
+ parts.push(`<path d="M${r2(cx + px * BASE)} ${r2(cy + py * BASE)}L${r2(bx + px * BASE)} ${r2(by + py * BASE)}L${r2(x2)} ${r2(y2)}L${r2(bx - px * BASE)} ${r2(by - py * BASE)}L${r2(cx - px * BASE)} ${r2(cy - py * BASE)}Z" fill="${color}" fill-opacity="0.16" stroke="${color}" stroke-width="1.6" stroke-linejoin="round"/>`);
39
+ // 端点标签:胶囊(同色小圆点 + 文字),上下方向居中、左右方向顺排
40
+ const label = a.label ? trunc(a.label, 8) : '';
41
+ const note = a.note ? trunc(a.note, 14) : '';
42
+ const tw = Math.max(textW(label, FS_LABEL), textW(note, FS_NOTE));
43
+ const pillW = tw + 34;
44
+ const pillH = a.note ? 41 : 26;
45
+ if (ux === 0) {
46
+ const py2 = y2 + (uy < 0 ? -pillH - 12 : 12);
47
+ // 圆点 + 文字作为一组居中(圆点占位,不然文字看着偏)
48
+ const tw2 = textW(label, FS_LABEL);
49
+ const dotX = cx - (tw2 + 14) / 2;
50
+ parts.push(`<rect x="${r2(cx - pillW / 2)}" y="${r2(py2)}" width="${r2(pillW)}" height="${pillH}" rx="${pillH / 2}" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1"/>`);
51
+ parts.push(`<circle cx="${r2(dotX)}" cy="${r2(py2 + (a.note ? 12 : 11))}" r="5" fill="${color}"/>`);
52
+ parts.push(`<text x="${r2(dotX + 10)}" y="${r2(py2 + (a.note ? 17 : 16))}" font-size="${FS_LABEL}" font-weight="600" fill="var(--d-ink)">${esc(label)}</text>`);
53
+ if (a.note) {
54
+ parts.push(`<text x="${r2(cx)}" y="${r2(py2 + 34)}" font-size="${FS_NOTE}" text-anchor="middle" fill="var(--d-muted)">${esc(note)}</text>`);
55
+ }
56
+ }
57
+ else {
58
+ const px2 = x2 + ux * 14;
59
+ parts.push(`<rect x="${r2(ux > 0 ? px2 : px2 - pillW)}" y="${r2(cy - pillH / 2)}" width="${r2(pillW)}" height="${pillH}" rx="${pillH / 2}" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1"/>`);
60
+ const lx = ux > 0 ? px2 + 8 : px2 - 8;
61
+ parts.push(`<circle cx="${r2(lx + (ux > 0 ? 4 : -4))}" cy="${r2(cy - (a.note ? 5 : 0))}" r="5" fill="${color}"/>`);
62
+ parts.push(`<text x="${r2(lx + (ux > 0 ? 13 : -13))}" y="${r2(cy - (a.note ? 2 : 5))}" font-size="${FS_LABEL}" font-weight="600" text-anchor="${ux > 0 ? 'start' : 'end'}" fill="var(--d-ink)">${esc(label)}</text>`);
63
+ if (a.note) {
64
+ // 说明对齐到**标签文字**的中线(不是整个胶囊)—— 圆点占了位,不然看着歪
65
+ const nc = ux > 0 ? lx + 13 + textW(label, FS_LABEL) / 2 : lx - 13 - textW(label, FS_LABEL) / 2;
66
+ parts.push(`<text x="${r2(nc)}" y="${r2(cy + 15)}" font-size="${FS_NOTE}" text-anchor="middle" fill="var(--d-muted)">${esc(note)}</text>`);
67
+ }
68
+ }
69
+ }
70
+ // 中心盘:外圈浅色 + 内点深色,中心的字放在盘的右上(不压盘)
71
+ parts.push(`<circle cx="${r2(cx)}" cy="${r2(cy)}" r="13" fill="var(--d-card)" stroke="var(--d-border)" stroke-width="1.5"/>`);
72
+ parts.push(`<circle cx="${r2(cx)}" cy="${r2(cy)}" r="4.5" fill="var(--d-ink)"/>`);
73
+ if (spec.center) {
74
+ parts.push(`<text x="${r2(cx + 20)}" y="${r2(cy - 21)}" font-size="${FS_LABEL}" font-weight="600" fill="var(--d-ink)">${esc(trunc(spec.center, 8))}</text>`);
75
+ }
76
+ return svgDoc(width, height, 'compass', version, spec.title ?? 'compass chart', spec, skin, titleBlock(spec.title) + parts.join(''));
77
+ }
@@ -1,5 +1,5 @@
1
1
  // 模板分发:ChartTemplate(+ChartInput/存档 spec)→ 渲染器。短链与 create 共用。
2
- import { ApiError, parseChartSpec, parseSeriesSpec, parseGaugeSpec, parseScatterSpec, parseComboSpec, looksMultiSeries } from '../specs/index.js';
2
+ import { ApiError, parseChartSpec, parseSeriesSpec, parseGaugeSpec, parseScatterSpec, parseComboSpec, parseLadderSpec, looksMultiSeries } from '../specs/index.js';
3
3
  import { renderPie } from './pie.js';
4
4
  import { renderBar } from './bar.js';
5
5
  import { renderBarGrid } from './bar-grid.js';
@@ -15,7 +15,18 @@ import { renderScatter } from './scatter.js';
15
15
  import { renderCombo } from './combo.js';
16
16
  import { renderQR, requireQRText, parseColor, QR_DEFAULTS } from './qr.js';
17
17
  import { renderSankey } from './sankey.js';
18
+ import { renderQuadrant } from './quadrant.js';
19
+ import { renderCompass } from './compass.js';
18
20
  import { parseSankeySpec } from '../specs/sankey.js';
21
+ import { parseQuadrantSpec } from '../specs/quadrant.js';
22
+ import { parseCompassSpec } from '../specs/compass.js';
23
+ import { parseButterflySpec } from '../specs/butterfly.js';
24
+ import { parseCalendarSpec } from '../specs/calendar.js';
25
+ import { renderButterfly } from './butterfly.js';
26
+ import { renderLadder, renderButterflyLadder } from './ladder.js';
27
+ import { renderCalendar } from './calendar.js';
28
+ import { renderTable } from './table.js';
29
+ import { parseTableSpec } from '../specs/table.js';
19
30
  function dispatch(template, input, o, stack = false) {
20
31
  switch (template) {
21
32
  case 'pie':
@@ -65,6 +76,20 @@ export function renderStored(doc, o) {
65
76
  return renderCombo(parseComboSpec(specJson), o);
66
77
  if (doc.template === 'sankey')
67
78
  return renderSankey(parseSankeySpec(specJson), o);
79
+ if (doc.template === 'quadrant')
80
+ return renderQuadrant(parseQuadrantSpec(specJson), o);
81
+ if (doc.template === 'compass')
82
+ return renderCompass(parseCompassSpec(specJson), o);
83
+ if (doc.template === 'butterfly')
84
+ return renderButterfly(parseButterflySpec(specJson), o);
85
+ if (doc.template === 'ladder')
86
+ return renderLadder(parseLadderSpec(specJson), o);
87
+ if (doc.template === 'butterfly-ladder')
88
+ return renderButterflyLadder(parseLadderSpec(specJson), o);
89
+ if (doc.template === 'calendar')
90
+ return renderCalendar(parseCalendarSpec(specJson), o);
91
+ if (doc.template === 'table')
92
+ return renderTable(parseTableSpec(specJson), o);
68
93
  const input = looksMultiSeries(specJson)
69
94
  ? { multi: true, spec: parseSeriesSpec(specJson, doc.template === 'line' || doc.template === 'area' ? 'line' : 'bar') }
70
95
  : { multi: false, spec: parseChartSpec(specJson, doc.template) };
@@ -1,5 +1,35 @@
1
1
  import { type DrawResult } from './query.js';
2
2
  type Obj = Record<string, unknown>;
3
+ /** 地图布局缺省(服务端与浏览器端同一套规则 —— 见 explore 页的同名实现) */
4
+ export declare function mapLayout(o: {
5
+ name: string;
6
+ width: number;
7
+ height: number;
8
+ /** 图例是否横排在底部(横排才占底部那条带) */
9
+ legendAtBottom: boolean;
10
+ }): Obj;
11
+ export declare const HEAD_RULES: {
12
+ readonly titleTop: 24;
13
+ readonly titleLeft: 19;
14
+ readonly titleFont: 18;
15
+ readonly titlePad: 5;
16
+ readonly subFont: 12;
17
+ readonly subGap: 10;
18
+ readonly titleGap: 8;
19
+ readonly legendRow: 20;
20
+ readonly legendGap: 8;
21
+ readonly gridTop: 60;
22
+ };
23
+ /**
24
+ * 把标题带、图例、绘图区排开:图例在顶部且会压到标题时往下推,
25
+ * 绘图区被压到时也往下推。只往下推 —— 用户给的空间永远算数。
26
+ * 就地改 option(调用方先拷好 title/legend/grid)。
27
+ *
28
+ * **自包含**:这个函数不引用模块外的任何东西(HEAD_RULES 除外,那个由调用方
29
+ * 注入)—— 浏览器端(explore 页重画)直接跑同一份源码,两边不会走偏。
30
+ * hub 那边用 `headLayout.toString()` 把源码塞进页面脚本。
31
+ */
32
+ export declare function headLayout(option: Obj): void;
3
33
  /**
4
34
  * Render a parsed ECharts option object to a skinned SVG — shared by the HTTP
5
35
  * layer (`?d=` urlencoded JSON) and the CLI (inline JSON / file / stdin, where