Skip to content

Event Handling

You can capture events that occur in v-network-graph by specifying the events and functions to be captured in the event-handlers attribute.

For a list of events that can be captured, see here.

Capture all events

If you specify "*" as the event type, all available events will be captured.

N1N2N3
06:39:04.996 view:pan {"x":582,"y":30}
06:39:04.995 view:load

Handle user clicking on a node

The following is an example of specifying a handler that catches only the specified events.
In this example, the color of the badge changes when the node is clicked.

Node 1Node 2Node 3Node 4

TIP

The handler for mouse events such as click are also passed a raw event object. By using this event object, it is possible to check if a modifier key (Ctrl, Alt, etc.) is pressed or not.

ts
import { EventHandlers } from "v-network-graph"
const eventHandlers: EventHandlers = {
  "node:click": ({ node, event }) => {
    if (event.ctrlKey) {
      // ...
    }
  }
}

Context menu

v-network-graph does not have the ability to show a context menu, but it can capture the right-clicking event ("contextmenu" event) on objects of the network graph. From this event, a standard Event object is obtained. You can combine your own components or other libraries to display the menu.

10Node 1Node 2Node 3Node 4
Menu for the background
Menu for the nodes
Menu for the edges
Right-click on a node, edge, or background to pop up a context menu.

Tooltip

By combining event handling and coordinate translation, it is possible to represent a combination of DOM elements such as tooltip.

Hover the mouse over a node to display a tooltip.

Even though there is no edge position stored in Layout, we can still create tooltip for edges by using the node positions.

Hover the mouse over an edge to display a tooltip.

Keyboard events

v-network-graph does not support key events, but they can be obtained using the standard HTML/Vue ways.
Below is an example of deleting a selected node/edge using the keyup event of delete key.

Node 1Node 2Node 3Node 4

Set raw event handlers to the node elements

This section shows how to set raw event handlers to a node when the event mechanism with event-handlers is insufficient.

The handling process of mousedown/up events in event-handlers is implemented with consideration to avoid situations where only one of node:pointerdown and node:pointerup can occur. This means that node:pointerup occurs only for the node where node:pointerdown occurs, and conversely, node:pointerup occurs for a node where node:pointerdown occurs even if the mouse button is released on another element in the browser.
This behavior may not be desirable, for example, if you want to be able to drag a node to specify a start point to an end point.

In such cases, it is possible to expose Node's visual elements in the following way, so please specify directly the own handler you want to set.

N1N2N3
Disabled dragging of background and nodes so that mouse down/up events can be tested inside/outside the node.