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.

106 lines
2.9 KiB

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script src="../dist/vis.js"></script>
  5. <link type="text/css" rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.9.0/vis.min.css">
  6. <style>
  7. #network {
  8. width: 100%;
  9. height: 400px;
  10. border: 1px solid gray;
  11. }
  12. </style>
  13. <script type="text/javascript">
  14. function addChild(Parent){
  15. var newNodes = 5;
  16. var angle = 2* Math.PI / newNodes;
  17. var r = 150;
  18. var shift = Math.random() * Math.PI;
  19. for(i=0; i< newNodes; i++) {
  20. var xnew = network.getPositions(Parent)[Parent].x + Math.cos(angle * i + shift) * r;
  21. var ynew = network.getPositions(Parent)[Parent].y + Math.sin(angle * i + shift) * r;
  22. var newId = guid();
  23. try {
  24. nodes.add(
  25. {id: newId,
  26. label: "New Node",
  27. x: xnew,
  28. y: ynew })
  29. }
  30. catch (err) {}
  31. var edgeId = guid();
  32. try {
  33. edges.add(
  34. {id: edgeId,
  35. from: Parent,
  36. to: newId})
  37. }
  38. catch (err) {}
  39. }
  40. };
  41. function guid() {
  42. function s4() {
  43. return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
  44. }
  45. return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
  46. s4() + '-' + s4() + s4() + s4();
  47. };
  48. var nodes, edges, network;
  49. function draw() {
  50. nodes = new vis.DataSet([
  51. {"id": "1",
  52. "label": "Start"}]);
  53. edges = new vis.DataSet([]);
  54. var container = document.getElementById('network');
  55. var data = {
  56. nodes: nodes,
  57. edges: edges
  58. };
  59. var options = {
  60. "edges": {arrows:'to', "smooth": {"type": 'dynamic'}},
  61. "physics": {
  62. "forceAtlas2Based": {
  63. "gravitationalConstant": -514,
  64. "springLength": 150,
  65. "springConstant": 0.1,
  66. "damping": 0.9},
  67. "maxVelocity": 15,
  68. "minVelocity": 1,
  69. "timestep": 0.9,
  70. "solver": "forceAtlas2Based"}};
  71. network = new vis.Network(container, data, options);
  72. network.fit();
  73. network.on("click", function (params) {
  74. if(params.nodes[0]!=null){addChild(params.nodes[0])}});
  75. }
  76. </script>
  77. </head>
  78. <body onload="draw();">
  79. <div id="network"></div>
  80. </body>
  81. </html>