Network diagrams are a staple in the data visualization world when you want to show how one thing relates to another. They are indispensable in the world of data modeling where you need to show class relationships. They are also used extensively in the realm of semantics and linked open data as they can also show how data is clustered thematically.

There are a number of network diagram visualization tools, including those part of the d3.js suite. But perhaps the easiest toolset to get up and running with is the visJS.org package. This Vis.js library makes use of the Canvas api and is optimized for both speed of rendering and level of interactivity.

The core network library can be downloaded from the visJS.org site along with a default CSS file that controls element styles.

Explore Web Development Training Courses

This in turn exposes the base vis object, along with its dependent vis.Network class. Typically, data is collected via arrays of objects along with a configuration object (options) that controls the output. This also takes, as an argument, a div element that can be resized to specify the display area of the network graph itself.

A network at its core consists of two entities – a node, which represents a thing, and an edge or vector, which represents a relationship between two nodes (or things). Nodes are represented as arrays of node objects, edges as arrays of edge objects, with an edge only showing up if it connects two nodes. This means that the most trivial network graph would look something like the following:

This produces two nodes, A and B, and a line connecting them. Out of the box the user can drag both nodes and edges around, and clicking on a node will change its shape (and initiate a click event on the node which can be captured).

Graph 1. A simple graph

A slightly more complex graph can incorporate arrows, add labels to edges, and change foreground and background colors and shapes.

Graph 2. A directed cyclic graph

In this case, the arrows attribute on the can take the values “to”, “from” or “from to” determining the direction of the vector, which indicates the location(s) of the arrowhead.

This example also indicates how colors are set – the color object either takes a string color value (which controls the border) or can take an object of the form color:{background:"red",border:"maroon"} which specifies the component colors. In general, the border color is also the color of the corresponding vector away from the object. The text object is handled as a separate entity, but can be set for both color and font.

The vis.js library provides a number of standard shapes (along with means to create custom shapes). Some of these (like ellipse or box) place the text on the inside of the shape, while others (such as square or star) place it on the outside. The inner shapes adjust to handle text within the shape, and multiline content can be managed by using the ‘\r’ sequence to indicate a line break.

The options object lets the designer specify different groups that a given node or edge can be a part of, which makes it possible to create something analogous to CSS classes where several common traits are specific at once. This can also be used to set geometry, physics and layout characteristics that apply globally. These can be seen in Graph 3.

Graph 3. A Complex, interactive graph

The group’s section is broken into an object where each label name is a group name, which in turn can utilize most of the properties of a given node. In the case of this graph, each core object has an associated set of displayed properties but belongs to a different group.

This options object also includes the mass property, which is used to set the inverse gravity property which is used to determine how much space exists between nodes. The physics and layout options available can be quite sophisticated, including determining how quickly the display settles into its final state once rendered (or modified) as well as determining whether the positions given are based upon physics or a formal hierarchy (left to right, up to down).

In addition to the options, this illustrates how events are managed. There is a global event handler on the network itself which can be used to capture events of a certain type (such as the “click” event). When a resource is clicked on, a summary object is sent to the designated handler:

This can then be used to determine both what was clicked on and where. In this case, when a node is clicked, the corresponding id for that node is displayed.

Network diagrams are both powerful tools for visualization of relationships and are absolutely required when dealing with highly referential data, especially in the semantic world. The (vis-js)[http://visjs.org) library is a fantastic library for building such graphs, and can be configured to handle a wide variety of storytelling and dashboard needs.

See the Pen Network Graph3 by Kurt Cagle (@kurt_cagle) on CodePen.


Explore Web Development Training Courses

Explore JavaScript Courses

The following two tabs change content below.

Kurt Cagle

Kurt Cagle (kurt.cagle@gmail.com) is an author of twenty books, blogger, and software architect, and is available for consulting or hire. He lives in Issaquah, WA with his wife, daughters, and cat.

Latest posts by Kurt Cagle (see all)