logo

G6

  • 文档
  • API
  • 图表示例
  • 社区
  • 所有产品antv logo arrow
  • 5.0.49
  • 简介
  • 开始使用
    • 快速开始
    • 安装
    • 前端框架集成
      • 在 React 中使用
      • 在 Vue 中使用
      • 在 Angular 中使用
    • 详细教程
  • 图 Graph
    • Graph - 图
    • Options 配置项
    • 内置扩展
    • Extension - 扩展
  • 数据 Data
  • 元素 Element
    • 元素总览
    • 元素状态
    • 节点 Node
      • 节点总览
      • 节点通用配置项
      • 圆形节点 Circle
      • 菱形节点 Diamond
      • 甜甜圈节点 Donut
      • 椭圆形节点 Ellipse
      • 六边形节点 Hexagon
      • HTML节点 Html
      • 图片节点 Image
      • 矩形节点 Rect
      • 五角形节点 Star
      • 三角形节点 Triangle
      • 自定义节点
      • 使用 React 定义节点
    • 边 Edge
      • 边总览
      • 边通用配置项
      • 三次贝塞尔曲线边 Cubic
      • 水平三次贝塞尔曲线边 CubicHorizontal
      • 垂直三次贝塞尔曲线边 CubicVertical
      • 直线边 Line
      • 二次贝塞尔曲线边 Quadratic
      • 折线边 Polyline
      • 自定义边
    • 组合 Combo
      • 组合总览
      • 组合通用配置项
      • 圆形组合 Circle
      • 矩形组合 Rect
      • 自定义 Combo
    • 图形 Shape
      • 图形 Shape 与 KeyShape
      • 原子 Shape 以及其属性
      • 复合 Shape 的设计与实现
  • 布局 Layout
    • 布局总览
    • 布局通用配置项
    • AntV Dagre 布局 AntvDagre
    • 环形布局 Circular
    • 复合布局 ComboCombined
    • 紧凑树布局 CompactBox
    • 同心圆布局 Concentric
    • 3D D3力导向布局 D3Force3D
    • D3力导向布局 D3Force
    • Dagre 布局
    • 生态树 Dendrogram
    • 鱼骨布局 Fishbone
    • ForceAtlas2 力导向布局
    • Force 力导向布局
    • Fruchterman 力导向布局
    • 网格布局 Grid
    • 缩进树 Indented
    • 高维数据降维布局 MDS
    • 脑图树 Mindmap
    • 径向布局 Radial
    • 随机布局 Random
    • 蛇形布局 Snake
    • 自定义布局
  • 交互 Behavior
    • 交互总览
    • 缩放画布 ZoomCanvas
    • 标签自适应显示 AutoAdaptLabel
    • 框选 BrushSelect
    • 点击选中 ClickSelect
    • 展开/收起元素 CollapseExpand
    • 创建边 CreateEdge
    • 拖拽画布 DragCanvas
    • 拖拽元素 DragElement
    • 力导向拖拽元素 DragElementForce
    • 缩放画布时固定元素大小 FixElementSize
    • 聚焦元素 FocusElement
    • 悬停激活 HoverActivate
    • 套索选择 LassoSelect
    • 优化视口变换 OptimizeViewportTransform
    • 滚动画布 ScrollCanvas
    • 自定义交互
  • 插件 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
  • 常见问题
  • 参与贡献

网格布局 Grid

上一篇
Fruchterman 力导向布局
下一篇
缩进树 Indented

资源

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...

概述

网格布局将节点按照网格形式排列,适用于需要整齐排列节点的场景。该布局支持自动计算行列数,也可以手动指定行列数,并支持防止节点重叠。

使用场景

  • 在数据可视化中需要展示矩阵或表格形式的数据关系

在线体验

