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.

160 lines
4.7 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Clustering</title>
  5. <script type="text/javascript" src="../../../dist/vis.js"></script>
  6. <link href="../../../dist/vis-network.min.css" rel="stylesheet" type="text/css"/>
  7. <style type="text/css">
  8. #mynetwork {
  9. width: 600px;
  10. height: 600px;
  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. <body>
  22. <p>
  23. You can zoom in and out to cluster/decluster.
  24. </p>
  25. Stabilize when clustering:<input type="checkbox" id="stabilizeCheckbox">
  26. <div id="mynetwork"></div>
  27. <script type="text/javascript">
  28. var clusterIndex = 0;
  29. var clusters = [];
  30. var lastClusterZoomLevel = 0;
  31. var clusterFactor = 0.9;
  32. // create an array with nodes
  33. var nodes = [
  34. {id: 1, label: 'Node 1'},
  35. {id: 2, label: 'Node 2'},
  36. {id: 3, label: 'Node 3'},
  37. {id: 4, label: 'Node 4'},
  38. {id: 5, label: 'Node 5'},
  39. {id: 6, label: 'Node 6'},
  40. {id: 7, label: 'Node 7'},
  41. {id: 8, label: 'Node 8'},
  42. {id: 9, label: 'Node 9'},
  43. {id: 10, label: 'Node 10'}
  44. ];
  45. // create an array with edges
  46. var edges = [
  47. {from: 1, to: 2},
  48. {from: 1, to: 3},
  49. {from: 10, to: 4},
  50. {from: 2, to: 5},
  51. {from: 6, to: 2},
  52. {from: 7, to: 5},
  53. {from: 8, to: 6},
  54. {from: 9, to: 7},
  55. {from: 10, to: 9}
  56. ];
  57. // create a network
  58. var container = document.getElementById('mynetwork');
  59. var data = {
  60. nodes: nodes,
  61. edges: edges
  62. };
  63. var options = {layout: {randomSeed: 8}, physics:{adaptiveTimestep:false}};
  64. var network = new vis.Network(container, data, options);
  65. // set the first initial zoom level
  66. network.once('initRedraw', function() {
  67. if (lastClusterZoomLevel === 0) {
  68. lastClusterZoomLevel = network.getScale();
  69. }
  70. });
  71. // we use the zoom event for our clustering
  72. network.on('zoom', function (params) {
  73. if (params.direction == '-') {
  74. if (params.scale < lastClusterZoomLevel*clusterFactor) {
  75. makeClusters(params.scale);
  76. lastClusterZoomLevel = params.scale;
  77. }
  78. }
  79. else {
  80. openClusters(params.scale);
  81. }
  82. });
  83. // if we click on a node, we want to open it up!
  84. network.on("selectNode", function (params) {
  85. if (params.nodes.length == 1) {
  86. if (network.isCluster(params.nodes[0]) == true) {
  87. network.openCluster(params.nodes[0])
  88. }
  89. }
  90. });
  91. // make the clusters
  92. function makeClusters(scale) {
  93. var clusterOptionsByData = {
  94. processProperties: function (clusterOptions, childNodes) {
  95. clusterIndex = clusterIndex + 1;
  96. var childrenCount = 0;
  97. for (var i = 0; i < childNodes.length; i++) {
  98. childrenCount += childNodes[i].childrenCount || 1;
  99. }
  100. clusterOptions.childrenCount = childrenCount;
  101. clusterOptions.label = "# " + childrenCount + "";
  102. clusterOptions.font = {size: childrenCount*5+30}
  103. clusterOptions.id = 'cluster:' + clusterIndex;
  104. clusters.push({id:'cluster:' + clusterIndex, scale:scale});
  105. return clusterOptions;
  106. },
  107. clusterNodeProperties: {borderWidth: 3, shape: 'database', font: {size: 30}}
  108. }
  109. network.clusterOutliers(clusterOptionsByData);
  110. if (document.getElementById('stabilizeCheckbox').checked === true) {
  111. // since we use the scale as a unique identifier, we do NOT want to fit after the stabilization
  112. network.setOptions({physics:{stabilization:{fit: false}}});
  113. network.stabilize();
  114. }
  115. }
  116. // open them back up!
  117. function openClusters(scale) {
  118. var newClusters = [];
  119. var declustered = false;
  120. for (var i = 0; i < clusters.length; i++) {
  121. if (clusters[i].scale < scale) {
  122. network.openCluster(clusters[i].id);
  123. lastClusterZoomLevel = scale;
  124. declustered = true;
  125. }
  126. else {
  127. newClusters.push(clusters[i])
  128. }
  129. }
  130. clusters = newClusters;
  131. if (declustered === true && document.getElementById('stabilizeCheckbox').checked === true) {
  132. // since we use the scale as a unique identifier, we do NOT want to fit after the stabilization
  133. network.setOptions({physics:{stabilization:{fit: false}}});
  134. network.stabilize();
  135. }
  136. }
  137. </script>
  138. </body>
  139. </html>