Skip to content

Commit 7b48f68

Browse files
authored
feat: updateLayout from no pipes to pipes, closes: #3726; fix: relayout with pipes; (#3840)
1 parent 915297d commit 7b48f68

File tree

6 files changed

+53
-32
lines changed

6 files changed

+53
-32
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# ChangeLog
22

3+
#### 4.6.18
4+
5+
- feat: updateLayout from no pipes to pipes, closes: #3726;
6+
- fix: relayout with pipes;
7+
38
#### 4.6.17
49

510
- fix: legend changeData problem, closes: #3561;

packages/core/src/graph/controller/layout.ts

+12-27
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ export default abstract class LayoutController {
5050

5151
protected isLayoutTypeSame(cfg): boolean {
5252
const current = this.getLayoutCfgType(cfg);
53-
// already has pipes
54-
if (Array.isArray(this.layoutType)) {
55-
return this.layoutType.every((type, index) => type === current[index]);
53+
const preHasPipies = Array.isArray(this.layoutType);
54+
const currentHasPipes = Array.isArray(current);
55+
// already has pipes, and the new one is pipes
56+
if (preHasPipies && currentHasPipes) {
57+
return (this.layoutType as string[]).every((type, index) => type === current[index]);
5658
}
57-
59+
// only one of the pre and current is pipes
60+
if (Array.isArray(current) || Array.isArray(this.layoutType)) {
61+
return false;
62+
}
63+
// both of the pre and current are not pipes
5864
return cfg?.type === this.layoutType;
5965
}
6066

@@ -171,28 +177,7 @@ export default abstract class LayoutController {
171177
} as GraphData;
172178
}
173179

174-
protected reLayoutMethod(layoutMethod, layoutCfg): Promise<void> {
175-
return new Promise((reslove, reject) => {
176-
const { graph } = this;
177-
const layoutType = layoutCfg?.type;
178-
179-
// 每个布局方法都需要注册
180-
layoutCfg.onLayoutEnd = () => {
181-
graph.emit('aftersublayout', { type: layoutType });
182-
reslove();
183-
};
184-
185-
layoutMethod.init(this.data);
186-
if (layoutType === 'force') {
187-
layoutMethod.ticking = false;
188-
layoutMethod.forceSimulation.stop();
189-
}
190-
191-
graph.emit('beforesublayout', { type: layoutType });
192-
layoutMethod.execute();
193-
if (layoutMethod.isCustomLayout && layoutCfg.onLayoutEnd) layoutCfg.onLayoutEnd();
194-
});
195-
}
180+
protected abstract execLayoutMethod(layoutCfg, order): Promise<void>;
196181

197182
// 重新布局
198183
public relayout(reloadData?: boolean) {
@@ -213,7 +198,7 @@ export default abstract class LayoutController {
213198
layoutMethods?.forEach((layoutMethod: any, index: number) => {
214199
const currentCfg = layoutCfg[index] || layoutCfg;
215200
start = start.then(() => {
216-
const relayoutPromise = this.reLayoutMethod(layoutMethod, currentCfg);
201+
const relayoutPromise = this.execLayoutMethod(layoutMethod, currentCfg);
217202
if (index === layoutMethods.length - 1) {
218203
layoutCfg.onAllLayoutEnd?.();
219204
}

packages/core/src/graph/graph.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -2484,10 +2484,11 @@ export default abstract class AbstractGraph extends EventEmitter implements IAbs
24842484
const oriLayoutCfg = { ...this.get('layout') };
24852485
const layoutCfg: any = {};
24862486
Object.assign(layoutCfg, oriLayoutCfg, cfg);
2487+
if (cfg.pipes && !cfg.type) delete layoutCfg.type;
2488+
else if (!cfg.pipes && layoutCfg.type) delete layoutCfg.pipes;
24872489
this.set('layout', layoutCfg);
24882490

24892491
if (!layoutController) return;
2490-
24912492
if (
24922493
layoutController.isLayoutTypeSame(layoutCfg) &&
24932494
layoutCfg.gpuEnabled === oriLayoutCfg.gpuEnabled

packages/pc/src/graph/controller/layout.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default class LayoutController extends AbstractLayout {
106106
}
107107
}
108108

109-
private execLayoutMethod(layoutCfg, order): Promise<void> {
109+
protected execLayoutMethod(layoutCfg, order): Promise<void> {
110110
return new Promise(async (reslove, reject) => {
111111
const { graph } = this;
112112
if (!graph || graph.get('destroyed')) return;

packages/site/docs/manual/middle/elements/nodes/jsx-node.en.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ G6.registerNode(
7979
Using JSX-like syntax to customize a complicated node.
8080

8181
```javascript
82+
// Propose the data for a node as following:
83+
const data = {
84+
nodes: [
85+
{
86+
id: 'node1',
87+
type: 'xml-card', // the custom node's type name
88+
metric: 'CPU usage',
89+
cpuUsage: 80
90+
},
91+
]
92+
}
93+
94+
// def for the drawing of the percentage bar
8295
const percentageBar = ({ width, used, height = 12 }) => `
8396
<rect style={{
8497
marginLeft: 10,
@@ -98,6 +111,7 @@ const percentageBar = ({ width, used, height = 12 }) => `
98111
</rect>
99112
`;
100113

114+
// def for the drawing of the jsx node
101115
const textXML = (cfg) => `
102116
<group>
103117
<rect style={{
@@ -112,7 +126,7 @@ const textXML = (cfg) => `
112126
radius: [0, 0, 6, 6] }}
113127
keyshape="true"
114128
cursor="move">
115-
<text style={{marginLeft: 10 ,fill: "red"}}>${FULL}</text>
129+
<text style={{marginLeft: 10 ,fill: 'red'}}>${FULL}</text>
116130
<text style={{ marginTop: 5, marginLeft: 10, fill: '#333'}}>${cfg.metric}: </text>
117131
<text style={{
118132
marginTop: 1,
@@ -125,6 +139,7 @@ const textXML = (cfg) => `
125139
</group>
126140
`;
127141

142+
// register the custom node to G6
128143
G6.registerNode('test', {
129144
jsx: textXML,
130145
});

packages/site/docs/manual/middle/elements/nodes/jsx-node.zh.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ G6.registerNode(
7979
我们再来看一个稍微复杂的案例。
8080

8181
```javascript
82+
// 假设一个节点数据如下:
83+
const data = {
84+
nodes: [
85+
{
86+
id: 'node1',
87+
type: 'xml-card', // 使用自定义的节点名称
88+
metric: 'CPU usage',
89+
cpuUsage: 80
90+
},
91+
]
92+
}
93+
94+
// 定义进度条的绘制方式
8295
const percentageBar = ({ width, used, height = 12 }) => `
8396
<rect style={{
8497
marginLeft: 10,
@@ -98,6 +111,7 @@ const percentageBar = ({ width, used, height = 12 }) => `
98111
</rect>
99112
`;
100113

114+
// 定义节点的 jsx 绘制方式
101115
const textXML = (cfg) => `
102116
<group>
103117
<rect style={{
@@ -112,7 +126,7 @@ const textXML = (cfg) => `
112126
radius: [0, 0, 6, 6] }}
113127
keyshape="true"
114128
cursor="move">
115-
<text style={{marginLeft: 10 ,fill: "red"}}>${FULL}</text>
129+
<text style={{marginLeft: 10 ,fill: 'red'}}>'FULL'</text>
116130
<text style={{ marginTop: 5, marginLeft: 10, fill: '#333'}}>${cfg.metric}: </text>
117131
<text style={{
118132
marginTop: 1,
@@ -125,7 +139,8 @@ const textXML = (cfg) => `
125139
</group>
126140
`;
127141

128-
G6.registerNode('test', {
142+
// 注册节点
143+
G6.registerNode('xml-card', {
129144
jsx: textXML,
130145
});
131146
```

0 commit comments

Comments
 (0)