vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.2 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Dynamic Data</title>
  5. <script type="text/javascript" src="../../../../dist/vis.js"></script>
  6. <link href="../../../../dist/vis.css" rel="stylesheet" type="text/css" />
  7. <style type="text/css">
  8. #mynetwork {
  9. width: 600px;
  10. height: 400px;
  11. border: 1px solid lightgray;
  12. }
  13. p {
  14. max-width:600px;
  15. }
  16. h4 {
  17. margin-bottom:3px;
  18. }
  19. </style>
  20. </head>
  21. <p>
  22. You can change any settings you want while the network is initialized using the vis Dataset, setOptions and setData. Finally you can destroy the network and completely reinitialize it.
  23. </p>
  24. <h4>DataSet (change the data while it's loaded and initialzed):</h4>
  25. <input type="button" onclick="addNode()" value="add node dynamically"> <br />
  26. <input type="button" onclick="changeNode1()" value="change node 1's color dynamically"> <br />
  27. <input type="button" onclick="removeRandomNode()" value="remove a random Node"> <br />
  28. <input type="button" onclick="resetAllNodes()" value="reload all nodes"> <br />
  29. <input type="button" onclick="resetAllNodesStabilize()" value="reload all nodes and stabilize"> <br />
  30. <h4>setOptions (change the global options):</h4>
  31. <input type="button" onclick="changeOptions()" value="change the global options"><br />
  32. <h4>setData (reinitialize the data): </h4>
  33. <input type="button" onclick="setTheData()" value="setData. This stabilizes again if stabilization is true."><br />
  34. <h4>Cleanly destroy the network and restart it:</h4>
  35. <input type="button" onclick="resetAll()" value="Destroy the network and restart it."><br />
  36. <div id="mynetwork"></div>
  37. <script type="text/javascript">
  38. var nodeIds, shadowState, nodesArray, nodes, edgesArray, edges, network;
  39. function startNetwork() {
  40. // this list is kept to remove a random node.. we do not add node 1 here because it's used for changes
  41. nodeIds = [2, 3, 4, 5];
  42. shadowState = false;
  43. // create an array with nodes
  44. nodesArray = [
  45. {id: 1, label: 'Node 1'},
  46. {id: 2, label: 'Node 2'},
  47. {id: 3, label: 'Node 3'},
  48. {id: 4, label: 'Node 4'},
  49. {id: 5, label: 'Node 5'}
  50. ];
  51. nodes = new vis.DataSet(nodesArray);
  52. // create an array with edges
  53. edgesArray = [
  54. {from: 1, to: 3},
  55. {from: 1, to: 2},
  56. {from: 2, to: 4},
  57. {from: 2, to: 5}
  58. ];
  59. edges = new vis.DataSet(edgesArray);
  60. // create a network
  61. var container = document.getElementById('mynetwork');
  62. var data = {
  63. nodes: nodes,
  64. edges: edges
  65. };
  66. var options = {};
  67. network = new vis.Network(container, data, options);
  68. }
  69. function addNode() {
  70. var newId = (Math.random() * 1e7).toString(32);
  71. nodes.add({id:newId, label:"I'm new!"});
  72. nodeIds.push(newId);
  73. }
  74. function changeNode1() {
  75. var newColor = '#' + Math.floor((Math.random() * 255 * 255 * 255)).toString(16);
  76. nodes.update([{id:1, color:{background:newColor}}]);
  77. }
  78. function removeRandomNode() {
  79. var randomNodeId = nodeIds[Math.floor(Math.random() * nodeIds.length)];
  80. nodes.remove({id:randomNodeId});
  81. var index = nodeIds.indexOf(randomNodeId);
  82. nodeIds.splice(index,1);
  83. }
  84. function changeOptions() {
  85. shadowState = !shadowState;
  86. network.setOptions({nodes:{shadow:shadowState},edges:{shadow:shadowState}});
  87. }
  88. function resetAllNodes() {
  89. nodes.clear();
  90. edges.clear();
  91. nodes.add(nodesArray);
  92. edges.add(edgesArray);
  93. }
  94. function resetAllNodesStabilize() {
  95. resetAllNodes();
  96. network.stabilize();
  97. }
  98. function setTheData() {
  99. nodes = new vis.DataSet(nodesArray);
  100. edges = new vis.DataSet(edgesArray);
  101. network.setData({nodes:nodes, edges:edges})
  102. }
  103. function resetAll() {
  104. if (network !== null) {
  105. network.destroy();
  106. network = null;
  107. }
  108. startNetwork();
  109. }
  110. startNetwork();
  111. </script>
  112. <script src="../../../googleAnalytics.js"></script>
  113. </body>
  114. </html>