Skip to content

Operations on Graphs ​

This page shows some examples of user operations on network graphs.

Select nodes ​

Set configs.node.selectable to true to make the node selectable by clicking or touching. If you specify a number instead of true, it will be taken as the maximum number of selectable nodes.
Specify 2-way binding for selected-nodes prop to get an array of node IDs selected. It is also possible to control the selection state on the network graph by setting a value to the same prop.

TIP

You can also select nodes by dragging a rectangle around them. See below for details.

Select edges ​

Set configs.edge.selectable to true to make the edge selectable by clicking or touching. If you specify a number instead of true, it will be taken as the maximum number of selectable edges.
Specify 2-way binding for selected-edges prop to get an array of edge IDs selected. It is also possible to control the selection state on the network graph by setting a value to the same prop.

Add/remove network elements ​

Adding/removing nodes and edges is automatically reflected by changing the reactive objects specified in nodes prop and edges prop respectively.
The newly added node will be placed in the center of the view area.
Deleting a node with edges will automatically delete the associated edges as well.
Multiple edges can be placed between the same two nodes.

These changes are tracked through Vue's reactivity. If you keep the data non-reactive for performance, for example with markRaw(), a change made in place is not tracked, and the graph may not show it or show only part of it. Assign a new object instead, for example with nodes held in a shallowRef:

js
nodes.value = markRaw({ ...nodes.value, [nodeId]: node })

Specify selectable/draggable for each node ​

configs.node.selectable and configs.node.draggable can specify a function that takes a node object as an argument and returns a boolean. By the return value, it is possible to specify the selectability/draggability of each node.

Specify selectable for each edge ​

configs.edge.selectable can specify a function that takes a edge object as an argument and returns a boolean. By the return value, it is possible to specify the selectability of each edge.

Summarize edges between the same nodes ​

If there are too many edges between nodes to be displayed, they are automatically summarized into a single line and displayed. The summarized edges will be displayed with the number of edges. Note: This feature is still under development, so it is not yet possible to select individual summarized edges.

To control whether edges are summarized, set configs.edge.summarize to a boolean or to a function that returns one.

Select nodes at once with a rectangle ​

You can select all nodes inside a dragged rectangle at once. Since dragging the background normally pans the view, this works in a separate mode called "box-selection" mode.

In the following example, clicking the "Start to select" button starts the "box-selection" mode. Releasing the pointer after dragging (pointerup) or pressing the Escape key exits the mode, and pointer operations work as usual again. If "Start with Ctrl/Cmd key" is checked, pressing the Cmd key on a Mac or Ctrl key otherwise will automatically start "box-selection" mode.

In addition, the appearance of rectangle can be customized in the configuration.

Select nodes with a rectangle (Consecutive selection) ​

In the example above, the "box-selection" mode was automatically exited when the user finished dragging, but in the example below, the mode continues until the user exits it explicitly.

"box-selection" mode exit operation:

  • Click on the view area
  • Press the Escape key
  • Call stopBoxSelection() which is <v-network-graph> instance method

In addition, in the example below, holding down the Shift key while dragging inverts the selection state of the nodes within the rectangle (i.e., it selects those that are not selected and deselects those that are selected).

TIP

If you do not want to exit "box-selection" mode even by clicking or pressing the Escape key, specify { stop: "manual" } to the startBoxSelection() method. The mode then exits only when you call the stopBoxSelection() method.