Usage guidesInteraction

Interaction overview

Browse built-in behaviors, configure and switch them, and listen for graph events.

What is a behavior?

A behavior defines how the graph responds to user input. For example, drag empty space to pan, use the wheel to zoom, or click a node to select it.

Built-in behaviors

Add built-in behaviors directly to behaviors. Open a behavior to see its options and example.

CategoryBehaviorTypeDescription
NavigationDrag canvasdrag-canvasDrag empty space to pan the graph.
NavigationZoom canvaszoom-canvasUse the mouse wheel to zoom around the canvas center.
SelectionClick to selectclick-selectClick a node or edge to toggle selection. Click empty canvas to clear it.
SelectionBrush selectbrush-selectDrag a rectangle to select nodes whose centers fall inside it.
SelectionLasso selectlasso-selectTrace a freehand outline to select nodes whose centers fall inside it.
EditingDrag nodes and combosdrag-nodeDrag a node, or drag empty space within a combo to move the entire combo.
EditingDrag edge endpointsdrag-edgeDrag an endpoint of an existing edge and use the drop event to read the proposed connection.
ExplorationHighlight on node hoverhighlight-nodeHover a node to emphasize it, its immediate neighbors, and their connecting edges.
ExplorationHighlight on edge hoverhighlight-edgeHover an edge to emphasize it and its two endpoints.
ExplorationHighlight neighborshover-highlightHighlight a node’s neighborhood or an edge and its endpoints on hover.
ExplorationTooltiptooltipShow details for a node or edge on hover or click.

Custom behaviors

Wrap reusable event handlers in a custom behavior, then register it and enable it by name. See custom behaviors

Configuration and usage

Basic configuration

Pass a behaviors array to new Graph(). A name uses the default settings. Omitting behaviors enables wheel zoom and canvas panning.

import { Graph } from '@antv/g6-lite';

const graph = new Graph({
  container,
  data,
  behaviors: ['drag-canvas', 'zoom-canvas', 'click-select'],
});

await graph.render();

Configure behavior options

Set the behavior name in type and put its options alongside it. In the example, click to select, Shift-click to add, or drag from empty space to select a rectangle.

Loading example…
Drag empty space to brush; Shift-click to add; drag a selected node to move the selection; click empty space to clear
const behaviors = [
  { type: 'zoom-canvas', minZoom: 0.5, maxZoom: 2 },
  'drag-node',
  { type: 'click-select', multiple: true, trigger: 'shift' },
  { type: 'brush-select', key: null },
] satisfies Array<BehaviorName | BuiltInBehaviorConfig>;

Update behaviors at runtime

Use setBehaviors() to replace the full list at runtime. Include every behavior you want to enable.

graph.setBehaviors(['drag-canvas', { type: 'zoom-canvas', sensitivity: 0.5 }]);

Try switching behaviors

Remove behaviors

Pass an empty array to disable all behaviors. Omit the argument to restore default panning and zooming.

graph.setBehaviors([]);

Behaviors and events

Use graph.on() to listen for node:click when a click should update your application UI. Unsubscribe with the same handler on unmount.

const onNodeClick = ({ nodeId }: { nodeId: string }) => console.log(nodeId);
graph.on('node:click', onNodeClick);

// On component unmount
graph.off('node:click', onNodeClick);

Common behavior options · Event guide