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.

122 lines
3.6 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>Graph2d | Streaming data</title>
  7. <style type="text/css">
  8. body, html, select {
  9. font: 10pt sans-serif;
  10. }
  11. </style>
  12. <script src="../../dist/vis.js"></script>
  13. <link href="../../dist/vis.css" rel="stylesheet" type="text/css" />
  14. </head>
  15. <body>
  16. <h2>Graph2d | Streaming data</h2>
  17. <p style="max-width: 700px;">
  18. This example demonstrates how to apply streaming data input to the Graph2d. The example shows two different ways to let the window move along with the new data, and there are more strategies for that. Note also that it is possible to disable moving and/or zooming the graph by setting options <code>moveable</code> and <code>zoomable</code> false.
  19. </p>
  20. <p>
  21. <label for="strategy">Strategy:</label>
  22. <select id="strategy">
  23. <option value="continuous" selected>Continuous (CPU intensive)</option>
  24. <option value="discrete">Discrete</option>
  25. <option value="static">Static</option>
  26. </select>
  27. </p>
  28. <div id="visualization"></div>
  29. <script type="text/javascript">
  30. var DELAY = 1000; // delay in ms to add new data points
  31. var strategy = document.getElementById('strategy');
  32. // create a graph2d with an (currently empty) dataset
  33. var container = document.getElementById('visualization');
  34. var dataset = new vis.DataSet();
  35. var now = vis.moment();
  36. // added initial set so you don't think the graph is empty
  37. dataset.add({x: now-2000,y: y((now-2000) / 1000)});
  38. dataset.add({x: now-1000,y: y((now-1000) / 1000)});
  39. dataset.add({x: now,y: y((now) / 1000)});
  40. dataset.add({x: now+1000,y: y((now+1000) / 1000)});
  41. var options = {
  42. start: vis.moment().add(-30, 'seconds'), // changed so its faster
  43. end: vis.moment(),
  44. dataAxis: {
  45. customRange: {
  46. left: {
  47. min:-10, max: 10
  48. }
  49. }
  50. }
  51. };
  52. var graph2d = new vis.Graph2d(container, dataset, options);
  53. // a function to generate data points
  54. function y(x) {
  55. return (Math.sin(x / 2) + Math.cos(x / 4)) * 5;
  56. }
  57. function renderStep() {
  58. // move the window (you can think of different strategies).
  59. var now = vis.moment();
  60. var range = graph2d.getWindow();
  61. var interval = range.end - range.start;
  62. switch (strategy.value) {
  63. case 'continuous':
  64. // continuously move the window
  65. graph2d.setWindow(now - interval, now, {animate: false});
  66. requestAnimationFrame(renderStep);
  67. break;
  68. case 'discrete':
  69. graph2d.setWindow(now - interval, now, {animate: false});
  70. setTimeout(renderStep, DELAY);
  71. break;
  72. default: // 'static'
  73. // move the window 90% to the left when now is larger than the end of the window
  74. if (now > range.end) {
  75. graph2d.setWindow(now - 0.1 * interval, now + 0.9 * interval);
  76. }
  77. setTimeout(renderStep, DELAY);
  78. break;
  79. }
  80. }
  81. renderStep();
  82. /**
  83. * Add a new datapoint to the graph
  84. */
  85. function addDataPoint() {
  86. // add a new data point to the dataset
  87. var now = vis.moment();
  88. dataset.add({
  89. x: now+2000, // changed so you dont see them appearing
  90. y: y(now / 1000)
  91. });
  92. // remove all data points which are no longer visible
  93. var range = graph2d.getWindow();
  94. var interval = range.end - range.start;
  95. var oldIds = dataset.getIds({
  96. filter: function (item) {
  97. return item.x < range.start - interval; // changed so you don't see them disappearing
  98. }
  99. });
  100. dataset.remove(oldIds);
  101. setTimeout(addDataPoint, DELAY);
  102. }
  103. addDataPoint();
  104. </script>
  105. </body>
  106. </html>