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.

210 lines
5.1 KiB

  1. <!DOCTYPE html>
  2. <!-- saved from url=(0044)http://kenedict.com/networks/worldcup14/vis/ , thanks Andre!-->
  3. <html>
  4. <head>
  5. <meta http-equiv="content-type" content="text/html; charset=UTF8">
  6. <title>Dynamic Data - Importing from Gephi (JSON)</title>
  7. <script type="text/javascript" src="../../dist/vis.js"></script>
  8. <link type="text/css" rel="stylesheet" href="../../dist/vis.css">
  9. <style type="text/css">
  10. #mynetwork {
  11. width: 800px;
  12. height: 800px;
  13. border: 1px solid lightgray;
  14. }
  15. div.nodeContent {
  16. position: relative;
  17. border: 1px solid lightgray;
  18. width: 480px;
  19. height: 780px;
  20. margin-top: -802px;
  21. margin-left: 810px;
  22. padding: 10px;
  23. }
  24. pre {
  25. padding: 5px;
  26. margin: 5px;
  27. }
  28. .string {
  29. color: green;
  30. }
  31. .number {
  32. color: darkorange;
  33. }
  34. .boolean {
  35. color: blue;
  36. }
  37. .null {
  38. color: magenta;
  39. }
  40. .key {
  41. color: red;
  42. }
  43. </style>
  44. <script src="../googleAnalytics.js"></script>
  45. </head>
  46. <body>
  47. <h2>Dynamic Data - Importing from Gephi (JSON)</h2>
  48. <div style="width:700px; font-size:14px; text-align: justify;">
  49. This example shows how to import a JSON file exported by Gephi. The two
  50. options available for the import are
  51. available through the checkboxes. You can download the Gephi JSON exporter
  52. here:
  53. <a href="https://marketplace.gephi.org/plugin/json-exporter/" target="_blank">https://marketplace.gephi.org/plugin/json-exporter/</a>.
  54. All of Gephi's attributes are also contained within the node elements. This
  55. means you can access all of this data through the DataSet.
  56. <br/>
  57. </div>
  58. <p>
  59. <label><input type="checkbox" id="allowedToMove"/> Allow to move after import.</label><br>
  60. <label><input type="checkbox" id="parseColor"/> Parse the color instead of
  61. copy (adds borders, highlights etc.)</label>
  62. </p>
  63. <div id="mynetwork"></div>
  64. <div class="nodeContent"><h4>Node Content:</h4>
  65. <pre id="nodeContent"></pre>
  66. </div>
  67. <script type="text/javascript">
  68. var network;
  69. var nodes = new vis.DataSet();
  70. var edges = new vis.DataSet();
  71. var gephiImported;
  72. var allowedToMoveCheckbox = document.getElementById('allowedToMove');
  73. allowedToMoveCheckbox.onchange = redrawAll;
  74. var parseColorCheckbox = document.getElementById('parseColor');
  75. parseColorCheckbox.onchange = redrawAll;
  76. var nodeContent = document.getElementById('nodeContent');
  77. loadJSON('./data/WorldCup2014.json', redrawAll);
  78. var container = document.getElementById('mynetwork');
  79. var data = {
  80. nodes: nodes,
  81. edges: edges
  82. };
  83. var options = {
  84. nodes: {
  85. shape: 'dot',
  86. font: {
  87. face: 'Tahoma'
  88. }
  89. },
  90. edges: {
  91. width: 0.15,
  92. color: {
  93. inherit: 'from'
  94. },
  95. smooth: {
  96. dynamic: false,
  97. type: 'continuous'
  98. }
  99. },
  100. interaction: {
  101. tooltipDelay: 200
  102. },
  103. physics: {
  104. stabilization: false,
  105. barnesHut: {
  106. gravitationalConstant: -10000,
  107. springConstant: 0.002,
  108. springLength: 150
  109. }
  110. },
  111. rendering: {
  112. hideEdgesOnDrag: true
  113. }
  114. };
  115. network = new vis.Network(container, data, options);
  116. /**
  117. * This function fills the DataSets. These DataSets will update the network.
  118. */
  119. function redrawAll(gephiJSON) {
  120. if (gephiJSON.nodes === undefined) {
  121. gephiJSON = gephiImported;
  122. }
  123. else {
  124. gephiImported = gephiJSON;
  125. }
  126. nodes.clear();
  127. edges.clear();
  128. var allowedToMove = allowedToMoveCheckbox.checked;
  129. var parseColor = parseColorCheckbox.checked;
  130. var parsed = vis.network.gephiParser.parseGephi(gephiJSON, {
  131. allowedToMove: allowedToMove,
  132. parseColor: parseColor
  133. });
  134. // add the parsed data to the DataSets.
  135. nodes.add(parsed.nodes);
  136. edges.add(parsed.edges);
  137. var data = nodes.get(2); // get the data from node 2
  138. nodeContent.innerHTML = syntaxHighlight(data); // show the data in the div
  139. network.fit(); // zoom to fit
  140. }
  141. // from http://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript
  142. function syntaxHighlight(json) {
  143. if (typeof json != 'string') {
  144. json = JSON.stringify(json, null, 2);
  145. }
  146. json = json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
  147. return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
  148. var cls = 'number';
  149. if (/^"/.test(match)) {
  150. if (/:$/.test(match)) {
  151. cls = 'key';
  152. } else {
  153. cls = 'string';
  154. }
  155. } else if (/true|false/.test(match)) {
  156. cls = 'boolean';
  157. } else if (/null/.test(match)) {
  158. cls = 'null';
  159. }
  160. return '<span class="' + cls + '">' + match + '</span>';
  161. });
  162. }
  163. function loadJSON(path, success, error) {
  164. var xhr = new XMLHttpRequest();
  165. xhr.onreadystatechange = function () {
  166. if (xhr.readyState === 4) {
  167. if (xhr.status === 200) {
  168. success(JSON.parse(xhr.responseText));
  169. }
  170. else {
  171. error(xhr);
  172. }
  173. }
  174. };
  175. xhr.open('GET', path, true);
  176. xhr.send();
  177. }
  178. </script>
  179. </body>
  180. </html>