@x-cmd/draw 0.5.15 → 0.5.16

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.
@@ -68,6 +68,28 @@ export declare const HEAD_RULES: {
68
68
  export declare function headLayout(option: Obj): void;
69
69
  /** 把用户的 option 补齐缺省(皮肤色板/字体/轴/标题/图例)—— **浏览器端也用这一份** */
70
70
  export declare function echartsOption(user: Obj, skin: Skin, width: number, height: number): EChartsOption;
71
+ /**
72
+ * 关系图(graph)的标签:离点留一点空隙。
73
+ *
74
+ * echarts 的缺省是**贴着点的边缘**(还往里压了几像素)⇒
75
+ * 那个大的中心点(半径大)⇒ 它的名字有一半压在自己身上 ✗(#188)。
76
+ *
77
+ * ⇒ 缺省给 8px 的间距(用户自己写了 distance ⇒ 不动 ✓)。
78
+ */
79
+ export declare function applyGraphLabelGap(option: Obj): void;
80
+ /**
81
+ * 力导向图:起点得是**确定的**。
82
+ *
83
+ * echarts 给力导向图的初始位置是**随机**的(内部 Math.random ✗)
84
+ * ⇒ 同一条 URL 每次画出来都不一样 ⇒「URL 即图」这条命断了 ✗
85
+ * (缓存也白存:每次回源都是新的一版)
86
+ *
87
+ * ⇒ 给它一个确定的起点:节点按数据顺序摆一条**螺线**(黄金角)
88
+ * —— 摊得开、不叠着,力导再从这儿松出来 ✓
89
+ *
90
+ * 用户自己写了坐标(x/y)或 force.initLayout ⇒ **不动** ✓
91
+ */
92
+ export declare function applyForceStart(option: Obj, width: number, height: number): void;
71
93
  /**
72
94
  * 扇区/块**里面**的字:颜色跟**那一块的底色**走。
73
95
  *
@@ -301,8 +301,80 @@ export function echartsOption(user, skin, width, height) {
301
301
  }));
302
302
  }
303
303
  }
304
+ // 力导向图:起点得是**确定的**(见 applyForceStart)
305
+ applyForceStart(option, width, height);
306
+ // 关系图:名字离点远一点(echarts 的缺省是**压在点边上**的 ⇒ 大点的名字盖住点 ✗)
307
+ applyGraphLabelGap(option);
304
308
  return option;
305
309
  }
310
+ /**
311
+ * 关系图(graph)的标签:离点留一点空隙。
312
+ *
313
+ * echarts 的缺省是**贴着点的边缘**(还往里压了几像素)⇒
314
+ * 那个大的中心点(半径大)⇒ 它的名字有一半压在自己身上 ✗(#188)。
315
+ *
316
+ * ⇒ 缺省给 8px 的间距(用户自己写了 distance ⇒ 不动 ✓)。
317
+ */
318
+ export function applyGraphLabelGap(option) {
319
+ const series = Array.isArray(option.series) ? option.series : isObj(option.series) ? [option.series] : [];
320
+ for (const s of series) {
321
+ if (!isObj(s) || s.type !== 'graph')
322
+ continue;
323
+ const label = isObj(s.label) ? s.label : undefined;
324
+ if (!label || label.show === false)
325
+ continue;
326
+ if (label.distance !== undefined)
327
+ continue; // 用户写了 ⇒ 不动
328
+ const pos = typeof label.position === 'string' ? label.position : 'left';
329
+ if (pos !== 'left' && pos !== 'right' && pos !== 'top' && pos !== 'bottom')
330
+ continue;
331
+ s.label = { ...label, distance: 8 };
332
+ }
333
+ }
334
+ /**
335
+ * 力导向图:起点得是**确定的**。
336
+ *
337
+ * echarts 给力导向图的初始位置是**随机**的(内部 Math.random ✗)
338
+ * ⇒ 同一条 URL 每次画出来都不一样 ⇒「URL 即图」这条命断了 ✗
339
+ * (缓存也白存:每次回源都是新的一版)
340
+ *
341
+ * ⇒ 给它一个确定的起点:节点按数据顺序摆一条**螺线**(黄金角)
342
+ * —— 摊得开、不叠着,力导再从这儿松出来 ✓
343
+ *
344
+ * 用户自己写了坐标(x/y)或 force.initLayout ⇒ **不动** ✓
345
+ */
346
+ export function applyForceStart(option, width, height) {
347
+ const series = Array.isArray(option.series) ? option.series : isObj(option.series) ? [option.series] : [];
348
+ for (const s of series) {
349
+ if (!isObj(s) || s.type !== 'graph')
350
+ continue;
351
+ const layout = s.layout;
352
+ const isForce = layout === 'force' || (isObj(layout) && layout.type === 'force');
353
+ if (!isForce)
354
+ continue;
355
+ if (!Array.isArray(s.data) || s.data.length < 2)
356
+ continue;
357
+ if (isObj(s.force) && s.force.initLayout !== undefined)
358
+ continue; // 用户定了起点 ⇒ 不动
359
+ const data = s.data;
360
+ if (data.some((it) => isObj(it) && (typeof it.x === 'number' || typeof it.y === 'number')))
361
+ continue;
362
+ const cx = width / 2;
363
+ const cy = height / 2;
364
+ // 摊开的方向跟画布一个样(宽的画布摊成扁的椭圆 ⇒ 不会挤成一团 ✓)
365
+ const rx = width * 0.42;
366
+ const ry = height * 0.42;
367
+ const n = data.length;
368
+ data.forEach((it, i) => {
369
+ if (!isObj(it))
370
+ return;
371
+ const a = i * 2.399963229728653; // 黄金角
372
+ const r = Math.sqrt((i + 0.5) / n); // 半径按序号开方 ⇒ 均匀摊开
373
+ it.x = Math.round(cx + rx * r * Math.cos(a));
374
+ it.y = Math.round(cy + ry * r * Math.sin(a));
375
+ });
376
+ }
377
+ }
306
378
  /**
307
379
  * 扇区/块**里面**的字:颜色跟**那一块的底色**走。
308
380
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-cmd/draw",
3
- "version": "0.5.15",
3
+ "version": "0.5.16",
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",