Events
Look up when graph events fire and what their handlers receive.
Use graph.on() to listen for events. See the Graph API for listener methods and the event guide for interactive examples.
Element events
Click and pointer events
| Action | Node | Edge | Combo | Canvas |
|---|---|---|---|---|
| Click the target. | node:click | edge:click | combo:click | canvas:click |
| Double-click the target. | node:dblclick | edge:dblclick | combo:dblclick | canvas:dblclick |
| Right-click the target. The browser’s default menu is suppressed. | node:contextmenu | edge:contextmenu | combo:contextmenu | canvas:contextmenu |
| Press the mouse button on the target. | node:pointerdown | edge:pointerdown | combo:pointerdown | canvas:pointerdown |
| Release the mouse button in the window. The canvas receives the event. | — | — | — | canvas:pointerup |
| Move into the target. | node:pointerover | edge:pointerover | combo:pointerover | — |
Fires immediately after pointerover. | node:pointerenter | edge:pointerenter | combo:pointerenter | — |
| Move the pointer over the target. | node:pointermove | edge:pointermove | combo:pointermove | canvas:pointermove |
| Leave the target. Coordinates report the pointer position on exit. | node:pointerleave | edge:pointerleave | combo:pointerleave | — |
Scroll over the target. Read nativeEvent.deltaY for the scroll amount. | node:wheel | edge:wheel | — | canvas:wheel |
Callback fields
| Field | Meaning |
|---|---|
targetType / targetId | Target type and element ID. Nodes, edges, and Combos have a targetId. |
nodeId / edgeId / comboId | The target element’s ID. Edge events also supply edgeIndex, source, and target. |
data | Optional business data attached to the node or edge. |
client | CSS pixel coordinates relative to the browser viewport. |
canvas | CSS pixel coordinates relative to the top-left of the canvas. |
graph | Graph coordinates for positioning within the graph. |
nativeEvent | A MouseEvent for mouse input or a WheelEvent for scrolling. |
On a node click, read the target, business data, and coordinates. Use the original mouse event to check whether Shift was held.
graph.on('node:click', (event) => {
console.log({
target: { type: event.targetType, id: event.targetId },
data: event.data,
client: event.client,
canvas: event.canvas,
graph: event.graph,
shiftKey: event.nativeEvent.shiftKey,
});
});Picking and gestures
Hit testing checks nodes, edges, Combos, then the canvas. When the pointer changes targets, the old target receives pointerleave, followed by pointerover, pointerenter, and pointermove on the new target. Over a Combo background, canvas:pointermove also fires and the wheel emits canvas:wheel. Dragging can suppress the following click.
Selection events
Enable click-select, brush-select, or lasso-select to receive the corresponding events. They fire after selection state is written.
| Action | Node | Edge | Selection set |
|---|---|---|---|
| Toggle selection. | node:selectchange | edge:selectchange | — |
| Clear a nonempty selection. | — | — | selection:clear |
| Hit nodes during brush selection, with throttling. | brush:select | — | — |
| Finish brush selection. The result can be empty. | brush:end | — | — |
| Hit nodes during lasso selection. | lasso:select | — | — |
| Finish lasso selection. The result can be empty. | lasso:end | — | — |
Callback fields
| Field | Meaning |
|---|---|
nodeId / edgeId | The ID of the element whose selection changed. Edge events also provide edgeIndex, source, and target. |
selected | The updated selection state after a toggle. |
ids | IDs cleared by selection:clear, which can include nodes and edges. |
nodeIds | Node IDs reported by brush and lasso events. brush:end returns all currently selected nodes when the gesture ends. |
event | The original MouseEvent when toggling or clearing selection. |
With brush-select enabled, listen for brush:end to read the selected nodes when the gesture ends.
graph.on('brush:end', ({ nodeIds }) => {
console.log('Selected nodes', nodeIds);
});Lasso events report the nodes hit by the current gesture. Use findNodesByState('selected') and findEdgesByState('selected') for the full graph selection. Selection and drag guide
Drag events
drag-node handles node and Combo drags. drag-edge handles endpoint drags.
| Action | Node | Edge | Combo |
|---|---|---|---|
| Start dragging. Nodes and Combos must move more than 3 CSS pixels. Edges start on primary-button press. | node:dragstart | edge:dragstart | combo:dragstart |
| Drag in progress. Node and Combo events fire after the node or Combo member positions are updated. | node:drag | edge:drag | combo:drag |
| Release the mouse button to end the drag. | node:dragend | edge:dragend | combo:dragend |
Callback fields
| Field | Meaning |
|---|---|
nodeId / nodeIds | The first dragged node ID and the full list of dragged node IDs in node drag events. |
comboId | The dragged Combo’s ID in Combo drag events. |
dx / dy | Cumulative displacement from the start, in graph coordinates, in node:drag and combo:drag. |
edgeId / edgeIndex | The edge ID and index in edge drag events. edgeId is optional. |
source / target | Endpoint node IDs. On a valid drop, edge:dragend returns proposed endpoints for the application to validate and apply. |
dragEnd | The endpoint being dragged in edge:dragstart and edge:drag (source or target). The closer endpoint is chosen, with target used on a tie. |
originalSource / originalTarget | Endpoint IDs before the drag, reported by edge:dragend. |
reconnected / newEndpoint | Whether the drop hit a valid target and, when present, its node ID in edge:dragend. |
event | The original MouseEvent. Node and Combo dragend events provide only the drag result. |
At the end of the drag, use nodeIds to look up the positions of all dragged nodes.
graph.on('node:dragend', ({ nodeIds }) => {
const positions = nodeIds.map((id) => graph.getElementPosition(id));
console.log(positions);
});Lifecycle and data events
Use these events to track layout, drawing, and data changes. Rendering, layout, and data updates
Layout and rendering
| Event | When it fires | Callback fields |
|---|---|---|
beforelayout | Before computation when a layout is configured and nodes are present. | nodeCount: number |
layout:start | Immediately after beforelayout, before computation. | nodeCount: number |
layout:end | After a valid computation, before positions are written back. duration is in milliseconds. | duration: number, nodeCount: number |
afterlayout | Immediately after layout:end. Await graph.layout() to read final positions. | duration: number, nodeCount: number |
beforerender | During an explicit render(), before fitting the viewport and drawing. | nodeCount: number, edgeCount: number |
afterrender | After the synchronous drawing step of an explicit render(). Await graph.render() before the first mutation. | nodeCount: number, edgeCount: number |
resize | After resizing the canvas and applying any configured fitting. Dimensions are in CSS pixels. | width: number, height: number |
destroy | When the instance is first destroyed, before canvas and listener cleanup. | No arguments |
render(), layout(), and setData() emit layout events when they run a layout. A cancelled layout may skip its end events.
Register a one-time afterrender listener before the first render(). Use datachange to read the node and edge counts after each data replacement.
graph.once('afterrender', ({ nodeCount, edgeCount }) => {
console.log('First draw', nodeCount, edgeCount);
});
await graph.render();
graph.on('datachange', ({ nodeCount, edgeCount }) => {
console.log('Data replaced', nodeCount, edgeCount);
});
await graph.setData({
nodes: [{ id: 'A', x: 0, y: 0 }, { id: 'B', x: 120, y: 0 }],
edges: [{ source: 'A', target: 'B' }],
});Data updates and Combos
| Event | When it fires | Callback fields |
|---|---|---|
datachange | After setData() replaces the full dataset. | nodeCount: number, edgeCount: number |
afteradditem | After adding a node or edge, once per item. | type: 'node', id: string or type: 'edge', id: string, source: string, target: string |
afterremoveitem | For each ID passed to remove a node or edge, including missing IDs. Edges removed along with a node have no separate event. | type (node / edge), id: string |
afterupdateitem | After a node position, node data, or edge data update, once per item. updateNodePosition() also emits for a missing node or unchanged coordinates. | type (node / edge), id: string, action (position or data for nodes, data for edges) |
combo:collapse | After a Combo changes to the collapsed state. | comboId: string |
combo:expand | After a Combo changes to the expanded state. | comboId: string |
EventEmitter
Use EventEmitter to send custom events between UI components. Pass a rendered graph, the toolbar’s Reset view button, and a status element to bindResetView(). Clicking the button restores the initial view and updates the status. Call the returned cleanup function when the page unmounts.
import { EventEmitter, type Graph } from '@antv/g6-lite';
export function bindResetView(graph: Graph, button: HTMLButtonElement, status: HTMLElement) {
const events = new EventEmitter();
const resetCamera = () => graph.resetCamera();
const showStatus = () => {
status.textContent = 'View reset';
};
const onClick = () => events.emit('view:reset');
events.on('view:reset', resetCamera);
events.on('view:reset', showStatus);
button.addEventListener('click', onClick);
return () => {
button.removeEventListener('click', onClick);
events.off('view:reset', resetCamera);
events.off('view:reset', showStatus);
};
}