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.

245 lines
6.4 KiB

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JS Bin</title>
  6. <script type="text/javascript" src="../dist/vis.js"></script> <!-- network handling framework -->
  7. <link href="../dist/vis.css" rel="stylesheet" type="text/css"/>
  8. <style type="text/css">
  9. #network{
  10. width: 1900px;
  11. height: 800px;
  12. border: 1px solid lightgray;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <h1>Network Test</h1>
  18. <div id="network"></div>
  19. <script type="text/javascript">
  20. var allNodes;
  21. var allEdges;
  22. var pathActive = false;
  23. var nodes = [{
  24. id: 1,
  25. label: 'test1'
  26. }, {
  27. id: 2,
  28. label: 'test2'
  29. }, {
  30. id: 3,
  31. label: 'test3'
  32. }, {
  33. id: 4,
  34. label: 'test4'
  35. }, {
  36. id: 5,
  37. label: 'test5'
  38. }];
  39. var edges = [{
  40. from: 1,
  41. to: 3
  42. }, {
  43. from: 1,
  44. to: 2
  45. }, {
  46. from: 3,
  47. to: 4
  48. }, {
  49. from: 2,
  50. to: 4
  51. }, {
  52. from: 4,
  53. to: 5
  54. }]
  55. var nodesDataset = new vis.DataSet(nodes);
  56. var edgesDataset = new vis.DataSet(edges);
  57. var container = document.getElementById('mynetwork');
  58. var data = {
  59. nodes: nodesDataset,
  60. edges: edgesDataset
  61. };
  62. var options = {
  63. autoResize: true,
  64. height: '100%',
  65. width: '100%',
  66. nodes: {
  67. shape: 'box'
  68. },
  69. edges: {
  70. smooth: {
  71. enabled: true,
  72. type: "dynamic"
  73. },
  74. },
  75. layout: {
  76. hierarchical: {
  77. levelSeparation: 220,
  78. nodeSpacing: 120,
  79. treeSpacing: 100,
  80. direction: 'LR',
  81. blockShifting: true,
  82. sortMethod: 'directed',
  83. edgeMinimization: false
  84. }
  85. },
  86. physics: {
  87. hierarchicalRepulsion: {
  88. centralGravity: 0.5,
  89. springLength: 300,
  90. springConstant: 0.1,
  91. nodeDistance: 150,
  92. damping: 0.09
  93. },
  94. }
  95. };
  96. var network = new vis.Network(container, data, options);
  97. allNodes = nodesDataset.get({
  98. returnType: "Object"
  99. });
  100. allEdges = edgesDataset.get({
  101. returnType: "Object"
  102. });
  103. showPath();
  104. showPath();
  105. function getNodeId(nodeLabels) {
  106. var resultArray = [];
  107. var unmatchedNodesLabel = [];
  108. for (var i = 0; i < nodeLabels.length; i++) {
  109. for (var nodeId in allNodes) {
  110. var matchflag = false;
  111. if (nodeLabels[i].toUpperCase() === allNodes[nodeId].label.toUpperCase()) {
  112. resultArray.push(allNodes[nodeId].id);
  113. matchflag = true;
  114. break;
  115. }
  116. }
  117. if (!matchflag) {
  118. console.log('An unmatched nodeLabel exists!' + nodeLabels[i]);
  119. }
  120. }
  121. return resultArray;
  122. }
  123. function getLinkedEdge(fromNodeId, toNodeId) {
  124. var result;
  125. if (fromNodeId) {
  126. var allLinkedEdges = network.getConnectedEdges(fromNodeId);
  127. for (var i = 0; i < allLinkedEdges.length; i++) {
  128. var linkedNodes = network.getConnectedNodes(allLinkedEdges[i]);
  129. for (var j = 0; j < linkedNodes.length; j++) {
  130. if (toNodeId === linkedNodes[j]) {
  131. result = allLinkedEdges[i];
  132. if (j == 0) {
  133. if (allEdges[allLinkedEdges[i]].arrows === undefined) {
  134. allEdges[allLinkedEdges[i]].arrows = 'from';
  135. } else {
  136. allEdges[allLinkedEdges[i]].arrows = 'to;from';
  137. }
  138. } else {
  139. if (allEdges[allLinkedEdges[i]].arrows === undefined) {
  140. allEdges[allLinkedEdges[i]].arrows = 'to';
  141. } else {
  142. allEdges[allLinkedEdges[i]].arrows = 'to;from';
  143. }
  144. }
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. return result;
  151. }
  152. function updateNodesEdges() {
  153. // update nodes and edges
  154. var updateNodes = [];
  155. var updateEdges = [];
  156. for (var nodeId in allNodes) {
  157. if (allNodes.hasOwnProperty(nodeId)) {
  158. updateNodes.push(allNodes[nodeId]);
  159. }
  160. }
  161. for (var edgeId in allEdges) {
  162. if (allEdges.hasOwnProperty(edgeId)) {
  163. updateEdges.push(allEdges[edgeId]);
  164. }
  165. }
  166. nodesDataset.update(updateNodes);
  167. edgesDataset.update(updateEdges);
  168. }
  169. function showPath() {
  170. // Test values
  171. var pathData = 'test1,test3,test4,test5';
  172. var nodesOrder = pathData.split(',');
  173. var orderedNodeId = getNodeId(nodesOrder);
  174. if (orderedNodeId.length > 0) {
  175. pathActive = true;
  176. // mark all nodes as hard to read
  177. for (var nodeId in allNodes) {
  178. allNodes[nodeId].color = 'rgba(200,200,200,0.5)';
  179. }
  180. // mark all edges as hard to read
  181. for (var edgeId in allEdges) {
  182. allEdges[edgeId].color = 'rgba(200,200,200,0.5)';
  183. }
  184. // the selected nodes and edges get its own color and its label back.
  185. for (var i = 0; i < orderedNodeId.length; i++) {
  186. allNodes[orderedNodeId[i]].color = undefined;
  187. // linked edge
  188. if (i < orderedNodeId.length) {
  189. var linkedEdge = getLinkedEdge(orderedNodeId[i], orderedNodeId[i + 1]);
  190. if (linkedEdge) {
  191. allEdges[linkedEdge].color = undefined;
  192. }
  193. }
  194. }
  195. updateNodesEdges();
  196. }
  197. // reset all nodes
  198. for (var nodeId in allNodes) {
  199. allNodes[nodeId].color = undefined;
  200. }
  201. // reset all edges <--- it does NOT work! although edge's color undfined!
  202. for (var edgeId in allEdges) {
  203. allEdges[edgeId].color = null;
  204. allEdges[edgeId].arrows = null;
  205. }
  206. updateNodesEdges();
  207. }
  208. </script>
  209. </body>
  210. </html>