createGraph(
{
data: {
nodes: Array.from({ length: 25 }, (_, i) => ({
id: `node-${i}`,
data: {
value: Math.random() * 100,
},
})),
edges: Array.from({ length: 20 }, (_, i) => ({
id: `edge-${i}`,
source: `node-${Math.floor(Math.random() * 25)}`,
target: `node-${Math.floor(Math.random() * 25)}`,
})),
},
autoFit: 'view',
node: {
style: {
size: 20,
label: true,
labelText: (datum) => datum.id,
labelBackground: true,
icon: false,
},
palette: {
type: 'group',
field: (datum) => datum.data.value,
color: ['#1783FF', '#00C9C9', '#F08F56', '#D580FF'],
},
},
edge: {
style: {
stroke: '#bfbfbf',
},
},
behaviors: ['drag-canvas'],
layout: {
type: 'grid',
cols: 5,
rows: 5,
width: 400,
height: 400,
preventOverlap: true,
nodeSize: 30,
condense: false,
},
},
{ width: 600, height: 400 },
(gui, graph) => {
const options = {
type: 'grid',
cols: 5,
rows: 5,
width: 400,
height: 400,
preventOverlap: true,
nodeSize: 30,
condense: false,
};
const optionFolder = gui.addFolder('Grid Layout Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'cols', 2, 10, 1);
optionFolder.add(options, 'rows', 2, 10, 1);
optionFolder.add(options, 'width', 200, 600, 50);
optionFolder.add(options, 'height', 200, 600, 50);
optionFolder.add(options, 'preventOverlap');
optionFolder.add(options, 'nodeSize', 10, 50, 5);
optionFolder.add(options, 'condense');
optionFolder.onChange(({ property, value }) => {
graph.setLayout({
type: 'grid',
[property]: value,
});
graph.layout();
});
},
);

配置方式

const graph = new Graph({
layout: {
type: 'grid',
begin: [0, 0],
cols: 5,
rows: 5,
width: 300,
height: 300,
preventOverlap: true,
nodeSize: 30,
condense: false,
},
});

配置项

属性描述类型默认值必选
type布局类型grid-✓
begin网格开始位置(左上角),默认为 [0, 0][number, number][0, 0]
cols网格的列数,为 undefined 时算法根据节点数量、布局空间、rows(若指定)自动计算numberundefined
rows网格的行数,为 undefined 时算法根据节点数量、布局空间、cols(若指定)自动计算number10
width布局区域宽度,在 G6 中使用当前容器的宽度作为默认值number300
height布局区域高度,在 G6 中使用当前容器的高度作为默认值number300
condense为 false 时表示利用所有可用画布空间,为 true 时表示利用最小的画布空间booleanfalse
nodeSize节点大小(直径),用于防止节点重叠时的碰撞检测Size | ((nodeData: Node) => Size)-
nodeSpacing节点间距,用于调整节点之间的间隔((node?: Node) => number) | number-
position指定每个节点所在的行和列(node?: Node) => { row?: number; col?: number; }undefined
preventOverlap是否防止节点重叠,需要配合 nodeSize 或节点数据中的 size 属性使用booleanfalse
preventOverlapPadding避免重叠时节点的间距 padding,preventOverlap 为 true 时生效number10
sortBy指定排序的依据(节点属性名),数值越高则该节点被放置得越中心。若为 undefined,则会计算节点的度数,度数越高,节点将被放置得越中心stringundefined

preventOverlap

boolean Default: false

是否防止重叠

必须配合下面属性 nodeSize 或节点数据中的 data.size 属性,只有在数据中设置了 data.size 或在该布局中配置了与当前图节点大小相同的 nodeSize 值,才能够进行节点重叠的碰撞检测

preventOverlapPadding

number Default: 10

避免重叠时节点的间距 padding,preventOverlap 为 true 时生效

sortBy

string Default: undefined

指定排序的依据(节点属性名),数值越高则该节点被放置得越中心。若为 undefined,则会计算节点的度数,度数越高,节点将被放置得越中心

在 G6 中使用当前容器的宽度作为 grid 布局 width 的默认值。单独使用此布局时默认值为 300

代码示例

基础用法

最简单的配置方式:

const graph = new Graph({
layout: {
type: 'grid',
cols: 5,
rows: 5,
},
data: {
nodes: Array.from({ length: 25 }, (_, i) => ({
id: `node-${i}`,
data: {
value: Math.random() * 100,
},
})),
edges: Array.from({ length: 20 }, (_, i) => ({
id: `edge-${i}`,
source: `node-${Math.floor(Math.random() * 25)}`,
target: `node-${Math.floor(Math.random() * 25)}`,
})),
},
});

效果如下:

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
width: 600,
height: 400,
layout: {
type: 'grid',
cols: 5,
rows: 5,
},
data: {
nodes: Array.from({ length: 25 }, (_, i) => ({
id: `node-${i}`,
data: {
value: Math.random() * 100,
},
})),
edges: Array.from({ length: 20 }, (_, i) => ({
id: `edge-${i}`,
source: `node-${Math.floor(Math.random() * 25)}`,
target: `node-${Math.floor(Math.random() * 25)}`,
})),
},
node: {
style: {
size: 20,
label: true,
labelText: (datum) => datum.id,
labelBackground: true,
},
},
edge: {
style: {
stroke: '#bfbfbf',
},
},
});
graph.render();

