@antv/g6 5.0.49 → 5.0.51

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 (205) hide show
  1. package/README.md +2 -1
  2. package/README.zh-CN.md +2 -1
  3. package/dist/g6.min.js +38 -59
  4. package/dist/lib/behaviors/brush-select.d.ts +2 -3
  5. package/dist/lib/behaviors/click-select.d.ts +2 -2
  6. package/dist/lib/behaviors/drag-element.d.ts +21 -2
  7. package/dist/lib/behaviors/focus-element.d.ts +18 -0
  8. package/dist/lib/exports.d.ts +1 -1
  9. package/dist/lib/layouts/types.d.ts +9 -1
  10. package/dist/lib/plugins/bubble-sets.d.ts +1 -1
  11. package/dist/lib/plugins/index.d.ts +2 -0
  12. package/dist/lib/plugins/legend.d.ts +10 -2
  13. package/dist/lib/plugins/minimap/index.d.ts +7 -4
  14. package/dist/lib/plugins/title/index.d.ts +85 -0
  15. package/dist/lib/runtime/element.d.ts +8 -0
  16. package/dist/lib/runtime/graph.d.ts +27 -0
  17. package/dist/lib/runtime/layout.d.ts +2 -1
  18. package/dist/lib/utils/data.d.ts +9 -0
  19. package/dist/lib/utils/element.d.ts +1 -1
  20. package/dist/lib/utils/vector.d.ts +9 -0
  21. package/dist/lib/version.d.ts +1 -1
  22. package/esm/behaviors/brush-select.d.ts +2 -3
  23. package/esm/behaviors/brush-select.js +14 -5
  24. package/esm/behaviors/brush-select.js.map +1 -1
  25. package/esm/behaviors/click-select.d.ts +2 -2
  26. package/esm/behaviors/click-select.js.map +1 -1
  27. package/esm/behaviors/create-edge.js +5 -1
  28. package/esm/behaviors/create-edge.js.map +1 -1
  29. package/esm/behaviors/drag-canvas.js +2 -7
  30. package/esm/behaviors/drag-canvas.js.map +1 -1
  31. package/esm/behaviors/drag-element-force.js +1 -1
  32. package/esm/behaviors/drag-element-force.js.map +1 -1
  33. package/esm/behaviors/drag-element.d.ts +21 -2
  34. package/esm/behaviors/drag-element.js +24 -3
  35. package/esm/behaviors/drag-element.js.map +1 -1
  36. package/esm/behaviors/focus-element.d.ts +18 -0
  37. package/esm/behaviors/focus-element.js +18 -1
  38. package/esm/behaviors/focus-element.js.map +1 -1
  39. package/esm/behaviors/lasso-select.js +3 -3
  40. package/esm/behaviors/lasso-select.js.map +1 -1
  41. package/esm/elements/combos/base-combo.js.map +1 -1
  42. package/esm/elements/edges/base-edge.js.map +1 -1
  43. package/esm/elements/nodes/base-node.js.map +1 -1
  44. package/esm/exports.d.ts +1 -1
  45. package/esm/exports.js +1 -1
  46. package/esm/exports.js.map +1 -1
  47. package/esm/layouts/types.d.ts +9 -1
  48. package/esm/plugins/bubble-sets.d.ts +1 -1
  49. package/esm/plugins/bubble-sets.js +30 -7
  50. package/esm/plugins/bubble-sets.js.map +1 -1
  51. package/esm/plugins/grid-line.js +1 -1
  52. package/esm/plugins/grid-line.js.map +1 -1
  53. package/esm/plugins/index.d.ts +2 -0
  54. package/esm/plugins/index.js +1 -0
  55. package/esm/plugins/index.js.map +1 -1
  56. package/esm/plugins/legend.d.ts +10 -2
  57. package/esm/plugins/legend.js.map +1 -1
  58. package/esm/plugins/minimap/index.d.ts +7 -4
  59. package/esm/plugins/minimap/index.js +41 -52
  60. package/esm/plugins/minimap/index.js.map +1 -1
  61. package/esm/plugins/title/index.d.ts +85 -0
  62. package/esm/plugins/title/index.js +172 -0
  63. package/esm/plugins/title/index.js.map +1 -0
  64. package/esm/plugins/tooltip.js +6 -1
  65. package/esm/plugins/tooltip.js.map +1 -1
  66. package/esm/registry/build-in.js +5 -4
  67. package/esm/registry/build-in.js.map +1 -1
  68. package/esm/runtime/canvas.js.map +1 -1
  69. package/esm/runtime/element.d.ts +8 -0
  70. package/esm/runtime/element.js +53 -8
  71. package/esm/runtime/element.js.map +1 -1
  72. package/esm/runtime/graph.d.ts +27 -0
  73. package/esm/runtime/graph.js +35 -2
  74. package/esm/runtime/graph.js.map +1 -1
  75. package/esm/runtime/layout.d.ts +2 -1
  76. package/esm/runtime/layout.js +8 -6
  77. package/esm/runtime/layout.js.map +1 -1
  78. package/esm/runtime/viewport.js +3 -0
  79. package/esm/runtime/viewport.js.map +1 -1
  80. package/esm/themes/base.js +1 -0
  81. package/esm/themes/base.js.map +1 -1
  82. package/esm/transforms/process-parallel-edges.js +16 -3
  83. package/esm/transforms/process-parallel-edges.js.map +1 -1
  84. package/esm/utils/data.d.ts +9 -0
  85. package/esm/utils/data.js +11 -0
  86. package/esm/utils/data.js.map +1 -1
  87. package/esm/utils/element.d.ts +1 -1
  88. package/esm/utils/element.js.map +1 -1
  89. package/esm/utils/shortcut.js +1 -1
  90. package/esm/utils/shortcut.js.map +1 -1
  91. package/esm/utils/vector.d.ts +9 -0
  92. package/esm/utils/vector.js +17 -0
  93. package/esm/utils/vector.js.map +1 -1
  94. package/esm/version.d.ts +1 -1
  95. package/esm/version.js +1 -1
  96. package/lib/behaviors/brush-select.d.ts +2 -3
  97. package/lib/behaviors/brush-select.js +14 -5
  98. package/lib/behaviors/brush-select.js.map +1 -1
  99. package/lib/behaviors/click-select.d.ts +2 -2
  100. package/lib/behaviors/click-select.js.map +1 -1
  101. package/lib/behaviors/create-edge.js +5 -1
  102. package/lib/behaviors/create-edge.js.map +1 -1
  103. package/lib/behaviors/drag-canvas.js +1 -6
  104. package/lib/behaviors/drag-canvas.js.map +1 -1
  105. package/lib/behaviors/drag-element-force.js +1 -1
  106. package/lib/behaviors/drag-element-force.js.map +1 -1
  107. package/lib/behaviors/drag-element.d.ts +21 -2
  108. package/lib/behaviors/drag-element.js +23 -2
  109. package/lib/behaviors/drag-element.js.map +1 -1
  110. package/lib/behaviors/focus-element.d.ts +18 -0
  111. package/lib/behaviors/focus-element.js +18 -1
  112. package/lib/behaviors/focus-element.js.map +1 -1
  113. package/lib/behaviors/lasso-select.js +3 -3
  114. package/lib/behaviors/lasso-select.js.map +1 -1
  115. package/lib/elements/combos/base-combo.js.map +1 -1
  116. package/lib/elements/edges/base-edge.js.map +1 -1
  117. package/lib/elements/nodes/base-node.js.map +1 -1
  118. package/lib/exports.d.ts +1 -1
  119. package/lib/exports.js +3 -2
  120. package/lib/exports.js.map +1 -1
  121. package/lib/layouts/types.d.ts +9 -1
  122. package/lib/plugins/bubble-sets.d.ts +1 -1
  123. package/lib/plugins/bubble-sets.js +30 -7
  124. package/lib/plugins/bubble-sets.js.map +1 -1
  125. package/lib/plugins/grid-line.js +1 -1
  126. package/lib/plugins/grid-line.js.map +1 -1
  127. package/lib/plugins/index.d.ts +2 -0
  128. package/lib/plugins/index.js +3 -1
  129. package/lib/plugins/index.js.map +1 -1
  130. package/lib/plugins/legend.d.ts +10 -2
  131. package/lib/plugins/legend.js.map +1 -1
  132. package/lib/plugins/minimap/index.d.ts +7 -4
  133. package/lib/plugins/minimap/index.js +41 -52
  134. package/lib/plugins/minimap/index.js.map +1 -1
  135. package/lib/plugins/title/index.d.ts +85 -0
  136. package/lib/plugins/title/index.js +176 -0
  137. package/lib/plugins/title/index.js.map +1 -0
  138. package/lib/plugins/tooltip.js +6 -1
  139. package/lib/plugins/tooltip.js.map +1 -1
  140. package/lib/registry/build-in.js +1 -0
  141. package/lib/registry/build-in.js.map +1 -1
  142. package/lib/runtime/canvas.js.map +1 -1
  143. package/lib/runtime/element.d.ts +8 -0
  144. package/lib/runtime/element.js +53 -8
  145. package/lib/runtime/element.js.map +1 -1
  146. package/lib/runtime/graph.d.ts +27 -0
  147. package/lib/runtime/graph.js +35 -2
  148. package/lib/runtime/graph.js.map +1 -1
  149. package/lib/runtime/layout.d.ts +2 -1
  150. package/lib/runtime/layout.js +8 -6
  151. package/lib/runtime/layout.js.map +1 -1
  152. package/lib/runtime/viewport.js +3 -0
  153. package/lib/runtime/viewport.js.map +1 -1
  154. package/lib/themes/base.js +1 -0
  155. package/lib/themes/base.js.map +1 -1
  156. package/lib/transforms/process-parallel-edges.js +16 -3
  157. package/lib/transforms/process-parallel-edges.js.map +1 -1
  158. package/lib/utils/data.d.ts +9 -0
  159. package/lib/utils/data.js +13 -0
  160. package/lib/utils/data.js.map +1 -1
  161. package/lib/utils/element.d.ts +1 -1
  162. package/lib/utils/element.js.map +1 -1
  163. package/lib/utils/shortcut.js +1 -1
  164. package/lib/utils/shortcut.js.map +1 -1
  165. package/lib/utils/vector.d.ts +9 -0
  166. package/lib/utils/vector.js +18 -0
  167. package/lib/utils/vector.js.map +1 -1
  168. package/lib/version.d.ts +1 -1
  169. package/lib/version.js +1 -1
  170. package/package.json +11 -11
  171. package/src/behaviors/brush-select.ts +17 -7
  172. package/src/behaviors/click-select.ts +1 -1
  173. package/src/behaviors/create-edge.ts +5 -3
  174. package/src/behaviors/drag-canvas.ts +2 -6
  175. package/src/behaviors/drag-element-force.ts +2 -1
  176. package/src/behaviors/drag-element.ts +37 -4
  177. package/src/behaviors/focus-element.ts +28 -1
  178. package/src/behaviors/lasso-select.ts +3 -3
  179. package/src/elements/combos/base-combo.ts +2 -1
  180. package/src/elements/edges/base-edge.ts +2 -1
  181. package/src/elements/nodes/base-node.ts +2 -1
  182. package/src/exports.ts +1 -0
  183. package/src/layouts/types.ts +9 -1
  184. package/src/plugins/bubble-sets.ts +37 -9
  185. package/src/plugins/grid-line.ts +1 -1
  186. package/src/plugins/index.ts +2 -0
  187. package/src/plugins/legend.ts +1 -1
  188. package/src/plugins/minimap/index.ts +50 -63
  189. package/src/plugins/title/index.ts +300 -0
  190. package/src/plugins/tooltip.ts +8 -2
  191. package/src/registry/build-in.ts +6 -4
  192. package/src/runtime/canvas.ts +4 -2
  193. package/src/runtime/element.ts +54 -9
  194. package/src/runtime/graph.ts +36 -2
  195. package/src/runtime/layout.ts +13 -6
  196. package/src/runtime/viewport.ts +14 -3
  197. package/src/themes/base.ts +1 -0
  198. package/src/transforms/process-parallel-edges.ts +17 -3
  199. package/src/types/edge.ts +1 -3
  200. package/src/utils/data.ts +12 -0
  201. package/src/utils/element.ts +5 -5
  202. package/src/utils/shortcut.ts +1 -1
  203. package/src/utils/vector.ts +17 -0
  204. package/src/version.ts +1 -1
  205. package/src/layouts/hierarchy.d.ts +0 -10
