logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.48
  • 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

GridLine

Previous
Fullscreen
Next
History

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 GridLine plugin provides visual auxiliary lines for the canvas, helping users precisely position and align graphic elements. It is an indispensable tool in graphic drawing.

Use Cases

The GridLine plugin is mainly suitable for the following scenarios:

  • Assisting users in precise drawing and element alignment
  • Providing visual references to enhance spatial awareness
  • Building a structured reference system when designing and editing graphics

Basic Usage

Below is a simple example of initializing the GridLine plugin:

const graph = new Graph({
plugins: [
{
type: 'grid-line',
key: 'my-grid-line', // Specify a unique identifier for dynamic updates
size: 20,
stroke: '#0001',
follow: true,
},
],
});

Online Experience

createGraph(
{
data: { nodes: [{ id: 'node-1' }] },
node: { style: { fill: '#7e3feb' } },
edge: { style: { stroke: '#8b9baf' } },
layout: { type: 'force' },
behaviors: ['drag-canvas'],
plugins: [{ type: 'grid-line', key: 'grid-line', size: 30 }],
},
{ width: 600, height: 300 },
(gui, graph) => {
const LINE_STYLE = ['none', 'hidden', 'dotted', 'dashed', 'solid', 'double', 'groove', 'ridge', 'inset', 'outset'];
const options = {
type: 'grid-line',
border: true,
borderLineWidth: 1,
borderStroke: '#eee',
borderStyle: 'solid',
follow: false,
lineWidth: 1,
size: 20,
stroke: '#eee',
};
const optionFolder = gui.addFolder('Gird Line Options');
optionFolder.add(options, 'type').disable(true);
optionFolder.add(options, 'size', 1, 50, 1);
optionFolder.add(options, 'lineWidth', 1, 10, 1);
optionFolder.addColor(options, 'stroke');
optionFolder.add(options, 'border');
optionFolder.add(options, 'borderLineWidth', 1, 10, 1);
optionFolder.add(options, 'borderStyle', LINE_STYLE);
optionFolder.addColor(options, 'borderStroke');
optionFolder.add(options, 'follow');
optionFolder.onChange(({ property, value }) => {
graph.updatePlugin({
key: 'grid-line',
[property]: value,
});
graph.render();
});
},
);

Configuration Options

PropertyDescriptionTypeDefaultRequired
typePlugin typestringgrid-line✓
keyUnique identifier for the plugin, used to get the plugin instance or update plugin optionsstring-
borderWhether to display the borderbooleantrue
borderLineWidthBorder line widthnumber1
borderStrokeBorder color, see CSS border-colorstring#eee
borderStyleBorder style, see CSS border-stylestringsolid
followWhether to follow canvas movementsboolean | {translate ?: boolean, zoom?: boolean}false
lineWidthGrid line widthnumber | string1
sizeGrid unit size in pixelsnumber20
strokeGrid line colorstring#eee

follow

The follow property controls whether the grid lines follow the canvas transformations. It supports two configuration methods:

  1. Boolean Configuration: When set to true, the grid lines follow both canvas translation and zoom; when set to false, they remain static.
// Enable both translation and zoom following
const graph = new Graph({
plugins: [
{
type: 'grid-line',
follow: true,
},
],
});
  1. Object Configuration: Allows more precise control over the grid line following behavior.
// Follow translation only, not zoom
const graph = new Graph({
plugins: [
{
type: 'grid-line',
follow: {
translate: true, // Follow translation
zoom: false, // Do not follow zoom
},
},
],
});
// Follow zoom only, not translation
const graph = new Graph({
plugins: [
{
type: 'grid-line',
follow: {
translate: false, // Do not follow translation
zoom: true, // Follow zoom
},
},
],
});

When grid lines follow zoom, they maintain a relative position to the canvas content, making alignment references more precise. Following translation allows the grid to move with the canvas content, enhancing the visual experience of spatial continuity.

Code Examples

Basic Grid Line

The simplest way is to use the preset configuration directly:

const graph = new Graph({
// Other configurations...
plugins: ['grid-line'],
});

The effect is as follows:

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
width: 300,
height: 150,
data: { nodes: [{ id: 'node-1', style: { x: 150, y: 75 } }] },
behaviors: ['drag-canvas'],
plugins: ['grid-line'],
});
graph.render();

Custom Style

You can customize the grid line style as needed:

const graph = new Graph({
// Other configurations...
plugins: [
{
type: 'grid-line',
stroke: '#1890ff33', // Blue semi-transparent grid line
lineWidth: 2,
size: 40, // Larger grid unit
borderStroke: '#1890ff', // Blue border
borderLineWidth: 2,
},
],
});

The effect is as follows:

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
width: 300,
height: 150,
data: { nodes: [{ id: 'node-1', style: { x: 150, y: 75 } }] },
behaviors: ['drag-canvas'],
plugins: [
{
type: 'grid-line',
stroke: '#1890ff33', // Blue semi-transparent grid line
lineWidth: 2,
size: 40, // Larger grid
borderStroke: '#1890ff', // Blue border
borderLineWidth: 2,
},
],
});
graph.render();

Follow Movement

Enabling the follow option allows the grid to move with the canvas, enhancing user experience:

const graph = new Graph({
// Other configurations...
behaviors: ['drag-canvas', 'zoom-canvas'],
plugins: [
{
type: 'grid-line',
follow: true, // Grid follows canvas movement
},
],
});

Try dragging/zooming the canvas to observe the grid following effect:

import { Graph } from '@antv/g6';
const graph = new Graph({
container: 'container',
width: 300,
height: 150,
data: { nodes: [{ id: 'node-1', style: { x: 150, y: 75 } }] },
behaviors: ['drag-canvas', 'zoom-canvas'],
plugins: [
{
type: 'grid-line',
follow: true, // Grid follows canvas movement
},
],
});
graph.render();

Dynamic Grid Update

Use the key identifier to dynamically update grid properties at runtime:

// Initial configuration
const graph = new Graph({
// Other configurations...
plugins: [
{
type: 'grid-line',
key: 'my-grid',
size: 20,
},
],
});
// Subsequent dynamic updates
graph.updatePlugin({
key: 'my-grid',
size: 40, // Update grid size
stroke: '#ff4d4f', // Update grid color
});

Cases