@antv/g6 5.0.29 → 5.0.31

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 (210) hide show
  1. package/dist/g6.min.js +29 -29
  2. package/dist/lib/animations/types.d.ts +7 -1
  3. package/dist/lib/elements/base-element.d.ts +3 -2
  4. package/dist/lib/elements/combos/base-combo.d.ts +0 -2
  5. package/dist/lib/elements/edges/base-edge.d.ts +3 -3
  6. package/dist/lib/elements/nodes/base-node.d.ts +3 -3
  7. package/dist/lib/elements/shapes/badge.d.ts +1 -1
  8. package/dist/lib/elements/shapes/base-shape.d.ts +1 -1
  9. package/dist/lib/exports.d.ts +3 -2
  10. package/dist/lib/layouts/fishbone.d.ts +72 -0
  11. package/dist/lib/layouts/index.d.ts +2 -0
  12. package/dist/lib/plugins/legend.d.ts +22 -14
  13. package/dist/lib/plugins/minimap/index.d.ts +2 -4
  14. package/dist/lib/plugins/timebar.d.ts +1 -2
  15. package/dist/lib/plugins/utils/canvas.d.ts +31 -0
  16. package/dist/lib/plugins/utils/dom.d.ts +22 -0
  17. package/dist/lib/runtime/data.d.ts +28 -0
  18. package/dist/lib/runtime/element.d.ts +0 -9
  19. package/dist/lib/types/element.d.ts +0 -8
  20. package/dist/lib/utils/dom.d.ts +0 -26
  21. package/dist/lib/utils/style.d.ts +1 -1
  22. package/dist/lib/utils/z-index.d.ts +8 -0
  23. package/dist/lib/version.d.ts +1 -1
  24. package/esm/animations/types.d.ts +7 -1
  25. package/esm/elements/base-element.d.ts +3 -2
  26. package/esm/elements/base-element.js +2 -1
  27. package/esm/elements/base-element.js.map +1 -1
  28. package/esm/elements/combos/base-combo.d.ts +0 -2
  29. package/esm/elements/combos/base-combo.js +7 -13
  30. package/esm/elements/combos/base-combo.js.map +1 -1
  31. package/esm/elements/edges/base-edge.d.ts +3 -3
  32. package/esm/elements/edges/base-edge.js +4 -4
  33. package/esm/elements/edges/base-edge.js.map +1 -1
  34. package/esm/elements/nodes/base-node.d.ts +3 -3
  35. package/esm/elements/nodes/base-node.js.map +1 -1
  36. package/esm/elements/shapes/badge.d.ts +1 -1
  37. package/esm/elements/shapes/base-shape.d.ts +1 -1
  38. package/esm/elements/shapes/base-shape.js.map +1 -1
  39. package/esm/exports.d.ts +3 -2
  40. package/esm/exports.js +1 -1
  41. package/esm/exports.js.map +1 -1
  42. package/esm/layouts/fishbone.d.ts +72 -0
  43. package/esm/layouts/fishbone.js +209 -0
  44. package/esm/layouts/fishbone.js.map +1 -0
  45. package/esm/layouts/index.d.ts +2 -0
  46. package/esm/layouts/index.js +1 -0
  47. package/esm/layouts/index.js.map +1 -1
  48. package/esm/plugins/background/index.js +2 -1
  49. package/esm/plugins/background/index.js.map +1 -1
  50. package/esm/plugins/contextmenu/index.js +1 -1
  51. package/esm/plugins/contextmenu/index.js.map +1 -1
  52. package/esm/plugins/edge-filter-lens/index.js +1 -0
  53. package/esm/plugins/edge-filter-lens/index.js.map +1 -1
  54. package/esm/plugins/grid-line.js +5 -3
  55. package/esm/plugins/grid-line.js.map +1 -1
  56. package/esm/plugins/legend.d.ts +22 -14
  57. package/esm/plugins/legend.js +39 -51
  58. package/esm/plugins/legend.js.map +1 -1
  59. package/esm/plugins/minimap/index.d.ts +2 -4
  60. package/esm/plugins/minimap/index.js +13 -30
  61. package/esm/plugins/minimap/index.js.map +1 -1
  62. package/esm/plugins/timebar.d.ts +1 -2
  63. package/esm/plugins/timebar.js +15 -47
  64. package/esm/plugins/timebar.js.map +1 -1
  65. package/esm/plugins/toolbar/index.js +1 -1
  66. package/esm/plugins/toolbar/index.js.map +1 -1
  67. package/esm/plugins/utils/canvas.d.ts +31 -0
  68. package/esm/plugins/utils/canvas.js +55 -0
  69. package/esm/plugins/utils/canvas.js.map +1 -0
  70. package/esm/plugins/utils/dom.d.ts +22 -0
  71. package/esm/plugins/utils/dom.js +52 -0
  72. package/esm/plugins/utils/dom.js.map +1 -0
  73. package/esm/plugins/watermark/index.js +1 -1
  74. package/esm/plugins/watermark/index.js.map +1 -1
  75. package/esm/preset.js +2 -0
  76. package/esm/preset.js.map +1 -1
  77. package/esm/registry/build-in.js +3 -2
  78. package/esm/registry/build-in.js.map +1 -1
  79. package/esm/runtime/animation.js +5 -2
  80. package/esm/runtime/animation.js.map +1 -1
  81. package/esm/runtime/data.d.ts +28 -0
  82. package/esm/runtime/data.js +131 -16
  83. package/esm/runtime/data.js.map +1 -1
  84. package/esm/runtime/element.d.ts +0 -9
  85. package/esm/runtime/element.js +7 -52
  86. package/esm/runtime/element.js.map +1 -1
  87. package/esm/runtime/graph.js +8 -9
  88. package/esm/runtime/graph.js.map +1 -1
  89. package/esm/types/element.d.ts +0 -8
  90. package/esm/utils/dom.d.ts +0 -26
  91. package/esm/utils/dom.js +0 -65
  92. package/esm/utils/dom.js.map +1 -1
  93. package/esm/utils/style.d.ts +1 -1
  94. package/esm/utils/style.js +2 -2
  95. package/esm/utils/style.js.map +1 -1
  96. package/esm/utils/z-index.d.ts +8 -0
  97. package/esm/utils/z-index.js +11 -0
  98. package/esm/utils/z-index.js.map +1 -0
  99. package/esm/version.d.ts +1 -1
  100. package/esm/version.js +1 -1
  101. package/lib/animations/types.d.ts +7 -1
  102. package/lib/elements/base-element.d.ts +3 -2
  103. package/lib/elements/base-element.js +2 -1
  104. package/lib/elements/base-element.js.map +1 -1
  105. package/lib/elements/combos/base-combo.d.ts +0 -2
  106. package/lib/elements/combos/base-combo.js +7 -13
  107. package/lib/elements/combos/base-combo.js.map +1 -1
  108. package/lib/elements/edges/base-edge.d.ts +3 -3
  109. package/lib/elements/edges/base-edge.js +4 -4
  110. package/lib/elements/edges/base-edge.js.map +1 -1
  111. package/lib/elements/nodes/base-node.d.ts +3 -3
  112. package/lib/elements/nodes/base-node.js.map +1 -1
  113. package/lib/elements/shapes/badge.d.ts +1 -1
  114. package/lib/elements/shapes/base-shape.d.ts +1 -1
  115. package/lib/elements/shapes/base-shape.js.map +1 -1
  116. package/lib/exports.d.ts +3 -2
  117. package/lib/exports.js +3 -2
  118. package/lib/exports.js.map +1 -1
  119. package/lib/layouts/fishbone.d.ts +72 -0
  120. package/lib/layouts/fishbone.js +213 -0
  121. package/lib/layouts/fishbone.js.map +1 -0
  122. package/lib/layouts/index.d.ts +2 -0
  123. package/lib/layouts/index.js +3 -1
  124. package/lib/layouts/index.js.map +1 -1
  125. package/lib/plugins/background/index.js +2 -1
  126. package/lib/plugins/background/index.js.map +1 -1
  127. package/lib/plugins/contextmenu/index.js +1 -1
  128. package/lib/plugins/contextmenu/index.js.map +1 -1
  129. package/lib/plugins/edge-filter-lens/index.js +1 -0
  130. package/lib/plugins/edge-filter-lens/index.js.map +1 -1
  131. package/lib/plugins/grid-line.js +5 -3
  132. package/lib/plugins/grid-line.js.map +1 -1
  133. package/lib/plugins/legend.d.ts +22 -14
  134. package/lib/plugins/legend.js +38 -50
  135. package/lib/plugins/legend.js.map +1 -1
  136. package/lib/plugins/minimap/index.d.ts +2 -4
  137. package/lib/plugins/minimap/index.js +13 -30
  138. package/lib/plugins/minimap/index.js.map +1 -1
  139. package/lib/plugins/timebar.d.ts +1 -2
  140. package/lib/plugins/timebar.js +15 -47
  141. package/lib/plugins/timebar.js.map +1 -1
  142. package/lib/plugins/toolbar/index.js +1 -1
  143. package/lib/plugins/toolbar/index.js.map +1 -1
  144. package/lib/plugins/utils/canvas.d.ts +31 -0
  145. package/lib/plugins/utils/canvas.js +58 -0
  146. package/lib/plugins/utils/canvas.js.map +1 -0
  147. package/lib/plugins/utils/dom.d.ts +22 -0
  148. package/lib/plugins/utils/dom.js +56 -0
  149. package/lib/plugins/utils/dom.js.map +1 -0
  150. package/lib/plugins/watermark/index.js +1 -1
  151. package/lib/plugins/watermark/index.js.map +1 -1
  152. package/lib/preset.js +2 -0
  153. package/lib/preset.js.map +1 -1
  154. package/lib/registry/build-in.js +2 -1
  155. package/lib/registry/build-in.js.map +1 -1
  156. package/lib/runtime/animation.js +5 -2
  157. package/lib/runtime/animation.js.map +1 -1
  158. package/lib/runtime/data.d.ts +28 -0
  159. package/lib/runtime/data.js +130 -15
  160. package/lib/runtime/data.js.map +1 -1
  161. package/lib/runtime/element.d.ts +0 -9
  162. package/lib/runtime/element.js +7 -52
  163. package/lib/runtime/element.js.map +1 -1
  164. package/lib/runtime/graph.js +8 -9
  165. package/lib/runtime/graph.js.map +1 -1
  166. package/lib/types/element.d.ts +0 -8
  167. package/lib/utils/dom.d.ts +0 -26
  168. package/lib/utils/dom.js +0 -68
  169. package/lib/utils/dom.js.map +1 -1
  170. package/lib/utils/style.d.ts +1 -1
  171. package/lib/utils/style.js +2 -2
  172. package/lib/utils/style.js.map +1 -1
  173. package/lib/utils/z-index.d.ts +8 -0
  174. package/lib/utils/z-index.js +14 -0
  175. package/lib/utils/z-index.js.map +1 -0
  176. package/lib/version.d.ts +1 -1
  177. package/lib/version.js +1 -1
  178. package/package.json +6 -6
  179. package/src/animations/types.ts +7 -1
  180. package/src/elements/base-element.ts +5 -3
  181. package/src/elements/combos/base-combo.ts +7 -14
  182. package/src/elements/edges/base-edge.ts +6 -7
  183. package/src/elements/nodes/base-node.ts +2 -3
  184. package/src/elements/shapes/base-shape.ts +1 -4
  185. package/src/exports.ts +2 -1
  186. package/src/global.d.ts +5 -0
  187. package/src/layouts/fishbone.ts +295 -0
  188. package/src/layouts/index.ts +2 -0
  189. package/src/plugins/background/index.ts +2 -1
  190. package/src/plugins/contextmenu/index.ts +1 -1
  191. package/src/plugins/edge-filter-lens/index.ts +1 -0
  192. package/src/plugins/grid-line.ts +5 -3
  193. package/src/plugins/legend.ts +67 -55
  194. package/src/plugins/minimap/index.ts +16 -52
  195. package/src/plugins/timebar.ts +16 -47
  196. package/src/plugins/toolbar/index.ts +1 -1
  197. package/src/plugins/utils/canvas.ts +89 -0
  198. package/src/plugins/utils/dom.ts +65 -0
  199. package/src/plugins/watermark/index.ts +1 -1
  200. package/src/preset.ts +3 -0
  201. package/src/registry/build-in.ts +3 -1
  202. package/src/runtime/animation.ts +5 -2
  203. package/src/runtime/data.ts +145 -16
  204. package/src/runtime/element.ts +4 -59
  205. package/src/runtime/graph.ts +8 -8
  206. package/src/types/element.ts +0 -9
  207. package/src/utils/dom.ts +0 -80
  208. package/src/utils/style.ts +3 -6
  209. package/src/utils/z-index.ts +11 -0
  210. package/src/version.ts +1 -1
