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.

184 lines
4.8 KiB

10 years ago
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Random nodes</title>
  5. <style type="text/css">
  6. body {
  7. font: 10pt sans;
  8. }
  9. #mynetwork {
  10. width: 600px;
  11. height: 600px;
  12. border: 1px solid lightgray;
  13. }
  14. </style>
  15. <script type="text/javascript" src="../../dist/vis.js"></script>
  16. <link href="../../dist/vis.css" rel="stylesheet" type="text/css" />
  17. <script type="text/javascript">
  18. var nodes = null;
  19. var edges = null;
  20. var network = null;
  21. function draw() {
  22. nodes = new vis.DataSet([{
  23. id: '1001',
  24. value: '1'
  25. }, {
  26. id: '1009',
  27. value: '2'
  28. }, {
  29. id: '1061',
  30. value: '3'
  31. }, {
  32. id: '1226',
  33. value: '4'
  34. }]);
  35. edges = new vis.DataSet([{
  36. id: '1001_1061',
  37. from: '1001',
  38. to: '1061',
  39. value: '1'
  40. }, {
  41. id: '1001_1226',
  42. from: '1001',
  43. to: '1226',
  44. value: '1'
  45. }, {
  46. id: '1009_1061',
  47. from: '1009',
  48. to: '1061',
  49. value: '2'
  50. }, {
  51. id: '1009_1226',
  52. from: '1009',
  53. to: '1226',
  54. value: '1'
  55. }, {
  56. id: '1061_1226',
  57. from: '1061',
  58. to: '1226',
  59. value: '1'
  60. }]);
  61. var container = document.getElementById('mynetwork');
  62. var data = {
  63. nodes: nodes,
  64. edges: edges
  65. };
  66. var nodeNormalColor = { // For 'normal' nodes
  67. background: '#92bbc7',
  68. border: '#5d93a6',
  69. borderWidth: 2,
  70. highlight: {
  71. background: '#5d93a6',
  72. border: '#537286'
  73. }
  74. };
  75. var nodeBlurColor = { // For 'blurred' nodes
  76. background: '#f0f0f0',
  77. border: '#f0f0f0'
  78. };
  79. var edgeNormalColor = {
  80. color: '#5d93a6',
  81. highlight: '#28132b'
  82. };
  83. var edgeBlurColor = {
  84. color: '#f0f0f0',
  85. highlight: '#f0f0f0'
  86. };
  87. var options = {
  88. nodes: {
  89. shape: 'dot',
  90. color: nodeNormalColor
  91. },
  92. edges: {
  93. inheritColor: false,
  94. color: edgeNormalColor,
  95. widthSelectionMultiplier: 1
  96. },
  97. physics: {
  98. 'barnesHut': {
  99. centralGravity: 0.5,
  100. springLength: 150,
  101. springConstant: 0.03,
  102. damping: 0.2
  103. }
  104. }
  105. };
  106. network = new vis.Network(container, data, options);
  107. // add event listeners
  108. network.on('select', function (params) {
  109. console.log(params.edges);
  110. var nodesData = {};
  111. var edgesData = {};
  112. var nodeResetQuery = [];
  113. var edgeResetQuery = [];
  114. var allEdges = edges.get();
  115. var allNodes = nodes.get();
  116. for (var i = 0; i < allNodes.length; i++) {
  117. nodesData[allNodes[i].id] = {id:allNodes[i].id, color: nodeBlurColor};
  118. nodeResetQuery.push({id:allNodes[i].id, color: nodeNormalColor});
  119. }
  120. for (var i = 0; i < allEdges.length; i++) {
  121. edgesData[allEdges[i].id] = {id:allEdges[i].id, color: edgeBlurColor};
  122. edgeResetQuery.push({id:allEdges[i].id, color: edgeNormalColor});
  123. }
  124. // deselect
  125. if (params.nodes.length == 0 && params.edges.length == 0) {
  126. nodes.update(nodeResetQuery);
  127. edges.update(edgeResetQuery);
  128. return;
  129. }
  130. // paint nodes and edges.
  131. for (var i = 0; i < params.nodes.length; i++) {
  132. nodesData[params.nodes[i]].color = nodeNormalColor;
  133. }
  134. for (var i = 0; i < params.edges.length; i++) {
  135. edgesData[params.edges[i]].color = edgeNormalColor;
  136. var selEdge = edges.get(params.edges[i]);
  137. nodesData[selEdge.to].color = nodeNormalColor;
  138. nodesData[selEdge.from].color = nodeNormalColor;
  139. }
  140. var nodeUpdateQuery = [];
  141. var edgeUpdateQuery = [];
  142. for (var nodeId in nodesData) {
  143. if (nodesData.hasOwnProperty(nodeId)) {
  144. nodeUpdateQuery.push(nodesData[nodeId]);
  145. }
  146. }
  147. for (var edgeId in edgesData) {
  148. if (edgesData.hasOwnProperty(edgeId)) {
  149. edgeUpdateQuery.push(edgesData[edgeId]);
  150. }
  151. }
  152. nodes.update(nodeUpdateQuery);
  153. edges.update(edgeUpdateQuery);
  154. // nodes.update(nodeResetQuery);
  155. // edges.update(edgeResetQuery);
  156. // nodes.update(nodeUpdateQuery);
  157. // edges.update(edgeUpdateQuery);
  158. });}
  159. </script>
  160. </head>
  161. <body onload="draw();">
  162. <br>
  163. <div id="mynetwork"></div>
  164. <p id="selection"></p>
  165. </body>
  166. </html>