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.

141 lines
4.4 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.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. <script src="../../googleAnalytics.js"></script>
  21. </head>
  22. <body>
  23. <p>
  24. Click any of the buttons below to cluster the network. On every push the network will be reinitialized first. You can click on a cluster to open it.
  25. </p>
  26. <input type="button" onclick="clusterByCid()" value="Cluster all nodes with CID = 1"> <br />
  27. <input type="button" onclick="clusterByColor()" value="Cluster by color"> <br />
  28. <input type="button" onclick="clusterByConnection()" value="Cluster 'node 1' by connections"> <br />
  29. <input type="button" onclick="clusterOutliers()" value="Cluster outliers"> <br />
  30. <input type="button" onclick="clusterByHubsize()" value="Cluster by hubsize"> <br />
  31. <div id="mynetwork"></div>
  32. <script type="text/javascript">
  33. // create an array with nodes
  34. var nodes = [
  35. {id: 1, label: 'Node 1', color:'orange'},
  36. {id: 2, label: 'Node 2', color:'DarkViolet', font:{color:'white'}},
  37. {id: 3, label: 'Node 3', color:'orange'},
  38. {id: 4, label: 'Node 4', color:'DarkViolet', font:{color:'white'}},
  39. {id: 5, label: 'Node 5', color:'orange'},
  40. {id: 6, label: 'cid = 1', cid:1, color:'orange'},
  41. {id: 7, label: 'cid = 1', cid:1, color:'DarkViolet', font:{color:'white'}},
  42. {id: 8, label: 'cid = 1', cid:1, color:'lime'},
  43. {id: 9, label: 'cid = 1', cid:1, color:'orange'},
  44. {id: 10, label: 'cid = 1', cid:1, color:'lime'}
  45. ];
  46. // create an array with edges
  47. var edges = [
  48. {from: 1, to: 2},
  49. {from: 1, to: 3},
  50. {from: 10, to: 4},
  51. {from: 2, to: 5},
  52. {from: 6, to: 2},
  53. {from: 7, to: 5},
  54. {from: 8, to: 6},
  55. {from: 9, to: 7},
  56. {from: 10, to: 9}
  57. ];
  58. // create a network
  59. var container = document.getElementById('mynetwork');
  60. var data = {
  61. nodes: nodes,
  62. edges: edges
  63. };
  64. var options = {layout:{randomSeed:8}};
  65. var network = new vis.Network(container, data, options);
  66. network.on("selectNode", function(params) {
  67. if (params.nodes.length == 1) {
  68. if (network.isCluster(params.nodes[0]) == true) {
  69. network.openCluster(params.nodes[0]);
  70. }
  71. }
  72. })
  73. function clusterByCid() {
  74. network.setData(data);
  75. var clusterOptionsByData = {
  76. joinCondition:function(childOptions) {
  77. return childOptions.cid == 1;
  78. },
  79. clusterNodeProperties: {id:'cidCluster', borderWidth:3, shape:'database'}
  80. }
  81. network.cluster(clusterOptionsByData);
  82. }
  83. function clusterByColor() {
  84. network.setData(data);
  85. var colors = ['orange','lime','DarkViolet'];
  86. var clusterOptionsByData;
  87. for (var i = 0; i < colors.length; i++) {
  88. var color = colors[i];
  89. clusterOptionsByData = {
  90. joinCondition: function (childOptions) {
  91. return childOptions.color.background == color; // the color is fully defined in the node.
  92. },
  93. processProperties: function (clusterOptions, childNodes, childEdges) {
  94. var totalMass = 0;
  95. for (var i = 0; i < childNodes.length; i++) {
  96. totalMass += childNodes[i].mass;
  97. }
  98. clusterOptions.mass = totalMass;
  99. return clusterOptions;
  100. },
  101. clusterNodeProperties: {id: 'cluster:' + color, borderWidth: 3, shape: 'database', color:color, label:'color:' + color}
  102. }
  103. network.cluster(clusterOptionsByData);
  104. }
  105. }
  106. function clusterByConnection() {
  107. network.setData(data);
  108. network.clusterByConnection(1)
  109. }
  110. function clusterOutliers() {
  111. network.setData(data);
  112. network.clusterOutliers();
  113. }
  114. function clusterByHubsize() {
  115. network.setData(data);
  116. var clusterOptionsByData = {
  117. processProperties: function(clusterOptions, childNodes) {
  118. clusterOptions.label = "[" + childNodes.length + "]";
  119. return clusterOptions;
  120. },
  121. clusterNodeProperties: {borderWidth:3, shape:'box', font:{size:30}}
  122. }
  123. network.clusterByHubsize(undefined, clusterOptionsByData);
  124. }
  125. </script>
  126. </body>
  127. </html>