Get your team started on a custom learning journey today!
Our Boulder, CO-based learning experts are ready to help!
Get your team started on a custom learning journey today!
Our Boulder, CO-based learning experts are ready to help!
Follow us on LinkedIn for our latest data and tips!
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.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.19.1/vis.min.css"/> <script type="javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.19.1/vis.min.js"/>
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.
<style type="text/css"> .network { display:block; width:800px; height:800px; border:solid; background-color:white; } </style> <div class="network">Network Graph</div>
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:
<script type="javascript"> var nodes = [ { id:"a", label:"A" }, { id:"b", label:"B" } ]; var edges = [ { from:"a", to:"b" } ]; var options = {}; var container = document.querySelector('.network'); network = new vis.Network(container, data, options); </script>
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).
A slightly more complex graph can incorporate arrows, add labels to edges, and change foreground and background colors and shapes.
<script type="javascript"> var nodes = [ { id:"a", label:"A", shape:"diamond", color:{background:"red",border:"maroon"}, font:{color:white} }, { id:"b", label:"B", shape:"oval", color:{background:"green"}, font:{color:white} }, { id:"c", label:"C",, shape:"square", color:{background:"blue"}, font:{color:white} ]; var edges = [ { from:"a", to:"b", label:"1", arrows:"to" }, { from:"b", to:"c", label:"2", arrows:"to" }, { from:"c", to:"a", label:"3", arrows:"to" }]; var options = {}; var container = document.querySelector('.network'); network = new vis.Network(container, data, options); </script>
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.
var nodes = [ { id: "source1:sales-row1", label: "<Sales>\nRow 1", group: "source1" }, { id:"source1:sales-row1-revenue", shape:"box", label:"$20,125,776.00", group:"source1" }, { id:"source1:sales-row1-region", label:"Northwest", group:"source1" }, { id:"source1:sales-row1-period", label:"2017Q1", group:"source1" }, { id: "source2:sales-row26", label: "<SalesReport>\nRow 26", group: "source2" }, { id:"source2:sales-row26-revenue", shape:"box", label:"$20,125,784.00", group:"source2" }, { id:"source2:sales-row26-region", label:"NW", group:"source2" }, { id:"source2:sales-row26-period", label:"2017Q1", group:"source2" }, { id: "source3:sales-row68", label: "<Sales>\nRow 68", group: "source3" }, { id:"source3:sales-row68-revenue", shape:"box", label:"$20,125,794.00", group:"source3" }, { id:"source3:sales-row68-region", label:"Northwest", group:"source3" }, { id:"source3:sales-row68-period", label:"2017Q1", group:"source3" }, { id:"canon:Sales_Report5", label:"<Sales_Report>\nSales Report 5", group:"canon" }, { id:"canon:Sales_Report5-revenue", shape:"box", label:"$20,125,785.00", group:"canon" }, { id:"canon:Sales_Report5-region", label:"Northwest", group:"canon" }, { id:"canon:Sales_Report5-period", label:"2017Q1", group:"canon" } ]; var edges = [ { from: "source1:sales-row1", to: "source1:sales-row1-revenue", label:"revenue", arrows:"to" }, { from: "source1:sales-row1", to: "source1:sales-row1-region", label:"region", arrows:"to" }, { from: "source1:sales-row1", to: "source1:sales-row1-period", label:"period", arrows:"to" }, { from: "source2:sales-row26", to: "source2:sales-row26-revenue", label:"netRevenue", arrows:"to" }, { from: "source2:sales-row26", to: "source2:sales-row26-region", label:"salesRegion", arrows:"to" }, { from: "source2:sales-row26", to: "source2:sales-row26-period", label:"period", arrows:"to" }, { from: "source3:sales-row68", to: "source3:sales-row68-revenue", label:"netRevenue", arrows:"to" }, { from: "source3:sales-row68", to: "source3:sales-row68-region", label:"salesRegion", arrows:"to" }, { from: "source3:sales-row68", to: "source3:sales-row68-period", label:"period", arrows:"to" }, { from: "source1:sales-row1", to: "canon:Sales_Report5", label: "same as", arrows: "to" }, { from: "source2:sales-row26", to: "canon:Sales_Report5", label: "same as", arrows: "to" }, { from: "source3:sales-row68", to: "canon:Sales_Report5", label: "same as", arrows: "to" }, { from: "canon:Sales_Report5", to: "canon:Sales_Report5-revenue", label:"revenue", arrows:"to" }, { from: "canon:Sales_Report5", to: "canon:Sales_Report5-region", label:"region", arrows:"to" }, { from: "canon:Sales_Report5", to: "canon:Sales_Report5-period", label:"period", arrows:"to" } ] // create a network var container = document.querySelector('.network'); var data = { nodes: nodes, edges: edges }; var options = { mass:3, nodes: { shape: 'oval', size: 30, font: { size: 12, color: '#ffffff' }, borderWidth: 2 }, edges: { width: 2 }, groups:{"source1":{ color:{ background:'red', border:'maroon' }, shadow:{enabled:true, color:'rgba(0,0,0,0.5)', x:6, y:6 } }, "source2":{ color:{background:'blue', border:'navy'}, shadow:{enabled:true, color:'rgba(0,0,0,0.5)', x:6, y:6 } }, "source3":{ color:{background:'green', border:'darkGreen'}, shadow:{enabled:true, color:'rgba(0,0,0,0.5)', x:6, y:6 } }, "canon":{ color:{background:'gold', border:'brown'}, font:{color:'black',size:14}, shadow:{enabled:true, color:'rgba(0,0,0,0.5)', x:6, y:6 } } } }; network = new vis.Network(container, data, options); network.on('click',(obj)=>{document.querySelector('.report').innerHTML =obj.nodes[0]})
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:
{ nodes: [Array of selected nodeIds], edges: [Array of selected edgeIds], event: [Object] original click event, pointer: { DOM: {x:pointer_x, y:pointer_y}, canvas: {x:canvas_x, y:canvas_y} } }
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.
Customized Technical Learning Solutions to Help Attract and Retain Talented Developers
Let DI help you design solutions to onboard, upskill or reskill your software development organization. Fully customized. 100% guaranteed.
DevelopIntelligence leads technical and software development learning programs for Fortune 500 companies. We provide learning solutions for hundreds of thousands of engineers for over 250 global brands.
“I appreciated the instructor’s technique of writing live code examples rather than using fixed slide decks to present the material.”
VMwareDevelopIntelligence has been in the technical/software development learning and training industry for nearly 20 years. We’ve provided learning solutions to more than 48,000 engineers, across 220 organizations worldwide.
Thank you for everyone who joined us this past year to hear about our proven methods of attracting and retaining tech talent.
© 2013 - 2022 DevelopIntelligence LLC - Privacy Policy
Let's review your current tech training programs and we'll help you baseline your success against some of our big industry partners. In this 30-minute meeting, we'll share our data/insights on what's working and what's not.
Training Journal sat down with our CEO for his thoughts on what’s working, and what’s not working.