自定义配置

可以通过多种方式自定义网格布局:

const graph = new Graph({
layout: {
type: 'grid',
begin: [50, 50], // 从坐标 [50, 50] 开始布局
cols: 4, // 指定 4 列
rows: 6, // 指定 6 行
width: 400, // 布局区域宽度
height: 600, // 布局区域高度
preventOverlap: true, // 防止节点重叠
nodeSize: 30, // 节点大小
condense: true, // 使用最小空间
sortBy: 'value', // 按 value 属性排序
},
data: {
nodes: Array.from({ length: 24 }, (_, i) => ({
id: `node-${i}`,
data: {
value: Math.random() * 100, // 用于排序的属性
},
})),
edges: Array.from({ length: 20 }, (_, i) => ({
id: `edge-${i}`,
source: `node-${Math.floor(Math.random() * 24)}`,
target: `node-${Math.floor(Math.random() * 24)}`,
})),
},
});

效果如下:

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
width: 600,
height: 400,
layout: {
type: 'grid',
begin: [50, 50],
cols: 4,
rows: 6,
width: 400,
height: 600,
preventOverlap: true,
nodeSize: 30,
condense: true,
sortBy: 'value',
},
data: {
nodes: Array.from({ length: 24 }, (_, i) => ({
id: `node-${i}`,
data: {
value: Math.random() * 100,
},
})),
edges: Array.from({ length: 20 }, (_, i) => ({
id: `edge-${i}`,
source: `node-${Math.floor(Math.random() * 24)}`,
target: `node-${Math.floor(Math.random() * 24)}`,
})),
},
node: {
style: {
size: 20,
label: true,
labelText: (datum) => datum.id,
labelBackground: true,
},
palette: {
type: 'group',
field: (datum) => datum.data.value,
color: ['#1783FF', '#00C9C9', '#F08F56', '#D580FF'],
},
},
edge: {
style: {
stroke: '#bfbfbf',
},
},
});
graph.render();

指定节点位置

可以通过 position 属性为特定节点指定位置:

const graph = new Graph({
layout: {
type: 'grid',
cols: 5,
rows: 5,
position: (node) => {
// 为特定节点指定位置
if (node.id === 'node-0') return { row: 0, col: 0 }; // 左上角
if (node.id === 'node-1') return { row: 0, col: 4 }; // 右上角
if (node.id === 'node-2') return { row: 4, col: 0 }; // 左下角
if (node.id === 'node-3') return { row: 4, col: 4 }; // 右下角
return undefined; // 其他节点自动布局
},
},
data: {
nodes: Array.from({ length: 25 }, (_, i) => ({
id: `node-${i}`,
})),
edges: Array.from({ length: 20 }, (_, i) => ({
id: `edge-${i}`,
source: `node-${Math.floor(Math.random() * 25)}`,
target: `node-${Math.floor(Math.random() * 25)}`,
})),
},
});

效果如下:

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
width: 600,
height: 400,
layout: {
type: 'grid',
cols: 5,
rows: 5,
position: (node) => {
if (node.id === 'node-0') return { row: 0, col: 0 };
if (node.id === 'node-1') return { row: 0, col: 4 };
if (node.id === 'node-2') return { row: 4, col: 0 };
if (node.id === 'node-3') return { row: 4, col: 4 };
return undefined;
},
},
data: {
nodes: Array.from({ length: 25 }, (_, i) => ({
id: `node-${i}`,
})),
edges: Array.from({ length: 20 }, (_, i) => ({
id: `edge-${i}`,
source: `node-${Math.floor(Math.random() * 25)}`,
target: `node-${Math.floor(Math.random() * 25)}`,
})),
},
node: {
style: {
size: 20,
label: true,
labelText: (datum) => datum.id,
labelBackground: true,
},
},
edge: {
style: {
stroke: '#bfbfbf',
},
},
});
graph.render();

实际案例

  • Grid布局