!!!###!!!title=API——VisActor/VGraph tutorial documents!!!###!!!!!!###!!!description=After creating a Graph instance, you can apply the following API to flexibly build graph analysis applications.!!!###!!!

Graph Instance Methods

After creating a Graph instance, you can apply the following API to flexibly build graph analysis applications.

Data Operations

Instance MethodReturn ValueDescription
data(data: DataStructure)voidSets the data. If there is existing data, it will be replaced.
updateData(data: DataStructure)voidUpdates the data, including adding, deleting, and modifying elements, as well as their positions and properties. If there is existing data, a merge operation will be performed.
updatePositions(data?: DataStructure)voidUpdates the positions of all elements without changing their properties. Suitable for layout updates, full-graph animations, etc. If data is passed, it will only update the positions of elements based on the passed data, without adding, deleting, or modifying them.
refresh()voidIf the graph only contains nodes and edges, and the node data has been directly modified, this method can be used to further improve the performance of updating instance positions.
clear()voidClears the data.
layout(id?: string, args?: LayoutOptions)void[NEW] Refreshes the layout. You can configure an ID to fix node positions and maintain a stable viewport for an optimized experience after the layout refresh. You can pass args as configuration items for a single layout. Supports declarative layout. If you instantiate the layout configuration yourself, please use graph.set('layout', layoutInstance) to use this method.

Instance Operations

Instance MethodReturn ValueDescription
add(type: 'node' | 'edge' | 'group', configs: any)EntityAdds a single instance based on the configuration. See the configuration items for each instance for details. Returns the newly added instance.
update(entity: Node | Edge | Group, configs: any)voidUpdates the configuration of a single instance.
remove(entity: Node | Edge | Group)voidDeletes an instance. This method will also delete the corresponding data for the instance. Deleting a Group will not delete all node instances under the Group.
getNodeById(id: string)Node | undefinedGets a node instance by its ID. Returns undefined if not found.
getNodes()Node[]Gets all node instances in the Graph. Returns an array of node instances.
getEdgeById(id: string)Edge | undefinedGets an edge instance by its ID. Returns undefined if not found.
getEdges()Edge[]Gets all edge instances in the Graph. Returns an array of edge instances.
getGroupById(id: string)Group | undefinedGets a group instance by its ID. Returns undefined if not found.
getGroups()Group[]Gets all group instances in the Graph. Returns an array of group instances.
destroy()voidDestroys the Graph instance, clearing all data and removing the canvas.

Rendering Operations

By default, autoDraw = true. Calling any instance method will directly trigger a redraw, resulting in a view change. Therefore, it is recommended to disable autoDraw during batch operations for better performance.

Instance MethodReturn ValueDescription
updateRenderMode(mode: 'canvas' | 'dom')voidUpdates the rendering mode of the nodes.
autoDraw()voidDetermines whether to redraw based on the autoDraw configuration item.
draw()voidRedraws directly.
animate()stringvGraph encapsulates a series of out-of-the-box animations. See Animation for details.
stopAnimate(id?: string)voidStops the animation with the specified ID. If the ID is empty, all animations will be stopped.

Viewport Operations

Instance MethodReturn ValueDescription
changeSize(width: number, height: number)voidAdjusts the size of the graph.
fitView()voidScales and translates the content of the graph according to the configured width and height, and centers the entire graph. Suitable for viewing the entire graph structure. Note: If minRatio and maxRatio are configured, this range will not be exceeded.
alignView(align: 'lt' | 'ct' | 'rt' | 'lc' | 'cc' | 'rc' | 'lb' | 'cb' | 'rb')voidMaintains the current zoom ratio and aligns the content of the graph with the specified direction. l: left; c: center; r: right; t: top; b: bottom. The default is lt.
focus(entity: Node | Edge | Group, animate?: boolean | { duration?: number, easing?: string})voidAccepts a node, edge, or group instance as an input parameter and moves the current viewport to the center of the instance. The move animation can be configured. When focus(node, true) is executed, the default animation effect is performed. The animation duration and effect can be overwritten.
getViewCenter()voidGets the center of the current viewport, which is related to width, height, and padding.
getViewPadding()number | number[]Gets the padding value of the current Graph's edge.
translate(x: number, y: number)voidTranslates the viewport horizontally (x) and vertically (y).
scale(ratio: number, center?: number[])voidScales the viewport, relative to the current zoom ratio. For example, if the current zoom ratio is 0.5, after graph.scale(2), the zoom ratio will be 1. By default, it scales from the viewport origin (0, 0). You can specify the scaling center center [x, y].
setZoomRatio(ratio: number)voidScales the viewport from the center of the canvas. When ratio > 1, it zooms in; when ratio < 1, it zooms out. This is an absolute zoom ratio. For example, if the current zoom ratio is 0.5, after graph.setZoomRatio(2), the zoom ratio will be 2.
getZoomRatio()numberGets the current zoom size of the Graph.

