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.3 KiB

10 years ago
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <title>Datastream example</title>
  5. <style>
  6. body {
  7. width: 600px;
  8. font-size: 14px;
  9. }
  10. </style>
  11. <script type="text/javascript" src="http://www.google.com/jsapi"></script>
  12. <script type="text/javascript" src="../graph3d.js"></script>
  13. <script type="text/javascript" src="ajax.js"></script>
  14. <script>
  15. var url = "datastream_csv.php";
  16. google.load("visualization", "1");
  17. // Set callback to run when API is loaded
  18. google.setOnLoadCallback(load);
  19. var loaded = false;
  20. var ajax = new Ajax();
  21. var dataBuffer = "";
  22. var indexTable = undefined;
  23. var dataTable = undefined;
  24. var graph = undefined;
  25. var options = undefined;
  26. function load() {
  27. // initialize data table
  28. indexTable = new Array();
  29. dataTable = new google.visualization.DataTable();
  30. dataTable.addColumn('number', 'x');
  31. dataTable.addColumn('number', 'y');
  32. dataTable.addColumn('number', 'z');
  33. dataTable.addColumn('number', 'color');
  34. // specify options
  35. options = {
  36. width: "500px",
  37. height: "500px",
  38. style: "dot-color",
  39. showPerspective: true,
  40. showGrid: true,
  41. showShadow: false,
  42. keepAspectRatio: true,
  43. verticalRatio: 0.5,
  44. cameraPosition: {
  45. distance: 1.4,
  46. horizontal: 0.0,
  47. vertical: 1.0
  48. }
  49. };
  50. // Instantiate our graph object.
  51. graph = new links.Graph3d(document.getElementById('graph'));
  52. // Draw our graph with the created data and options
  53. graph.draw(dataTable, options);
  54. loaded = true;
  55. }
  56. /**
  57. * Update the data in the datatable
  58. */
  59. function setData(id, x, y, z, color) {
  60. if (indexTable[id] != undefined) {
  61. // update data
  62. var row = indexTable[id];
  63. dataTable.setValue(row, 0, x);
  64. dataTable.setValue(row, 1, y);
  65. dataTable.setValue(row, 2, z);
  66. dataTable.setValue(row, 3, color);
  67. }
  68. else {
  69. // add data
  70. var row = dataTable.addRow([x, y,z, color]);
  71. indexTable[id] = row;
  72. }
  73. }
  74. /**
  75. * Parse data in csv format.
  76. * The data is supposed to be in CSV format and has five numeric columns:
  77. * id, x, y,z, color
  78. * Rows are separated by a line and, and fields by a comma. spaces are
  79. * neglected.
  80. * @param {string} data
  81. * @param {string} delimeter Optional delimeter. A comma "," by default
  82. */
  83. function parseData(data, delimeter) {
  84. if (delimeter == undefined) {
  85. delimeter = ",";
  86. }
  87. dataBuffer += data;
  88. var id = undefined;
  89. var x = undefined;
  90. var y = undefined;
  91. var z = undefined;
  92. var color = undefined;
  93. var rows = dataBuffer.split("\n");
  94. for (var i = 0; i < rows.length; i++) {
  95. var row = rows[i].split(delimeter);
  96. if (row.length == 5) {
  97. var id = parseInt(row[0]);
  98. var x = parseFloat(row[1]);
  99. var y = parseFloat(row[2]);
  100. var z = parseFloat(row[3]);
  101. var color = parseFloat(row[4]);
  102. setData(id, x, y, z, color);
  103. }
  104. else {
  105. // corrupt data or empty line
  106. }
  107. }
  108. // empty the buffer up to the last line
  109. // TODO: handle the buffer smarter, makes not much sense right now.
  110. var lastReturn = dataBuffer.lastIndexOf("\n");
  111. if (lastReturn >= 0)
  112. dataBuffer = dataBuffer.substring(lastReturn);
  113. else
  114. dataBuffer = "";
  115. }
  116. function start() {
  117. if(!loaded) {
  118. alert("Still loading Google API. One moment please...");
  119. return;
  120. }
  121. var callback = function (newData) {
  122. parseData(newData);
  123. graph.redraw(dataTable);
  124. }
  125. ajax.abort();
  126. ajax.requestLongPoll("GET", url, callback);
  127. }
  128. function stop () {
  129. ajax.abort();
  130. }
  131. </script>
  132. </head>
  133. <body>
  134. <h1>Datastream example</h1>
  135. <p>
  136. This example demonstrates reading a data stream, via a long poll.
  137. </p>
  138. <p>
  139. Note that some browsers buffer data from a stream before sending it to the
  140. screen. In that case "nothing" seems to happens until the buffer limit is
  141. reached.
  142. </p>
  143. <p>
  144. When the datasource is located on an external server,
  145. the datasource must enable cross domain scripting
  146. (Access-Control-Allow-Origin must be set on the server side).
  147. </p>
  148. <p>
  149. <input type="button" id="start" value="Start" onclick="start();">
  150. <input type="button" is="stop" value="Stop" onclick="stop();">
  151. </p>
  152. <div id="graph"></div>
  153. </body>
  154. </html>