@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.
- package/README.md +79 -297
- package/dist/lib.d.ts +26 -3
- package/dist/lib.js +31 -4
- package/dist/node/cli.js +1 -1
- 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 +90 -3
- package/dist/render/archmap.js +174 -102
- package/dist/render/area.js +9 -7
- package/dist/render/banner.d.ts +8 -0
- package/dist/render/banner.js +204 -0
- package/dist/render/bar-grouped.js +3 -2
- package/dist/render/bar-stacked.js +3 -2
- package/dist/render/bar.js +3 -2
- 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 +3 -2
- 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 +30 -0
- package/dist/render/echarts-ssr.js +337 -16
- package/dist/render/gantt.d.ts +11 -0
- package/dist/render/gantt.js +12 -19
- 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 +2 -2
- package/dist/render/line.js +2 -2
- package/dist/render/maps.d.ts +34 -0
- package/dist/render/maps.js +197 -2
- package/dist/render/mermaid.js +25 -6
- package/dist/render/mindmap.js +3 -2
- package/dist/render/onepage.d.ts +62 -0
- package/dist/render/onepage.js +670 -0
- package/dist/render/orgicon.d.ts +12 -0
- package/dist/render/orgicon.js +31 -0
- package/dist/render/png.js +3 -1
- 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 +100 -1
- package/dist/render/route.d.ts +86 -0
- package/dist/render/route.js +1077 -0
- package/dist/render/scatter.js +73 -7
- package/dist/render/seq.d.ts +41 -0
- package/dist/render/seq.js +248 -0
- package/dist/render/src-spec.d.ts +5 -0
- package/dist/render/src-spec.js +49 -0
- package/dist/render/svg.d.ts +16 -2
- package/dist/render/svg.js +85 -7
- 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/us-states.js +4 -6
- 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 +5 -0
- package/dist/specs/base.js +14 -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 +268 -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/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/types/dagre.d.ts +39 -0
- package/dist/types/dagre.js +3 -0
- package/package.json +17 -1
|
@@ -10,9 +10,9 @@ import { ApiError, clip } from '../specs/index.js';
|
|
|
10
10
|
import { resolveVersion } from '../version.js';
|
|
11
11
|
import { resolveSkin } from '../styles/index.js';
|
|
12
12
|
import { FONT } from '../theme.js';
|
|
13
|
-
import { esc, metaTag, skinStyle } from './svg.js';
|
|
13
|
+
import { esc, metaTag, skinStyle, editTag } from './svg.js';
|
|
14
14
|
import { clampInt, schemeParam } from './query.js';
|
|
15
|
-
import { registerMaps } from './maps.js';
|
|
15
|
+
import { mapAspect, registerMaps, worldWrapLayout } from './maps.js';
|
|
16
16
|
// ?d= 上限:再大就撞各端 URL 长度红线(Workers/代理 ~16KB);超限直接拒绝,
|
|
17
17
|
// 而不是静默截断(数据在 URL 上是产品第一性原则,不可悄悄丢)
|
|
18
18
|
const D_MAX = 16000;
|
|
@@ -40,27 +40,197 @@ const AXIS_NEUTRALS = {
|
|
|
40
40
|
const AXES = ['xAxis', 'yAxis', 'radiusAxis', 'angleAxis', 'singleAxis'];
|
|
41
41
|
// 需要笛卡尔坐标系的系列:这些系列的裸 option 在 SSR 下不会自动建轴(见 withDrawDefaults)
|
|
42
42
|
const CARTESIAN = new Set(['bar', 'line', 'scatter', 'effectScatter', 'boxplot', 'candlestick', 'heatmap', 'pictorialBar', 'custom']);
|
|
43
|
-
//
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
// 地图系列的布局缺省:
|
|
44
|
+
// aspectScale=1 —— echarts 默认 0.75(给经纬度 GeoJSON 用的经验值),会把投影过的
|
|
45
|
+
// 地图横向压扁 25%;地图数据本身已是等距投影的平面坐标,纵横比按数据来(aspect=1)
|
|
46
|
+
// 尺寸按画布算 —— 顶部 62px 留给标题与 Edit @X-CMD 角标;图例横排(orient=horizontal)
|
|
47
|
+
// 时底部再留 56px,竖排(orient=vertical,靠左)则左侧留 48px 且地图右移,
|
|
48
|
+
// 不跟图例叠在一起;四周再留 12px 余量给州名标签(边缘的标签会溢出地图框)
|
|
49
|
+
// layoutSize 是 min(画布宽高) 的百分比:地图比画布宽时它给宽,比画布高时它给高
|
|
50
|
+
// (echarts 按地图自身纵横比决定),所以两条分支都要算。
|
|
51
|
+
const MAP_TOP = 62;
|
|
52
|
+
const MAP_LEGEND_BOTTOM = 56;
|
|
53
|
+
const MAP_LEGEND_SIDE = 48;
|
|
54
|
+
const MAP_MARGIN = 12;
|
|
55
|
+
/** 地图布局缺省(服务端与浏览器端同一套规则 —— 见 explore 页的同名实现) */
|
|
56
|
+
export function mapLayout(o) {
|
|
57
|
+
const aspect = mapAspect(o.name) ?? 1.7;
|
|
58
|
+
const left = o.legendAtBottom ? 0 : MAP_LEGEND_SIDE;
|
|
59
|
+
const bottom = o.legendAtBottom ? MAP_LEGEND_BOTTOM : 0;
|
|
60
|
+
const availW = Math.max(40, o.width - left - MAP_MARGIN * 2);
|
|
61
|
+
const availH = Math.max(40, o.height - MAP_TOP - bottom - MAP_MARGIN * 2);
|
|
62
|
+
// echarts:layoutSize 给的是「短边」那个方向的尺寸 —— 地图比画布宽时给宽,否则给高
|
|
63
|
+
const size = aspect >= 1
|
|
64
|
+
? Math.min(availW, availH * aspect)
|
|
65
|
+
: Math.min(availH, availW / aspect);
|
|
66
|
+
const unit = Math.min(o.width, o.height);
|
|
67
|
+
const centerX = left + MAP_MARGIN + availW / 2;
|
|
68
|
+
const centerY = MAP_TOP + MAP_MARGIN + availH / 2;
|
|
69
|
+
return {
|
|
70
|
+
aspectScale: 1,
|
|
71
|
+
layoutSize: `${((size / unit) * 100).toFixed(2)}%`,
|
|
72
|
+
layoutCenter: [
|
|
73
|
+
`${((centerX / o.width) * 100).toFixed(2)}%`,
|
|
74
|
+
`${((centerY / o.height) * 100).toFixed(2)}%`,
|
|
75
|
+
],
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
// 标题带 / 图例 / 绘图区 的排布(仓库硬规则:文字不得重叠)。
|
|
79
|
+
//
|
|
80
|
+
// echarts 6 的图例缺省在**底部**(bottom:15),但常见写法是放顶部(legend.top)。
|
|
81
|
+
// 那时若标题也在,两者天然叠着:标题缺省 top=15、标题高 28(15..43);
|
|
82
|
+
// 图例写 top=30 时那一行在 32..52 —— 与标题重叠 11px(用户常见手感:「贴太近」)。
|
|
83
|
+
//
|
|
84
|
+
// 这里按「标题带 → 图例 → 绘图区」往下排一层,只往下推、绝不往上提:
|
|
85
|
+
// 图例顶 = max(用户写的, 标题带底 + titleGap)
|
|
86
|
+
// 绘图区顶 = max(用户写的, 图例底 + legendGap / 标题带底 + legendGap)
|
|
87
|
+
// 浏览器端(explore 页重画)用同一套数 —— 见 HEAD_RULES。
|
|
88
|
+
export const HEAD_RULES = {
|
|
89
|
+
titleTop: 24, // echarts 标题缺省 top(与核心层同一条基线)
|
|
90
|
+
titleLeft: 19, // echarts 标题缺省 left(减去 padding,文字左边与核心层对齐)
|
|
91
|
+
titleFont: 18, // 标题缺省字号
|
|
92
|
+
titlePad: 5, // 标题缺省 padding(上下各 5)
|
|
93
|
+
subFont: 12, // 副标题缺省字号
|
|
94
|
+
subGap: 10, // 标题与副标题之间的 itemGap
|
|
95
|
+
titleGap: 8, // 标题带底 → 图例顶
|
|
96
|
+
legendRow: 20, // 一行图例的可见高度(12px 文字 + 标记 + 余量)
|
|
97
|
+
legendGap: 8, // 图例底 / 标题带底 → 绘图区顶
|
|
98
|
+
gridTop: 60, // echarts 绘图区缺省 top
|
|
99
|
+
};
|
|
100
|
+
/** title/legend/grid 都可能是对象也可能是数组,取第一个 */
|
|
101
|
+
function firstObj(v) {
|
|
102
|
+
const a = Array.isArray(v) ? v[0] : v;
|
|
103
|
+
return isObj(a) ? a : undefined;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 把标题带、图例、绘图区排开:图例在顶部且会压到标题时往下推,
|
|
107
|
+
* 绘图区被压到时也往下推。只往下推 —— 用户给的空间永远算数。
|
|
108
|
+
* 就地改 option(调用方先拷好 title/legend/grid)。
|
|
109
|
+
*
|
|
110
|
+
* **自包含**:这个函数不引用模块外的任何东西(HEAD_RULES 除外,那个由调用方
|
|
111
|
+
* 注入)—— 浏览器端(explore 页重画)直接跑同一份源码,两边不会走偏。
|
|
112
|
+
* hub 那边用 `headLayout.toString()` 把源码塞进页面脚本。
|
|
113
|
+
*/
|
|
114
|
+
export function headLayout(option) {
|
|
115
|
+
const R = HEAD_RULES;
|
|
116
|
+
const isObj = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
117
|
+
const firstObj = (v) => {
|
|
118
|
+
const a = Array.isArray(v) ? v[0] : v;
|
|
119
|
+
return isObj(a) ? a : undefined;
|
|
120
|
+
};
|
|
121
|
+
const title = firstObj(option.title);
|
|
122
|
+
const legend = firstObj(option.legend);
|
|
123
|
+
const grid = firstObj(option.grid);
|
|
124
|
+
// 有笛卡尔系列(=用 grid 定位)吗?饼/雷达/仪表没有 grid,推不动
|
|
125
|
+
const isCartesian = () => {
|
|
126
|
+
const CARTESIAN = new Set(['bar', 'line', 'scatter', 'effectScatter', 'boxplot', 'candlestick', 'heatmap', 'pictorialBar', 'custom']);
|
|
127
|
+
const series = Array.isArray(option.series) ? option.series : isObj(option.series) ? [option.series] : [];
|
|
128
|
+
if (series.some((s) => isObj(s) && typeof s.type === 'string' && CARTESIAN.has(s.type)))
|
|
129
|
+
return true;
|
|
130
|
+
// 没写 series 类型但写了 xAxis/yAxis —— 也按笛卡尔算
|
|
131
|
+
return option.xAxis !== undefined || option.yAxis !== undefined;
|
|
132
|
+
};
|
|
133
|
+
// 标题带:标题在底部(bottom)时不占顶部
|
|
134
|
+
const titleText = title && typeof title.text === 'string' ? title.text.trim() : '';
|
|
135
|
+
const titleAtBottom = title !== undefined && typeof title.bottom === 'number';
|
|
136
|
+
const titleTop = title && typeof title.top === 'number' ? title.top : R.titleTop;
|
|
137
|
+
const fontSize = Number(title?.textStyle?.fontSize ?? R.titleFont);
|
|
138
|
+
const pad = Number(title?.padding ?? R.titlePad);
|
|
139
|
+
const subH = title && typeof title.subtext === 'string' && title.subtext.trim() !== ''
|
|
140
|
+
? R.subFont + R.subGap : 0;
|
|
141
|
+
// 标题带高度按 max(实际字号, 18) 算:浏览器端(explore 页重画)的标题字号与
|
|
142
|
+
// 服务端可能不同(16 vs 18),取下限才能让两端算出同一个图例位置。
|
|
143
|
+
const bandH = Math.max(fontSize, R.titleFont) + pad * 2;
|
|
144
|
+
// 标题靠左上角:和核心层(以及其余所有图种)一个位置 —— echarts 缺省是居中,
|
|
145
|
+
// 居中的话标题会压在图上(饼图尤其明显)。用户自己写了 left/top 就不动。
|
|
146
|
+
if (titleText && !titleAtBottom && title) {
|
|
147
|
+
if (title.left === undefined)
|
|
148
|
+
title.left = R.titleLeft;
|
|
149
|
+
if (title.top === undefined)
|
|
150
|
+
title.top = R.titleTop;
|
|
151
|
+
}
|
|
152
|
+
const titleBottom = titleText && !titleAtBottom ? titleTop + bandH + subH : 0;
|
|
153
|
+
// 图例:只在**顶部**时参与这一层(缺省在底部,不与标题冲突)
|
|
154
|
+
let legendBottom = 0;
|
|
155
|
+
if (legend && legend.show !== false) {
|
|
156
|
+
const dataItems = Array.isArray(legend.data) ? legend.data.length : -1;
|
|
157
|
+
const seriesItems = (Array.isArray(option.series) ? option.series : isObj(option.series) ? [option.series] : [])
|
|
158
|
+
.filter((s) => isObj(s) && typeof s.name === 'string' && s.name !== '').length;
|
|
159
|
+
const items = dataItems >= 0 ? dataItems : seriesItems;
|
|
160
|
+
const topRaw = typeof legend.top === 'number' ? legend.top : legend.top === 'top' ? 0 : undefined;
|
|
161
|
+
if (items > 0 && topRaw !== undefined) {
|
|
162
|
+
const top = titleBottom ? Math.max(topRaw, titleBottom + R.titleGap) : topRaw;
|
|
163
|
+
if (top !== legend.top)
|
|
164
|
+
legend.top = top;
|
|
165
|
+
// 竖排图例一列多行,高度按条目数算
|
|
166
|
+
legendBottom = top + (legend.orient === 'vertical' ? items * R.legendRow : R.legendRow);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
// y 轴的名字画在轴的**上端**(grid.top 上面那一条)—— 标题带没给它留位置,
|
|
170
|
+
// 它就压在标题上(「政府收入与增长」和「平均实际增速 %/年」叠成一团)。
|
|
171
|
+
const yAxisNameH = (() => {
|
|
172
|
+
const ya = firstObj(option.yAxis);
|
|
173
|
+
if (!ya || ya.show === false)
|
|
174
|
+
return 0;
|
|
175
|
+
if (typeof ya.name !== 'string' || ya.name.trim() === '')
|
|
176
|
+
return 0;
|
|
177
|
+
const loc = ya.nameLocation;
|
|
178
|
+
if (loc === 'middle' || loc === 'center')
|
|
179
|
+
return 0; // 侧着写,不占顶部
|
|
180
|
+
return 20; // 名字那一条(12px 字 + 留白)
|
|
181
|
+
})();
|
|
182
|
+
// 绘图区:不许被标题带/图例压住
|
|
183
|
+
const band = Math.max(titleBottom ? titleBottom + R.legendGap : 0, legendBottom ? legendBottom + R.legendGap : 0);
|
|
184
|
+
// 顶部有带子时,再给 y 轴的名字让出它那一条
|
|
185
|
+
const need = band + (band ? yAxisNameH : 0);
|
|
186
|
+
if (need > 0) {
|
|
187
|
+
if (grid) {
|
|
188
|
+
const gridTop = typeof grid.top === 'number' ? grid.top : R.gridTop;
|
|
189
|
+
if (gridTop < need)
|
|
190
|
+
grid.top = need;
|
|
191
|
+
}
|
|
192
|
+
else if (isCartesian()) {
|
|
193
|
+
// 用户没写 grid 时 echarts 用缺省 top(60)—— 标题带没被让开,
|
|
194
|
+
// 副标题就压在 y 轴刻度上(「2026」和「400」叠成「2026400」)。
|
|
195
|
+
// 补一个只带 top 的 grid,其余仍走 echarts 缺省。
|
|
196
|
+
option.grid = { top: need };
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
function withDrawDefaults(user, skin, width, height) {
|
|
46
201
|
const option = {
|
|
47
202
|
animation: false, // 静态 SVG:开动画只会输出第 0 帧
|
|
48
203
|
color: skin.series.map((_, i) => `var(--d-s${i + 1})`), // 皮肤系列色板(padSeries 已补足 10 色)
|
|
49
204
|
textStyle: { fontFamily: FONT, color: 'var(--d-text)' },
|
|
50
205
|
...user,
|
|
51
206
|
};
|
|
207
|
+
// 标题/图例/绘图区的位置后面要算,先拷一份 —— 补缺省不该改到用户的对象
|
|
208
|
+
for (const key of ['title', 'legend', 'grid']) {
|
|
209
|
+
if (isObj(option[key]))
|
|
210
|
+
option[key] = { ...option[key] };
|
|
211
|
+
}
|
|
52
212
|
// spread 会整体覆盖子对象,用户的半成品 textStyle 也要补齐字体/文字色缺省
|
|
53
213
|
if (isObj(option.textStyle))
|
|
54
214
|
fillDefaults(option.textStyle, { fontFamily: FONT, color: 'var(--d-text)' });
|
|
55
215
|
if (isObj(option.title))
|
|
56
|
-
fillDefaults(option.title, { textStyle: { color: 'var(--d-ink)' }, subtextStyle: { color: 'var(--d-muted)' } });
|
|
216
|
+
fillDefaults(option.title, { textStyle: { color: 'var(--d-ink)', fontWeight: 500 }, subtextStyle: { color: 'var(--d-muted)' } });
|
|
57
217
|
if (isObj(option.legend))
|
|
58
218
|
fillDefaults(option.legend, { textStyle: { color: 'var(--d-muted)' } });
|
|
219
|
+
// 标题带/图例/绘图区排开:图例在顶部时不能压住标题(仓库硬规则:文字不得重叠)
|
|
220
|
+
headLayout(option);
|
|
59
221
|
// SSR 补轴:echarts SSR 不像浏览器那样为笛卡尔系列自动建坐标系,裸 option 会抛
|
|
60
222
|
// 'xAxis "0" not found'。按浏览器对等行为补空轴对象({} = 缺省 x=category/y=value),
|
|
61
223
|
// 仅在用户完全没写该轴时落;写了一根(多轴场景)也不动
|
|
62
224
|
const seriesArr = Array.isArray(option.series) ? option.series : isObj(option.series) ? [option.series] : [];
|
|
63
|
-
|
|
225
|
+
// 只给**真正挂在笛卡尔坐标系上**的系列补轴:系列写明 coordinateSystem:'polar'
|
|
226
|
+
// (或别的非笛卡尔)时,补出来的 xAxis/yAxis 会被 echarts 画成一对没人要的 L 形轴线
|
|
227
|
+
const onCartesian = (s) => {
|
|
228
|
+
if (typeof s.type !== 'string' || !CARTESIAN.has(s.type))
|
|
229
|
+
return false;
|
|
230
|
+
const cs = s.coordinateSystem;
|
|
231
|
+
return cs === undefined || cs === 'cartesian2d';
|
|
232
|
+
};
|
|
233
|
+
if (seriesArr.some((s) => isObj(s) && onCartesian(s))) {
|
|
64
234
|
if (option.xAxis === undefined)
|
|
65
235
|
option.xAxis = {};
|
|
66
236
|
if (option.yAxis === undefined)
|
|
@@ -81,10 +251,12 @@ function withDrawDefaults(user, skin) {
|
|
|
81
251
|
for (const s of seriesArr) {
|
|
82
252
|
if (isObj(s))
|
|
83
253
|
fillDefaults(s, { label: { textBorderWidth: 0, color: 'var(--d-text)' } });
|
|
84
|
-
//
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
254
|
+
// 地图系列:布局缺省(纵横比按数据 + 尺寸按画布,见 mapLayout)
|
|
255
|
+
if (isObj(s) && s.type === 'map' && typeof s.map === 'string') {
|
|
256
|
+
const vm = user.visualMap;
|
|
257
|
+
const legendAtBottom = vm !== undefined && vm.orient !== 'vertical';
|
|
258
|
+
fillDefaults(s, mapLayout({ name: s.map, width, height, legendAtBottom }));
|
|
259
|
+
}
|
|
88
260
|
}
|
|
89
261
|
return option;
|
|
90
262
|
}
|
|
@@ -100,6 +272,151 @@ function ariaOf(option) {
|
|
|
100
272
|
function stripTextStrokes(svg) {
|
|
101
273
|
return svg.replace(/<text\b[^>]*>/g, (tag) => tag.replace(/ (stroke|stroke-width|stroke-opacity|stroke-linejoin|stroke-dasharray|stroke-miterlimit|paint-order)="[^"]*"/g, ''));
|
|
102
274
|
}
|
|
275
|
+
// 路径的包围盒(绝对坐标)—— 只为「裁掉画布外的图形」服务。
|
|
276
|
+
// 只认 echarts SSR 实际用到的指令(M/L/l/H/V/C/Q/S/T/A/Z);认不出的就返回 undefined
|
|
277
|
+
// (调用方保守起见不裁)。
|
|
278
|
+
function pathBBox(d) {
|
|
279
|
+
const tokens = d.match(/[A-Za-z]|[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?/g);
|
|
280
|
+
if (!tokens)
|
|
281
|
+
return undefined;
|
|
282
|
+
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
|
|
283
|
+
let cx = 0, cy = 0, sx = 0, sy = 0;
|
|
284
|
+
const put = (x, y) => {
|
|
285
|
+
if (x < x0)
|
|
286
|
+
x0 = x;
|
|
287
|
+
if (x > x1)
|
|
288
|
+
x1 = x;
|
|
289
|
+
if (y < y0)
|
|
290
|
+
y0 = y;
|
|
291
|
+
if (y > y1)
|
|
292
|
+
y1 = y;
|
|
293
|
+
};
|
|
294
|
+
let i = 0;
|
|
295
|
+
let cmd = '';
|
|
296
|
+
while (i < tokens.length) {
|
|
297
|
+
if (/[A-Za-z]/.test(tokens[i]))
|
|
298
|
+
cmd = tokens[i++];
|
|
299
|
+
if (!cmd)
|
|
300
|
+
break;
|
|
301
|
+
const rel = cmd === cmd.toLowerCase();
|
|
302
|
+
const num = () => {
|
|
303
|
+
if (i >= tokens.length)
|
|
304
|
+
return undefined;
|
|
305
|
+
const v = Number(tokens[i++]);
|
|
306
|
+
return Number.isFinite(v) ? v : undefined;
|
|
307
|
+
};
|
|
308
|
+
const pair = () => {
|
|
309
|
+
const a = num();
|
|
310
|
+
const b = num();
|
|
311
|
+
if (a === undefined || b === undefined)
|
|
312
|
+
return undefined;
|
|
313
|
+
return [rel ? cx + a : a, rel ? cy + b : b];
|
|
314
|
+
};
|
|
315
|
+
switch (cmd.toUpperCase()) {
|
|
316
|
+
case 'M': {
|
|
317
|
+
const p = pair();
|
|
318
|
+
if (!p)
|
|
319
|
+
return undefined;
|
|
320
|
+
[cx, cy] = p;
|
|
321
|
+
sx = cx;
|
|
322
|
+
sy = cy;
|
|
323
|
+
put(cx, cy);
|
|
324
|
+
cmd = rel ? 'l' : 'L'; // 后续的坐标对是隐式 lineto
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
case 'L': {
|
|
328
|
+
const p = pair();
|
|
329
|
+
if (!p)
|
|
330
|
+
return undefined;
|
|
331
|
+
[cx, cy] = p;
|
|
332
|
+
put(cx, cy);
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
case 'H': {
|
|
336
|
+
const a = num();
|
|
337
|
+
if (a === undefined)
|
|
338
|
+
return undefined;
|
|
339
|
+
cx = rel ? cx + a : a;
|
|
340
|
+
put(cx, cy);
|
|
341
|
+
break;
|
|
342
|
+
}
|
|
343
|
+
case 'V': {
|
|
344
|
+
const a = num();
|
|
345
|
+
if (a === undefined)
|
|
346
|
+
return undefined;
|
|
347
|
+
cy = rel ? cy + a : a;
|
|
348
|
+
put(cx, cy);
|
|
349
|
+
break;
|
|
350
|
+
}
|
|
351
|
+
case 'C': {
|
|
352
|
+
const a = num(), b = num();
|
|
353
|
+
const p = pair();
|
|
354
|
+
if (a === undefined || b === undefined || !p)
|
|
355
|
+
return undefined;
|
|
356
|
+
put(p[0], p[1]);
|
|
357
|
+
[cx, cy] = p;
|
|
358
|
+
put(cx, cy);
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case 'Q':
|
|
362
|
+
case 'S':
|
|
363
|
+
case 'T': {
|
|
364
|
+
const a = num();
|
|
365
|
+
const p = pair();
|
|
366
|
+
if (a === undefined || !p)
|
|
367
|
+
return undefined;
|
|
368
|
+
[cx, cy] = p;
|
|
369
|
+
put(cx, cy);
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
case 'A': {
|
|
373
|
+
const r1 = num(), r2 = num(), rot = num(), big = num(), sweep = num();
|
|
374
|
+
const p = pair();
|
|
375
|
+
if (r1 === undefined || r2 === undefined || rot === undefined || big === undefined || sweep === undefined || !p)
|
|
376
|
+
return undefined;
|
|
377
|
+
[cx, cy] = p;
|
|
378
|
+
put(cx, cy);
|
|
379
|
+
break;
|
|
380
|
+
}
|
|
381
|
+
case 'Z': {
|
|
382
|
+
cx = sx;
|
|
383
|
+
cy = sy;
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
default:
|
|
387
|
+
return undefined; // 不认识的指令 → 不裁
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
if (!Number.isFinite(x0))
|
|
391
|
+
return undefined;
|
|
392
|
+
return { x0, x1, y0, y1 };
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* 画布外的图形裁掉 —— 世界地图「绕一圈」时,画布外还挂着两份世界(路径数据全在里面),
|
|
396
|
+
* SVG 因此大一大截(explore 页把整份内联进 HTML)。只裁**整块**在画布外的路径,
|
|
397
|
+
* 半个在画布内的留下;带圆弧的路径不裁(弧可能鼓出端点之外,算不准)。
|
|
398
|
+
*/
|
|
399
|
+
function cullOutsideCanvas(svg, w, h) {
|
|
400
|
+
return svg.replace(/<path\b[^>]*>(?:<\/path>)?/g, (tag) => {
|
|
401
|
+
const d = /\sd="([^"]*)"/.exec(tag)?.[1];
|
|
402
|
+
const tr = /transform="translate\(([-\d.eE+]+)[ ,]+([-\d.eE+]+)\)"/.exec(tag);
|
|
403
|
+
if (!d || !tr)
|
|
404
|
+
return tag;
|
|
405
|
+
if (/[Aa]/.test(d))
|
|
406
|
+
return tag; // 圆弧:算不准,不裁
|
|
407
|
+
const bb = pathBBox(d);
|
|
408
|
+
if (!bb)
|
|
409
|
+
return tag;
|
|
410
|
+
const dx = Number(tr[1]);
|
|
411
|
+
const dy = Number(tr[2]);
|
|
412
|
+
if (!Number.isFinite(dx) || !Number.isFinite(dy))
|
|
413
|
+
return tag;
|
|
414
|
+
const x0 = bb.x0 + dx, x1 = bb.x1 + dx, y0 = bb.y0 + dy, y1 = bb.y1 + dy;
|
|
415
|
+
if (x1 < 0 || x0 > w || y1 < 0 || y0 > h)
|
|
416
|
+
return '';
|
|
417
|
+
return tag;
|
|
418
|
+
});
|
|
419
|
+
}
|
|
103
420
|
// 后处理:根 svg 补 role/aria/font-family,内嵌皮肤变量(含 Inter 子集)+ metadata;
|
|
104
421
|
// 注入 Edit @X-CMD 角标(#27,svgDoc 覆盖核心层,此处覆盖 echarts)
|
|
105
422
|
function decorate(svg, skin, version, option) {
|
|
@@ -115,9 +432,9 @@ function decorate(svg, skin, version, option) {
|
|
|
115
432
|
const opt = option;
|
|
116
433
|
// 可点击(#13):链到 echarts explore,?d= 携带完整 option,点击即得可编辑副本
|
|
117
434
|
const tagHref = `https://x-cmd.com/draw/explore/echarts?d=${encodeURIComponent(JSON.stringify(option))}`;
|
|
118
|
-
const tag =
|
|
435
|
+
const tag = editTag(tagHref, w, 54, tagFs);
|
|
119
436
|
// 标签必须在 </svg> 之前(之后的 XML 非法,img 严格解析失败)
|
|
120
|
-
const body = stripTextStrokes(svg.slice(m[0].length)).replace(/<\/svg>\s*$/, '');
|
|
437
|
+
const body = cullOutsideCanvas(stripTextStrokes(svg.slice(m[0].length)), w, h).replace(/<\/svg>\s*$/, '');
|
|
121
438
|
return root + skinStyle(skin) + metaTag('echarts', version, option) + body + tag + '</svg>';
|
|
122
439
|
}
|
|
123
440
|
const EXAMPLE_D = encodeURIComponent(JSON.stringify({ xAxis: { type: 'category', data: ['A', 'B'] }, yAxis: { type: 'value' }, series: [{ type: 'bar', data: [1, 2] }] }));
|
|
@@ -134,11 +451,15 @@ export async function renderEchartsOption(user, params) {
|
|
|
134
451
|
const skin = resolveSkin('line', params.get('t'), schemeParam(params)); // echarts 无专属变体,借用 line 的家族注册表
|
|
135
452
|
const width = clampInt(params, 'w', 560, 200, 1600);
|
|
136
453
|
const height = clampInt(params, 'h', 320, 140, 1200);
|
|
137
|
-
const option = withDrawDefaults(user, skin);
|
|
138
454
|
// 动态加载:核心 /draw 路径(含 Worker 冷启动)不为 echarts 付 ~1MB 的加载成本
|
|
139
455
|
const echarts = await import('echarts');
|
|
140
|
-
// 地图数据(US 各州 GeoJSON)同样按需载入:option 没用 map/geo
|
|
141
|
-
|
|
456
|
+
// 地图数据(US 各州 GeoJSON)同样按需载入:option 没用 map/geo 就不碰它;
|
|
457
|
+
// 先注册再补缺省 —— 布局要按地图的真实纵横比算
|
|
458
|
+
await registerMaps(echarts, user);
|
|
459
|
+
const option = withDrawDefaults(user, skin, width, height);
|
|
460
|
+
// 世界地图挪到某个经度时:底图绕一圈(registerMaps 已注册绕圈后的数据),
|
|
461
|
+
// 布局按份数放大 —— 两步都在 maps.ts,浏览器端跑同一份源码
|
|
462
|
+
worldWrapLayout(option);
|
|
142
463
|
const chart = echarts.init(null, null, { renderer: 'svg', ssr: true, width, height });
|
|
143
464
|
// echarts 对 var() 调色板会打 "illegal color" 警告(派生色 parseColor 不认 CSS 变量),
|
|
144
465
|
// 但直写路径(fill/stroke)原样透传、输出正确——渲染成功时这些警告纯属噪音,吞掉;
|
package/dist/render/gantt.d.ts
CHANGED
|
@@ -19,6 +19,16 @@ export interface GanttDoc {
|
|
|
19
19
|
}
|
|
20
20
|
/** 解析 mermaid gantt 源码 → GanttDoc(addTask 收到的是 ":flags, id, start, 时长" 原始串,自行拆) */
|
|
21
21
|
export declare function parseGantt(text: string): GanttDoc;
|
|
22
|
+
export interface Span {
|
|
23
|
+
name: string;
|
|
24
|
+
start: number;
|
|
25
|
+
end: number;
|
|
26
|
+
flags: Set<string>;
|
|
27
|
+
sectionIdx: number;
|
|
28
|
+
tip: string;
|
|
29
|
+
}
|
|
30
|
+
/** 展开任务:日期解析、after 链、里程碑零宽 → 统一 Span 列表 */
|
|
31
|
+
export declare function resolveSpans(doc: GanttDoc): Span[];
|
|
22
32
|
/** 渲染(视觉 2.0,对齐行业惯例):双行轴(月/年带+日/周/月刻度)、实线 hairline 网格、
|
|
23
33
|
* section 交替色带、周末底纹、today 实线;状态色 done=实色灰 / crit=淡红+描边 / active=满色+墨描边。
|
|
24
34
|
* opts.scale 显式时间粒度(缺省按跨度自适应);任务可带语法扩展的完成百分比(条内深色进度段) */
|
|
@@ -29,6 +39,7 @@ export declare function renderGantt(doc: GanttDoc, opts: {
|
|
|
29
39
|
width?: number;
|
|
30
40
|
height?: number;
|
|
31
41
|
scale?: 'day' | 'week' | 'month';
|
|
42
|
+
tagQuery?: string;
|
|
32
43
|
}): DrawResult;
|
|
33
44
|
/** /draw/gantt?m=... 入口(m 可省首行 gantt 关键字;/draw/mermaid 的 gantt 分派亦走此) */
|
|
34
45
|
export declare function renderGanttFromUrl(url: URL, interactive?: boolean): DrawResult;
|
package/dist/render/gantt.js
CHANGED
|
@@ -50,10 +50,8 @@ export function parseGantt(text) {
|
|
|
50
50
|
}
|
|
51
51
|
const meta = String(raw ?? '').replace(/^:/, '').split(',').map(x => x.trim()).filter(Boolean);
|
|
52
52
|
const flags = [];
|
|
53
|
-
|
|
54
|
-
let start;
|
|
53
|
+
const pos = [];
|
|
55
54
|
let after;
|
|
56
|
-
let endOrDur;
|
|
57
55
|
let progress;
|
|
58
56
|
let consumedFlags = true;
|
|
59
57
|
for (const tk of meta) {
|
|
@@ -71,18 +69,13 @@ export function parseGantt(text) {
|
|
|
71
69
|
after = tk.slice(6).trim();
|
|
72
70
|
continue;
|
|
73
71
|
}
|
|
74
|
-
|
|
75
|
-
id = tk;
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
if (start === undefined) {
|
|
79
|
-
start = tk;
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
if (endOrDur === undefined) {
|
|
83
|
-
endOrDur = tk;
|
|
84
|
-
}
|
|
72
|
+
pos.push(tk);
|
|
85
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];
|
|
86
79
|
cur.tasks.push({ name: name.trim(), id, flags, start, after, endOrDur, ...(progress !== undefined ? { progress } : {}) });
|
|
87
80
|
},
|
|
88
81
|
};
|
|
@@ -99,7 +92,7 @@ export function parseGantt(text) {
|
|
|
99
92
|
return doc;
|
|
100
93
|
}
|
|
101
94
|
/** 展开任务:日期解析、after 链、里程碑零宽 → 统一 Span 列表 */
|
|
102
|
-
function resolveSpans(doc) {
|
|
95
|
+
export function resolveSpans(doc) {
|
|
103
96
|
const byId = new Map();
|
|
104
97
|
doc.sections.forEach(s => s.tasks.forEach(t => { if (t.id)
|
|
105
98
|
byId.set(t.id, t); }));
|
|
@@ -271,8 +264,8 @@ export function renderGantt(doc, opts) {
|
|
|
271
264
|
for (const tk of ticks) {
|
|
272
265
|
const x = Math.min(xOf(tk.t), x1);
|
|
273
266
|
parts.push(`<line x1="${r2(x)}" y1="${bodyTop}" x2="${r2(x)}" y2="${bodyBottom}" stroke="var(--d-grid)"/>`);
|
|
274
|
-
if (!doc.title && x > width -
|
|
275
|
-
continue; //
|
|
267
|
+
if (!doc.title && x > width - 140)
|
|
268
|
+
continue; // 角标胶囊区,只画线不画字
|
|
276
269
|
const weekend = daily && (new Date(tk.t).getUTCDay() === 0 || new Date(tk.t).getUTCDay() === 6);
|
|
277
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>`);
|
|
278
271
|
}
|
|
@@ -402,7 +395,7 @@ export function renderGantt(doc, opts) {
|
|
|
402
395
|
`#am.hl [data-node],#am.hl text[data-row]{opacity:.3}` +
|
|
403
396
|
`#am.hl [data-node].on,#am.hl text[data-row].on{opacity:1}</style>`
|
|
404
397
|
: '';
|
|
405
|
-
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
|
|
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);
|
|
406
399
|
return { svg, version: opts.v.version, cacheControl: opts.v.cacheControl };
|
|
407
400
|
}
|
|
408
401
|
/** /draw/gantt?m=... 入口(m 可省首行 gantt 关键字;/draw/mermaid 的 gantt 分派亦走此) */
|
|
@@ -418,5 +411,5 @@ export function renderGanttFromUrl(url, interactive = false) {
|
|
|
418
411
|
const height = /(\d+)/.test(params.get('h') ?? '') ? Math.max(140, Math.min(2000, +params.get('h'))) : undefined;
|
|
419
412
|
const sv = params.get('scale');
|
|
420
413
|
const scale = sv === 'day' || sv === 'week' || sv === 'month' ? sv : undefined;
|
|
421
|
-
return renderGantt(doc, { v, skin, interactive, width, height, ...(scale ? { scale } : {}) });
|
|
414
|
+
return renderGantt(doc, { v, skin, interactive, width, height, tagQuery: url.search, ...(scale ? { scale } : {}) });
|
|
422
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;
|