Coordinate Transformation

Generally, three types of coordinates are converted during graph analysis:

  • client: Window coordinates, with the top-left corner of the browser display area as (0, 0).
  • viewport: Graph visible area coordinates, with the top-left corner of the graph container as (0, 0).
  • canvas: Actual canvas coordinates. When there are no scaling, translation, or other operations, it is consistent with the viewport coordinates.

vGraph provides convenient coordinate conversion methods, which are usually used to determine the clicked canvas position in the event handler, or to position the dom based on the instance's position.

Instance MethodReturn ValueDescription
clientToViewport(clientX: number, clientY: number){x: number, y: number}Converts window coordinates to graph visible area viewport coordinates.
clientToCanvas(clientX: number, clientY: number){x: number, y: number}Converts window coordinates to canvas coordinates.
canvasToViewport(x: number, y: number){x: number, y: number}Converts canvas coordinates to visible area viewport coordinates.
viewportToCanvas(vx: number, vy: number){x: number, y: number}Converts visible area viewport coordinates to canvas coordinates.

Configuration Operations

Instance MethodReturn ValueDescription
get(key:string)anyGets the current graph configuration item information.
set(options: {[key: string]: any})voidSets the current graph configuration item. Note: This operation will not trigger automatic redrawing. You need to manually call graph.draw().

Interaction Operations

Graph interactions are generally achieved by listening to multiple events and giving corresponding responses. vgraph has built-in a series of common interactions. See the Behaviors section for details.

Instance MethodReturn ValueDescription
addBehavior(Behavior, Options)voidEnables an interaction based on the configuration item options.
enableBehavior(type: string)voidEnables the specified interaction.
disableBehavior(type: string)voidDisables the specified interaction.
getBehavior(type: string)voidGets the interaction configuration based on the interaction type.
removeBehavior(type: string)voidDisables and removes an interaction.
handleEvent(e: MouseEvent | TouchEvent)voidTriggers a native event on the graph. This is mostly used in scenarios where you need elements in the graph to respond when you drag a DOM element into the graph.

Export Operations

Instance MethodReturn ValueDescription
getData(fn?:() => entityData)DataStructureExports the current data in the graph. You can pass a function to filter the data to be exported.
downloadImage(name?: string, maxWidth?: number, maxHeight?: number, onDownloadFinish?: () => void, backgroundColor?: string)voidExports an image. The image size is maxWidth * maxHeight, which defaults to the actual size of the graph. onDownloadFinish is the callback for when the export is complete. backgroundColor is the custom background color of the graph, which is transparent by default.
downloadImageWithImageCheck({name?: string, maxWidth?: number, maxHeight?: number, maxWaitTime?: number, onDownloadFinish?: (failImageUrls: string[]) => void, backgroundColor?: string})voidWaits for the images in the graph to finish loading before exporting the image, waiting for a maximum of maxWaitTime ms. The image size is maxWidth * maxHeight, which defaults to the actual size of the graph. onDownloadFinish is the callback for when the export is complete. backgroundColor is the custom background color of the graph, which is transparent by default.

Note downloadImage is only for canvas nodes. For graphs with react and vue nodes, vGraph provides the utility method resizeToExport to be used with third-party dom-to-canvas libraries for exporting.

import domtocanvas from 'third-party-export-lib';
import { Graph, resizeToExport } from '@visactor/vgraph';

const graph = new Graph({ container, ....});
// Viewport information before exporting
const { matrix } = resizeToExport(graph);
// After the view is re-rendered
onDomRendered(() => {
  domtocanvas(container);
  // Restore the viewport information before exporting
  graph.setMatrix(matrix);
  // Restore the original graph size
  graph.changeSize(containerWidth, containerHeight);
});