logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.45
  • 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 Overview
      • Shape Properties
  • Layout
    • Layout Overview
    • Build-in Layout
      • AntvDagre
      • Circular
      • ComboCombined
      • Common Layout Configuration Options
      • CompactBox
      • Concentric
      • D3Force
      • D3Force3D
      • Dagre
      • Dendrogram
      • Fishbone
      • Force
      • ForceAtlas2
      • Fruchterman
      • Grid Layout
      • Indented
      • Mds
      • Mindmap
      • Radial
      • Random
      • Snake
    • 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

Behavior Overview

Previous
Custom Layout
Next
AutoAdaptLabel

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

What is Behavior

Behavior refers to the interactive operations between users and chart elements, such as dragging the canvas, selecting nodes, zooming the view, etc. Good behavior design allows users to explore and understand graph data more intuitively. Proper configuration of behaviors is a key step in building efficient and usable charts.

Changes in G6 5.0 Behavior System

G6 5.0 removed the concept of "Behavior Mode" (Mode), and directly lists the required behavior behaviors in behaviors, simplifying the configuration. This makes behavior configuration more intuitive and easier to get started with.

import { Graph } from '@antv/g6';
const graph = new Graph({
behaviors: ['drag-canvas', 'zoom-canvas', 'click-select'],
});

Built-in Behaviors

G6 provides a variety of built-in behaviors that are ready to use without registration:

CategoryBehavior NameRegistration TypeFunction Description
Navigation
Drag Canvasdrag-canvasDrag the entire canvas view
Zoom Canvaszoom-canvasZoom the canvas view
Scroll Canvasscroll-canvasScroll the canvas using the wheel
Optimize Viewport Transformoptimize-viewport-transformOptimize view transform performance
Selection
Click Selectclick-selectClick to select graph elements
Brush Selectbrush-selectSelect elements by dragging a rectangular area
Lasso Selectlasso-selectFreely draw an area to select elements
Editing
Create Edgecreate-edgeInteractively create new edges
Drag Elementdrag-elementDrag nodes or combos
Force-directed Dragdrag-element-forceDrag nodes in force-directed layout
Data Exploration
Collapse/Expandcollapse-expandExpand or collapse subtree nodes
Focus Elementfocus-elementFocus on specific elements and automatically adjust the view
Hover Activatehover-activateHighlight elements when hovering
Visual Optimization
Fix Element Sizefix-element-sizeFix the element size to a specified value
Auto-adapt Labelauto-adapt-labelAutomatically adjust label position

For detailed configuration of each behavior, refer to the Built-in Behavior Documentation.

Behavior

Some behaviors may overlap in triggering mechanisms, such as brush-select and drag-canvas both using mouse dragging. In such cases, you can avoid conflicts by modifying the trigger key (e.g., hold Shift to drag and select).

Custom Behaviors

When built-in behaviors cannot meet the requirements, G6 provides powerful customization capabilities:

  • Extend by inheriting built-in behaviors
  • Create entirely new behavior behaviors

Unlike built-in behaviors, custom behaviors need to be registered before use. For detailed tutorials, refer to the Custom Behavior documentation.

Configuration and Usage

Basic Configuration

The simplest way is to directly specify the required behaviors through the behaviors array when initializing the graph instance:

const graph = new Graph({
// Other configurations...
behaviors: ['drag-canvas', 'zoom-canvas', 'click-select'],
});

Configure Behavior Parameters

For behaviors that require custom parameters, you can configure properties using the object form:

const graph = new Graph({
// Other configurations...
behaviors: [
'drag-canvas',
{
type: 'zoom-canvas',
sensitivity: 1.5, // Configure sensitivity
key: 'zoom-behavior', // Specify a key for the behavior for subsequent updates
},
],
});

Dynamically Update Behaviors

G6 supports dynamically managing behavior behaviors during the runtime of the graph instance to meet complex behavior needs:

You can adjust behaviors using the setBehaviors method:

// Add new behavior
graph.setBehaviors((behaviors) => [...behaviors, 'lasso-select']);
// Remove behavior
graph.setBehaviors((behaviors) => behaviors.filter((b) => b !== 'click-select'));

You can update the configuration of behaviors using the updateBehavior method:

// Update a single behavior
graph.updateBehavior({
key: 'zoom-behavior',
sensitivity: 2,
enable: false, // Disable the behavior
});

Note

When using the updateBehavior method, you need to specify a unique key for the behavior during initialization.

Uninstall Behaviors

You can also uninstall behaviors using the setBehaviors method by setting the behavior configuration list to empty:

graph.setBehaviors([]);

For more behavior-related APIs, refer to the Behavior API Documentation.

Behavior and Events

Behaviors are essentially implemented through event listening and response. Although built-in behaviors have encapsulated common behavior behaviors, you can also directly implement custom behavior logic through the event API.

Event Listening Example

// Use event constants (recommended)
import { NodeEvent, EdgeEvent } from '@antv/g6';
// Listen for node clicks
graph.on(NodeEvent.CLICK, (evt) => {
const { target } = evt;
graph.setElementState(target.id, 'selected');
});
// Listen for edge hover
graph.on(EdgeEvent.POINTER_OVER, (evt) => {
const { target } = evt;
graph.setElementState(target.id, 'highlight');
});

The event system is the foundation for implementing behaviors. Mastering the event API is crucial for understanding and extending behavior behaviors. For more event-related information, refer to the Event Documentation.