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

ActionNodeEdgeComboCanvas
Click the target.node:clickedge:clickcombo:clickcanvas:click
Double-click the target.node:dblclickedge:dblclickcombo:dblclickcanvas:dblclick
Right-click the target. The browser’s default menu is suppressed.node:contextmenuedge:contextmenucombo:contextmenucanvas:contextmenu
Press the mouse button on the target.node:pointerdownedge:pointerdowncombo:pointerdowncanvas:pointerdown
Release the mouse button in the window. The canvas receives the event.———canvas:pointerup
Move into the target.node:pointeroveredge:pointerovercombo:pointerover—
Fires immediately after pointerover.node:pointerenteredge:pointerentercombo:pointerenter—
Move the pointer over the target.node:pointermoveedge:pointermovecombo:pointermovecanvas:pointermove
Leave the target. Coordinates report the pointer position on exit.node:pointerleaveedge:pointerleavecombo:pointerleave—
Scroll over the target. Read nativeEvent.deltaY for the scroll amount.node:wheeledge:wheel—canvas:wheel

Callback fields

FieldMeaning
targetType / targetIdTarget type and element ID. Nodes, edges, and Combos have a targetId.
nodeId / edgeId / comboIdThe target element’s ID. Edge events also supply edgeIndex, source, and target.
dataOptional business data attached to the node or edge.
clientCSS pixel coordinates relative to the browser viewport.
canvasCSS pixel coordinates relative to the top-left of the canvas.
graphGraph coordinates for positioning within the graph.
nativeEventA 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.

ActionNodeEdgeSelection set
Toggle selection.node:selectchangeedge: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

FieldMeaning
nodeId / edgeIdThe ID of the element whose selection changed. Edge events also provide edgeIndex, source, and target.
selectedThe updated selection state after a toggle.
idsIDs cleared by selection:clear, which can include nodes and edges.
nodeIdsNode IDs reported by brush and lasso events. brush:end returns all currently selected nodes when the gesture ends.
eventThe 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.

ActionNodeEdgeCombo
Start dragging. Nodes and Combos must move more than 3 CSS pixels. Edges start on primary-button press.node:dragstartedge:dragstartcombo:dragstart
Drag in progress. Node and Combo events fire after the node or Combo member positions are updated.node:dragedge:dragcombo:drag
Release the mouse button to end the drag.node:dragendedge:dragendcombo:dragend

Callback fields

FieldMeaning
nodeId / nodeIdsThe first dragged node ID and the full list of dragged node IDs in node drag events.
comboIdThe dragged Combo’s ID in Combo drag events.
dx / dyCumulative displacement from the start, in graph coordinates, in node:drag and combo:drag.
edgeId / edgeIndexThe edge ID and index in edge drag events. edgeId is optional.
source / targetEndpoint node IDs. On a valid drop, edge:dragend returns proposed endpoints for the application to validate and apply.
dragEndThe endpoint being dragged in edge:dragstart and edge:drag (source or target). The closer endpoint is chosen, with target used on a tie.
originalSource / originalTargetEndpoint IDs before the drag, reported by edge:dragend.
reconnected / newEndpointWhether the drop hit a valid target and, when present, its node ID in edge:dragend.
eventThe 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

EventWhen it firesCallback fields
beforelayoutBefore computation when a layout is configured and nodes are present.nodeCount: number
layout:startImmediately after beforelayout, before computation.nodeCount: number
layout:endAfter a valid computation, before positions are written back. duration is in milliseconds.duration: number, nodeCount: number
afterlayoutImmediately after layout:end. Await graph.layout() to read final positions.duration: number, nodeCount: number
beforerenderDuring an explicit render(), before fitting the viewport and drawing.nodeCount: number, edgeCount: number
afterrenderAfter the synchronous drawing step of an explicit render(). Await graph.render() before the first mutation.nodeCount: number, edgeCount: number
resizeAfter resizing the canvas and applying any configured fitting. Dimensions are in CSS pixels.width: number, height: number
destroyWhen 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

EventWhen it firesCallback fields
datachangeAfter setData() replaces the full dataset.nodeCount: number, edgeCount: number
afteradditemAfter adding a node or edge, once per item.type: 'node', id: string or type: 'edge', id: string, source: string, target: string
afterremoveitemFor 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
afterupdateitemAfter 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:collapseAfter a Combo changes to the collapsed state.comboId: string
combo:expandAfter 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);
  };
}