logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.48
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 内置节点
        • 节点通用配置项
        • Diamond 菱形
        • Donut 甜甜圈
        • Ellipse 椭圆形
        • Hexagon 六边形
        • Html HTML
        • Image 图片
        • Rect 矩形
        • Star 五角形
        • Triangle 三角形
        • Circle 圆形
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 内置边
        • 边通用配置项
        • Cubic 三次贝塞尔曲线
        • CubicHorizontal 水平三次贝塞尔曲线
        • CubicVertical 垂直三次贝塞尔曲线
        • Line 直线
        • Polyline 折线
        • Quadratic 二次贝塞尔曲线
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 内置组合
        • 组合配置项
        • Circle 圆形
        • Rect 矩形
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 内置布局
      • 布局通用配置项
      • AntvDagre 布局
      • Circular 环形布局
      • ComboCombined 复合布局
      • CompactBox 紧凑树
      • Concentric 同心圆布局
      • D3Force 力导向布局
      • D3Force3D 3D 力导向布局
      • Dagre 布局
      • Dendrogram 生态树
      • Fishbone 鱼骨布局
      • Force 力导向布局
      • ForceAtlas2 力导向布局
      • Fruchterman 力导向布局
      • Grid 网格布局
      • Indented 缩进树
      • MDS 高维数据降维布局
      • Mindmap 脑图树
      • Radial 径向布局
      • Random 随机布局
      • Snake 蛇形布局
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 内置交互
      • AutoAdaptLabel 标签自适应显示
      • BrushSelect 框选
      • ClickSelect 点击选中
      • CollapseExpand 展开/收起元素
      • CreateEdge 创建边
      • DragCanvas 拖拽画布
      • DragElement 拖拽元素
      • DragElementForce 力导向拖拽元素
      • FixElementSize 缩放画布时固定元素大小
      • FocusElement 聚焦元素
      • HoverActivate 悬停激活
      • LassoSelect 套索选择
      • OptimizeViewportTransform 优化视口变换
      • ScrollCanvas 滚动画布
      • ZoomCanvas 缩放画布
    • 自定义交互
  • 插件 Plugin
    • 插件总览
    • 内置插件
      • Background 背景
      • BubbleSets 气泡集
      • Contextmenu 上下文菜单
      • EdgeBundling 边绑定
      • EdgeFilterLens 边过滤镜
      • Fisheye 鱼眼放大镜
      • Fullscreen 全屏展示
      • GridLine 网格线
      • History 历史记录
      • Hull 轮廓包围
      • Legend 图例
      • Minimap 小地图
      • Snapline 对齐线
      • Timebar 时间条
      • Toolbar 工具栏
      • Tooltip 提示框
      • Watermark 水印
    • 自定义插件
  • 数据处理 Transform
    • 数据处理总览
    • 内置数据处理
      • MapNodeSize 动态调整节点大小
      • PlaceRadialLabels 径向标签
      • ProcessParallelEdges 平行边
    • 自定义数据处理
  • 主题 Theme
    • 主题总览
    • 自定义主题
    • 色板
    • 自定义色板
  • 动画 Animation
    • 动画总览
    • 自定义动画
  • 扩展阅读
    • 事件
    • 渲染器
    • 坐标系
    • 下载图片
    • 使用 iconfont
    • 使用 3D
    • 项目打包
  • 版本特性
    • 新版本特性
    • 升级到 5.0
  • 常见问题
  • 参与贡献

自定义布局

上一篇
Snake 蛇形布局
下一篇
交互总览

Resources

Ant Design
Galacea Effects
Umi-React 应用开发框架
Dumi-组件/文档研发工具
ahooks-React Hooks 库

社区

体验科技专栏
seeconfSEE Conf-蚂蚁体验科技大会

帮助

GitHub
StackOverflow

more products更多产品

Ant DesignAnt Design-企业级 UI 设计语言
yuque语雀-知识创作与分享工具
EggEgg-企业级 Node 开发框架
kitchenKitchen-Sketch 工具集
GalaceanGalacean-互动图形解决方案
xtech蚂蚁体验科技
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

概述

G6 中布局分为迭代布局和非迭代布局两种,迭代布局是指布局算法会迭代多次直到收敛,非迭代布局是指布局算法只执行一次。

实现布局

非迭代布局

实现一个非迭代布局相对简单,只需要实现 BaseLayout 中的 execute 方法即可,下面是一个简单的自定义布局实现:

import { BaseLayout } from '@antv/g6';
import type { GraphData } from '@antv/g6';
class DiagonalLayout extends BaseLayout {
id = 'diagonal-layout';
async execute(data: GraphData): Promise<GraphData> {
const { nodes = [] } = data;
return {
nodes: nodes.map((node, index) => ({
id: node.id,
style: {
x: 50 * index + 25,
y: 50 * index + 25,
},
})),
};
}
}

