logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.47
  • Introduction
  • Data
  • Getting Started
    • Quick Start
    • Installation
    • Integration
      • react
      • vue
      • angular
    • Step-by-step guide
  • Graph
    • Extensions En
    • Graph
    • Options
    • extension
  • Element
    • Element Overview
    • Element State
    • Node
      • Node Overview
      • Build-in Node
        • Common Node Configurations
        • Diamond
        • Donut
        • Ellipse
        • Hexagon
        • Html
        • Image
        • Rect
        • Star
        • Triangle
        • Circle
      • Custom Node
      • Define Nodes with React
    • Edge
      • Edge Overview
      • Build-in Edge
        • Common Edge Configurations
        • Cubic Bezier Curve
        • CubicHorizontal Bezier Curve
        • CubicVertical Bezier Curve
        • Line
        • Polyline
        • Quadratic Bezier Curve
      • Custom Edge
    • Combo
      • Combo Overview
      • Build-in Combo
        • Circle
        • Combo Configuration Options
        • Rect
      • Custom Combo
    • Shape
      • Shape and KeyShape
      • Atomic Shapes and Their Properties
      • Design and Implementation of Composite Shape
  • Layout
    • Layout Overview
    • Build-in Layout
      • 3D Force-Directed Layout
      • AntvDagre Layout
      • Circular Layout
      • ComboCombined Layout
      • Common Layout Configuration Options
      • CompactBox
      • Concentric Layout
      • D3 Force-Directed Layout
      • Dagre Layout
      • Dendrogram Layout
      • Fishbone Layout
      • Force Force-directed Layout
      • ForceAtlas2 Force-directed Layout
      • Fruchterman Force-directed Layout
      • Grid Layout
      • Indented Tree
      • MDS High-dimensional Data Dimensionality Reduction Layout
      • Mindmap Tree
      • Radial Layout
      • Random Layout
      • Snake Layout
    • Custom Layout
  • Behavior
    • Behavior Overview
    • Build-in Behavior
      • AutoAdaptLabel
      • BrushSelect
      • ClickSelect
      • CollapseExpand
      • CreateEdge
      • DragCanvas
      • DragElement
      • DragElementForce
      • FixElementSize
      • FocusElement
      • HoverActivate
      • LassoSelect
      • OptimizeViewportTransform
      • ScrollCanvas
      • ZoomCanvas
    • Custom Behavior
  • Plugin
    • Plugin Overview
    • Build-in Plugin
      • Background
      • BubbleSets
      • Contextmenu
      • EdgeBundling
      • EdgeFilterLens
      • Fisheye
      • Fullscreen
      • GridLine
      • History
      • Hull
      • Legend
      • Minimap
      • Snapline
      • Timebar
      • Toolbar
      • Tooltip
      • Watermark
    • Custom Plugin
  • Transform
    • Data Transformation Overview
    • Build-in Transform
      • MapNodeSize
      • PlaceRadialLabels
      • ProcessParallelEdges
    • Custom Transform
  • Theme
    • Theme Overview
    • Custom Theme
    • Palette
    • Custom Palette
  • Animation
    • Animation Overview
    • Custom Animation
  • Further Reading
    • Event
    • renderer
    • coordinate
    • download-image
    • Using Iconfont
    • Use 3D
    • Bundle Project
  • What's new
    • Feature
    • Upgrade To 5.0
  • FAQ
  • contribute

Grid Layout

Previous
Fruchterman Force-directed Layout
Next
Indented Tree

Resources

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-互动图形解决方案
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

Overview

The grid layout arranges nodes in a grid pattern, suitable for scenarios where nodes need to be arranged neatly. This layout supports automatic calculation of the number of rows and columns, or you can specify them manually. It also supports preventing node overlap.

Use Cases

  • Visualizing data in a matrix or table format

Online Demo

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();
});
},
);

Configuration

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

Options

PropertyDescriptionTypeDefaultRequired
typeLayout typegrid-✓
beginGrid start position (top-left corner), default is [0, 0][number, number][0, 0]
colsNumber of columns. If undefined, the algorithm calculates it automatically based on node count, layout space, and rows (if set)numberundefined
rowsNumber of rows. If undefined, the algorithm calculates it automatically based on node count, layout space, and cols (if set)number10
widthLayout area width. In G6, the container width is used as the default valuenumber300
heightLayout area height. In G6, the container height is used as the default valuenumber300
condenseIf false, uses all available canvas space; if true, uses the minimum canvas spacebooleanfalse
nodeSizeNode size (diameter), used for collision detection when preventing overlapSize | ((nodeData: Node) => Size)-
nodeSpacingNode spacing, used to adjust the gap between nodes((node?: Node) => number) | number-
positionSpecify the row and column for each node(node?: Node) => { row?: number; col?: number; }undefined
preventOverlapWhether to prevent node overlap. Requires nodeSize or size property in node databooleanfalse
preventOverlapPaddingPadding when preventing overlap. Effective when preventOverlap is truenumber10
sortBySort basis (node property name). Higher values are placed more centrally. If undefined, degree is used for sortingstringundefined

preventOverlap

boolean Default: false

Whether to prevent overlap

Must be used with nodeSize or the size property in node data. Only when data has data.size or nodeSize is set in the layout, collision detection for node overlap can be performed.

preventOverlapPadding

number Default: 10

Padding when preventing overlap. Effective when preventOverlap is true.

sortBy

string Default: undefined

Sort basis (node property name). Higher values are placed more centrally. If undefined, degree is used for sorting. In G6, the container width is used as the default value for grid layout width. When used alone, the default is 300.

Code Examples

Basic Usage

The simplest configuration:

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)}`,
})),
},
});

Result:

createGraph(
{
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',
},
},
},
{ width: 600, height: 400 },
);

Custom Configuration

You can customize the grid layout in various ways:

const graph = new Graph({
layout: {
type: 'grid',
begin: [50, 50], // Start layout from [50, 50]
cols: 4, // 4 columns
rows: 6, // 6 rows
width: 400, // Layout area width
height: 600, // Layout area height
preventOverlap: true, // Prevent node overlap
nodeSize: 30, // Node size
condense: true, // Use minimum space
sortBy: 'value', // Sort by value property
},
data: {
nodes: Array.from({ length: 24 }, (_, i) => ({
id: `node-${i}`,
data: {
value: Math.random() * 100, // Property for sorting
},
})),
edges: Array.from({ length: 20 }, (_, i) => ({
id: `edge-${i}`,
source: `node-${Math.floor(Math.random() * 24)}`,
target: `node-${Math.floor(Math.random() * 24)}`,
})),
},
});

Result:

createGraph(
{
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',
},
},
},
{ width: 600, height: 400 },
);

Specify Node Position

You can specify the position for specific nodes using the position property:

const graph = new Graph({
layout: {
type: 'grid',
cols: 5,
rows: 5,
position: (node) => {
// Specify position for specific nodes
if (node.id === 'node-0') return { row: 0, col: 0 }; // Top-left
if (node.id === 'node-1') return { row: 0, col: 4 }; // Top-right
if (node.id === 'node-2') return { row: 4, col: 0 }; // Bottom-left
if (node.id === 'node-3') return { row: 4, col: 4 }; // Bottom-right
return undefined; // Other nodes are auto-arranged
},
},
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)}`,
})),
},
});

Result:

createGraph(
{
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',
},
},
},
{ width: 600, height: 400 },
);

Real Cases

  • Grid Layout