!!!###!!!title=Events——VisActor/VGraph tutorial documents!!!###!!!!!!###!!!description=vgraph has three granularities of events to meet the interaction needs of different scenarios. They are:- Graph level: Corresponds to Graph and TreeGraph events.- Instance level: Corresponds to events for Node, Edge, and Group instances.- Graphic level: Corresponds to basic graphic object events that visually compose each type of instance.Events will bubble up from the graphic to the instance and then to the graph. For example, if a user clicks on an icon on a node, the following will be triggered in order:- The icon's click event- The node's click event- The graph's node:click event- The graph's click event<p><img src="/vgraph/guide/event.png" width="400"></p>The vgraph event object is composed as follows:```typescriptconst e = { // Event type, e.g., click, mouseenter type: string; // The target object of the action, often corresponding to the event granularity mentioned above target: Graph | Node | Edge | Group | Shape; // The node related to the event's target object, which is the graphic object that actually triggered the event. Exists only in graph events and instance events. relatedTarget: Shape; // The horizontal x-coordinate and vertical y-coordinate of the mouse pointer relative to the top-left corner of the browser viewport when the event is triggered clientX: number; clientY: number; // The native browser event nativeEvent: Event; // Stops event bubbling stopPropagation(): void;};```!!!###!!!

Events

vgraph has three granularities of events to meet the interaction needs of different scenarios. They are:

  • Graph level: Corresponds to Graph and TreeGraph events.
  • Instance level: Corresponds to events for Node, Edge, and Group instances.
  • Graphic level: Corresponds to basic graphic object events that visually compose each type of instance.

Events will bubble up from the graphic to the instance and then to the graph. For example, if a user clicks on an icon on a node, the following will be triggered in order:

  • The icon's click event
  • The node's click event
  • The graph's node:click event
  • The graph's click event

The vgraph event object is composed as follows:

const e = {
    // Event type, e.g., click, mouseenter
    type: string;
    // The target object of the action, often corresponding to the event granularity mentioned above
    target: Graph |  Node | Edge | Group | Shape;
    // The node related to the event's target object, which is the graphic object that actually triggered the event. Exists only in graph events and instance events.
    relatedTarget: Shape;
    // The horizontal x-coordinate and vertical y-coordinate of the mouse pointer relative to the top-left corner of the browser viewport when the event is triggered
    clientX: number;
    clientY: number;
    // The native browser event
    nativeEvent: Event;
    // Stops event bubbling
    stopPropagation(): void;
};

graph Events

The most commonly used are graph-level events. On the graph, you can listen for and react to instance events by category, supporting native event types on nodes, edges, and groups. The event format is ${entity.type}:${eventType}. For example, a node click event is defined as graph.on('node:click', eventHandler). The currently supported event types are:

  • click
  • dblclick
  • mouseenter
  • mouseleave
  • mouseover
  • mouseout
  • mousedown
  • mouseup
  • mousemove
  • contextmenu

Due to the many needs for interaction with the blank area of the canvas in actual business, graph has also designed events for blank area interaction. For example, to listen for a click event on a blank area, it is recommended to use graph.on('canvas:click', callback). At the same time, as the top-level container of the graph, the graph can also directly listen to atomic events, which will be triggered whenever the corresponding event occurs anywhere in the graph. At this point, you can use e.relatedTarget to distinguish the object that triggered the event.

In addition, the graph instance also supports interaction-level events. vGraph exports the constant GRAPH_EVENTS for easy reference. The specific events are listed below:

Event NameConstantDescription
changeGRAPH_EVENTS.CHANGEA general event for data changes in the graph, including adding, deleting, and modifying elements, layout, etc.
transformedGRAPH_EVENTS.TRANSFORMEDEvent for changes in the graph's viewport.
changesizeGRAPH_EVENTS.CHANGE_SIZEEvent for updating the canvas size.
add:startGRAPH_EVENTS.ADD_STARTData is about to be added.
add:endGRAPH_EVENTS.ADD_ENDData addition is complete.
update:startGRAPH_EVENTS.UPDATE_STARTData is about to be updated.
update:endGRAPH_EVENTS.UPDATE_ENDData update is complete.
visibility:endGRAPH_EVENTS.VISIBILITY_ENDThe visibility of a graphic element has changed.
remove:startGRAPH_EVENTS.REMOVE_STARTData is about to be deleted.
remove:endGRAPH_EVENTS.REMOVE_ENDData deletion is complete.
state:startGRAPH_EVENTS.STATE_STARTThe state of a graphic element is about to change.
state:endGRAPH_EVENTS.STATE_ENDThe state of a graphic element has been updated.
state:startGRAPH_EVENTS.STATE_STARTThe state of a graphic element is about to change, used for actively updating the state via setState.
state:endGRAPH_EVENTS.STATE_ENDThe state of a graphic element has been updated, used for actively updating the state via setState.
batchstate:startGRAPH_EVENTS.BATCH_STATE_STARTThe state of a graphic element is about to change, used for listening to batch scenarios in editing scenes, and will not trigger state:start at the same time.
batchstate:endGRAPH_EVENTS.BATCH_STATE_ENDThe state of a graphic element has been updated, used for listening to batch scenarios in editing scenes, and will not trigger state:end at the same time.
layout:startGRAPH_EVENTS.LAYOUT_STARTAbout to re-layout.
layout:endGRAPH_EVENTS.LAYOUT_ENDLayout is complete.
data:startGRAPH_EVENTS.DATA_STARTAbout to switch to new data.
data:endGRAPH_EVENTS.DATA_ENDSwitched to new data.
updatedata:startGRAPH_EVENTS.UPDATE_DATA_STARTAbout to update the graph with new data.
updatedata:endGRAPH_EVENTS.UPDATE_DATA_ENDGraph update with new data is complete.
clear:startGRAPH_EVENTS.CLEAR_STARTAbout to clear data.
clear:endGRAPH_EVENTS.CLEAR_ENDData has been cleared.
animationframeGRAPH_EVENTS.ANIMATION_FRAMEAnimation frame refresh.
animation:startGRAPH_EVENTS.ANIMATION_STARTStart animation.
animation:endGRAPH_EVENTS.ANIMATION_ENDAll current animations are complete.
draw:startGRAPH_EVENTS.DRAW_STARTAbout to re-render.
draw:endGRAPH_EVENTS.DRAW_ENDRendering complete event.

Instance Events

Instance-level events involve listening to a single instance and reacting to it. For example, node.on('click', callback). A click event on a node will bubble up to the graph, so the node:click event on the graph will also be triggered subsequently. This is recommended for special operations on a single node.

Graphic Events

Graphic-level events are mostly used for custom interactions of personalized graphics for nodes, lines, and groups. For example, in the bubbling example above, sometimes you don't want a click on an icon to trigger a node-level click event. In such cases, you can listen to the icon event to handle it.