@antv/g6 5.0.0-alpha.7 → 5.0.0-alpha.9

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.
Files changed (297) hide show
  1. package/dist/g6.min.js +6 -19
  2. package/esm/index.d.ts +26 -6
  3. package/esm/item/combo.d.ts +72 -2
  4. package/esm/item/combo.js +122 -2
  5. package/esm/item/combo.js.map +1 -1
  6. package/esm/item/edge.d.ts +3 -3
  7. package/esm/item/edge.js +4 -5
  8. package/esm/item/edge.js.map +1 -1
  9. package/esm/item/item.d.ts +4 -4
  10. package/esm/item/item.js +38 -9
  11. package/esm/item/item.js.map +1 -1
  12. package/esm/item/node.d.ts +20 -16
  13. package/esm/item/node.js +14 -8
  14. package/esm/item/node.js.map +1 -1
  15. package/esm/runtime/controller/data.d.ts +12 -4
  16. package/esm/runtime/controller/data.js +354 -62
  17. package/esm/runtime/controller/data.js.map +1 -1
  18. package/esm/runtime/controller/interaction.js +16 -4
  19. package/esm/runtime/controller/interaction.js.map +1 -1
  20. package/esm/runtime/controller/item.d.ts +11 -2
  21. package/esm/runtime/controller/item.js +344 -52
  22. package/esm/runtime/controller/item.js.map +1 -1
  23. package/esm/runtime/controller/layout.js +19 -5
  24. package/esm/runtime/controller/layout.js.map +1 -1
  25. package/esm/runtime/graph.d.ts +54 -17
  26. package/esm/runtime/graph.js +165 -34
  27. package/esm/runtime/graph.js.map +1 -1
  28. package/esm/stdlib/behavior/click-select.d.ts +3 -0
  29. package/esm/stdlib/behavior/click-select.js +13 -1
  30. package/esm/stdlib/behavior/click-select.js.map +1 -1
  31. package/esm/stdlib/behavior/collapse-expand-combo.d.ts +29 -0
  32. package/esm/stdlib/behavior/collapse-expand-combo.js +35 -0
  33. package/esm/stdlib/behavior/collapse-expand-combo.js.map +1 -0
  34. package/esm/stdlib/behavior/drag-combo.d.ts +99 -0
  35. package/esm/stdlib/behavior/drag-combo.js +341 -0
  36. package/esm/stdlib/behavior/drag-combo.js.map +1 -0
  37. package/esm/stdlib/behavior/drag-node.d.ts +14 -10
  38. package/esm/stdlib/behavior/drag-node.js +134 -37
  39. package/esm/stdlib/behavior/drag-node.js.map +1 -1
  40. package/esm/stdlib/behavior/hover-activate.d.ts +3 -1
  41. package/esm/stdlib/behavior/hover-activate.js +3 -1
  42. package/esm/stdlib/behavior/hover-activate.js.map +1 -1
  43. package/esm/stdlib/behavior/orbit-canvas-3d.js +1 -1
  44. package/esm/stdlib/behavior/orbit-canvas-3d.js.map +1 -1
  45. package/esm/stdlib/behavior/track-canvas-3d.js +1 -1
  46. package/esm/stdlib/behavior/track-canvas-3d.js.map +1 -1
  47. package/esm/stdlib/index.d.ts +25 -3
  48. package/esm/stdlib/index.js +25 -3
  49. package/esm/stdlib/index.js.map +1 -1
  50. package/esm/stdlib/item/combo/circle.d.ts +29 -0
  51. package/esm/stdlib/item/combo/circle.js +56 -0
  52. package/esm/stdlib/item/combo/circle.js.map +1 -0
  53. package/esm/stdlib/item/edge/base.d.ts +2 -2
  54. package/esm/stdlib/item/edge/base.js +3 -0
  55. package/esm/stdlib/item/edge/base.js.map +1 -1
  56. package/esm/stdlib/item/edge/cubic-horizon.d.ts +37 -0
  57. package/esm/stdlib/item/edge/cubic-horizon.js +71 -0
  58. package/esm/stdlib/item/edge/cubic-horizon.js.map +1 -0
  59. package/esm/stdlib/item/edge/cubic-vertical.d.ts +37 -0
  60. package/esm/stdlib/item/edge/cubic-vertical.js +71 -0
  61. package/esm/stdlib/item/edge/cubic-vertical.js.map +1 -0
  62. package/esm/stdlib/item/edge/cubic.d.ts +60 -0
  63. package/esm/stdlib/item/edge/cubic.js +99 -0
  64. package/esm/stdlib/item/edge/cubic.js.map +1 -0
  65. package/esm/stdlib/item/edge/index.d.ts +1 -0
  66. package/esm/stdlib/item/edge/index.js +1 -0
  67. package/esm/stdlib/item/edge/index.js.map +1 -1
  68. package/esm/stdlib/item/edge/line.d.ts +1 -1
  69. package/esm/stdlib/item/edge/quadratic.d.ts +41 -0
  70. package/esm/stdlib/item/edge/quadratic.js +66 -0
  71. package/esm/stdlib/item/edge/quadratic.js.map +1 -0
  72. package/esm/stdlib/item/node/base.d.ts +38 -37
  73. package/esm/stdlib/item/node/base.js +4 -4
  74. package/esm/stdlib/item/node/base.js.map +1 -1
  75. package/esm/stdlib/item/node/base3d.d.ts +1 -0
  76. package/esm/stdlib/item/node/base3d.js +1 -0
  77. package/esm/stdlib/item/node/base3d.js.map +1 -1
  78. package/esm/stdlib/item/node/index.d.ts +1 -0
  79. package/esm/stdlib/item/node/index.js +1 -0
  80. package/esm/stdlib/item/node/index.js.map +1 -1
  81. package/esm/stdlib/item/node/rect.d.ts +31 -0
  82. package/esm/stdlib/item/node/rect.js +59 -0
  83. package/esm/stdlib/item/node/rect.js.map +1 -0
  84. package/esm/stdlib/plugin/fisheye/index.d.ts +102 -0
  85. package/esm/stdlib/plugin/fisheye/index.js +487 -0
  86. package/esm/stdlib/plugin/fisheye/index.js.map +1 -0
  87. package/esm/stdlib/plugin/menu/index.d.ts +42 -0
  88. package/esm/stdlib/plugin/menu/index.js +236 -0
  89. package/esm/stdlib/plugin/menu/index.js.map +1 -0
  90. package/esm/stdlib/plugin/tooltip/index.d.ts +75 -0
  91. package/esm/stdlib/plugin/tooltip/index.js +377 -0
  92. package/esm/stdlib/plugin/tooltip/index.js.map +1 -0
  93. package/esm/stdlib/theme/dark.js +4 -0
  94. package/esm/stdlib/theme/dark.js.map +1 -1
  95. package/esm/stdlib/theme/light.js +54 -9
  96. package/esm/stdlib/theme/light.js.map +1 -1
  97. package/esm/types/combo.d.ts +52 -3
  98. package/esm/types/data.d.ts +4 -4
  99. package/esm/types/edge.d.ts +2 -3
  100. package/esm/types/event.d.ts +9 -1
  101. package/esm/types/event.js +8 -8
  102. package/esm/types/event.js.map +1 -1
  103. package/esm/types/graph.d.ts +49 -13
  104. package/esm/types/hook.d.ts +8 -1
  105. package/esm/types/item.d.ts +4 -4
  106. package/esm/types/node.d.ts +4 -5
  107. package/esm/types/theme.d.ts +1 -1
  108. package/esm/util/animate.js.map +1 -1
  109. package/esm/util/canvas.js +8 -0
  110. package/esm/util/canvas.js.map +1 -1
  111. package/esm/util/data.d.ts +65 -0
  112. package/esm/util/data.js +150 -0
  113. package/esm/util/data.js.map +1 -0
  114. package/esm/util/event.d.ts +8 -0
  115. package/esm/util/event.js +16 -0
  116. package/esm/util/event.js.map +1 -1
  117. package/esm/util/item.d.ts +2 -1
  118. package/esm/util/item.js +36 -13
  119. package/esm/util/item.js.map +1 -1
  120. package/esm/util/shape.d.ts +39 -4
  121. package/esm/util/shape.js +101 -1
  122. package/esm/util/shape.js.map +1 -1
  123. package/esm/util/zoom.d.ts +2 -2
  124. package/lib/index.d.ts +26 -6
  125. package/lib/item/combo.d.ts +72 -2
  126. package/lib/item/combo.js +122 -2
  127. package/lib/item/combo.js.map +1 -1
  128. package/lib/item/edge.d.ts +3 -3
  129. package/lib/item/edge.js +4 -5
  130. package/lib/item/edge.js.map +1 -1
  131. package/lib/item/item.d.ts +4 -4
  132. package/lib/item/item.js +37 -8
  133. package/lib/item/item.js.map +1 -1
  134. package/lib/item/node.d.ts +20 -16
  135. package/lib/item/node.js +14 -8
  136. package/lib/item/node.js.map +1 -1
  137. package/lib/runtime/controller/data.d.ts +12 -4
  138. package/lib/runtime/controller/data.js +353 -61
  139. package/lib/runtime/controller/data.js.map +1 -1
  140. package/lib/runtime/controller/interaction.js +15 -3
  141. package/lib/runtime/controller/interaction.js.map +1 -1
  142. package/lib/runtime/controller/item.d.ts +11 -2
  143. package/lib/runtime/controller/item.js +342 -50
  144. package/lib/runtime/controller/item.js.map +1 -1
  145. package/lib/runtime/controller/layout.js +19 -5
  146. package/lib/runtime/controller/layout.js.map +1 -1
  147. package/lib/runtime/graph.d.ts +54 -17
  148. package/lib/runtime/graph.js +165 -34
  149. package/lib/runtime/graph.js.map +1 -1
  150. package/lib/stdlib/behavior/click-select.d.ts +3 -0
  151. package/lib/stdlib/behavior/click-select.js +13 -1
  152. package/lib/stdlib/behavior/click-select.js.map +1 -1
  153. package/lib/stdlib/behavior/collapse-expand-combo.d.ts +29 -0
  154. package/lib/stdlib/behavior/collapse-expand-combo.js +38 -0
  155. package/lib/stdlib/behavior/collapse-expand-combo.js.map +1 -0
  156. package/lib/stdlib/behavior/drag-combo.d.ts +99 -0
  157. package/lib/stdlib/behavior/drag-combo.js +344 -0
  158. package/lib/stdlib/behavior/drag-combo.js.map +1 -0
  159. package/lib/stdlib/behavior/drag-node.d.ts +14 -10
  160. package/lib/stdlib/behavior/drag-node.js +134 -37
  161. package/lib/stdlib/behavior/drag-node.js.map +1 -1
  162. package/lib/stdlib/behavior/hover-activate.d.ts +3 -1
  163. package/lib/stdlib/behavior/hover-activate.js +3 -2
  164. package/lib/stdlib/behavior/hover-activate.js.map +1 -1
  165. package/lib/stdlib/behavior/orbit-canvas-3d.js +1 -1
  166. package/lib/stdlib/behavior/orbit-canvas-3d.js.map +1 -1
  167. package/lib/stdlib/behavior/track-canvas-3d.js +1 -1
  168. package/lib/stdlib/behavior/track-canvas-3d.js.map +1 -1
  169. package/lib/stdlib/index.d.ts +25 -3
  170. package/lib/stdlib/index.js +25 -3
  171. package/lib/stdlib/index.js.map +1 -1
  172. package/lib/stdlib/item/combo/circle.d.ts +29 -0
  173. package/lib/stdlib/item/combo/circle.js +60 -0
  174. package/lib/stdlib/item/combo/circle.js.map +1 -0
  175. package/lib/stdlib/item/edge/base.d.ts +2 -2
  176. package/lib/stdlib/item/edge/base.js +3 -0
  177. package/lib/stdlib/item/edge/base.js.map +1 -1
  178. package/lib/stdlib/item/edge/cubic-horizon.d.ts +37 -0
  179. package/lib/stdlib/item/edge/cubic-horizon.js +75 -0
  180. package/lib/stdlib/item/edge/cubic-horizon.js.map +1 -0
  181. package/lib/stdlib/item/edge/cubic-vertical.d.ts +37 -0
  182. package/lib/stdlib/item/edge/cubic-vertical.js +75 -0
  183. package/lib/stdlib/item/edge/cubic-vertical.js.map +1 -0
  184. package/lib/stdlib/item/edge/cubic.d.ts +60 -0
  185. package/lib/stdlib/item/edge/cubic.js +103 -0
  186. package/lib/stdlib/item/edge/cubic.js.map +1 -0
  187. package/lib/stdlib/item/edge/index.d.ts +1 -0
  188. package/lib/stdlib/item/edge/index.js +1 -0
  189. package/lib/stdlib/item/edge/index.js.map +1 -1
  190. package/lib/stdlib/item/edge/line.d.ts +1 -1
  191. package/lib/stdlib/item/edge/quadratic.d.ts +41 -0
  192. package/lib/stdlib/item/edge/quadratic.js +70 -0
  193. package/lib/stdlib/item/edge/quadratic.js.map +1 -0
  194. package/lib/stdlib/item/node/base.d.ts +38 -37
  195. package/lib/stdlib/item/node/base.js +4 -4
  196. package/lib/stdlib/item/node/base.js.map +1 -1
  197. package/lib/stdlib/item/node/base3d.d.ts +1 -0
  198. package/lib/stdlib/item/node/base3d.js +1 -0
  199. package/lib/stdlib/item/node/base3d.js.map +1 -1
  200. package/lib/stdlib/item/node/index.d.ts +1 -0
  201. package/lib/stdlib/item/node/index.js +1 -0
  202. package/lib/stdlib/item/node/index.js.map +1 -1
  203. package/lib/stdlib/item/node/rect.d.ts +31 -0
  204. package/lib/stdlib/item/node/rect.js +63 -0
  205. package/lib/stdlib/item/node/rect.js.map +1 -0
  206. package/lib/stdlib/plugin/fisheye/index.d.ts +102 -0
  207. package/lib/stdlib/plugin/fisheye/index.js +490 -0
  208. package/lib/stdlib/plugin/fisheye/index.js.map +1 -0
  209. package/lib/stdlib/plugin/menu/index.d.ts +42 -0
  210. package/lib/stdlib/plugin/menu/index.js +242 -0
  211. package/lib/stdlib/plugin/menu/index.js.map +1 -0
  212. package/lib/stdlib/plugin/tooltip/index.d.ts +75 -0
  213. package/lib/stdlib/plugin/tooltip/index.js +383 -0
  214. package/lib/stdlib/plugin/tooltip/index.js.map +1 -0
  215. package/lib/stdlib/theme/dark.js +4 -0
  216. package/lib/stdlib/theme/dark.js.map +1 -1
  217. package/lib/stdlib/theme/light.js +54 -9
  218. package/lib/stdlib/theme/light.js.map +1 -1
  219. package/lib/types/combo.d.ts +52 -3
  220. package/lib/types/data.d.ts +4 -4
  221. package/lib/types/edge.d.ts +2 -3
  222. package/lib/types/event.d.ts +9 -1
  223. package/lib/types/event.js +10 -10
  224. package/lib/types/event.js.map +1 -1
  225. package/lib/types/graph.d.ts +49 -13
  226. package/lib/types/hook.d.ts +8 -1
  227. package/lib/types/item.d.ts +4 -4
  228. package/lib/types/item.js +1 -1
  229. package/lib/types/item.js.map +1 -1
  230. package/lib/types/node.d.ts +4 -5
  231. package/lib/types/theme.d.ts +1 -1
  232. package/lib/util/animate.js.map +1 -1
  233. package/lib/util/canvas.js +8 -0
  234. package/lib/util/canvas.js.map +1 -1
  235. package/lib/util/data.d.ts +65 -0
  236. package/lib/util/data.js +161 -0
  237. package/lib/util/data.js.map +1 -0
  238. package/lib/util/event.d.ts +8 -0
  239. package/lib/util/event.js +18 -1
  240. package/lib/util/event.js.map +1 -1
  241. package/lib/util/item.d.ts +2 -1
  242. package/lib/util/item.js +36 -13
  243. package/lib/util/item.js.map +1 -1
  244. package/lib/util/shape.d.ts +39 -4
  245. package/lib/util/shape.js +105 -2
  246. package/lib/util/shape.js.map +1 -1
  247. package/lib/util/zoom.d.ts +2 -2
  248. package/package.json +15 -18
  249. package/src/item/combo.ts +220 -2
  250. package/src/item/edge.ts +7 -8
  251. package/src/item/item.ts +58 -15
  252. package/src/item/node.ts +47 -24
  253. package/src/runtime/controller/data.ts +390 -68
  254. package/src/runtime/controller/interaction.ts +23 -3
  255. package/src/runtime/controller/item.ts +444 -64
  256. package/src/runtime/controller/layout.ts +24 -5
  257. package/src/runtime/graph.ts +225 -38
  258. package/src/stdlib/behavior/click-select.ts +15 -2
  259. package/src/stdlib/behavior/collapse-expand-combo.ts +59 -0
  260. package/src/stdlib/behavior/drag-combo.ts +533 -0
  261. package/src/stdlib/behavior/drag-node.ts +195 -62
  262. package/src/stdlib/behavior/hover-activate.ts +4 -5
  263. package/src/stdlib/behavior/orbit-canvas-3d.ts +1 -1
  264. package/src/stdlib/behavior/track-canvas-3d.ts +1 -1
  265. package/src/stdlib/index.ts +25 -3
  266. package/src/stdlib/item/combo/circle.ts +113 -0
  267. package/src/stdlib/item/edge/base.ts +5 -2
  268. package/src/stdlib/item/edge/cubic-horizon.ts +114 -0
  269. package/src/stdlib/item/edge/cubic-vertical.ts +116 -0
  270. package/src/stdlib/item/edge/cubic.ts +198 -0
  271. package/src/stdlib/item/edge/index.ts +1 -0
  272. package/src/stdlib/item/edge/quadratic.ts +136 -0
  273. package/src/stdlib/item/node/base.ts +79 -41
  274. package/src/stdlib/item/node/base3d.ts +2 -0
  275. package/src/stdlib/item/node/index.ts +1 -0
  276. package/src/stdlib/item/node/rect.ts +124 -0
  277. package/src/stdlib/plugin/fisheye/index.ts +578 -0
  278. package/src/stdlib/plugin/menu/index.ts +260 -0
  279. package/src/stdlib/plugin/tooltip/index.ts +424 -0
  280. package/src/stdlib/theme/dark.ts +4 -0
  281. package/src/stdlib/theme/light.ts +77 -9
  282. package/src/types/combo.ts +61 -3
  283. package/src/types/data.ts +6 -4
  284. package/src/types/edge.ts +2 -2
  285. package/src/types/event.ts +8 -8
  286. package/src/types/graph.ts +73 -16
  287. package/src/types/hook.ts +8 -1
  288. package/src/types/item.ts +10 -9
  289. package/src/types/node.ts +4 -4
  290. package/src/types/theme.ts +1 -1
  291. package/src/util/animate.ts +2 -3
  292. package/src/util/canvas.ts +10 -0
  293. package/src/util/data.ts +178 -0
  294. package/src/util/event.ts +29 -0
  295. package/src/util/item.ts +75 -12
  296. package/src/util/shape.ts +142 -5
  297. package/src/util/zoom.ts +2 -2
