logo

G6

  • Docs
  • API
  • Playground
  • Community
  • Productsantv logo arrow
  • 5.0.45
  • Data
  • Canvas Operations
  • Element Operations
  • Graph Instance
  • Drawing and Rendering
  • Viewport Operations
  • Layout
  • Graph Options
  • Behavior
  • Plugin
  • Theme
  • Data Transformation
  • Event Listening
  • Coordinate Transformation
  • Export Image

Event Listening

Previous
Data Transformation
Next
Coordinate Transformation

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 of the Event System

G6 provides a powerful event mechanism that allows you to respond to various interactive behaviors occurring in the chart, such as node clicks, edge hovers, canvas drags, etc. Through the event system, you can implement complex interactive logic to enhance user experience.

Event Categories

Events in G6 can be broadly categorized into the following types:

  1. Element Events: Events related to nodes, edges, and Combos, such as node:click, edge:mouseenter
  2. Canvas Events: Events related to the entire canvas, such as canvas:drag, canvas:zoom
  3. Lifecycle Events: Events related to the chart lifecycle, such as beforerender, afterrender

Event Naming Convention

G6 events follow the [object]:[event] format, for example:

  • node:click - Node click event
  • edge:mouseenter - Mouse enters edge event
  • canvas:drag - Canvas drag event

Best Practice: Using Constant Enums

G6 provides a complete set of event constant enums, and it is strongly recommended to use these constants instead of directly using string event names:

import { NodeEvent, EdgeEvent, CanvasEvent, GraphEvent } from '@antv/g6';
// Use constant enums to listen to events
graph.on(NodeEvent.CLICK, handleNodeClick);
graph.on(EdgeEvent.POINTER_OVER, handleEdgeHover);
graph.on(CanvasEvent.DRAG, handleCanvasDrag);
graph.on(GraphEvent.AFTER_RENDER, handleAfterRender);

Advantages:

  • Type safety, avoiding string spelling errors
  • Provides intelligent code hints and auto-completion

API Reference

Graph.on(eventName, callback, once)

Listen to a specified event and execute a callback function when the event is triggered.

on<T extends IEvent = IEvent>(eventName: string, callback: (event: T) => void, once?: boolean): this;

Parameters

ParameterDescriptionTypeDefaultRequired
eventNameName of the event to listen tostring-✓
callbackCallback function executed when the event is triggered(event: T) => void-✓
onceWhether to listen only onceboolean-

Return Value

  • Type: this (Graph instance)
  • Description: Returns the graph instance itself, supporting chain calls

Example

import { NodeEvent, EdgeEvent, CanvasEvent } from '@antv/g6';
// Listen to node click event
graph.on(NodeEvent.CLICK, (evt) => {
const { target } = evt; // Get the ID of the clicked node
console.log(`Node ${target.id} was clicked`);
// Get node data
const nodeData = graph.getNodeData(target.id);
console.log('Node data:', nodeData);
// Modify node state
graph.setElementState(target.id, 'selected');
});
// Listen to edge mouse enter event
graph.on(EdgeEvent.POINTER_OVER, (evt) => {
const { target } = evt;
graph.setElementState(target.id, 'highlight');
});
// Listen to canvas drag event
graph.on(CanvasEvent.DRAG, (evt) => {
console.log('Canvas is being dragged');
});

Graph.once(eventName, callback)

Listen to an event once, and automatically remove the listener after the event is triggered once.

once<T extends IEvent = IEvent>(eventName: string, callback: (event: T) => void): this;

Parameters

ParameterDescriptionTypeDefaultRequired
eventNameName of the event to listen tostring-✓
callbackCallback function executed when the event is triggered(event: T) => void-✓

Return Value

  • Type: this (Graph instance)
  • Description: Returns the graph instance itself, supporting chain calls

Example

import { GraphEvent, NodeEvent } from '@antv/g6';
// Listen to the chart's first load completion event, executed only once
graph.once(GraphEvent.AFTER_RENDER, () => {
console.log('Chart rendered for the first time');
// Execute one-time initialization operations
highlightImportantNodes();
});
// Wait for the user to click a node for the first time and then perform operations
graph.once(NodeEvent.CLICK, (evt) => {
console.log('User clicked a node for the first time:', evt.target.id);
showTutorialTip('You can drag nodes to change their position');
});

Graph.off()

Remove all event listeners.

