@x-cmd/draw 0.5.4 → 0.5.5

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.
@@ -1,3 +1,10 @@
1
+ /** 一个矩形(标注避让用) */
2
+ export interface Box {
3
+ x: number;
4
+ y: number;
5
+ w: number;
6
+ h: number;
7
+ }
1
8
  export interface Anno {
2
9
  kind: 'oval' | 'text' | 'hl' | 'note' | 'arrow' | 'path';
3
10
  /** 椭圆:中心;文字:标签中心;高亮:左上角;评论:引线的落点;箭头:起点;折线:第一点(都是百分比) */
@@ -71,6 +78,7 @@ export declare function annoLayer(annos: Anno[], frame: {
71
78
  h: number;
72
79
  }, k?: number, opts?: {
73
80
  uniform?: boolean;
81
+ avoid?: Box[];
74
82
  }): string;
75
83
  /**
76
84
  * 图里的**绘图区**(数据区)✓ —— 标注的百分比要按它换算,不是按整个画布 ✓
@@ -93,4 +101,14 @@ export declare function plotAreaOf(svg: string): {
93
101
  * 把标注层塞进一张已经画好的 SVG(盖在最后面 = 最上面)。
94
102
  * 坐标按**绘图区**换算 —— 画布尺寸变了,百分比的位置不变 ✓。
95
103
  */
104
+ /**
105
+ * 图里已有的**文字块** —— 标注要躲开它们。
106
+ *
107
+ * 为什么:标注(评论气泡)是自己挑位置的 ⇒ 可能正好落在某个标签上
108
+ * (架构图那个「auth」副标题、导图那个「Agents」)⇒ 读者两样都读不到。
109
+ *
110
+ * 做法:把图里的 `<text>` 扫一遍,按字号与锚点推出每段字的盒子。
111
+ * 组上的 translate 也要算进去(有些渲染器把东西放在平移过的组里)。
112
+ */
113
+ export declare function textBoxesOf(svg: string): Box[];
96
114
  export declare function injectAnnos(svg: string, annos: Anno[] | undefined): string;
@@ -610,13 +610,40 @@ export function annoLayer(annos, frame, k = 1, opts) {
610
610
  by = frame.y + (a.ty / 100) * frame.h;
611
611
  }
612
612
  else {
613
- // 缺省:框放在引线的哪一侧 —— 先试右边,再试左边;上面放不下就放下面
614
- bx = ax + gap;
615
- if (bx + bw > frame.x + frame.w)
616
- bx = ax - gap - bw;
617
- by = ay - gap - bh;
618
- if (by < frame.y)
619
- by = ay + gap;
613
+ // 缺省:框自己挑位置 —— **躲开图里的字**(标注压住字 = 读者读不到)。
614
+ // 候选位置:引线四周的一圈(右/左/上/下 × 几档远近)⇒ 挑压字最少、离锚点最近的那个。
615
+ const xs = [ax + gap, ax + gap * 2.5, ax + gap * 4, ax - bw / 2, ax - gap - bw, ax - gap * 2.5 - bw, ax - gap * 4 - bw];
616
+ const ys = [ay - gap - bh, ay - bh / 2, ay + gap, ay + gap + bh / 2];
617
+ const hits = (x, y) => {
618
+ let n = 0;
619
+ for (const b of opts?.avoid ?? []) {
620
+ if (x < b.x + b.w && x + bw > b.x && y < b.y + b.h && y + bh > b.y)
621
+ n++;
622
+ }
623
+ // 框不能盖住**锚点** —— 锚点就是「指着哪儿」的那个记号,盖住了等于没说
624
+ if (x < ax + 8 && x + bw > ax - 8 && y < ay + 8 && y + bh > ay - 8)
625
+ n += 3;
626
+ return n;
627
+ };
628
+ let best = [xs[0], ys[0]];
629
+ let bestScore = Infinity;
630
+ let bestDist = Infinity;
631
+ for (const cy0 of ys) {
632
+ for (const cx0 of xs) {
633
+ const x0 = Math.min(Math.max(cx0, frame.x), Math.max(frame.x, frame.x + frame.w - bw));
634
+ const y0 = Math.min(Math.max(cy0, frame.y), Math.max(frame.y, frame.y + frame.h - bh));
635
+ const score = hits(x0, y0);
636
+ const dist = Math.hypot(x0 + bw / 2 - ax, y0 + bh / 2 - ay);
637
+ // 压字少的优先;一样多 ⇒ 离锚点近的优先(引线短 ⇒ 好读)
638
+ if (score < bestScore || (score === bestScore && dist < bestDist)) {
639
+ bestScore = score;
640
+ bestDist = dist;
641
+ best = [x0, y0];
642
+ }
643
+ }
644
+ }
645
+ bx = best[0];
646
+ by = best[1];
620
647
  }
621
648
  // 夹回画布(太小放不下时宁可压边,也不能跑出去)
622
649
  bx = Math.min(Math.max(bx, frame.x), Math.max(frame.x, frame.x + frame.w - bw));
@@ -696,6 +723,50 @@ export function plotAreaOf(svg) {
696
723
  * 把标注层塞进一张已经画好的 SVG(盖在最后面 = 最上面)。
697
724
  * 坐标按**绘图区**换算 —— 画布尺寸变了,百分比的位置不变 ✓。
698
725
  */
726
+ /**
727
+ * 图里已有的**文字块** —— 标注要躲开它们。
728
+ *
729
+ * 为什么:标注(评论气泡)是自己挑位置的 ⇒ 可能正好落在某个标签上
730
+ * (架构图那个「auth」副标题、导图那个「Agents」)⇒ 读者两样都读不到。
731
+ *
732
+ * 做法:把图里的 `<text>` 扫一遍,按字号与锚点推出每段字的盒子。
733
+ * 组上的 translate 也要算进去(有些渲染器把东西放在平移过的组里)。
734
+ */
735
+ export function textBoxesOf(svg) {
736
+ const out = [];
737
+ const stack = [[0, 0]];
738
+ const tagRe = /<g\b([^>]*?)\/>|<g\b([^>]*?)>|<\/g>|<text\b([^>]*?)>([\s\S]*?)<\/text>/g;
739
+ for (let m = tagRe.exec(svg); m; m = tagRe.exec(svg)) {
740
+ if (m[1] !== undefined)
741
+ continue; // 自闭合的 <g/>
742
+ if (m[2] !== undefined) {
743
+ const tr = /translate\((-?[\d.]+)[ ,]+(-?[\d.]+)\)/.exec(m[2]);
744
+ const [ox, oy] = stack[stack.length - 1];
745
+ stack.push(tr ? [ox + Number(tr[1]), oy + Number(tr[2])] : [ox, oy]);
746
+ continue;
747
+ }
748
+ if (m[0].startsWith('</g')) {
749
+ if (stack.length > 1)
750
+ stack.pop();
751
+ continue;
752
+ }
753
+ const attrs = m[3] ?? '';
754
+ const content = (m[4] ?? '').replace(/<[^>]*>/g, '').trim();
755
+ if (!content)
756
+ continue;
757
+ const x = Number(/\bx="(-?[\d.]+)"/.exec(attrs)?.[1] ?? NaN);
758
+ const y = Number(/\by="(-?[\d.]+)"/.exec(attrs)?.[1] ?? NaN);
759
+ if (!Number.isFinite(x) || !Number.isFinite(y))
760
+ continue;
761
+ const fs = Number(/font-size="([\d.]+)"/.exec(attrs)?.[1] ?? 13);
762
+ const anchor = /text-anchor="(\w+)"/.exec(attrs)?.[1] ?? 'start';
763
+ const tw = textWidth(content, fs);
764
+ const [ox, oy] = stack[stack.length - 1];
765
+ const x0 = anchor === 'end' ? x - tw : anchor === 'middle' ? x - tw / 2 : x;
766
+ out.push({ x: x0 + ox, y: y + oy - fs * 0.8, w: tw, h: fs * 1.05 });
767
+ }
768
+ return out;
769
+ }
699
770
  export function injectAnnos(svg, annos) {
700
771
  if (!annos?.length)
701
772
  return svg;
@@ -735,7 +806,7 @@ export function injectAnnos(svg, annos) {
735
806
  const plot = plotAreaOf(svg);
736
807
  // 图有值映射 ⇒ 标注的 y 是**数据值** ⇒ 先换算成绘图区的百分比 ✓
737
808
  const d = dscaleOf(svg);
738
- const layer = annoLayer(d ? annoValuesToPct(annos, d) : annos, plot ?? { x: x0, y: y0, w: vw, h: vh }, k, { uniform: !d });
809
+ const layer = annoLayer(d ? annoValuesToPct(annos, d) : annos, plot ?? { x: x0, y: y0, w: vw, h: vh }, k, { uniform: !d, avoid: textBoxesOf(svg) });
739
810
  const at = svg.lastIndexOf('</svg>');
740
811
  if (at < 0)
741
812
  return svg;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-cmd/draw",
3
- "version": "0.5.4",
3
+ "version": "0.5.5",
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",