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-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. 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.
  24. </p>
  25. <input type="button" onclick="clusterByCid()" value="Cluster all nodes with CID = 1"> <br />
  26. <input type="button" onclick="clusterByColor()" value="Cluster by color"> <br />
  27. <input type="button" onclick="clusterByConnection()" value="Cluster 'node 1' by connections"> <br />
  28. <input type="button" onclick="clusterOutliers()" value="Cluster outliers"> <br />
  29. <input type="button" onclick="clusterByHubsize()" value="Cluster by hubsize"> <br />
  30. <div id="mynetwork"></div>
  31. <script type="text/javascript">
  32. // create an array with nodes
  33. var nodes = [
  34. {id: 1, label: 'Node 1', color:'orange'},
  35. {id: 2, label: 'Node 2', color:'DarkViolet', font:{color:'white'}},
  36. {id: 3, label: 'Node 3', color:'orange'},
  37. {id: 4, label: 'Node 4', color:'DarkViolet', font:{color:'white'}},
  38. {id: 5, label: 'Node 5', color:'orange'},
  39. {id: 6, label: 'cid = 1', cid:1, color:'orange'},
  40. {id: 7, label: 'cid = 1', cid:1, color:'DarkViolet', font:{color:'white'}},
  41. {id: 8, label: 'cid = 1', cid:1, color:'lime'},
  42. {id: 9, label: 'cid = 1', cid:1, color:'orange'},
  43. {id: 10, label: 'cid = 1', cid:1, color:'lime'}
  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}};
  64. var network = new vis.Network(container, data, options);
  65. network.on("selectNode", function(params) {
  66. if (params.nodes.length == 1) {
  67. if (network.isCluster(params.nodes[0]) == true) {
  68. network.openCluster(params.nodes[0]);
  69. }
  70. }
  71. });
  72. function clusterByCid() {
  73. network.setData(data);
  74. var clusterOptionsByData = {
  75. joinCondition:function(childOptions) {
  76. return childOptions.cid == 1;
  77. },
  78. clusterNodeProperties: {id:'cidCluster', borderWidth:3, shape:'database'}
  79. };
  80. network.cluster(clusterOptionsByData);
  81. }
  82. function clusterByColor() {
  83. network.setData(data);
  84. var colors = ['orange','lime','DarkViolet'];
  85. var clusterOptionsByData;
  86. for (var i = 0; i < colors.length; i++) {
  87. var color = colors[i];
  88. clusterOptionsByData = {
  89. joinCondition: function (childOptions) {
  90. return childOptions.color.background == color; // the color is fully defined in the node.
  91. },
  92. processProperties: function (clusterOptions, childNodes, childEdges) {
  93. var totalMass = 0;
  94. for (var i = 0; i < childNodes.length; i++) {
  95. totalMass += childNodes[i].mass;
  96. }
  97. clusterOptions.mass = totalMass;
  98. return clusterOptions;
  99. },
  100. clusterNodeProperties: {id: 'cluster:' + color, borderWidth: 3, shape: 'database', color:color, label:'color:' + color}
  101. };
  102. network.cluster(clusterOptionsByData);
  103. }
  104. }
  105. function clusterByConnection() {
  106. network.setData(data);
  107. network.clusterByConnection(1)
  108. }
  109. function clusterOutliers() {
  110. network.setData(data);
  111. network.clusterOutliers();
  112. }
  113. function clusterByHubsize() {
  114. network.setData(data);
  115. var clusterOptionsByData = {
  116. processProperties: function(clusterOptions, childNodes) {
  117. clusterOptions.label = "[" + childNodes.length + "]";
  118. return clusterOptions;
  119. },
  120. clusterNodeProperties: {borderWidth:3, shape:'box', font:{size:30}}
  121. };
  122. network.clusterByHubsize(undefined, clusterOptionsByData);
  123. }
  124. </script>
  125. </body>
  126. </html>