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.

140 lines
3.2 KiB

9 years ago
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Network | Basic usage</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: 1400px;
  10. height: 1000px;
  11. border: 1px solid lightgray;
  12. }
  13. </style>
  14. <script type="text/javascript">
  15. </script>
  16. </head>
  17. <body>
  18. <my-toolbar>
  19. <button id="toggleAddNode">add Nodes</button>
  20. <button id="btnDelete">delete</button>
  21. <!-- other Vis manipulation buttons -->
  22. <button id="togglePhysics">pause Physics</button>
  23. <button id="togglePin">toggle pin</button>
  24. </my-toolbar>
  25. <div id="mynetwork"></div>
  26. <script>
  27. document.getElementById("toggleAddNode").onclick = addNode;
  28. document.getElementById("btnDelete").onclick = visDelete;
  29. document.getElementById("togglePhysics").onclick = togglePhysics;
  30. document.getElementById("togglePin").onclick = togglePin;
  31. function storePositions() {
  32. network.storePositions();
  33. }
  34. var inAddNodeMode = false;
  35. function addNode() {
  36. if (inAddNodeMode) {
  37. network.disableEditMode();
  38. inAddNodeMode = false;
  39. } else {
  40. network.addNodeMode();
  41. inAddNodeMode = true;
  42. }
  43. }
  44. function visDelete() {
  45. network.deleteSelected();
  46. }
  47. function togglePhysics() {
  48. isPhysicsEnabled = !isPhysicsEnabled;
  49. network.setOptions({
  50. physics: isPhysicsEnabled
  51. });
  52. }
  53. var isPhysicsEnabled = true;
  54. function togglePin() {
  55. network.storePositions();
  56. // update node.fixed
  57. }
  58. // create an array with nodes
  59. var nodes = new vis.DataSet([{
  60. id: 1,
  61. label: 'Node 1'
  62. }, {
  63. id: 2,
  64. label: 'Node 2'
  65. }, {
  66. id: 3,
  67. label: 'Node 3'
  68. }, {
  69. id: 4,
  70. label: 'Node 4'
  71. }, {
  72. id: 5,
  73. label: 'Node 5'
  74. }]);
  75. // create an array with edges
  76. var edges = new vis.DataSet([{
  77. from: 1,
  78. to: 3
  79. }, {
  80. from: 1,
  81. to: 2
  82. }, {
  83. from: 2,
  84. to: 4
  85. }, {
  86. from: 2,
  87. to: 5
  88. }]);
  89. // create a network
  90. var container = document.getElementById('mynetwork');
  91. var data = {
  92. nodes: nodes,
  93. edges: edges
  94. };
  95. var options = {
  96. manipulation: {
  97. enabled: false,
  98. initiallyActive: false,
  99. addNode: false,
  100. //addEdge: onConnectEdge,
  101. //editNode: onEditNode,
  102. //editEdge: true,
  103. deleteNode: onDelete,
  104. // this is false, but the manipulation api can still delete edges
  105. deleteEdge: false
  106. }
  107. };
  108. function onAddNode(nodeToBeAdded, callback) {
  109. //$log.debug('onAdd', nodeToBeAdded);
  110. console.log('onAdd');
  111. // do stuff to the new node
  112. inAddNodeMode = false;
  113. callback(nodeToBeAdded);
  114. }
  115. function onDelete(toBeDeletedData, callback) {
  116. console.log('deleting data', toBeDeletedData);
  117. // toBeDeletedData is object with nodes: [] and edges: []
  118. callback(toBeDeletedData);
  119. }
  120. var network = new vis.Network(container, data, options);
  121. </script>
  122. </body>
  123. </html>