Skip to content

Commit

Permalink
feat: add parameter config support to layout function (#6762)
Browse files Browse the repository at this point in the history
* feat: add parameter config support to layout function

* chore: optimize default options of layout
  • Loading branch information
zhongyunWan authored Feb 10, 2025
1 parent 0c818c0 commit a82674b
Show file tree
Hide file tree
Showing 9 changed files with 2,741 additions and 8 deletions.
538 changes: 538 additions & 0 deletions packages/g6/__tests__/snapshots/runtime/layout/empty.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
538 changes: 538 additions & 0 deletions packages/g6/__tests__/snapshots/runtime/layout/extra.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
538 changes: 538 additions & 0 deletions packages/g6/__tests__/snapshots/runtime/layout/layout-array.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
538 changes: 538 additions & 0 deletions packages/g6/__tests__/snapshots/runtime/layout/other-type.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
538 changes: 538 additions & 0 deletions packages/g6/__tests__/snapshots/runtime/layout/recover.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions packages/g6/__tests__/unit/runtime/layout.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { layoutCircularBasic } from '@@/demos';
import { createDemoGraph } from '@@/utils';

describe('layout options', () => {
it('layoutEmptyOptions', async () => {
const graph = await createDemoGraph(layoutCircularBasic);
await expect(graph).toMatchSnapshot(__filename, 'empty');
graph.destroy();
});

it('layoutExtraOptions', async () => {
const graph = await createDemoGraph(layoutCircularBasic);
await graph.layout({
type: 'circular',
radius: 1000,
});
await expect(graph).toMatchSnapshot(__filename, 'extra');
graph.destroy();
});

it('layoutOtherTypeOptionsAndRecover', async () => {
const graph = await createDemoGraph(layoutCircularBasic);
await graph.layout({
type: 'concentric',
});
await expect(graph).toMatchSnapshot(__filename, 'other-type');
await graph.layout();
await expect(graph).toMatchSnapshot(__filename, 'recover');
graph.destroy();
});

it('layoutArrayOptions', async () => {
const graph = await createDemoGraph(layoutCircularBasic);
await graph.layout(
Array.from({ length: 4 }, () => ({
type: 'circular',
})),
);
await expect(graph).toMatchSnapshot(__filename, 'layout-array');
graph.destroy();
});
});
5 changes: 3 additions & 2 deletions packages/g6/src/runtime/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1173,10 +1173,11 @@ export class Graph extends EventEmitter {
* <zh/> 执行布局
*
* <en/> Execute layout
* @param layoutOptions - <zh/> 布局配置项 | <en/> Layout options
* @apiCategory layout
*/
public async layout() {
await this.context.layout!.postLayout();
public async layout(layoutOptions?: LayoutOptions) {
await this.context.layout!.postLayout(layoutOptions);
}

/**
Expand Down
9 changes: 5 additions & 4 deletions packages/g6/src/runtime/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { COMBO_KEY, GraphEvent, TREE_KEY } from '../constants';
import { BaseLayout } from '../layouts';
import type { AntVLayout } from '../layouts/types';
import { getExtension } from '../registry/get';
import type { GraphData, NodeData } from '../spec';
import type { GraphData, LayoutOptions, NodeData } from '../spec';
import type { STDLayoutOptions } from '../spec/layout';
import type { DrawData } from '../transforms/types';
import type { AdaptiveLayout, ID, TreeData } from '../types';
Expand Down Expand Up @@ -93,10 +93,11 @@ export class LayoutController {
* <zh/> 后布局,即在完成绘制后执行布局
*
* <en/> Post layout, that is, perform layout after drawing
* @param layoutOptions - <zh/> 布局配置项 | <en/> Layout options
*/
public async postLayout() {
if (!this.options) return;
const pipeline = Array.isArray(this.options) ? this.options : [this.options];
public async postLayout(layoutOptions: LayoutOptions | undefined = this.options) {
if (!layoutOptions) return;
const pipeline = Array.isArray(layoutOptions) ? layoutOptions : [layoutOptions];
const { graph } = this.context;
emit(graph, new GraphLifeCycleEvent(GraphEvent.BEFORE_LAYOUT, { type: 'post' }));
for (const options of pipeline) {
Expand Down
3 changes: 1 addition & 2 deletions packages/site/examples/layout/mechanism/demo/switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ fetch('https://gw.alipayobjects.com/os/antvdemo/assets/data/relations.json')
mds: { type: 'mds', linkDistance: 100 },
};
graph.stopLayout();
graph.setLayout(options[layout]);
graph.layout();
graph.layout(options[layout]);
});
});
});

0 comments on commit a82674b

Please sign in to comment.