@x-cmd/draw 0.5.0 → 0.5.2
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/client.d.ts +4 -2
- package/dist/client.js +23 -5
- package/dist/draw-client.iife.js +51 -0
- package/dist/draw-echarts.iife.js +1 -0
- package/dist/echarts-client.d.ts +34 -0
- package/dist/echarts-client.js +109 -0
- package/dist/render/echarts-ssr.d.ts +4 -0
- package/dist/render/echarts-ssr.js +3 -2
- package/dist/render/gantt.js +38 -32
- package/dist/render/gantt2.js +10 -7
- package/dist/render/globe-proj.d.ts +2 -1
- package/dist/render/hbar-spec.js +25 -15
- package/dist/render/svg.d.ts +10 -0
- package/dist/render/svg.js +12 -0
- package/package.json +8 -3
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
type Obj = Record<string, unknown>;
|
|
2
|
+
interface EChartsLike {
|
|
3
|
+
init(el: HTMLElement, theme?: unknown, opts?: unknown): {
|
|
4
|
+
setOption(o: unknown, notMerge?: boolean): void;
|
|
5
|
+
resize(): void;
|
|
6
|
+
dispose(): void;
|
|
7
|
+
};
|
|
8
|
+
registerMap(name: string, geo: unknown): void;
|
|
9
|
+
}
|
|
10
|
+
declare global {
|
|
11
|
+
interface Window {
|
|
12
|
+
echarts?: EChartsLike;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** 等 CDN 的 echarts 到货(最多等 15 秒) */
|
|
16
|
+
export declare function waitEcharts(timeoutMs?: number): Promise<EChartsLike | null>;
|
|
17
|
+
/**
|
|
18
|
+
* 把一张 echarts 图画进元素。
|
|
19
|
+
*
|
|
20
|
+
* @param el 容器
|
|
21
|
+
* @param option 用户 option(与 /draw/echarts?d=… 里的那份等价)
|
|
22
|
+
* @param size 画布大小(缺省用容器自己的)
|
|
23
|
+
*/
|
|
24
|
+
export declare function renderEchartsTo(el: HTMLElement, option: Obj, size?: {
|
|
25
|
+
w: number;
|
|
26
|
+
h: number;
|
|
27
|
+
}): Promise<boolean>;
|
|
28
|
+
/**
|
|
29
|
+
* 扫页面,把带 `data-echarts` 的元素都画出来。
|
|
30
|
+
*
|
|
31
|
+
* <div data-echarts='{"type":"bar","xAxis":{"type":"category","data":["A","B"]},"series":[{"type":"bar","data":[1,2]}]}'></div>
|
|
32
|
+
*/
|
|
33
|
+
export declare function autoRenderEcharts(root?: ParentNode): Promise<number>;
|
|
34
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
// echarts 族的**浏览器入口** —— 单独一个,因为它重。
|
|
2
|
+
//
|
|
3
|
+
// echarts 自己 1MB+ ⇒ 不进主包(`@x-cmd/draw/client` 里没有它)⇒
|
|
4
|
+
// 浏览器端用 **CDN 的 echarts**:
|
|
5
|
+
// <script src="https://cdn.jsdelivr.net/npm/echarts@6/dist/echarts.min.js"></script>
|
|
6
|
+
// <script src="https://cdn.jsdelivr.net/npm/@x-cmd/draw@latest/echarts"></script>
|
|
7
|
+
// <div data-echarts='{"type":"bar","xAxis":{...},"series":[...]}'></div>
|
|
8
|
+
//
|
|
9
|
+
// 与服务端那条路(/draw/echarts?d=…)**同一份 option 补缺省逻辑** ⇒ 两端同图 ✓
|
|
10
|
+
// (服务端是 SSR 出静态 SVG;浏览器端是真 echarts ⇒ 有动画、有 tooltip、能点)
|
|
11
|
+
//
|
|
12
|
+
// 「一份实现」的边界:option 的补缺省、地图底图、皮肤色板在库里一份;
|
|
13
|
+
// echarts 本体是外部库(浏览器端从 CDN 取,服务端 npm 装)—— 不是我们抄一份。
|
|
14
|
+
import { echartsOption } from './render/echarts-ssr.js';
|
|
15
|
+
import { resolveSkin } from './styles/index.js';
|
|
16
|
+
import { loadMapGeoJson } from './render/maps.js';
|
|
17
|
+
/** 等 CDN 的 echarts 到货(最多等 15 秒) */
|
|
18
|
+
export async function waitEcharts(timeoutMs = 15000) {
|
|
19
|
+
const t0 = Date.now();
|
|
20
|
+
while (Date.now() - t0 < timeoutMs) {
|
|
21
|
+
if (typeof window !== 'undefined' && window.echarts)
|
|
22
|
+
return window.echarts;
|
|
23
|
+
await new Promise((r) => setTimeout(r, 60));
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
/** option 里用到哪些地图 ⇒ 把底图注册进去(浏览器端也要,否则 echarts 找不到) */
|
|
28
|
+
async function registerMaps(echarts, user) {
|
|
29
|
+
const names = new Set();
|
|
30
|
+
const collect = (x) => {
|
|
31
|
+
if (Array.isArray(x)) {
|
|
32
|
+
x.forEach(collect);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (!x || typeof x !== 'object')
|
|
36
|
+
return;
|
|
37
|
+
const o = x;
|
|
38
|
+
if (o.type === 'map' && typeof o.map === 'string')
|
|
39
|
+
names.add(o.map);
|
|
40
|
+
};
|
|
41
|
+
collect(user.series);
|
|
42
|
+
collect(user.geo);
|
|
43
|
+
for (const n of names) {
|
|
44
|
+
try {
|
|
45
|
+
const geo = await loadMapGeoJson(n);
|
|
46
|
+
if (geo)
|
|
47
|
+
echarts.registerMap(n, geo);
|
|
48
|
+
}
|
|
49
|
+
catch { /* 没收录的地图交给 echarts 自己报错 */ }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* 把一张 echarts 图画进元素。
|
|
54
|
+
*
|
|
55
|
+
* @param el 容器
|
|
56
|
+
* @param option 用户 option(与 /draw/echarts?d=… 里的那份等价)
|
|
57
|
+
* @param size 画布大小(缺省用容器自己的)
|
|
58
|
+
*/
|
|
59
|
+
export async function renderEchartsTo(el, option, size) {
|
|
60
|
+
const echarts = await waitEcharts();
|
|
61
|
+
if (!echarts) {
|
|
62
|
+
el.textContent = '[draw] echarts 没加载(引一下 CDN 的 echarts)';
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
await registerMaps(echarts, option);
|
|
66
|
+
const w = size?.w ?? el.clientWidth ?? 560;
|
|
67
|
+
const h = size?.h ?? el.clientHeight ?? 320;
|
|
68
|
+
const skin = resolveSkin('line', option.t ?? null, 'dark');
|
|
69
|
+
const opt = echartsOption(option, skin, w, h);
|
|
70
|
+
const inst = echarts.init(el, undefined, { renderer: 'canvas', width: w, height: h });
|
|
71
|
+
inst.setOption(opt, true);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 扫页面,把带 `data-echarts` 的元素都画出来。
|
|
76
|
+
*
|
|
77
|
+
* <div data-echarts='{"type":"bar","xAxis":{"type":"category","data":["A","B"]},"series":[{"type":"bar","data":[1,2]}]}'></div>
|
|
78
|
+
*/
|
|
79
|
+
export async function autoRenderEcharts(root = typeof document !== 'undefined' ? document : undefined) {
|
|
80
|
+
if (typeof document === 'undefined')
|
|
81
|
+
return 0;
|
|
82
|
+
const scope = root ?? document;
|
|
83
|
+
const els = Array.from(scope.querySelectorAll('[data-echarts]'));
|
|
84
|
+
let n = 0;
|
|
85
|
+
for (const el of els) {
|
|
86
|
+
if (el.getAttribute('data-echarts-done') === '1')
|
|
87
|
+
continue;
|
|
88
|
+
const raw = el.getAttribute('data-echarts');
|
|
89
|
+
if (!raw)
|
|
90
|
+
continue;
|
|
91
|
+
try {
|
|
92
|
+
const option = JSON.parse(raw);
|
|
93
|
+
await renderEchartsTo(el, option);
|
|
94
|
+
el.setAttribute('data-echarts-done', '1');
|
|
95
|
+
n++;
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
el.textContent = `[draw] ${e instanceof Error ? e.message : String(e)}`;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return n;
|
|
102
|
+
}
|
|
103
|
+
if (typeof document !== 'undefined') {
|
|
104
|
+
const boot = () => { void autoRenderEcharts(document); };
|
|
105
|
+
if (document.readyState === 'loading')
|
|
106
|
+
document.addEventListener('DOMContentLoaded', boot);
|
|
107
|
+
else
|
|
108
|
+
boot();
|
|
109
|
+
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { Skin } from '../styles/index.js';
|
|
1
2
|
import { type DrawResult } from './query.js';
|
|
3
|
+
import type { EChartsOption } from 'echarts';
|
|
2
4
|
type Obj = Record<string, unknown>;
|
|
3
5
|
export declare const MAP_BANDS: {
|
|
4
6
|
/** 顶部标题带:24(顶)+18(字)+余量 */
|
|
@@ -64,6 +66,8 @@ export declare const HEAD_RULES: {
|
|
|
64
66
|
* hub 那边用 `headLayout.toString()` 把源码塞进页面脚本。
|
|
65
67
|
*/
|
|
66
68
|
export declare function headLayout(option: Obj): void;
|
|
69
|
+
/** 把用户的 option 补齐缺省(皮肤色板/字体/轴/标题/图例)—— **浏览器端也用这一份** */
|
|
70
|
+
export declare function echartsOption(user: Obj, skin: Skin, width: number, height: number): EChartsOption;
|
|
67
71
|
/**
|
|
68
72
|
* Render a parsed ECharts option object to a skinned SVG — shared by the HTTP
|
|
69
73
|
* layer (`?d=` urlencoded JSON) and the CLI (inline JSON / file / stdin, where
|
|
@@ -230,7 +230,8 @@ export function headLayout(option) {
|
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
}
|
|
233
|
-
|
|
233
|
+
/** 把用户的 option 补齐缺省(皮肤色板/字体/轴/标题/图例)—— **浏览器端也用这一份** */
|
|
234
|
+
export function echartsOption(user, skin, width, height) {
|
|
234
235
|
const option = {
|
|
235
236
|
animation: false, // 静态 SVG:开动画只会输出第 0 帧
|
|
236
237
|
color: skin.series.map((_, i) => `var(--d-s${i + 1})`), // 皮肤系列色板(padSeries 已补足 10 色)
|
|
@@ -515,7 +516,7 @@ export async function renderEchartsOption(user, params) {
|
|
|
515
516
|
height = Math.min(1200, Math.max(140, h));
|
|
516
517
|
}
|
|
517
518
|
}
|
|
518
|
-
const option =
|
|
519
|
+
const option = echartsOption(user, skin, width, height);
|
|
519
520
|
// 世界地图挪到某个经度时:底图绕一圈(registerMaps 已注册绕圈后的数据),
|
|
520
521
|
// 布局按份数放大 —— 两步都在 maps.ts,浏览器端跑同一份源码
|
|
521
522
|
worldWrapLayout(option);
|
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 };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { GeoProjection } from 'd3-geo';
|
|
1
2
|
/** 视角:球心的经纬度 + 缩放倍数 ✓(分享链接要带着它 ⇒ 角度一致 ✓) */
|
|
2
3
|
export interface GlobeView {
|
|
3
4
|
/** 球心经度(度,正东为正) */
|
|
@@ -34,7 +35,7 @@ export declare const GLOBE_COUNTRIES: GlobeCountry[];
|
|
|
34
35
|
* 判正反面只看一个点 ⇒ 俄罗斯横跨 11 个时区必然判错 ✗)。
|
|
35
36
|
* `precision(0.4)` ⇒ 陆地的边贴着球面走(不是屏幕上的弦 ✓)。
|
|
36
37
|
*/
|
|
37
|
-
export declare function globeProjection(w: number, h: number, view?: GlobeView):
|
|
38
|
+
export declare function globeProjection(w: number, h: number, view?: GlobeView): GeoProjection;
|
|
38
39
|
/** 路径的额外收拾:像素空间抽稀(容差 px)⇒ 形状不变、字串小一半 ✓ */
|
|
39
40
|
export interface GlobePathOpts {
|
|
40
41
|
/**
|
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
|
}
|
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-cmd/draw",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
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",
|
|
@@ -37,6 +37,10 @@
|
|
|
37
37
|
"./render/*": {
|
|
38
38
|
"types": "./dist/render/*.d.ts",
|
|
39
39
|
"default": "./dist/render/*.js"
|
|
40
|
+
},
|
|
41
|
+
"./echarts": {
|
|
42
|
+
"types": "./dist/echarts-client.d.ts",
|
|
43
|
+
"default": "./dist/echarts-client.js"
|
|
40
44
|
}
|
|
41
45
|
},
|
|
42
46
|
"bin": {
|
|
@@ -55,7 +59,7 @@
|
|
|
55
59
|
"node": ">=18.19"
|
|
56
60
|
},
|
|
57
61
|
"scripts": {
|
|
58
|
-
"build": "tsc -p tsconfig.build.json && node scripts/copy-ofl.mjs",
|
|
62
|
+
"build": "tsc -p tsconfig.build.json && node scripts/copy-ofl.mjs && npm run build:client && npm run build:echarts",
|
|
59
63
|
"prepare": "npm run build",
|
|
60
64
|
"check": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.node.json --noEmit && tsc -p tsconfig.build.json",
|
|
61
65
|
"serve": "tsx src/node/server.ts",
|
|
@@ -76,7 +80,8 @@
|
|
|
76
80
|
"diagrams:check": "node scripts/diagrams.mjs --check",
|
|
77
81
|
"release-notes": "node scripts/release-notes.mjs",
|
|
78
82
|
"prepublishOnly": "npm run build",
|
|
79
|
-
"build:client": "esbuild src/client.ts --bundle --minify --format=iife --global-name=DrawChart --platform=browser --target=es2020 --outfile=dist/draw-client.iife.js"
|
|
83
|
+
"build:client": "esbuild src/client.ts --bundle --minify --format=iife --global-name=DrawChart --platform=browser --target=es2020 --external:node:fs --external:node:module --outfile=dist/draw-client.iife.js",
|
|
84
|
+
"build:echarts": "esbuild src/echarts-client.ts --bundle --minify --format=iife --global-name=DrawEcharts --platform=browser --target=es2020 --external:node:fs --external:node:module --outfile=dist/draw-echarts.iife.js"
|
|
80
85
|
},
|
|
81
86
|
"dependencies": {
|
|
82
87
|
"@resvg/resvg-wasm": "^2.6.2",
|