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.

169 lines
5.3 KiB

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