在上面的代码中,我们实现了一个简单的布局算法,将节点从左上角沿对角线排列。

import { Graph, BaseLayout, register, ExtensionCategory } from '@antv/g6';
class DiagonalLayout extends BaseLayout {
id = 'diagonal-layout';
async execute(data) {
const { nodes = [] } = data;
return {
nodes: nodes.map((node, index) => ({
id: node.id,
style: {
x: 50 * index + 25,
y: 50 * index + 25,
},
})),
};
}
}
register(ExtensionCategory.LAYOUT, 'diagonal', DiagonalLayout);
const graph = new Graph({
container: 'container',
width: 200,
height: 200,
data: {
nodes: [{ id: 'node-1' }, { id: 'node-2' }, { id: 'node-3' }, { id: 'node-4' }],
},
layout: {
type: 'diagonal',
},
});
graph.render();

提示

execute 方法返回的是一个 GraphData 对象,里面仅需要包含元素的基本信息(如 id、source、target)以及布局新增的属性(如 x、y、边的控制点等),不需要包含其他无用的信息。 如果仅需要对节点进行布局,可以只返回节点信息,不需要返回边的信息。

迭代布局

迭代布局的实现同样需要继承 BaseLayout,但是除 execute 外还需要实现 tick 和 stop 方法,tick 方法用于将布局迭代到指定轮次,stop 方法用于停止布局迭代。

此外,迭代布局中需要在每次迭代调用 options.onTick 以触发图的更新。

type onTick = (data: GraphData) => void;

下面是一个简单的迭代布局实现:

import { BaseLayout } from '@antv/g6';
import type { GraphData, BaseLayoutOptions } from '@antv/g6';
interface TickTockLayoutOptions extends BaseLayoutOptions {
onTick: (data: GraphData) => void;
}
class TickTockLayout extends BaseLayout<TickTockLayoutOptions> {
public id = 'custom-layout';
private tickCount = 0;
private data?: GraphData;
private timer?: number;
private resolve?: () => void;
private promise?: Promise<void>;
async execute(data: GraphData, options: TickTockLayoutOptions): Promise<GraphData> {
const { onTick } = { ...this.options, ...options };
this.tickCount = 0;
this.data = data;
this.promise = new Promise((resolve) => {
this.resolve = resolve;
});
this.timer = window.setInterval(() => {
onTick(this.simulateTick());
if (this.tickCount === 10) this.stop();
}, 200);
await this.promise;
return this.simulateTick();
}
simulateTick = () => {
const x = this.tickCount++ % 2 === 0 ? 50 : 150;
return {
nodes: (this?.data?.nodes || []).map((node, index) => ({
id: node.id,
style: { x, y: (index + 1) * 30 },
})),
};
};
tick = () => {
return this.simulateTick();
};
stop = () => {
clearInterval(this.timer);
this.resolve?.();
};
}

在这个例子中,我们实现了一个简单的迭代布局算法,每 200ms 将节点的 x 坐标在 50 和 150 之间切换,并按照节点顺序在 y 方向上排列。

import { Graph, BaseLayout, register, ExtensionCategory } from '@antv/g6';
class TickTockLayout extends BaseLayout {
id = 'tick-tock-layout';
async execute(data, options) {
const { onTick } = { ...this.options, ...options };
this.tickCount = 0;
this.data = data;
this.promise = new Promise((resolve) => {
this.resolve = resolve;
});
this.timer = window.setInterval(() => {
onTick(this.simulateTick());
if (this.tickCount === 10) this.stop();
}, 200);
await this.promise;
return this.simulateTick();
}
simulateTick = () => {
const x = this.tickCount++ % 2 === 0 ? 50 : 150;
return {
nodes: (this?.data?.nodes || []).map((node, index) => ({
id: node.id,
style: { x, y: (index + 1) * 30 },
})),
};
};
tick = () => {
return this.simulateTick();
};
stop = () => {
clearInterval(this.timer);
this.resolve?.();
};
}
register(ExtensionCategory.LAYOUT, 'tick-tock', TickTockLayout);
const graph = new Graph({
container: 'container',
width: 200,
height: 200,
animation: true,
data: {
nodes: [{ id: 'node-1' }, { id: 'node-2' }, { id: 'node-3' }, { id: 'node-4' }, { id: 'node-5' }],
},
layout: {
type: 'tick-tock',
},
});
graph.render();

注册布局

通过 G6 提供的 register 方法注册即可,详见注册布局

配置布局

可在 options.layout 中配置布局的类型和参数,详见配置布局