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.

176 lines
5.7 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. <script src="../../googleAnalytics.js"></script>
  36. </head>
  37. <body>
  38. <p>
  39. In this example, the network data can be exported to JSON and imported back into the network.
  40. 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.
  41. </p>
  42. <div id="network"></div>
  43. <div>
  44. <textarea id=input_output></textarea>
  45. <input type="button" id="import_button" onclick="importNetwork()" value="import"></input>
  46. <input type="button" id="export_button" onclick="exportNetwork()" value="export"></input>
  47. <input type="button" id="destroy_button" onclick="destroyNetwork()" value="destroy"></input>
  48. </div>
  49. <script type="text/javascript">
  50. var network;
  51. var container;
  52. var exportArea;
  53. var importButton;
  54. var exportButton;
  55. function init() {
  56. container = document.getElementById('network');
  57. exportArea = document.getElementById('input_output');
  58. importButton = document.getElementById('import_button');
  59. exportButton = document.getElementById('export_button');
  60. draw();
  61. }
  62. function addContextualInformation(elem, index, array) {
  63. addId(elem, index);
  64. addConnections(elem, index);
  65. }
  66. function addId(elem, index) {
  67. elem.id = index;
  68. }
  69. function addConnections(elem, index) {
  70. // need to replace this with a tree of the network, then get child direct children of the element
  71. elem.connections = network.getConnectedNodes(index);
  72. }
  73. function destroyNetwork() {
  74. network.destroy();
  75. }
  76. function clearOutputArea() {
  77. exportArea.value = "";
  78. }
  79. function draw() {
  80. // create a network of nodes
  81. var data = getScaleFreeNetwork(5);
  82. network = new vis.Network(container, data, {manipulation:{enabled:true}});
  83. clearOutputArea();
  84. }
  85. function exportNetwork() {
  86. clearOutputArea();
  87. var nodes = objectToArray(network.getPositions());
  88. nodes.forEach(addContextualInformation);
  89. // pretty print node data
  90. var exportValue = JSON.stringify(nodes, undefined, 2);
  91. exportArea.value = exportValue;
  92. resizeExportArea();
  93. }
  94. function importNetwork() {
  95. var inputValue = exportArea.value;
  96. var inputData = JSON.parse(inputValue);
  97. var data = {
  98. nodes: getNodeData(inputData),
  99. edges: getEdgeData(inputData)
  100. }
  101. network = new vis.Network(container, data, {});
  102. resizeExportArea();
  103. }
  104. function getNodeData(data) {
  105. var networkNodes = [];
  106. data.forEach(function(elem, index, array) {
  107. networkNodes.push({id: elem.id, label: elem.id, x: elem.x, y: elem.y});
  108. });
  109. return new vis.DataSet(networkNodes);
  110. }
  111. function getEdgeData(data) {
  112. var networkEdges = [];
  113. data.forEach(function(node, index, array) {
  114. // add the connection
  115. node.connections.forEach(function(connId, cIndex, conns) {
  116. networkEdges.push({from: node.id, to: connId});
  117. var elementConnections = array[connId].connections;
  118. // remove the connection from the other node to prevent duplicate connections
  119. var duplicateIndex = elementConnections.findIndex(function(connection) {
  120. connection === node.id;
  121. });
  122. elementConnections = elementConnections.splice(0, duplicateIndex - 1).concat(elementConnections.splice(duplicateIndex + 1, elementConnections.length))
  123. });
  124. });
  125. return new vis.DataSet(networkEdges);
  126. }
  127. function objectToArray(obj) {
  128. return Object.keys(obj).map(function (key) { return obj[key]; });
  129. }
  130. function resizeExportArea() {
  131. exportArea.style.height = (1 + exportArea.scrollHeight) + "px";
  132. }
  133. init();
  134. </script>
  135. </body>
  136. </html>