off(): this;

Return Value

  • Type: this (Graph instance)
  • Description: Returns the graph instance itself, supporting chain calls

Example

// Remove all event listeners
graph.off();
console.log('All event listeners have been removed');

Graph.off(eventName)

Remove all listeners of a specified event type.

off(eventName: string): this;

Parameters

ParameterDescriptionTypeDefaultRequired
eventNameName of the event to removestring-✓

Return Value

  • Type: this (Graph instance)
  • Description: Returns the graph instance itself, supporting chain calls

Example

import { NodeEvent } from '@antv/g6';
// Remove all node click event listeners
graph.off(NodeEvent.CLICK);
console.log('All node click event listeners have been removed');
// Remove related temporary event listeners after a certain operation mode ends
function exitEditMode() {
// Remove all listeners in edit mode
graph.off(NodeEvent.DRAG_END);
graph.off(NodeEvent.DROP);
console.log('Exited edit mode');
}

Graph.off(eventName, callback)

Remove a specific callback function for a specific event.

off(eventName: string, callback: (...args: any[]) => void): this;

Parameters

ParameterDescriptionTypeDefaultRequired
eventNameName of the event to removestring-✓
callbackCallback function to remove(...args: any[]) => void-✓

Return Value

  • Type: this (Graph instance)
  • Description: Returns the graph instance itself, supporting chain calls

Example

import { NodeEvent } from '@antv/g6';
// Define callback function
const handleNodeClick = (evt) => {
console.log('Node clicked:', evt.target.id);
};
// Add listener
graph.on(NodeEvent.CLICK, handleNodeClick);
// Later, remove this specific listener at a certain point
graph.off(NodeEvent.CLICK, handleNodeClick);
console.log('Specific node click event listener has been removed');

Event Constant Enums

G6 provides various event constant enums to facilitate developers in using standardized event names. Below is a detailed description of all event constants:

Node Events (NodeEvent)

Constant NameEvent NameDescription
CLICKnode:clickTriggered when a node is clicked
DBLCLICKnode:dblclickTriggered when a node is double-clicked
POINTER_OVERnode:pointeroverTriggered when the pointer enters a node
POINTER_LEAVEnode:pointerleaveTriggered when the pointer leaves a node
POINTER_ENTERnode:pointerenterTriggered when the pointer enters a node or its child elements (non-bubbling)
POINTER_MOVEnode:pointermoveTriggered when the pointer moves over a node
POINTER_OUTnode:pointeroutTriggered when the pointer leaves a node
POINTER_DOWNnode:pointerdownTriggered when the pointer is pressed down on a node
POINTER_UPnode:pointerupTriggered when the pointer is released on a node
CONTEXT_MENUnode:contextmenuTriggered when the context menu is opened on a node
DRAG_STARTnode:dragstartTriggered when dragging a node starts
DRAGnode:dragTriggered during node dragging
DRAG_ENDnode:dragendTriggered when node dragging ends
DRAG_ENTERnode:dragenterTriggered when a draggable item enters a node
DRAG_OVERnode:dragoverTriggered when a draggable item is over a node
DRAG_LEAVEnode:dragleaveTriggered when a draggable item leaves a node
DROPnode:dropTriggered when a draggable item is dropped on a node

Edge Events (EdgeEvent)

Constant NameEvent NameDescription
CLICKedge:clickTriggered when an edge is clicked
DBLCLICKedge:dblclickTriggered when an edge is double-clicked
POINTER_OVERedge:pointeroverTriggered when the pointer enters an edge
POINTER_LEAVEedge:pointerleaveTriggered when the pointer leaves an edge
POINTER_ENTERedge:pointerenterTriggered when the pointer enters an edge or its child elements (non-bubbling)
POINTER_MOVEedge:pointermoveTriggered when the pointer moves over an edge
POINTER_OUTedge:pointeroutTriggered when the pointer leaves an edge
POINTER_DOWNedge:pointerdownTriggered when the pointer is pressed down on an edge
POINTER_UPedge:pointerupTriggered when the pointer is released on an edge
CONTEXT_MENUedge:contextmenuTriggered when the context menu is opened on an edge
DRAG_ENTERedge:dragenterTriggered when a draggable item enters an edge
DRAG_OVERedge:dragoverTriggered when a draggable item is over an edge
DRAG_LEAVEedge:dragleaveTriggered when a draggable item leaves an edge
DROPedge:dropTriggered when a draggable item is dropped on an edge