@@ -0,0 +1,578 @@
1
+ import { clone, throttle } from '@antv/util';
2
+ import { DisplayObject } from '@antv/g';
3
+ import { Point } from '../../../types/common';
4
+ import { IGraph } from '../../../types';
5
+ import { ShapeStyle } from '../../../types/item';
6
+ import { IG6GraphEvent } from '../../../types/event';
7
+ import { Plugin as Base, IPluginBaseConfig } from '../../../types/plugin';
8
+
9
+ const DELTA = 0.05;
10
+
11
+ interface FisheyeConfig extends IPluginBaseConfig {
12
+ trigger?: 'mousemove' | 'click' | 'drag';
13
+ d?: number;
14
+ r?: number;
15
+ delegateStyle?: ShapeStyle;
16
+ showLabel?: boolean;
17
+ scaleRBy?: 'wheel' | 'drag' | 'unset' | undefined;
18
+ scaleDBy?: 'wheel' | 'drag' | 'unset' | undefined;
19
+ maxR?: number;
20
+ minR?: number;
21
+ maxD?: number;
22
+ minD?: number;
23
+ throttle?: number;
24
+ showDPercent?: boolean;
25
+ }
26
+
27
+ const lensDelegateStyle = {
28
+ stroke: '#000',
29
+ strokeOpacity: 0.8,
30
+ lineWidth: 2,
31
+ fillOpacity: 0.1,
32
+ fill: '#ccc',
33
+ };
34
+ export default class Fisheye extends Base {
35
+ private pointerDown: Point | undefined = undefined;
36
+ private cacheCenter: Point | undefined = undefined;
37
+ private dragPrePos: Point | undefined = undefined;
38
+ private dragging = false;
39
+ private molecularParam: number;
40
+ private dPercentText: DisplayObject;
41
+ private delegate: DisplayObject;
42
+ private r2: number;
43
+
44
+ private cachedOriginPositions: {
45
+ [id: string]: {
46
+ x: number;
47
+ y: number;
48
+ texts: DisplayObject[];
49
+ };
50
+ } = {};
51
+
52
+ private cachedTransientTexts: {
53
+ [id: string]: DisplayObject;
54
+ } = {};
55
+
56
+ private delegateCenterDiff: { x: number; y: number };
57
+
58
+ constructor(options?: FisheyeConfig) {
59
+ super(options);
60
+ }
61
+ public getDefaultCfgs(): FisheyeConfig {
62
+ return {
63
+ trigger: 'mousemove',
64
+ d: 1.5,
65
+ r: 300,
66
+ delegateStyle: clone(lensDelegateStyle),
67
+ showLabel: true,
68
+ maxD: 5,
69
+ minD: 0,
70
+ throttle: 16,
71
+ scaleRBy: 'drag',
72
+ scaleDBy: 'drag',
73
+ showDPercent: true,
74
+ };
75
+ }
76
+
77
+ // class-methods-use-this
78
+ public getEvents() {
79
+ const events = {
80
+ pointerdown: this.onPointerDown,
81
+ pointerup: this.onPointerUp,
82
+ wheel: this.onWheel,
83
+ afterlayout: this.initOriginPositions,
84
+ } as {
85
+ [key: string]: any;
86
+ };
87
+ switch ((this as any).options.trigger) {
88
+ case 'click':
89
+ return {
90
+ ...events,
91
+ click: this.magnify,
92
+ };
93
+ case 'drag':
94
+ return {
95
+ ...events,
96
+ pointermove: this.onPointerMove,
97
+ };
98
+ default:
99
+ return {
100
+ ...events,
101
+ pointermove: this.magnify,
102
+ };
103
+ }
104
+ }
105
+
106
+ public init(graph: IGraph) {
107
+ super.init(graph);
108
+ const { r, d } = this.options;
109
+ this.cachedTransientTexts = {};
110
+ this.initOriginPositions();
111
+ this.r2 = r * r;
112
+ this.molecularParam = (d + 1) * r;
113
+ }
114
+
115
+ private initOriginPositions = () => {
116
+ const { graph } = this;
117
+ const positions = {};
118
+ graph.getAllNodesData().forEach((node) => {
119
+ positions[node.id] = { x: node.data.x, y: node.data.y, texts: [] };
120
+ });
121
+ this.cachedOriginPositions = positions;
122
+ return positions;
123
+ };
124
+
125
+ // Determine whether it is dragged in the delegate
126
+ protected isInLensDelegate(lensDelegate, pointer): boolean {
127
+ const { cx: lensX, cy: lensY, r: lensR } = lensDelegate.style;
128
+ if (
129
+ pointer.x >= lensX - lensR &&
130
+ pointer.x <= lensX + lensR &&
131
+ pointer.y >= lensY - lensR &&
132
+ pointer.y <= lensY + lensR
133
+ ) {
134
+ return true;
135
+ }
136
+ return false;
137
+ }
138
+
139
+ protected onPointerUp(e: IG6GraphEvent) {
140
+ this.pointerDown = undefined;
141
+ this.cacheCenter = undefined;
142
+ this.dragging = false;
143
+ }
144
+
145
+ protected onPointerMove(e: IG6GraphEvent) {
146
+ if (!this.dragging && this.pointerDown) {
147
+ this.dragging = true;
148
+ }
149
+ if (!this.pointerDown) return;
150
+ this.moveDelegate(this.delegate, e);
151
+ }
152
+
153
+ protected onPointerDown(e: IG6GraphEvent) {
154
+ const { delegate: lensDelegate } = this;
155
+ if (!lensDelegate || lensDelegate.destroyed) {
156
+ this.cacheCenter = { x: e.canvas.x, y: e.canvas.y };
157
+ this.magnify(e);
158
+ } else {
159
+ this.cacheCenter = {
160
+ x: lensDelegate.attr('cx'),
161
+ y: lensDelegate.attr('cy'),
162
+ };
163
+ }
164
+ this.pointerDown = { x: e.canvas.x, y: e.canvas.y };
165
+ this.dragging = false;
166
+ this.dragPrePos = { x: e.canvas.x, y: e.canvas.y };
167
+ }
168
+
169
+ protected moveDelegate(lensDelegate, e) {
170
+ if (this.isInLensDelegate(lensDelegate, { x: e.canvas.x, y: e.canvas.y })) {
171
+ this.delegateCenterDiff = {
172
+ x: e.canvas.x - this.pointerDown.x,
173
+ y: e.canvas.y - this.pointerDown.y,
174
+ };
175
+ this.magnify(e, this.cacheCenter);
176
+ }
177
+ }
178
+
179
+ protected onWheel(e: IG6GraphEvent) {
180
+ const { delegate: lensDelegate, options } = this;
181
+ const { scaleDBy, scaleRBy } = options;
182
+ if (!lensDelegate || lensDelegate.destroyed) return;
183
+ if (scaleDBy !== 'wheel' && scaleRBy !== 'wheel') return;
184
+ if (this.isInLensDelegate(lensDelegate, { x: e.canvas.x, y: e.canvas.y })) {
185
+ if (scaleRBy === 'wheel') {
186
+ this.scaleRByWheel(e);
187
+ }
188
+ if (scaleDBy === 'wheel') {
189
+ this.scaleDByWheel(e);
190
+ }
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Scale the range by wheel
196
+ * @param e mouse wheel event
197
+ */
198
+ protected scaleRByWheel(e: IG6GraphEvent) {
199
+ if (!e || !e.originalEvent) return;
200
+ if (e.preventDefault) e.preventDefault();
201
+ const { graph, options } = this;
202
+ let ratio;
203
+ const { delegate: lensDelegate, maxR, minR, d } = options;
204
+ let { r } = options;
205
+ const lensCenter = lensDelegate
206
+ ? {
207
+ x: lensDelegate.attr('cx'),
208
+ y: lensDelegate.attr('cy'),
209
+ }
210
+ : undefined;
211
+ const mousePos = lensCenter || { x: e.canvas.x, y: e.canvas.y };
212
+ if ((e.originalEvent as any).deltaY < 0) {
213
+ ratio = 1 - DELTA;
214
+ } else {
215
+ ratio = 1 / (1 - DELTA);
216
+ }
217
+ const graphHeight = graph.canvas.context.config.canvas.height;
218
+ if (
219
+ (r > (maxR || graphHeight) && ratio > 1) ||
220
+ (r < (minR || graphHeight * 0.05) && ratio < 1)
221
+ ) {
222
+ ratio = 1;
223
+ }
224
+ r *= ratio;
225
+ this.options.r = r;
226
+ this.r2 = r * r;
227
+ this.molecularParam = (d + 1) * r;
228
+ this.delegateCenterDiff = undefined;
229
+ this.magnify(e, mousePos);
230
+ }
231
+
232
+ /**
233
+ * Scale the range by dragging
234
+ * @param e mouse event
235
+ */
236
+ protected scaleRByDrag = throttle(
237
+ this.scaleRByDragMethod.bind(this),
238
+ this.options.throttle,
239
+ { leading: true, trailing: true },
240
+ );
241
+ protected scaleRByDragMethod(e: IG6GraphEvent) {
242
+ if (!e) return;
243
+ const { dragPrePos, graph, options } = this;
244
+ const { maxR, minR, d } = options;
245
+ let { r } = options;
246
+ let ratio;
247
+ if (e.canvas.x - dragPrePos.x < 0) {
248
+ ratio = 1 - DELTA;
249
+ } else {
250
+ ratio = 1 / (1 - DELTA);
251
+ }
252
+ const graphCanvasEl = graph.canvas.context.config.canvas;
253
+ const [
254
+ graphWidth = graphCanvasEl?.width || 500,
255
+ graphHeight = graphCanvasEl?.height || 500,
256
+ ] = graph.getSize();
257
+ if (
258
+ (r > (maxR || graphHeight) && ratio > 1) ||
259
+ (r < (minR || graphHeight * 0.05) && ratio < 1)
260
+ ) {
261
+ ratio = 1;
262
+ }
263
+ r *= ratio;
264
+ this.options.r = r;
265
+ this.r2 = r * r;
266
+ this.molecularParam = (d + 1) * r;
267
+ this.magnify(e);
268
+ this.dragPrePos = { x: e.canvas.x, y: e.canvas.y };
269
+ }
270
+
271
+ /**
272
+ * Scale the magnifying factor by wheel
273
+ * @param e mouse wheel event
274
+ */
275
+ protected scaleDByWheel(evt: IG6GraphEvent) {
276
+ if (!evt && !evt.originalEvent) return;
277
+ if (evt.preventDefault) evt.preventDefault();
278
+ let delta = 0;
279
+ if ((evt.originalEvent as any).deltaY < 0) {
280
+ delta = -0.1;
281
+ } else {
282
+ delta = 0.1;
283
+ }
284
+ const { d, maxD, minD, r } = this.options;
285
+ const newD = d + delta;
286
+ if (newD < maxD && newD > minD) {
287
+ this.options.d = newD;
288
+ this.molecularParam = (newD + 1) * r;
289
+ const { delegate: lensDelegate } = this;
290
+ const lensCenter = lensDelegate
291
+ ? {
292
+ x: lensDelegate.attr('cx'),
293
+ y: lensDelegate.attr('cy'),
294
+ }
295
+ : undefined;
296
+ this.delegateCenterDiff = undefined;
297
+ this.magnify(evt, lensCenter);
298
+ }
299
+ }
300
+
301
+ /**
302
+ * Scale the magnifying factor by dragging
303
+ * @param e mouse event
304
+ */
305
+ protected scaleDByDrag = throttle(
306
+ this.scaleDByDragMethod.bind(this),
307
+ this.options.throttle,
308
+ { leading: true, trailing: true },
309
+ );
310
+ protected scaleDByDragMethod(e: IG6GraphEvent) {
311
+ const { d, maxD, minD, r } = this.options;
312
+ const delta = e.canvas.x - this.dragPrePos.x > 0 ? 0.1 : -0.1;
313
+ const newD = d + delta;
314
+ if (newD < maxD && newD > minD) {
315
+ this.options.d = newD;
316
+ this.molecularParam = (newD + 1) * r;
317
+ this.magnify(e);
318
+ }
319
+ this.dragPrePos = { x: e.canvas.x, y: e.canvas.y };
320
+ }
321
+
322
+ /**
323
+ * Response function for mousemove, click, or drag to magnify
324
+ * @param e mouse event
325
+ */
326
+ protected magnify(e: IG6GraphEvent, mousePos?) {
327
+ this.restoreCache();
328
+ const {
329
+ r2,
330
+ graph,
331
+ cachedOriginPositions,
332
+ cachedTransientTexts,
333
+ molecularParam,
334
+ delegateCenterDiff,
335
+ options,
336
+ } = this;
337
+ const positions = Object.keys(cachedOriginPositions).map((id) => ({
338
+ id,
339
+ data: {
340
+ x: cachedOriginPositions[id].x,
341
+ y: cachedOriginPositions[id].y,
342
+ },
343
+ }));
344
+ const { r, d, showLabel, trigger } = options;
345
+ const nodes = graph.getAllNodesData();
346
+ const nodeLength = nodes.length;
347
+ const point = graph.getCanvasByClient(e.client);
348
+ let mCenter = mousePos
349
+ ? { x: mousePos.x, y: mousePos.y }
350
+ : { x: point.x, y: point.y };
351
+ if (this.dragging && (trigger === 'mousemove' || trigger === 'click')) {
352
+ mCenter = this.cacheCenter;
353
+ }
354
+ if (delegateCenterDiff) {
355
+ mCenter.x += delegateCenterDiff.x;
356
+ mCenter.y += delegateCenterDiff.y;
357
+ }
358
+ this.updateDelegate(mCenter, r, e);
359
+ for (let i = 0; i < nodeLength; i++) {
360
+ const model = nodes[i];
361
+ const { x, y } = cachedOriginPositions[model.id]; // calculate based on the origin positions
362
+ if (isNaN(x) || isNaN(y)) continue;
363
+ // the square of the distance between the node and the magnified center
364
+ const dist2 =
365
+ (x - mCenter.x) * (x - mCenter.x) + (y - mCenter.y) * (y - mCenter.y);
366
+ if (!isNaN(dist2) && dist2 < r2 && dist2 !== 0) {
367
+ const dist = Math.sqrt(dist2);
368
+ // // (r * (d + 1) * (dist / r)) / (d * (dist / r) + 1);
369
+ const magnifiedDist = (molecularParam * dist) / (d * dist + r);
370
+ const vecX = (x - mCenter.x) / dist;
371
+ const vecY = (y - mCenter.y) / dist;
372
+ const magnifiedX = vecX * magnifiedDist + mCenter.x;
373
+ const magnifiedY = vecY * magnifiedDist + mCenter.y;
374
+ const magnifiedNode = {
375
+ id: model.id,
376
+ data: { x: magnifiedX, y: magnifiedY },
377
+ };
378
+ const textContent =
379
+ model.data.label || (model.data.labelShape as any)?.text;
380
+ if (showLabel && 2 * dist < r) {
381
+ const transientTextID = `node-text-${model.id}`;
382
+ const cachedTransientText = cachedTransientTexts[transientTextID];
383
+ if (cachedTransientText) {
384
+ cachedTransientText.attr({
385
+ x: magnifiedNode.data.x,
386
+ y: magnifiedNode.data.y,
387
+ });
388
+ cachedTransientText.show();
389
+ cachedOriginPositions[model.id].texts.push(cachedTransientText);
390
+ } else if (textContent) {
391
+ const text = graph.drawTransient('text', transientTextID, {
392
+ style: {
393
+ text: textContent,
394
+ x: magnifiedNode.data.x,
395
+ y: magnifiedNode.data.y,
396
+ textAlign: 'center',
397
+ textBaseline: 'middle',
398
+ fill: '#000',
399
+ lineWidth: 1,
400
+ fontSize: 12,
401
+ isBillboard: true,
402
+ lod: -1,
403
+ },
404
+ });
405
+ cachedTransientTexts[transientTextID] = text;
406
+ cachedOriginPositions[model.id].texts.push(text);
407
+ }
408
+ }
409
+ positions[model.id] = magnifiedNode;
410
+ } else {
411
+ // update the origin positions outside of the lens
412
+ if (!cachedOriginPositions[model.id]) {
413
+ cachedOriginPositions[model.id] = { x, y, texts: [] };
414
+ }
415
+ }
416
+ }
417
+ graph.updateNodePosition(Object.values(positions));
418
+ }
419
+
420
+ protected restoreCache() {
421
+ const cachePositions = { ...this.cachedOriginPositions };
422
+ Object.values(this.cachedOriginPositions).forEach((position) => {
423
+ position.texts?.forEach((text) => text.hide());
424
+ });
425
+ return cachePositions;
426
+ }
427
+
428
+ /**
429
+ * Adjust part of the parameters, including trigger, d, r, maxR, minR, maxD, minD, scaleRBy, and scaleDBy
430
+ * @param {FisheyeConfig} cfg
431
+ */
432
+ public updateParams(cfg: FisheyeConfig) {
433
+ const { r, d, trigger, minD, maxD, minR, maxR, scaleDBy, scaleRBy } = cfg;
434
+ if (!isNaN(cfg.r)) {
435
+ this.options.r = r;
436
+ this.r2 = r * r;
437
+ }
438
+ if (!isNaN(d)) {
439
+ this.options.d = d;
440
+ }
441
+ if (!isNaN(maxD)) {
442
+ this.options.maxD = maxD;
443
+ }
444
+ if (!isNaN(minD)) {
445
+ this.options.minD = minD;
446
+ }
447
+ if (!isNaN(maxR)) {
448
+ this.options.maxR = maxR;
449
+ }
450
+ if (!isNaN(minR)) {
451
+ this.options.minR = minR;
452
+ }
453
+ const { graph, dPercentText, options } = this;
454
+ const { d: nd, r: nr } = options;
455
+ this.molecularParam = (nd + 1) * nr;
456
+ if (trigger === 'mousemove' || trigger === 'click' || trigger === 'drag') {
457
+ this.options.trigger = trigger;
458
+ }
459
+ if (scaleDBy === 'drag' || scaleDBy === 'wheel' || scaleDBy === 'unset') {
460
+ this.options.scaleDBy = scaleDBy;
461
+ graph.drawTransient('circle', 'lens-shape', { action: 'remove' });
462
+ if (dPercentText) {
463
+ graph.drawTransient('text', 'lens-text-shape', { action: 'remove' });
464
+ }
465
+ }
466
+ if (scaleRBy === 'drag' || scaleRBy === 'wheel' || scaleRBy === 'unset') {
467
+ this.options.scaleRBy = scaleRBy;
468
+ graph.drawTransient('circle', 'lens-shape', { action: 'remove' });
469
+ if (dPercentText) {
470
+ graph.drawTransient('text', 'lens-text-shape', { action: 'remove' });
471
+ }
472
+ }
473
+ }
474
+
475
+ /**
476
+ * Update the delegate shape of the lens
477
+ * @param {Point} mCenter the center of the shape
478
+ * @param {number} r the radius of the shape
479
+ */
480
+ private updateDelegate(mCenter, r, e) {
481
+ const { graph, dPercentText } = this;
482
+ let lensDelegate = this.delegate;
483
+ if (!lensDelegate || lensDelegate.destroyed) {
484
+ const attrs = this.options.delegateStyle || lensDelegateStyle;
485
+ lensDelegate = graph.drawTransient('circle', 'lens-shape', {
486
+ style: {
487
+ r: r / 1.5,
488
+ cx: mCenter.x,
489
+ cy: mCenter.y,
490
+ ...attrs,
491
+ },
492
+ });
493
+ } else {
494
+ lensDelegate.attr({
495
+ cx: mCenter.x,
496
+ cy: mCenter.y,
497
+ r: r / 1.5,
498
+ });
499
+ }
500
+
501
+ const { d, minD, maxD, showDPercent } = this.options;
502
+
503
+ // draw the zoom factor percentage text
504
+ if (showDPercent) {
505
+ const percent = Math.round(((d - minD) / (maxD - minD)) * 100);
506
+ const textY = mCenter.y + r / 1.5 + 16;
507
+ if (!dPercentText || dPercentText.destroyed) {
508
+ const text = graph.drawTransient('text', 'lens-text-shape', {
509
+ style: {
510
+ text: `${percent}%`,
511
+ x: mCenter.x,
512
+ y: textY,
513
+ fill: '#aaa',
514
+ stroke: '#fff',
515
+ lineWidth: 1,
516
+ fontSize: 12,
517
+ },
518
+ });
519
+ this.dPercentText = text;
520
+ } else {
521
+ dPercentText.attr({
522
+ text: `${percent}%`,
523
+ x: mCenter.x,
524
+ y: textY,
525
+ });
526
+ }
527
+ }
528
+ this.delegate = lensDelegate;
529
+
530
+ // Adjust the range of r and d
531
+ if (this.options.trigger !== 'drag') {
532
+ if (this.options.scaleRBy === 'drag') {
533
+ if (!this.pointerDown) return;
534
+ this.scaleRByDrag(e);
535
+ }
536
+
537
+ if (this.options.scaleDBy === 'drag') {
538
+ if (!this.pointerDown) return;
539
+ this.scaleDByDrag(e);
540
+ }
541
+ }
542
+ }
543
+
544
+ /**
545
+ * Clear the fisheye lens
546
+ */
547
+ public clear() {
548
+ const {
549
+ graph,
550
+ delegate: lensDelegate,
551
+ cachedOriginPositions,
552
+ dPercentText,
553
+ } = this;
554
+ this.restoreCache();
555
+ graph.updateNodePosition(
556
+ Object.keys(cachedOriginPositions).map((id) => ({
557
+ id,
558
+ data: {
559
+ x: cachedOriginPositions[id].x,
560
+ y: cachedOriginPositions[id].y,
561
+ },
562
+ })),
563
+ );
564
+ if (lensDelegate && !lensDelegate.destroyed) {
565
+ graph.drawTransient('circle', 'lens-shape', { action: 'remove' });
566
+ }
567
+ if (dPercentText && !dPercentText.destroyed) {
568
+ graph.drawTransient('text', 'lens-text-shape', { action: 'remove' });
569
+ }
570
+ }
571
+
572
+ /**
573
+ * Destroy the component
574
+ */
575
+ public destroy() {
576
+ this.clear();
577
+ }
578
+ }