package/src/exports.ts CHANGED
@@ -63,6 +63,7 @@ export {
63
63
  D3ForceLayout,
64
64
  DagreLayout,
65
65
  dendrogram as DendrogramLayout,
66
+ FishboneLayout,
66
67
  ForceAtlas2Layout,
67
68
  ForceLayout,
68
69
  FruchtermanLayout,
@@ -180,6 +181,7 @@ export type {
180
181
  } from './elements/shapes';
181
182
  export type { UpsertHooks } from './elements/shapes/base-shape';
182
183
  export type { ContourLabelStyleProps, ContourStyleProps } from './elements/shapes/contour';
184
+ export type { FishboneLayoutOptions } from './layouts';
183
185
  export type { BaseLayoutOptions, WebWorkerLayoutOptions } from './layouts/types';
184
186
  export type { CategoricalPalette } from './palettes/types';
185
187
  export type {
@@ -232,7 +234,6 @@ export type {
232
234
  } from './transforms';
233
235
  export type { DrawData } from './transforms/types';
234
236
  export type {
235
- BaseElementStyleProps,
236
237
  CardinalPlacement,
237
238
  CollapsedMarkerStyleProps,
238
239
  Combo,
package/src/global.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import '@antv/g';
2
+ import type { RuntimeContext } from './runtime/types';
2
3
 
3
4
  declare module '@antv/g' {
4
5
  interface BaseStyleProps {
@@ -9,4 +10,8 @@ declare module '@antv/g' {
9
10
  */
10
11
  $layer?: string;
11
12
  }
13
+
14
+ interface DisplayObjectConfig {
15
+ context?: RuntimeContext;
16
+ }
12
17
  }
@@ -0,0 +1,295 @@
1
+ import { isEmpty, memoize } from '@antv/util';
2
+ import { idOf } from '../exports';
3
+ import type { EdgeData, GraphData, NodeData } from '../spec';
4
+ import type { ElementDatum, ID, Point, Size, STDSize } from '../types';
5
+ import { parseSize } from '../utils/size';
6
+ import { BaseLayout } from './base-layout';
7
+
8
+ export interface FishboneLayoutOptions {
9
+ /**
10
+ * <zh/> 节点大小
11
+ *
12
+ * <en/> Node size
13
+ */
14
+ nodeSize?: Size | ((node: NodeData) => Size);
15
+ /**
16
+ * <zh/> 排布方向
17
+ * - `'RL'` 从右到左,鱼头在右
18
+ * - `'LR'` 从左到右,鱼头在左
19
+ *
20
+ * <en/> Layout direction
21
+ * - `'RL'` From right to left, the fish head is on the right
22
+ * - `'LR'` From left to right, the fish head is on the left
23
+ * @defaultValue `'LR'`
24
+ */
25
+ direction?: 'RL' | 'LR';
26
+ /**
27
+ * <zh/> 获取水平间距
28
+ *
29
+ * <en/> Get horizontal spacing
30
+ */
31
+ hGap?: number;
32
+ /**
33
+ * <zh/> 获取垂直间距
34
+ *
35
+ * <en/> Get vertical spacing
36
+ */
37
+ vGap?: number;
38
+ /**
39
+ * <zh/> 获取鱼骨间距
40
+ *
41
+ * <en/> Get rib separation
42
+ * @defaultValue () => 60
43
+ */
44
+ getRibSep?: (node: NodeData) => number;
45
+ /**
46
+ * <zh/> 布局宽度
47
+ *
48
+ * <en/> Layout width
49
+ */
50
+ width?: number;
51
+ /**
52
+ * <zh/> 布局高度
53
+ *
54
+ * <en/> Layout height
55
+ */
56
+ height?: number;
57
+ }
58
+
59
+ type NodeResult = { id: ID; x: number; y: number };
60
+ type EdgeResult = { id: ID; controlPoints: Point[]; relatedNodeId: ID };
61
+ type LayoutResult = { nodes: NodeResult[]; edges: EdgeResult[] };
62
+
63
+ /**
64
+ * <zh/> 鱼骨图布局
65
+ *
66
+ * <en/> Fishbone layout
67
+ * @remarks
68
+ * <zh/> [鱼骨图布局](https://en.wikipedia.org/wiki/Ishikawa_diagram)是一种专门用于表示层次结构数据的图形布局方式。它通过模拟鱼骨的形状,将数据节点按照层次结构排列,使得数据的层次关系更加清晰直观。鱼骨图布局特别适用于需要展示因果关系、层次结构或分类信息的数据集。
69
+ *
70
+ * <en/> [Fishbone layout](https://en.wikipedia.org/wiki/Ishikawa_diagram) is a graphical layout method specifically designed to represent hierarchical data. By simulating the shape of a fishbone, it arranges data nodes according to their hierarchical structure, making the hierarchical relationships of the data clearer and more intuitive. The fishbone diagram layout is particularly suitable for datasets that need to display causal relationships, hierarchical structures, or classification information.
71
+ */
72
+ export class FishboneLayout extends BaseLayout {
73
+ id = 'fishbone';
74
+
75
+ static defaultOptions: Partial<FishboneLayoutOptions> = {
76
+ direction: 'RL',
77
+ getRibSep: () => 60,
78
+ };
79
+
80
+ private getRoot() {
81
+ const roots = this.context.model.getRootsData();
82
+ if (isEmpty(roots) || roots.length > 2) return;
83
+
84
+ return roots[0];
85
+ }
86
+
87
+ private formatSize(nodeSize: Size | ((node: NodeData) => Size)): (node: NodeData) => STDSize {
88
+ const nodeSizeFunc = typeof nodeSize === 'function' ? nodeSize : () => nodeSize;
89
+ return (node: NodeData) => parseSize(nodeSizeFunc(node));
90
+ }
91
+
92
+ private doLayout(root: NodeData, options: Required<FishboneLayoutOptions>): LayoutResult {
93
+ const { hGap, getRibSep, vGap, nodeSize, height } = options;
94
+
95
+ const { model } = this.context;
96
+
97
+ const getSize = this.formatSize(nodeSize);
98
+ let ribX = getSize(root)[0] + getRibSep(root);
99
+
100
+ const getHorizontalOffset = (node: NodeData, result = 0): number => {
101
+ result += hGap * ((node.children || []).length + 1);
102
+
103
+ node.children?.forEach((childId) => {
104
+ const child = model.getNodeLikeDatum(childId) as NodeData;
105
+ child.children?.forEach((grandChildId) => {
106
+ const grandChild = model.getNodeLikeDatum(grandChildId) as NodeData;
107
+ result = getHorizontalOffset(grandChild, result);
108
+ });
109
+ });
110
+
111
+ return result;
112
+ };
113
+
114
+ const getAuxiliaryPoint = (node: NodeData): number => {
115
+ if (node.depth === 1) return ribX;
116
+
117
+ const parent = model.getParentData(node.id, 'tree') as NodeData;
118
+
119
+ if (isAtEvenDepth(node)) {
120
+ const ancestor = model.getParentData(parent.id, 'tree') as NodeData;
121
+ const deltaY = calculateY(node) - calculateY(ancestor);
122
+ return getAuxiliaryPoint(parent) + (deltaY * hGap) / vGap;
123
+ } else {
124
+ const nodeIndex = (parent.children || []).indexOf(node.id);
125
+ const followingSiblingsIncludeSelf = model.getNodeData((parent.children || []).slice(nodeIndex));
126
+ return (
127
+ calculateX(parent) -
128
+ followingSiblingsIncludeSelf.reduce((acc, sibling) => acc + getHorizontalOffset(sibling), 0) -
129
+ getSize(parent)[0] / 2
130
+ );
131
+ }
132
+ };
133
+
134
+ const calculateX = memoize(
135
+ (node: NodeData): number => {
136
+ if (isRoot(node)) return getSize(node)[0] / 2;
137
+
138
+ const parent = model.getParentData(node.id, 'tree') as NodeData;
139
+
140
+ if (isAtEvenDepth(node)) {
141
+ return getAuxiliaryPoint(node) + getHorizontalOffset(node) + getSize(node)[0] / 2;
142
+ } else {
143
+ const deltaY = calculateY(node) - calculateY(parent);
144
+ const ratio = hGap / vGap;
145
+ return getAuxiliaryPoint(node) + deltaY * ratio;
146
+ }
147
+ },
148
+ (node) => node.id,
149
+ );
150
+
151
+ const getParentY = (nodeId: ID): number => calculateY(model.getParentData(nodeId, 'tree')!);
152
+
153
+ const calculateY = memoize(
154
+ (node: NodeData): number => {
155
+ if (isRoot(node)) return height / 2;
156
+
157
+ if (!isAtEvenDepth(node)) {
158
+ // If the node has no children, calculate Y based on the parent
159
+ if (isEmpty(node.children)) return getParentY(node.id) + vGap;
160
+
161
+ // If the last child has no children, calculate Y based on the last child
162
+ const lastChild = model.getNodeLikeDatum(node.children!.slice(-1)[0]);
163
+ if (isEmpty(lastChild.children)) return calculateY(lastChild) + vGap;
164
+
165
+ // If the last child has children, calculate Y based on the last descendant of the last child
166
+ const lastDescendant = model.getDescendantsData(node.id).slice(-1)[0];
167
+ return (isAtEvenDepth(lastDescendant) ? getParentY(lastDescendant.id) : calculateY(lastDescendant)) + vGap;
168
+ } else {
169
+ // depth > 0 && isAtEvenDepth(node)
170
+ const parent = model.getParentData(node.id, 'tree') as NodeData;
171
+ const nodeIndex = parent.children!.indexOf(node.id);
172
+ // If the node is the first sibling, return Y based on parent
173
+ if (nodeIndex === 0) return getParentY(parent.id) + vGap;
174
+
175
+ // If the previous sibling has no children, calculate Y based on the previous sibling
176
+ const prevSibling = model.getNodeLikeDatum(parent.children![nodeIndex - 1]);
177
+ if (isEmpty(prevSibling.children)) return calculateY(prevSibling) + vGap;
178
+
179
+ // If the previous sibling has children, calculate Y based on the last descendant of the previous sibling
180
+ const descendants = model.getDescendantsData(prevSibling.id);
181
+ return (
182
+ Math.max(
183
+ ...descendants.map((descendant) =>
184
+ isAtEvenDepth(descendant) ? getParentY(descendant.id) : calculateY(descendant),
185
+ ),
186
+ ) + vGap
187
+ );
188
+ }
189
+ },
190
+ (node) => node.id,
191
+ );
192
+
193
+ let tmpRibX = 0;
194
+ const result: LayoutResult = { nodes: [], edges: [] };
195
+
196
+ const layout = (node: NodeData) => {
197
+ node.children?.forEach((childId) => layout(model.getNodeLikeDatum(childId)));
198
+
199
+ const y = calculateY(node);
200
+ const x = calculateX(node);
201
+ result.nodes.push({ id: node.id, x, y });
202
+
203
+ if (isRoot(node)) return;
204
+
205
+ const edge = model.getRelatedEdgesData(node.id, 'in')[0];
206
+ const controlPoint = [getAuxiliaryPoint(node), isAtEvenDepth(node) ? y : getParentY(node.id)] as Point;
207
+ result.edges.push({ id: idOf(edge), controlPoints: [controlPoint], relatedNodeId: node.id });
208
+
209
+ tmpRibX = Math.max(tmpRibX, x + getRibSep(node));
210
+ if (node.depth === 1) ribX = tmpRibX;
211
+ };
212
+
213
+ layout(root);
214
+
215
+ return result;
216
+ }
217
+
218
+ private placeAlterative(result: LayoutResult, root: NodeData) {
219
+ const oddIndexedRibs = (root.children || []).filter((_, index) => index % 2 !== 0);
220
+ if (oddIndexedRibs.length === 0) return result;
221
+
222
+ const { model } = this.context;
223
+ const rootY = result.nodes.find((node) => node.id === root.id)!.y;
224
+
225
+ const shouldFlip = (nodeId: ID) => {
226
+ const ancestors = model.getAncestorsData(nodeId, 'tree');
227
+ if (isEmpty(ancestors)) return false;
228
+ const ribId = ancestors.length === 1 ? nodeId : ancestors[ancestors.length - 2].id;
229
+ return oddIndexedRibs.includes(ribId);
230
+ };
231
+
232
+ result.nodes.forEach((node) => {
233
+ if (shouldFlip(node.id)) node.y = 2 * rootY - node.y;
234
+ });
235
+ result.edges.forEach((edge) => {
236
+ if (shouldFlip(edge.relatedNodeId)) {
237
+ edge.controlPoints = edge.controlPoints.map((point) => [point[0], 2 * rootY - point[1]]);
238
+ }
239
+ });
240
+ }
241
+
242
+ private rightToLeft(result: LayoutResult, options: Required<FishboneLayoutOptions>) {
243
+ result.nodes.forEach((node) => (node.x = options.width! - node.x));
244
+ result.edges.forEach((edge) => {
245
+ edge.controlPoints = edge.controlPoints.map((point) => [options.width! - point[0], point[1]]);
246
+ });
247
+ return result;
248
+ }
249
+
250
+ async execute(data: GraphData, propOptions: FishboneLayoutOptions): Promise<GraphData> {
251
+ const options = { ...FishboneLayout.defaultOptions, ...this.options, ...propOptions };
252
+ const { direction, nodeSize } = options;
253
+
254
+ const root = this.getRoot();
255
+ if (!root) return data;
256
+
257
+ const getSize = this.formatSize(nodeSize);
258
+ options.vGap ||= Math.max(...(data.nodes || []).map((node) => getSize(node)[1]));
259
+ options.hGap ||= Math.max(...(data.nodes || []).map((node) => getSize(node)[0]));
260
+
261
+ let result = this.doLayout(root, options);
262
+
263
+ this.placeAlterative(result, root);
264
+
265
+ if (direction === 'RL') {
266
+ result = this.rightToLeft(result, options);
267
+ }
268
+
269
+ const { model } = this.context;
270
+ const nodes: NodeData[] = [];
271
+ const edges: EdgeData[] = [];
272
+
273
+ result.nodes.forEach((node) => {
274
+ const { id, x, y } = node;
275
+ const nodeData = model.getNodeLikeDatum(id);
276
+ nodes.push(assignElementStyle(nodeData, { x, y }) as NodeData);
277
+ });
278
+
279
+ result.edges.forEach((edge) => {
280
+ const { id, controlPoints } = edge;
281
+ const edgeData = model.getEdgeDatum(id);
282
+ edges.push(assignElementStyle(edgeData, { controlPoints }) as EdgeData);
283
+ });
284
+
285
+ return { nodes, edges };
286
+ }
287
+ }
288
+
289
+ const assignElementStyle = (element: ElementDatum, style: Record<string, unknown>) => {
290
+ return { ...element, style: { ...(element.style || {}), ...style } };
291
+ };
292
+
293
+ const isRoot = (node: NodeData) => node.depth === 0;
294
+
295
+ const isAtEvenDepth = (node: NodeData) => (node.depth ||= 0) % 2 === 0;
@@ -15,3 +15,5 @@ export {
15
15
  RandomLayout,
16
16
  } from '@antv/layout';
17
17
  export { BaseLayout } from './base-layout';
18
+ export { FishboneLayout } from './fishbone';
19
+ export type { FishboneLayoutOptions } from './fishbone';
@@ -1,8 +1,8 @@
1
1
  import { omit } from '@antv/util';
2
2
  import type { RuntimeContext } from '../../runtime/types';
3
- import { createPluginContainer } from '../../utils/dom';
4
3
  import type { BasePluginOptions } from '../base-plugin';
5
4
  import { BasePlugin } from '../base-plugin';
5
+ import { createPluginContainer } from '../utils/dom';
6
6
 
7
7
  /**
8
8
  * <zh/> 背景配置项
@@ -24,6 +24,7 @@ export class Background extends BasePlugin<BackgroundOptions> {
24
24
  static defaultOptions: Partial<BackgroundOptions> = {
25
25
  transition: 'background 0.5s',
26
26
  backgroundSize: 'cover',
27
+ zIndex: '-1',
27
28
  };
28
29
 
29
30
  private $element: HTMLElement = createPluginContainer('background');
@@ -1,9 +1,9 @@
1
1
  import type { RuntimeContext } from '../../runtime/types';
2
2
  import type { Element } from '../../types';
3
3
  import type { IElementEvent } from '../../types/event';
4
- import { createPluginContainer, insertDOM } from '../../utils/dom';
5
4
  import type { BasePluginOptions } from '../base-plugin';
6
5
  import { BasePlugin } from '../base-plugin';
6
+ import { createPluginContainer, insertDOM } from '../utils/dom';
7
7
  import type { Item } from './util';
8
8
  import { CONTEXTMENU_CSS, getContentFromItems } from './util';
9
9
  /**
@@ -128,6 +128,7 @@ const defaultLensStyle: BaseStyleProps = {
128
128
  lineWidth: 1,
129
129
  stroke: '#000',
130
130
  strokeOpacity: 0.8,
131
+ zIndex: -Infinity,
131
132
  };
132
133
 
133
134
  const DELTA = 0.05;
@@ -1,10 +1,10 @@
1
1
  import { GraphEvent } from '../constants';
2
2
  import type { RuntimeContext } from '../runtime/types';
3
3
  import type { Point } from '../types';
4
- import { createPluginContainer, insertBefore } from '../utils/dom';
5
4
  import { ViewportEvent } from '../utils/event';
6
5
  import { add, mod } from '../utils/vector';
7
6
  import { BasePlugin, BasePluginOptions } from './base-plugin';
7
+ import { createPluginContainer } from './utils/dom';
8
8
 
9
9
  /**
10
10
  * <zh/> 网格线配置项
@@ -98,7 +98,9 @@ export class GridLine extends BasePlugin<GridLineOptions> {
98
98
  stroke: '#eee',
99
99
  };
100
100
 
101
- private $element: HTMLElement = createPluginContainer('grid-line');
101
+ private $element: HTMLElement = createPluginContainer('grid-line', true, {
102
+ zIndex: '-1',
103
+ });
102
104
 
103
105
  private offset: Point = [0, 0];
104
106
 
@@ -106,7 +108,7 @@ export class GridLine extends BasePlugin<GridLineOptions> {
106
108
  super(context, Object.assign({}, GridLine.defaultOptions, options));
107
109
 
108
110
  const $container = this.context.canvas.getContainer()!;
109
- insertBefore($container, this.$element);
111
+ $container.prepend(this.$element);
110
112
 
111
113
  this.updateStyle();
112
114
  this.bindEvents();
@@ -1,5 +1,6 @@
1
- import { Category, Layout, Selection } from '@antv/component';
1
+ import { Category, Selection } from '@antv/component';
2
2
  import { CategoryStyleProps } from '@antv/component/lib/ui/legend/types';
3
+ import { Canvas } from '@antv/g';
3
4
  import { get, isFunction } from '@antv/util';
4
5
  import { GraphEvent } from '../constants';
5
6
  import type { RuntimeContext } from '../runtime/types';
@@ -7,6 +8,7 @@ import type { ElementDatum, ElementType, ID, State } from '../types';
7
8
  import type { CardinalPlacement } from '../types/placement';
8
9
  import type { BasePluginOptions } from './base-plugin';
9
10
  import { BasePlugin } from './base-plugin';
11
+ import { createPluginCanvas } from './utils/canvas';
10
12
 
11
13
  interface Datum extends Record<string, any> {
12
14
  id?: string;
@@ -40,6 +42,24 @@ export interface LegendOptions extends BasePluginOptions, Omit<CategoryStyleProp
40
42
  * @defaultValue 'bottom'
41
43
  */
42
44
  position?: CardinalPlacement;
45
+ /**
46
+ * <zh/> 图例挂载的容器,无则挂载到 Graph 所在容器
47
+ *
48
+ * <en/> The container where the legend is mounted, if not, it will be mounted to the container where the Graph is located
49
+ */
50
+ container?: HTMLElement | string;
51
+ /**
52
+ * <zh/> 图例画布类名,传入外置容器时不生效
53
+ *
54
+ * <en/> The class name of the legend canvas, which does not take effect when an external container is passed in
55
+ */
56
+ className?: string;
57
+ /**
58
+ * <zh/> 图例的容器样式,传入外置容器时不生效
59
+ *
60
+ * <en/> The style of the legend container, which does not take effect when an external container is passed in
61
+ */
62
+ containerStyle?: Partial<CSSStyleDeclaration>;
43
63
  /**
44
64
  * <zh/> 节点分类标识
45
65
  *
@@ -80,9 +100,10 @@ export class Legend extends BasePlugin<LegendOptions> {
80
100
  colPadding: 10,
81
101
  itemMarkerSize: 16,
82
102
  itemLabelFontSize: 16,
103
+ width: 240,
104
+ height: 160,
83
105
  };
84
106
  private typePrefix = '__data__';
85
- private element: Layout | null = null;
86
107
  private draw = false;
87
108
  private fieldMap = {
88
109
  node: new Map<string, ID[]>(),
@@ -90,6 +111,9 @@ export class Legend extends BasePlugin<LegendOptions> {
90
111
  combo: new Map<string, ID[]>(),
91
112
  };
92
113
  private selectedItems: string[] = [];
114
+ private category?: Category;
115
+ private container?: HTMLElement;
116
+ private canvas?: Canvas;
93
117
 
94
118
  constructor(context: RuntimeContext, options: LegendOptions) {
95
119
  super(context, Object.assign({}, Legend.defaultOptions, options));
@@ -110,8 +134,11 @@ export class Legend extends BasePlugin<LegendOptions> {
110
134
  }
111
135
 
112
136
  private clear() {
113
- this.element?.destroy();
114
- this.element = null;
137
+ this.canvas?.destroy();
138
+ this.container?.remove();
139
+ this.canvas = undefined;
140
+ this.container = undefined;
141
+
115
142
  this.draw = false;
116
143
  }
117
144
 
@@ -184,10 +211,9 @@ export class Legend extends BasePlugin<LegendOptions> {
184
211
  * <en/> Refresh the status of the legend element
185
212
  */
186
213
  public updateElement() {
187
- if (!this.element) return;
188
- const category = this.element.getChildByIndex(0) as Category;
214
+ if (!this.category) return;
189
215
 
190
- category.update({
216
+ this.category.update({
191
217
  itemMarkerOpacity: ({ id }) => {
192
218
  if (!this.selectedItems.length || this.selectedItems.includes(id)) return 1;
193
219
  return 0.5;
@@ -224,7 +250,7 @@ export class Legend extends BasePlugin<LegendOptions> {
224
250
 
225
251
  private getMarkerData = (field: string | ((item: ElementDatum) => string), elementType: ElementType) => {
226
252
  if (!field) return [];
227
- const { model, element, graph } = this.context;
253
+ const { model, element } = this.context;
228
254
  const { nodes, edges, combos } = model.getData();
229
255
  const items: { [key: string]: Datum } = {};
230
256
 
@@ -239,7 +265,8 @@ export class Legend extends BasePlugin<LegendOptions> {
239
265
  combo: 'rect',
240
266
  };
241
267
 
242
- /** 用于将 G6 element 转换为 componets 支持的类型 */
268
+ // 用于将 G6 element 转换为 components 支持的类型
269
+ // Used to convert G6 element to types supported by components
243
270
  const markerMapping: { [key: string]: string } = {
244
271
  circle: 'circle',
245
272
  ellipse: 'circle', // 待 components 支持 ellipse
@@ -303,69 +330,52 @@ export class Legend extends BasePlugin<LegendOptions> {
303
330
  return Object.values(items);
304
331
  };
305
332
 
306
- /**
307
- * <zh/> 图例布局
308
- *
309
- * <en/> Legend layout
310
- * @param position - <zh/> 图例位置| <en/> Legend position
311
- * @returns <zh/> 图例布局样式| <en/> Legend layout style
312
- */
313
- public layout = (position: CardinalPlacement) => {
314
- const preset = {
315
- flexDirection: 'row',
316
- alignItems: 'flex-end',
317
- justifyContent: 'center',
318
- };
319
- let { flexDirection, alignItems, justifyContent } = preset;
333
+ private upsertCanvas() {
334
+ if (this.canvas) return this.canvas;
320
335
 
321
- const layout = {
322
- top: ['row', 'flex-start', 'center'],
323
- bottom: ['row', 'flex-end', 'center'],
324
- left: ['column', 'flex-start', 'center'],
325
- right: ['column', 'flex-end', 'center'],
326
- };
336
+ const graphCanvas = this.context.canvas;
337
+ const [canvasWidth, canvasHeight] = graphCanvas.getSize();
327
338
 
328
- if (position in layout) {
329
- [flexDirection, alignItems, justifyContent] = layout[position];
330
- }
331
- return {
332
- display: 'flex',
333
- flexDirection,
334
- justifyContent,
335
- alignItems,
336
- };
337
- };
339
+ const { width = canvasWidth, height = canvasHeight, position, container, containerStyle, className } = this.options;
340
+ const [$container, canvas] = createPluginCanvas({
341
+ width,
342
+ height,
343
+ graphCanvas,
344
+ container,
345
+ containerStyle,
346
+ placement: position,
347
+ className: 'legend',
348
+ });
349
+
350
+ this.container = $container;
351
+ if (className) $container.classList.add(className);
352
+ this.canvas = canvas;
353
+
354
+ return this.canvas;
355
+ }
338
356
 
339
357
  private createElement = () => {
340
358
  if (this.draw) {
341
359
  this.updateElement();
342
360
  return;
343
361
  }
344
- const { canvas } = this.context;
345
- const [canvasWidth, canvasHeight] = canvas.getSize();
346
362
  const {
347
- width = canvasWidth,
348
- height = canvasHeight,
363
+ width,
364
+ height,
349
365
  nodeField,
350
366
  edgeField,
351
367
  comboField,
352
368
  trigger,
353
369
  position,
370
+ container,
371
+ containerStyle,
372
+ className,
354
373
  ...rest
355
374
  } = this.options;
356
375
  const nodeItems = this.getMarkerData(nodeField, 'node');
357
376
  const edgeItems = this.getMarkerData(edgeField, 'edge');
358
377
  const comboItems = this.getMarkerData(comboField, 'combo');
359
378
  const items = [...nodeItems, ...comboItems, ...edgeItems];
360
- const layout = this.layout(position);
361
-
362
- const layoutWrapper = new Layout({
363
- style: {
364
- width,
365
- height,
366
- ...layout,
367
- },
368
- });
369
379
 
370
380
  const categoryStyle = Object.assign(
371
381
  {
@@ -386,9 +396,11 @@ export class Legend extends BasePlugin<LegendOptions> {
386
396
  className: 'legend',
387
397
  style: categoryStyle,
388
398
  });
389
- layoutWrapper.appendChild(category);
390
- canvas.appendChild(layoutWrapper as any);
391
- this.element = layoutWrapper;
399
+ this.category = category;
400
+
401
+ const canvas = this.upsertCanvas();
402
+ canvas.appendChild(category);
403
+
392
404
  this.draw = true;
393
405
  };
394
406