Combo Events (ComboEvent)

Constant NameEvent NameDescription
CLICKcombo:clickTriggered when a Combo is clicked
DBLCLICKcombo:dblclickTriggered when a Combo is double-clicked
POINTER_OVERcombo:pointeroverTriggered when the pointer enters a Combo
POINTER_LEAVEcombo:pointerleaveTriggered when the pointer leaves a Combo
POINTER_ENTERcombo:pointerenterTriggered when the pointer enters a Combo or its child elements (non-bubbling)
POINTER_MOVEcombo:pointermoveTriggered when the pointer moves over a Combo
POINTER_OUTcombo:pointeroutTriggered when the pointer leaves a Combo
POINTER_DOWNcombo:pointerdownTriggered when the pointer is pressed down on a Combo
POINTER_UPcombo:pointerupTriggered when the pointer is released on a Combo
CONTEXT_MENUcombo:contextmenuTriggered when the context menu is opened on a Combo
DRAG_STARTcombo:dragstartTriggered when dragging a Combo starts
DRAGcombo:dragTriggered during Combo dragging
DRAG_ENDcombo:dragendTriggered when Combo dragging ends
DRAG_ENTERcombo:dragenterTriggered when a draggable item enters a Combo
DRAG_OVERcombo:dragoverTriggered when a draggable item is over a Combo
DRAG_LEAVEcombo:dragleaveTriggered when a draggable item leaves a Combo
DROPcombo:dropTriggered when a draggable item is dropped on a Combo

Canvas Events (CanvasEvent)

Constant NameEvent NameDescription
CLICKcanvas:clickTriggered when clicking on the canvas blank area
DBLCLICKcanvas:dblclickTriggered when double-clicking on the canvas blank area
POINTER_OVERcanvas:pointeroverTriggered when the pointer enters the canvas
POINTER_LEAVEcanvas:pointerleaveTriggered when the pointer leaves the canvas
POINTER_ENTERcanvas:pointerenterTriggered when the pointer enters the canvas or its child elements (non-bubbling)
POINTER_MOVEcanvas:pointermoveTriggered when the pointer moves over the canvas
POINTER_OUTcanvas:pointeroutTriggered when the pointer leaves the canvas
POINTER_DOWNcanvas:pointerdownTriggered when the pointer is pressed down on the canvas
POINTER_UPcanvas:pointerupTriggered when the pointer is released on the canvas
CONTEXT_MENUcanvas:contextmenuTriggered when the context menu is opened on the canvas
DRAG_STARTcanvas:dragstartTriggered when dragging the canvas starts
DRAGcanvas:dragTriggered during canvas dragging
DRAG_ENDcanvas:dragendTriggered when canvas dragging ends
DRAG_ENTERcanvas:dragenterTriggered when a draggable item enters the canvas
DRAG_OVERcanvas:dragoverTriggered when a draggable item is over the canvas
DRAG_LEAVEcanvas:dragleaveTriggered when a draggable item leaves the canvas
DROPcanvas:dropTriggered when a draggable item is dropped on the canvas
WHEELcanvas:wheelTriggered when scrolling the mouse wheel on the canvas

Graph Lifecycle Events (GraphEvent)