@@ -51,11 +51,11 @@ export interface BubbleSetsOptions extends BasePluginOptions, IBubbleSetOptions,
51
51
  * <en/> The principle is to represent sets by creating a shape similar to a bubble. Each set is represented by a unique "bubble", and the elements in the set are contained within this bubble. If two sets have an intersection, then the two bubbles will have an overlapping part, which represents the intersection of the two sets.
52
52
  */
53
53
  export class BubbleSets extends BasePlugin<BubbleSetsOptions> {
54
- private shape!: Contour;
54
+ private shape?: Contour;
55
55
 
56
56
  private bubbleSets!: BubbleSetsJS;
57
57
 
58
- private path!: PathArray;
58
+ private path: PathArray | null = null;
59
59
 
60
60
  private members: Map<ID, IRectangle | ILine> = new Map();
61
61
 
@@ -90,8 +90,9 @@ export class BubbleSets extends BasePlugin<BubbleSetsOptions> {
90
90
 
91
91
  private init() {
92
92
  this.bubbleSets = new BubbleSetsJS(this.options);
93
- this.members = new Map();
94
- this.avoidMembers = new Map();
93
+ this.members.clear();
94
+ this.avoidMembers.clear();
95
+ this.path = null;
95
96
  }
96
97
 
97
98
  private parseOptions() {
@@ -139,8 +140,21 @@ export class BubbleSets extends BasePlugin<BubbleSetsOptions> {
139
140
  const currAvoidMembers = this.options.avoidMembers;
140
141
  const prevAvoidMembers = [...this.avoidMembers.keys()];
141
142
 
142
- if (!forceUpdateId && isEqual(currMembers, prevMembers) && isEqual(currAvoidMembers, prevAvoidMembers))
143
+ if (currMembers.length === 0 && currAvoidMembers.length === 0) {
144
+ this.members.clear();
145
+ this.avoidMembers.clear();
146
+ this.path = [] as unknown as PathArray;
143
147
  return this.path;
148
+ }
149
+
150
+ if (
151
+ !forceUpdateId &&
152
+ this.path &&
153
+ isEqual(currMembers, prevMembers) &&
154
+ isEqual(currAvoidMembers, prevAvoidMembers)
155
+ ) {
156
+ return this.path;
157
+ }
144
158
 
145
159
  const { enter: membersToEnter = [], exit: membersToExit = [] } = arrayDiff(prevMembers, currMembers, (d) => d);
146
160
  const { enter: avoidMembersToEnter = [], exit: avoidMembersToExit = [] } = arrayDiff(
@@ -150,8 +164,17 @@ export class BubbleSets extends BasePlugin<BubbleSetsOptions> {
150
164
  );
151
165
 
152
166
  if (forceUpdateId) {
153
- membersToExit.push(forceUpdateId);
154
- membersToEnter.push(forceUpdateId);
167
+ const isMemberNow = currMembers.includes(forceUpdateId);
168
+ const isAvoidNow = currAvoidMembers.includes(forceUpdateId);
169
+
170
+ if (isMemberNow) {
171
+ membersToExit.push(forceUpdateId);
172
+ membersToEnter.push(forceUpdateId);
173
+ }
174
+ if (isAvoidNow) {
175
+ avoidMembersToExit.push(forceUpdateId);
176
+ avoidMembersToEnter.push(forceUpdateId);
177
+ }
155
178
  }
156
179
 
157
180
  const updateBubbleSets = (ids: ID[], isEntering: boolean, isMember: boolean) => {
@@ -247,7 +270,7 @@ export class BubbleSets extends BasePlugin<BubbleSetsOptions> {
247
270
  */
248
271
  public addAvoidMember(avoidMembers: ID | ID[]) {
249
272
  const avoidMembersToAdd = Array.isArray(avoidMembers) ? avoidMembers : [avoidMembers];
250
- if (avoidMembersToAdd.some((AvoidMember) => this.options.members.includes(AvoidMember))) {
273
+ if (avoidMembersToAdd.some((avoidMember) => this.options.members.includes(avoidMember))) {
251
274
  this.options.members = this.options.members.filter((id) => !avoidMembersToAdd.includes(id));
252
275
  }
253
276
  this.options.avoidMembers = [...new Set([...this.options.avoidMembers, ...avoidMembersToAdd])];
@@ -294,7 +317,12 @@ export class BubbleSets extends BasePlugin<BubbleSetsOptions> {
294
317
  public destroy(): void {
295
318
  this.context.graph.off(GraphEvent.AFTER_RENDER, this.drawBubbleSets);
296
319
  this.context.graph.off(GraphEvent.AFTER_ELEMENT_UPDATE, this.updateBubbleSetsPath);
297
- this.shape.destroy();
320
+
321
+ if (this.shape) {
322
+ this.shape.destroy();
323
+ this.shape = undefined;
324
+ }
325
+
298
326
  super.destroy();
299
327
  }
300
328
  }
@@ -177,7 +177,7 @@ export class GridLine extends BasePlugin<GridLineOptions> {
177
177
  data: { scale, origin },
178
178
  } = event;
179
179
 
180
- if (!scale) return;
180
+ if (!scale || (origin === undefined && this.context.viewport === undefined)) return;
181
181
 
182
182
  const prevScale = this.currentScale;
183
183
  this.currentScale = scale;
@@ -14,6 +14,7 @@ export { Legend } from './legend';
14
14
  export { Minimap } from './minimap';
15
15
  export { Snapline } from './snapline';
16
16
  export { Timebar } from './timebar';
17
+ export { Title } from './title';
17
18
  export { Toolbar } from './toolbar';
18
19
  export { Tooltip } from './tooltip';
19
20
  export { Watermark } from './watermark';
@@ -34,6 +35,7 @@ export type { LegendOptions } from './legend';
34
35
  export type { MinimapOptions } from './minimap';
35
36
  export type { SnaplineOptions } from './snapline';
36
37
  export type { TimebarOptions } from './timebar';
38
+ export type { SubTitleStyle, TitleOptions, TitleStyle } from './title';
37
39
  export type { ToolbarOptions } from './toolbar';
38
40
  export type { TooltipOptions } from './tooltip';
39
41
  export type { WatermarkOptions } from './watermark';
@@ -248,7 +248,7 @@ export class Legend extends BasePlugin<LegendOptions> {
248
248
  };
249
249
  };
250
250
 
251
- private getMarkerData = (field: string | ((item: ElementDatum) => string), elementType: ElementType) => {
251
+ protected getMarkerData = (field: string | ((item: ElementDatum) => string), elementType: ElementType) => {
252
252
  if (!field) return [];
253
253
  const { model, element } = this.context;
254
254
  const { nodes, edges, combos } = model.getData();
@@ -3,7 +3,7 @@ import { debounce, throttle } from '@antv/util';
3
3
  import { GraphEvent } from '../../constants';
4
4
  import type { RuntimeContext } from '../../runtime/types';
5
5
  import { GraphData } from '../../spec';
6
- import type { ElementDatum, ElementType, ID, IGraphLifeCycleEvent, Padding, Placement, Vector3 } from '../../types';
6
+ import type { ElementDatum, ElementType, IGraphLifeCycleEvent, Padding, Placement, Vector3 } from '../../types';
7
7
  import { isVisible } from '../../utils/element';
8
8
  import { idOf } from '../../utils/id';
9
9
  import { parsePadding } from '../../utils/padding';
@@ -56,13 +56,17 @@ export interface MinimapOptions extends BasePluginOptions {
56
56
  * @remarks
57
57
  * <zh/>
58
58
  * - 'key' 使用元素的主图形作为缩略图形
59
- * - 也可以传入一个函数,接收元素的 id 和类型,返回一个图形
59
+ * - 'icon' 使用元素中心的 icon 作为缩略图形
60
+ * - 更多图形名称可查阅 https://g6.antv.antgroup.com/manual/element/node/base-node#style
61
+ * - 也可以传入一个函数,接收元素的 [id, 类型, 元素节点],返回一个自定义样式的图形
60
62
  *
61
63
  * <en/>
62
64
  * - 'key' uses the key shape of the element as the thumbnail shape
63
- * - You can also pass in a function that receives the id and type of the element and returns a shape
65
+ * - 'icon' uses the icon shape of the element as the thumbnail shape
66
+ * - more shape name see https://g6.antv.antgroup.com/manual/element/node/base-node#style
67
+ * - You can also pass in a function that receives the [id, type of the element, element] and returns a custom shape
64
68
  */
65
- shape?: 'key' | ((id: string, elementType: ElementType) => DisplayObject);
69
+ shape?: string | 'key' | 'icon' | ((id: string, elementType: ElementType, element: DisplayObject) => DisplayObject);
66
70
  /**
67
71
  * <zh/> 缩略图画布类名,传入外置容器时不生效
68
72
  *
@@ -169,8 +173,6 @@ export class Minimap extends BasePlugin<MinimapOptions> {
169
173
 
170
174
  private onRender!: () => void;
171
175
 
172
- private shapes = new Map<ID, DisplayObject>();
173
-
174
176
  /**
175
177
  * <zh/> 创建或更新缩略图
176
178
  *
@@ -214,65 +216,46 @@ export class Minimap extends BasePlugin<MinimapOptions> {
214
216
  const { shape } = this.options;
215
217
  const { element } = this.context;
216
218
 
217
- if (shape === 'key') {
218
- const ids = new Set<ID>();
219
-
220
- const iterate = (datum: ElementDatum) => {
221
- const id = idOf(datum);
222
- ids.add(id);
223
-
224
- const target = element!.getElement(id);
225
- if (!target) return;
226
-
227
- const shape = target.getShape('key');
228
- const cloneShape = this.shapes.get(id) || shape.cloneNode();
229
-
230
- cloneShape.setPosition(shape.getPosition());
231
- // keep zIndex / id
232
- if (target.style.zIndex) cloneShape.style.zIndex = target.style.zIndex;
233
- cloneShape.id = target.id;
234
-
235
- if (!this.shapes.has(id)) {
236
- canvas.appendChild(cloneShape);
237
- this.shapes.set(id, cloneShape);
219
+ const iterate = (datum: ElementDatum, elType: ElementType) => {
220
+ const id = idOf(datum);
221
+ const target = element?.getElement(id);
222
+ if (!target) return;
223
+
224
+ const keyShape = target.getShape('key');
225
+ let cloneShape: DisplayObject;
226
+
227
+ if (typeof shape === 'string') {
228
+ const shapeName = shape;
229
+ const miniShape = target.getShape(shapeName);
230
+ cloneShape = miniShape.cloneNode();
231
+ } else {
232
+ const miniShape = shape(id, elType, target);
233
+ if (miniShape === target) {
234
+ cloneShape = miniShape.cloneNode(true);
238
235
  } else {
239
- Object.entries(shape.attributes).forEach(([key, value]) => {
240
- if (cloneShape.style[key] !== value) cloneShape.style[key] = value;
241
- });
236
+ cloneShape = miniShape;
242
237
  }
243
- };
244
-
245
- // 注意执行顺序 / Note the execution order
246
- edges.forEach(iterate);
247
- combos.forEach(iterate);
248
- nodes.forEach(iterate);
249
-
250
- this.shapes.forEach((shape, id) => {
251
- if (!ids.has(id)) {
252
- canvas.removeChild(shape);
253
- this.shapes.delete(id);
254
- }
255
- });
256
-
257
- return;
258
- }
259
-
260
- const setPosition = (id: ID, shape: DisplayObject) => {
261
- const target = element!.getElement(id)!;
262
- const position = target.getPosition();
263
- shape.setPosition(position);
264
- return shape;
238
+ }
239
+
240
+ /**
241
+ * 这里使用的是 keyShape 的位置
242
+ * 对于整个元素的位置而言,使用 keyShape 位置会比较准确
243
+ * 也比较合理
244
+ */
245
+ cloneShape.setPosition(keyShape.getPosition());
246
+ // keep zIndex / id
247
+ if (target.style.zIndex) cloneShape.style.zIndex = target.style.zIndex;
248
+ cloneShape.id = target.id;
249
+
250
+ canvas.appendChild(cloneShape);
265
251
  };
266
252
 
267
253
  canvas.removeChildren();
268
254
 
269
- edges.forEach((datum) => canvas.appendChild(shape(idOf(datum), 'edge')));
270
- combos.forEach((datum) => {
271
- canvas.appendChild(setPosition(idOf(datum), shape(idOf(datum), 'combo')));
272
- });
273
- nodes.forEach((datum) => {
274
- canvas.appendChild(setPosition(idOf(datum), shape(idOf(datum), 'node')));
275
- });
255
+ // 注意执行顺序 / Note the execution order
256
+ edges.forEach((datum) => iterate(datum, 'edge'));
257
+ combos.forEach((datum) => iterate(datum, 'combo'));
258
+ nodes.forEach((datum) => iterate(datum, 'node'));
276
259
  }
277
260
 
278
261
  private container!: HTMLElement;
@@ -463,12 +446,16 @@ export class Minimap extends BasePlugin<MinimapOptions> {
463
446
  // 当拖拽画布导致 mask 缩小时,拖拽 mask 时,能够恢复到实际大小
464
447
  // When dragging the canvas causes the mask to shrink, dragging the mask will restore it to its actual size
465
448
  if (width < fullWidth) {
466
- if (movementX > 0) ((x = lower(x - movementX, 0)), (width = upper(width + movementX, minimapWidth)));
467
- else if (movementX < 0) width = upper(width - movementX, minimapWidth);
449
+ if (movementX > 0) {
450
+ x = lower(x - movementX, 0);
451
+ width = upper(width + movementX, minimapWidth);
452
+ } else if (movementX < 0) width = upper(width - movementX, minimapWidth);
468
453
  }
469
454
  if (height < fullHeight) {
470
- if (movementY > 0) ((y = lower(y - movementY, 0)), (height = upper(height + movementY, minimapHeight)));
471
- else if (movementY < 0) height = upper(height - movementY, minimapHeight);
455
+ if (movementY > 0) {
456
+ y = lower(y - movementY, 0);
457
+ height = upper(height + movementY, minimapHeight);
458
+ } else if (movementY < 0) height = upper(height - movementY, minimapHeight);
472
459
  }
473
460
 
474
461
  Object.assign(this.mask.style, {
@@ -0,0 +1,300 @@
1
+ import { Canvas } from '@antv/g';
2
+ import { GraphEvent } from '../../constants';
3
+ import type { LabelStyleProps } from '../../elements/shapes/label';
4
+ import { Label } from '../../elements/shapes/label';
5
+ import type { RuntimeContext } from '../../runtime/types';
6
+ import type { Prefix } from '../../types';
7
+ import { parsePadding } from '../../utils/padding';
8
+ import { subStyleProps } from '../../utils/prefix';
9
+ import type { BasePluginOptions } from '../base-plugin';
10
+ import { BasePlugin } from '../base-plugin';
11
+ import { createPluginCanvas } from '../utils/canvas';
12
+
13
+ const commonStyle: Partial<LabelStyleProps> = {
14
+ fill: '#1D2129',
15
+ wordWrap: true, // 自动换行
16
+ maxLines: 1, // 最大行数
17
+ textOverflow: 'ellipsis', // 溢出隐藏省略号
18
+ textBaseline: 'top',
19
+
20
+ /**
21
+ * textAlign 需要和 x 结合使用
22
+ * 举例: 前提条件: 画布 width = 600
23
+ * - textAlign: 'start' | 'left
24
+ * 需要设 x = 0
25
+ * - textAlign: 'end' | 'right'
26
+ * 需要设 x = 600 (即画布的宽度)
27
+ * - textAlign: 'center'
28
+ * 需要设 x = 300 (即画布的宽度 / 2)
29
+ */
30
+ textAlign: 'start',
31
+ x: 0,
32
+ };
33
+ const defaultTitleStyle: Partial<LabelStyleProps> = {
34
+ ...commonStyle,
35
+ fillOpacity: 0.9,
36
+ fontSize: 16,
37
+ fontWeight: 'bold',
38
+ };
39
+ const defaultSubTitleStyle: Partial<LabelStyleProps> = {
40
+ ...commonStyle,
41
+ fillOpacity: 0.65,
42
+ fontSize: 12,
43
+ fontWeight: 'normal',
44
+ };
45
+ const defaultOptions: Partial<TitleOptions> = {
46
+ align: 'left',
47
+ spacing: 8,
48
+ size: 44,
49
+ padding: [16, 24, 0, 24],
50
+ };
51
+
52
+ const titleKey = 'title';
53
+ const subtitleKey = 'subtitle';
54
+
55
+ /**
56
+ * <zh/> 标题的样式
57
+ *
58
+ * <en/> Title styles
59
+ */
60
+ export type TitleStyle = Prefix<typeof titleKey, Omit<LabelStyleProps, 'x' | 'y' | 'text'>>;
61
+ /**
62
+ * <zh/> 副标题的样式
63
+ *
64
+ * <en/> Subtitle styles
65
+ */
66
+ export type SubTitleStyle = Prefix<typeof subtitleKey, Omit<LabelStyleProps, 'x' | 'y' | 'text'>>;
67
+
68
+ /**
69
+ * <zh/> 标题插件配置项
70
+ *
71
+ * <en/> Title plugin options
72
+ */
73
+ export interface TitleOptions extends BasePluginOptions, TitleStyle, SubTitleStyle {
74
+ /**
75
+ * <zh/> 整个标题的高度
76
+ *
77
+ * <en/> whole title height
78
+ * @defaultValue 44
79
+ */
80
+ size?: number;
81
+ /**
82
+ * <zh/> 整个标题位于图的位置
83
+ *
84
+ * <en/> The entire title is located at the position of the graph
85
+ * @defaultValue 'left'
86
+ */
87
+ align?: 'left' | 'center' | 'right';
88
+ /**
89
+ * <zh/> 主标题、副标题之间的上下间距
90
+ *
91
+ * <en/> The y spacing between the title and subtitle
92
+ * @defaultValue 8
93
+ */
94
+ spacing?: number;
95
+ /**
96
+ * <zh/> 标题内边距
97
+ *
98
+ * <en/> whole title padding
99
+ * @defaultValue [16, 24, 0, 24]
100
+ */
101
+ padding?: number | number[];
102
+ /**
103
+ * <zh/> 标题内容
104
+ *
105
+ * <en/> title text
106
+ */
107
+ [titleKey]: string;
108
+ /**
109
+ * <zh/> 副标题内容
110
+ *
111
+ * <en/> subtitle text
112
+ */
113
+ [subtitleKey]?: string | null;
114
+ /**
115
+ * <zh/> 标题画布类名,传入外置容器时不生效
116
+ *
117
+ * <en/> The class name of the title canvas, which does not take effect when an external container is passed in
118
+ */
119
+ className?: string;
120
+ }
121
+
122
+ export class Title extends BasePlugin<TitleOptions> {
123
+ private canvas!: Canvas;
124
+ private container!: HTMLElement;
125
+
126
+ private get padding() {
127
+ return parsePadding(this.options.padding);
128
+ }
129
+
130
+ constructor(context: RuntimeContext, options: TitleOptions) {
131
+ const combineOption = Object.assign({}, defaultOptions, options);
132
+ super(context, combineOption);
133
+
134
+ this.bindEvents();
135
+ }
136
+
137
+ private onRender = () => {
138
+ const canvas = this.updateCanvas();
139
+ this.renderTitle(canvas);
140
+ };
141
+
142
+ private bindEvents() {
143
+ const { graph } = this.context;
144
+ graph.on(GraphEvent.AFTER_RENDER, this.onRender);
145
+ graph.on(GraphEvent.AFTER_ANIMATE, this.onRender);
146
+ }
147
+
148
+ private unbindEvents() {
149
+ const { graph } = this.context;
150
+ graph.off(GraphEvent.AFTER_RENDER, this.onRender);
151
+ graph.off(GraphEvent.AFTER_ANIMATE, this.onRender);
152
+ }
153
+
154
+ public destroy(): void {
155
+ this.unbindEvents();
156
+ this.canvas?.destroy();
157
+ this.container?.remove();
158
+ super.destroy();
159
+ }
160
+
161
+ private updateCanvas() {
162
+ const { size, className, align } = this.options;
163
+ const [width] = this.context.canvas.getSize();
164
+ const [pt = 0, , pb = 0] = this.padding;
165
+ const height = size + pt + pb;
166
+
167
+ if (this.canvas) {
168
+ const { width: w, height: h } = this.canvas.getConfig();
169
+ if (width !== w || height !== h) this.canvas.resize(width, height);
170
+ } else {
171
+ const positions = {
172
+ left: 'left-top',
173
+ center: 'top',
174
+ right: 'right-top',
175
+ } as const;
176
+
177
+ const [$container, canvas] = createPluginCanvas({
178
+ width,
179
+ height,
180
+ placement: positions[align] || positions.left,
181
+ className: 'title-canvas',
182
+ graphCanvas: this.context.canvas,
183
+ });
184
+
185
+ if (className) $container.classList.add(className);
186
+
187
+ this.container = $container;
188
+ this.canvas = canvas;
189
+ }
190
+
191
+ return this.canvas;
192
+ }
193
+
194
+ private renderTitle(canvas: Canvas) {
195
+ const titles = new TitleComponent({
196
+ options: this.options,
197
+ ctx: this.context,
198
+ });
199
+
200
+ canvas.removeChildren();
201
+ titles.getTitle().forEach((label) => {
202
+ if (label) canvas.appendChild(label);
203
+ });
204
+ }
205
+ }
206
+
207
+ class TitleComponent {
208
+ private options: TitleOptions;
209
+
210
+ private context: RuntimeContext;
211
+ private get padding() {
212
+ return parsePadding(this.options.padding);
213
+ }
214
+
215
+ constructor(props: { ctx: RuntimeContext; options: TitleOptions }) {
216
+ const { options, ctx } = props;
217
+
218
+ this.options = options;
219
+
220
+ this.context = ctx;
221
+ }
222
+
223
+ public getTitle() {
224
+ const {
225
+ [titleKey]: propsTitle,
226
+ [subtitleKey]: propsSubtitle,
227
+ spacing = 44,
228
+ padding,
229
+ align,
230
+ ...style
231
+ } = this.options;
232
+
233
+ const titleText = propsTitle;
234
+ const subTitleText = propsSubtitle;
235
+
236
+ const titleStyle = subStyleProps(style, titleKey) as LabelStyleProps;
237
+ const subtitleStyle = subStyleProps(style, subtitleKey) as LabelStyleProps;
238
+
239
+ const [topGraphWidth] = this.context.graph.getSize();
240
+ const [pt = 0, pr = 0, , pl = 0] = this.padding;
241
+ const canvasWidth = topGraphWidth;
242
+ const textWidth = canvasWidth - pl - pr;
243
+
244
+ let subTitle: Label | null = null;
245
+
246
+ let alignX = pl;
247
+ let textAlign = 'left' as 'left' | 'center' | 'right';
248
+
249
+ switch (align) {
250
+ case 'left':
251
+ alignX = pl;
252
+ textAlign = 'left';
253
+ break;
254
+ case 'center':
255
+ alignX = canvasWidth / 2;
256
+ textAlign = 'center';
257
+ break;
258
+
259
+ case 'right':
260
+ alignX = canvasWidth - pr;
261
+ textAlign = 'right';
262
+ break;
263
+ default:
264
+ alignX = pl;
265
+ textAlign = 'left';
266
+ }
267
+
268
+ const title = new Label({
269
+ className: titleKey,
270
+ style: {
271
+ ...defaultTitleStyle,
272
+ wordWrapWidth: textWidth - 5,
273
+ x: alignX,
274
+ y: pt,
275
+ textAlign,
276
+ ...titleStyle,
277
+ text: titleText,
278
+ },
279
+ });
280
+
281
+ const titleBBox = title.getBBox();
282
+
283
+ if (subTitleText) {
284
+ subTitle = new Label({
285
+ className: 'subTitle',
286
+ style: {
287
+ ...defaultSubTitleStyle,
288
+ wordWrapWidth: textWidth - 5,
289
+ x: alignX,
290
+ y: titleBBox.height + spacing + pt,
291
+ textAlign,
292
+ ...subtitleStyle,
293
+ text: subTitleText,
294
+ },
295
+ });
296
+ }
297
+
298
+ return [title, subTitle];
299
+ }
300
+ }
@@ -13,7 +13,8 @@ import { BasePlugin } from './base-plugin';
13
13
  * <en/> Tooltip plugin options
14
14
  */
15
15
  export interface TooltipOptions
16
- extends BasePluginOptions,
16
+ extends
17
+ BasePluginOptions,
17
18
  Pick<TooltipStyleProps, 'position' | 'offset' | 'enterable' | 'style' | 'container' | 'title'> {
18
19
  /**
19
20
  * <zh/> 触发行为,可选 hover | click
@@ -254,7 +255,12 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
254
255
  const { getContent, title } = this.options;
255
256
  const items: ElementDatum[] = this.getElementData(id, targetType as ElementType);
256
257
 
257
- if (!this.tooltipElement || !this.isEnable(event, items)) return;
258
+ if (!this.tooltipElement) return;
259
+ // if shown, when is not enable, hide
260
+ if (!this.isEnable(event, items)) {
261
+ this.hide(event);
262
+ return;
263
+ }
258
264
 
259
265
  let tooltipContent: { [key: string]: unknown } = {};
260
266
  if (getContent) {
@@ -87,6 +87,7 @@ import {
87
87
  Minimap,
88
88
  Snapline,
89
89
  Timebar,
90
+ Title,
90
91
  Toolbar,
91
92
  Tooltip,
92
93
  Watermark,
@@ -152,20 +153,20 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
152
153
  layout: {
153
154
  'antv-dagre': AntVDagreLayout,
154
155
  'combo-combined': ComboCombinedLayout,
155
- 'compact-box': compactBox,
156
+ 'compact-box': compactBox as any,
156
157
  'd3-force': D3ForceLayout,
157
158
  'force-atlas2': ForceAtlas2Layout,
158
159
  circular: CircularLayout,
159
160
  concentric: ConcentricLayout,
160
161
  dagre: DagreLayout,
161
- dendrogram,
162
+ dendrogram: dendrogram as any,
162
163
  fishbone: FishboneLayout,
163
164
  force: ForceLayout,
164
165
  fruchterman: FruchtermanLayout,
165
166
  grid: GridLayout,
166
- indented,
167
+ indented: indented as any,
167
168
  mds: MDSLayout,
168
- mindmap,
169
+ mindmap: mindmap as any,
169
170
  radial: RadialLayout,
170
171
  random: RandomLayout,
171
172
  snake: SnakeLayout,
@@ -208,6 +209,7 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
208
209
  minimap: Minimap,
209
210
  snapline: Snapline,
210
211
  timebar: Timebar,
212
+ title: Title,
211
213
  toolbar: Toolbar,
212
214
  tooltip: Tooltip,
213
215
  watermark: Watermark,
@@ -8,8 +8,10 @@ import type { CanvasLayer, Point } from '../types';
8
8
  import { getBBoxSize, getCombinedBBox } from '../utils/bbox';
9
9
  import { parsePoint, toPointObject } from '../utils/point';
10
10
 
11
- export interface CanvasConfig
12
- extends Pick<GCanvasConfig, 'container' | 'devicePixelRatio' | 'width' | 'height' | 'cursor' | 'background'> {
11
+ export interface CanvasConfig extends Pick<
12
+ GCanvasConfig,
13
+ 'container' | 'devicePixelRatio' | 'width' | 'height' | 'cursor' | 'background'
14
+ > {
13
15
  /**
14
16
  * <zh/> 渲染器
15
17
  *