@x-cmd/draw 0.5.0 → 0.5.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.
- 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/globe-proj.d.ts +2 -1
- 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);
|
|
@@ -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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-cmd/draw",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
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",
|