Constant NameEvent NameDescription
BEFORE_CANVAS_INITbeforecanvasinitTriggered before canvas initialization
AFTER_CANVAS_INITaftercanvasinitTriggered after canvas initialization
BEFORE_SIZE_CHANGEbeforesizechangeTriggered before viewport size change
AFTER_SIZE_CHANGEaftersizechangeTriggered after viewport size change
BEFORE_ELEMENT_CREATEbeforeelementcreateTriggered before element creation
AFTER_ELEMENT_CREATEafterelementcreateTriggered after element creation
BEFORE_ELEMENT_UPDATEbeforeelementupdateTriggered before element update
AFTER_ELEMENT_UPDATEafterelementupdateTriggered after element update
BEFORE_ELEMENT_DESTROYbeforeelementdestroyTriggered before element destruction
AFTER_ELEMENT_DESTROYafterelementdestroyTriggered after element destruction
BEFORE_ELEMENT_TRANSLATEbeforeelementtranslateTriggered before element translation
AFTER_ELEMENT_TRANSLATEafterelementtranslateTriggered after element translation
BEFORE_DRAWbeforedrawTriggered before drawing starts
AFTER_DRAWafterdrawTriggered after drawing ends
BEFORE_RENDERbeforerenderTriggered before rendering starts
AFTER_RENDERafterrenderTriggered after rendering completes
BEFORE_ANIMATEbeforeanimateTriggered before animation starts
AFTER_ANIMATEafteranimateTriggered after animation ends
BEFORE_LAYOUTbeforelayoutTriggered before layout starts
AFTER_LAYOUTafterlayoutTriggered after layout ends
BEFORE_STAGE_LAYOUTbeforestagelayoutTriggered before each stage in pipeline layout
AFTER_STAGE_LAYOUTafterstagelayoutTriggered after each stage in pipeline layout
BEFORE_TRANSFORMbeforetransformTriggered before viewport transformation
AFTER_TRANSFORMaftertransformTriggered after viewport transformation
BATCH_STARTbatchstartTriggered when batch operation starts
BATCH_ENDbatchendTriggered when batch operation ends
BEFORE_DESTROYbeforedestroyTriggered before chart destruction
AFTER_DESTROYafterdestroyTriggered after chart destruction
BEFORE_RENDERER_CHANGEbeforerendererchangeTriggered before renderer change
AFTER_RENDERER_CHANGEafterrendererchangeTriggered after renderer change

Container Events (ContainerEvent)

Constant NameEvent NameDescription
KEY_DOWNkeydownTriggered when a keyboard key is pressed down
KEY_UPkeyupTriggered when a keyboard key is released

Common Events (CommonEvent)

These are events without prefixes and can be used to listen to global events:

Constant NameEvent NameDescription
CLICKclickTriggered when any element is clicked
DBLCLICKdblclickTriggered when any element is double-clicked
POINTER_OVERpointeroverTriggered when the pointer enters any element
POINTER_LEAVEpointerleaveTriggered when the pointer leaves any element
POINTER_ENTERpointerenterTriggered when the pointer enters any element or its child elements (non-bubbling)
POINTER_MOVEpointermoveTriggered when the pointer moves over any element
POINTER_OUTpointeroutTriggered when the pointer leaves any element
POINTER_DOWNpointerdownTriggered when the pointer is pressed down on any element
POINTER_UPpointerupTriggered when the pointer is released on any element
CONTEXT_MENUcontextmenuTriggered when the context menu is opened on any element
DRAG_STARTdragstartTriggered when dragging any element starts
DRAGdragTriggered during any element dragging
DRAG_ENDdragendTriggered when any element dragging ends
DRAG_ENTERdragenterTriggered when a draggable item enters any element
DRAG_OVERdragoverTriggered when a draggable item is over any element
DRAG_LEAVEdragleaveTriggered when a draggable item leaves any element
DROPdropTriggered when a draggable item is dropped on any element
KEY_DOWNkeydownTriggered when a keyboard key is pressed down
KEY_UPkeyupTriggered when a keyboard key is released
WHEELwheelTriggered when scrolling the mouse wheel
PINCHpinchTriggered when pinching or spreading fingers on a multi-touch screen

Tips for Use

Chain Calls

G6's event API supports chain calls, allowing you to register multiple events consecutively:

import { NodeEvent, EdgeEvent, CanvasEvent } from '@antv/g6';
// Use constant enums + chain calls
graph.on(NodeEvent.CLICK, handleNodeClick).on(EdgeEvent.CLICK, handleEdgeClick).on(CanvasEvent.WHEEL, handleCanvasZoom);

Event Delegation

You can use the event bubbling mechanism to listen to all child element events on the parent element:

import { CommonEvent } from '@antv/g6';
// Handle all element click events uniformly
graph.on(CommonEvent.CLICK, (evt) => {
const { targetType, target } = evt;
if (targetType === 'node') {
console.log('Clicked on node:', target.id);
} else if (targetType === 'edge') {
console.log('Clicked on edge:', target.id);
} else {
console.log('Clicked on canvas blank area');
}
});

Event Object Properties

Most event callback functions receive an event object containing the following common properties:

  • target - The element that triggered the event
  • targetType - The type of the element that triggered the event (node/edge/combo/canvas)
  • originalTarget - The original graphic that triggered the event
  • currentTarget - The current object that triggered the event
  • originalEvent - The original browser event object

With these properties, you can precisely control interactive behavior.