Skip to content

Miscellaneous

Get the content as SVG data

You can obtain the displayed content as SVG text data.
In this example, the SVG data is downloaded.

The above example saves the XML data as it is drawn as SVG.
If the SVG contains image elements (<image /> tag), the URLs specified will be still included. If relative URLs are specified, it would be no longer valid.
As measures to address this problem, by passing the { embedImages: true } argument to the exportAsSvgText() function, the URLs in the <image /> elements can be converted to the data-url(base64) format and embed them in the SVG document. However, whether or not this format can be displayed depends on the software that opens the exported SVG file. (Google Chrome can display it).

Below is an example of exporting with embedded images. An example using images also shown in Appearance Customization.

TIP

Performing exportAsSvgData() instead of exportAsSvgText() will get a DOM object. After manipulating the DOM (e.g., removing unwanted elements or rewriting attributes), you can retrieve the SVG string from outerHTML.

Grid

Coordinates translation

Mutual coordinate translation between the DOM and SVG is provided by the methods of the v-network-graph component.

As an example of DOM to SVG coordinate translation, the process of adding a new node at the position clicked by the user is shown below.

For an example of SVG to DOM coordinate translation, please see the Tooltip example in Event Handling section.

Centering on Load

This example pans centered on the specified coordinates when loaded.
By default, the content is centered according to the content on load. If you want to pan to any position on load, disable this with the configs.view.autoPanAndZoomOnLoad config.

Auto panning on resize

By default, it automatically pans to keep the display area centered when it is resized. To disable this behavior, set configs.view.autoPanOnResize to false.

Get and set the viewing area

The coordinates of the currently displayed area can be get and set. When setting, the zoom and pan to display the specified area will be automatically calculated.
If the aspect ratio of the area to be set differs from that of the <v-network-graph> element size on the screen, the position will be adjusted so that the center position remains the same.

By using this function, the display position at a certain point in time can be restored later.

Specify function in view config

The library does not provide an interface to specify a function for the value of configs.view.*. However, it is possible to specify a function by using getter of JavaScript.

In the following example, the configuration specifies a function that changes whether panning/zooming is enabled depending on the the state of shift/ctrl key presses.

Node expand/collapse

For the implementation of node expand and collapse, the structure of node relationships is a tree structure, which is a type of graph structure. This means that the parent-child relationship needs to be included in the data, and there are restrictions such as not circulating edges in the part of tree structure.
To keep the generality and simplicity of the library, please handle these things on the application.

The following is an example of creating a partially tree-structured graph and passing it to v-network-graph so that the parent node can collapse its child nodes.

Find the shortest path

v-network-graph is intended for drawing graphs, and does not contain any code for graph algorithms. On the other hand, it can be used with any graph algorithm.
The following is an example of drawing the shortest path using Dijkstra's algorithm. The edge label indicates the cost of that edge. v-network-graph's path drawing is directed by a list of edges, so it is converted from a list of nodes to a list of edges at the end.