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.

185 lines
5.9 KiB

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  5. <meta content="utf-8" http-equiv="encoding">
  6. <title>Network | Saving and loading networks</title>
  7. <style type="text/css">
  8. body {
  9. font: 10pt sans;
  10. }
  11. #network {
  12. float:left;
  13. width: 600px;
  14. height: 600px;
  15. margin:5px;
  16. border: 1px solid lightgray;
  17. }
  18. #config {
  19. float:left;
  20. width: 400px;
  21. height: 600px;
  22. }
  23. #input_output {
  24. height: 10%;
  25. width: 15%;
  26. }
  27. p {
  28. font-size:16px;
  29. max-width:700px;
  30. }
  31. </style>
  32. <script type="text/javascript" src="../exampleUtil.js"></script>
  33. <script type="text/javascript" src="../../../dist/vis.js"></script>
  34. <link href="../../../dist/vis.css" rel="stylesheet" type="text/css" />
  35. </head>
  36. <body>
  37. <p>
  38. In this example, the network data can be exported to JSON and imported back into the network.
  39. Try this out by exporting the network to JSON, clearing the network and then importing it again. The nodes will all appear in the same position as they were before the network was destroyed.
  40. </p>
  41. <div id="network"></div>
  42. <div>
  43. <textarea id=input_output></textarea>
  44. <input type="button" id="import_button" onclick="importNetwork()" value="import"></input>
  45. <input type="button" id="export_button" onclick="exportNetwork()" value="export"></input>
  46. <input type="button" id="destroy_button" onclick="destroyNetwork()" value="destroy"></input>
  47. </div>
  48. <script type="text/javascript">
  49. var network;
  50. var container;
  51. var exportArea;
  52. var importButton;
  53. var exportButton;
  54. function init() {
  55. container = document.getElementById('network');
  56. exportArea = document.getElementById('input_output');
  57. importButton = document.getElementById('import_button');
  58. exportButton = document.getElementById('export_button');
  59. draw();
  60. }
  61. function addConnections(elem, index) {
  62. // need to replace this with a tree of the network, then get child direct children of the element
  63. elem.connections = network.getConnectedNodes(index);
  64. }
  65. function destroyNetwork() {
  66. network.destroy();
  67. }
  68. function clearOutputArea() {
  69. exportArea.value = "";
  70. }
  71. function draw() {
  72. // create a network of nodes
  73. var data = getScaleFreeNetwork(5);
  74. network = new vis.Network(container, data, {manipulation:{enabled:true}});
  75. clearOutputArea();
  76. }
  77. function exportNetwork() {
  78. clearOutputArea();
  79. var nodes = objectToArray(network.getPositions());
  80. nodes.forEach(addConnections);
  81. // pretty print node data
  82. var exportValue = JSON.stringify(nodes, undefined, 2);
  83. exportArea.value = exportValue;
  84. resizeExportArea();
  85. }
  86. function importNetwork() {
  87. var inputValue = exportArea.value;
  88. var inputData = JSON.parse(inputValue);
  89. var data = {
  90. nodes: getNodeData(inputData),
  91. edges: getEdgeData(inputData)
  92. }
  93. network = new vis.Network(container, data, {});
  94. resizeExportArea();
  95. }
  96. function getNodeData(data) {
  97. var networkNodes = [];
  98. data.forEach(function(elem, index, array) {
  99. networkNodes.push({id: elem.id, label: elem.id, x: elem.x, y: elem.y});
  100. });
  101. return new vis.DataSet(networkNodes);
  102. }
  103. function getNodeById(data, id) {
  104. for (var n = 0; n < data.length; n++) {
  105. if (data[n].id == id) { // double equals since id can be numeric or string
  106. return data[n];
  107. }
  108. };
  109. throw 'Can not find id \'' + id + '\' in data';
  110. }
  111. function getEdgeData(data) {
  112. var networkEdges = [];
  113. data.forEach(function(node) {
  114. // add the connection
  115. node.connections.forEach(function(connId, cIndex, conns) {
  116. networkEdges.push({from: node.id, to: connId});
  117. let cNode = getNodeById(data, connId);
  118. var elementConnections = cNode.connections;
  119. // remove the connection from the other node to prevent duplicate connections
  120. var duplicateIndex = elementConnections.findIndex(function(connection) {
  121. return connection == node.id; // double equals since id can be numeric or string
  122. });
  123. if (duplicateIndex != -1) {
  124. elementConnections.splice(duplicateIndex, 1);
  125. };
  126. });
  127. });
  128. return new vis.DataSet(networkEdges);
  129. }
  130. function objectToArray(obj) {
  131. return Object.keys(obj).map(function (key) {
  132. obj[key].id = key;
  133. return obj[key];
  134. });
  135. }
  136. function resizeExportArea() {
  137. exportArea.style.height = (1 + exportArea.scrollHeight) + "px";
  138. }
  139. init();
  140. </script>
  141. </body>
  142. </html>