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.
1 2 3 4 5 |
<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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
{ 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.