Skip to content

Commit 1a52e3d

Browse files
authored
feat: force2 from graphin-force; feat: preset for layout; feat: tweak… (#3853)
* feat: force2 from graphin-force; feat: preset for layout; feat: tweak incremental layout init for force like layouts; * chore: update tests * chore: upgrade version num * chore: onLayoutEnd with nodes param
1 parent 7b48f68 commit 1a52e3d

File tree

16 files changed

+146
-47
lines changed

16 files changed

+146
-47
lines changed

CHANGELOG.md

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

3+
#### 4.7.0-beta
4+
5+
- feat: force2 from graphin-force;
6+
- feat: preset for layout;
7+
- feat: tweak incremental layout init for force like layouts;
8+
39
#### 4.6.18
410

511
- feat: updateLayout from no pipes to pipes, closes: #3726;

packages/core/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antv/g6-core",
3-
"version": "0.6.17",
3+
"version": "0.7.0-beta.2",
44
"description": "A Graph Visualization Framework in JavaScript",
55
"keywords": [
66
"antv",

packages/core/src/global.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const colorSet = {
6464
};
6565

6666
export default {
67-
version: '0.6.17',
67+
version: '0.7.0-beta.1',
6868
rootContainerClassName: 'root-container',
6969
nodeContainerClassName: 'node-container',
7070
edgeContainerClassName: 'edge-container',

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

+16-5
Original file line numberDiff line numberDiff line change
@@ -86,25 +86,30 @@ export default abstract class LayoutController {
8686

8787
// 更换布局
8888
public changeLayout(cfg) {
89-
this.layoutCfg = cfg;
90-
this.layoutType = cfg.type || this.layoutType;
89+
const { disableTriggerLayout, ...otherCfgs } = cfg;
90+
this.layoutCfg = otherCfgs;
91+
this.layoutType = otherCfgs.type || this.layoutType;
9192

92-
this.destoryLayoutMethods();
93+
// 不触发重新布局,仅更新参数
94+
if (disableTriggerLayout) return;
9395
this.layout();
9496
}
9597

9698
// 更换数据
9799
public changeData(success) {
98-
this.destoryLayoutMethods();
99100
this.layout(success);
100101
}
101102

102-
public destoryLayoutMethods() {
103+
public destoryLayoutMethods(): string[] {
103104
const { layoutMethods } = this;
105+
const destroyedLayoutTypes = [];
104106
layoutMethods?.forEach((layoutMethod) => {
107+
const layoutType = layoutMethod.getType?.();
108+
if (layoutType) destroyedLayoutTypes.push(layoutType);
105109
layoutMethod.destroy();
106110
});
107111
this.layoutMethods = [];
112+
return destroyedLayoutTypes;
108113
}
109114

110115
// 销毁布局,不能使用 this.destroy,因为 controller 还需要被使用,只是把布局算法销毁
@@ -292,6 +297,8 @@ export default abstract class LayoutController {
292297
}
293298
}
294299

300+
public abstract initWithPreset(): boolean;
301+
295302
// 初始化节点到 center 附近
296303
public initPositions(center, nodes): boolean {
297304
const { graph } = this;
@@ -301,6 +308,10 @@ export default abstract class LayoutController {
301308
const nodeLength = nodes ? nodes.length : 0;
302309
if (!nodeLength) return;
303310

311+
const hasPreset = this.initWithPreset();
312+
313+
if (hasPreset) return false;
314+
304315
const width = graph.get('width') * 0.85;
305316
const height = graph.get('height') * 0.85;
306317
const horiNum = Math.ceil(Math.sqrt(nodeLength) * (width / height));

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

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { AbstractCanvas, BBox } from '@antv/g-base';
22
import { Point, IGroup } from '@antv/g-base';
33
import { isNumber, isString } from '@antv/util';
44
import { Item, Matrix, Padding, GraphAnimateConfig, IEdge, FitViewRules } from '../../types';
5-
import { formatPadding } from '../../util/base';
5+
import { formatPadding, isNaN } from '../../util/base';
66
import { applyMatrix, invertMatrix, lerpArray } from '../../util/math';
77
import { IAbstractGraph } from '../../interface/graph';
88
import { transform } from '@antv/matrix-util/lib/ext';
@@ -55,6 +55,7 @@ export default class ViewController {
5555
// Translate
5656
const vx = bbox.x + viewCenter.x - groupCenter.x - bbox.minX;
5757
const vy = bbox.y + viewCenter.y - groupCenter.y - bbox.minY;
58+
if (isNaN(vx) || isNaN(vy)) return;
5859
const translatedMatrix = transform(matrix, [['t', vx, vy]]);
5960

6061
// Zoom
@@ -79,10 +80,13 @@ export default class ViewController {
7980
const animationConfig = getAnimateCfgWithCallback({
8081
animateCfg,
8182
callback: () => {
83+
group.setMatrix(zoomedMatrix);
8284
graph.emit('viewportchange', { action: 'translate', matrix: translatedMatrix });
8385
graph.emit('viewportchange', { action: 'zoom', matrix: zoomedMatrix });
8486
}
8587
});
88+
group.stopAnimate();
89+
group.setMatrix(startMatrix);
8690
group.animate((ratio: number) => {
8791
return { matrix: lerpArray(startMatrix, zoomedMatrix, ratio) };
8892
}, animationConfig);
@@ -118,7 +122,10 @@ export default class ViewController {
118122
if (animate) {
119123
this.animatedFitView(group, startMatrix, animateCfg, bbox, viewCenter, groupCenter, ratio);
120124
} else {
121-
graph.translate(viewCenter.x - groupCenter.x, viewCenter.y - groupCenter.y);
125+
const dx = viewCenter.x - groupCenter.x;
126+
const dy = viewCenter.y - groupCenter.y;
127+
if (isNaN(dx) || isNaN(dy)) return;
128+
graph.translate(dx, dy);
122129

123130
if (!graph.zoom(ratio, viewCenter)) {
124131
console.warn('zoom failed, ratio out of range, ratio: %f', ratio);

packages/core/src/graph/graph.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ export default abstract class AbstractGraph extends EventEmitter implements IAbs
24392439
* 若 cfg 含有 type 字段或为 String 类型,且与现有布局方法不同,则更换布局
24402440
* 若 cfg 不包括 type ,则保持原有布局方法,仅更新布局配置项
24412441
*/
2442-
public updateLayout(cfg: any, align?: 'center' | 'begin', alignPoint?: IPoint, stack: boolean = true): void {
2442+
public updateLayout(cfg: any = {}, align?: 'center' | 'begin', alignPoint?: IPoint, stack: boolean = true): void {
24432443
const layoutController = this.get('layoutController');
24442444

24452445
if (isString(cfg)) {
@@ -2458,7 +2458,7 @@ export default abstract class AbstractGraph extends EventEmitter implements IAbs
24582458
// translate to point coordinate system
24592459
toPoint = this.getPointByCanvas(toPoint.x, toPoint.y);
24602460

2461-
const forceTypes = ['force', 'gForce', 'fruchterman'];
2461+
const forceTypes = ['force', 'gForce', 'fruchterman', 'force2'];
24622462

24632463
// if it is force layout, only center takes effect, and assign center force
24642464
if (forceTypes.includes(cfg.type) || (!cfg.type && forceTypes.includes(layoutController?.layoutType))) {

packages/element/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antv/g6-element",
3-
"version": "0.6.17",
3+
"version": "0.7.0-beta.2",
44
"description": "A Graph Visualization Framework in JavaScript",
55
"keywords": [
66
"antv",
@@ -61,7 +61,7 @@
6161
},
6262
"dependencies": {
6363
"@antv/g-base": "^0.5.1",
64-
"@antv/g6-core": "0.6.17",
64+
"@antv/g6-core": "0.7.0-beta.2",
6565
"@antv/util": "~2.0.5"
6666
},
6767
"devDependencies": {

packages/g6/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antv/g6",
3-
"version": "4.6.17",
3+
"version": "4.7.0-beta.2",
44
"description": "A Graph Visualization Framework in JavaScript",
55
"keywords": [
66
"antv",
@@ -66,7 +66,7 @@
6666
]
6767
},
6868
"dependencies": {
69-
"@antv/g6-pc": "0.6.17"
69+
"@antv/g6-pc": "0.7.0-beta.2"
7070
},
7171
"devDependencies": {
7272
"@babel/core": "^7.7.7",

packages/g6/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import G6 from '@antv/g6-pc';
22

3-
G6.version = '4.6.17';
3+
G6.version = '4.7.0-beta.2';
44

55
export * from '@antv/g6-pc';
66
export default G6;
7-
export const version = '4.6.17';
7+
export const version = '4.7.0-beta.2';

packages/pc/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@antv/g6-pc",
3-
"version": "0.6.17",
3+
"version": "0.7.0-beta.2",
44
"description": "A Graph Visualization Framework in JavaScript",
55
"keywords": [
66
"antv",
@@ -75,11 +75,11 @@
7575
"@antv/g-canvas": "^0.5.2",
7676
"@antv/g-math": "^0.1.1",
7777
"@antv/g-svg": "^0.5.1",
78-
"@antv/g6-core": "0.6.17",
79-
"@antv/g6-element": "0.6.17",
80-
"@antv/g6-plugin": "0.6.17",
78+
"@antv/g6-core": "0.7.0-beta.2",
79+
"@antv/g6-element": "0.7.0-beta.2",
80+
"@antv/g6-plugin": "0.7.0-beta.2",
8181
"@antv/hierarchy": "^0.6.7",
82-
"@antv/layout": "^0.2.5",
82+
"@antv/layout": "^0.3.0-beta.1",
8383
"@antv/matrix-util": "^3.1.0-beta.3",
8484
"@antv/path-util": "^2.0.3",
8585
"@antv/util": "~2.0.5",

packages/pc/src/behavior/drag-combo.ts

-2
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ export default {
198198
updatePositions(evt: IG6GraphEvent, restore: boolean) {
199199
// 当启用 delegate 时,拖动结束时需要更新 combo
200200
if (this.enableDelegate || restore) {
201-
console.log('updatePositions', this.targets);
202201
each(this.targets, (item) => {
203202
this.updateCombo(item, evt, restore);
204203
});
@@ -424,7 +423,6 @@ export default {
424423
let x: number = evt.x - origin.x + this.point[itemId].x;
425424
let y: number = evt.y - origin.y + this.point[itemId].y;
426425

427-
console.log('restore', restore);
428426
if (restore) {
429427
x += origin.x - evt.x;
430428
y += origin.y - evt.y;

packages/pc/src/global.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const textColor = 'rgb(0, 0, 0)';
77
const colorSet = getColorsWithSubjectColor(subjectColor, backColor);
88

99
export default {
10-
version: '0.6.17',
10+
version: '0.7.0-beta.2',
1111
rootContainerClassName: 'root-container',
1212
nodeContainerClassName: 'node-container',
1313
edgeContainerClassName: 'edge-container',

0 commit comments

Comments
 (0)