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.4 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. var options = {
  37. start: vis.moment().add(-30, 'seconds'), // changed so its faster
  38. end: vis.moment(),
  39. dataAxis: {
  40. customRange: {
  41. left: {
  42. min:-10, max: 10
  43. }
  44. }
  45. },
  46. drawPoints: {
  47. style: 'circle' // square, circle
  48. },
  49. shaded: {
  50. orientation: 'bottom' // top, bottom
  51. }
  52. };
  53. var graph2d = new vis.Graph2d(container, dataset, options);
  54. // a function to generate data points
  55. function y(x) {
  56. return (Math.sin(x / 2) + Math.cos(x / 4)) * 5;
  57. }
  58. function renderStep() {
  59. // move the window (you can think of different strategies).
  60. var now = vis.moment();
  61. var range = graph2d.getWindow();
  62. var interval = range.end - range.start;
  63. switch (strategy.value) {
  64. case 'continuous':
  65. // continuously move the window
  66. graph2d.setWindow(now - interval, now, {animate: false});
  67. requestAnimationFrame(renderStep);
  68. break;
  69. case 'discrete':
  70. graph2d.setWindow(now - interval, now, {animate: false});
  71. setTimeout(renderStep, DELAY);
  72. break;
  73. default: // 'static'
  74. // move the window 90% to the left when now is larger than the end of the window
  75. if (now > range.end) {
  76. graph2d.setWindow(now - 0.1 * interval, now + 0.9 * interval);
  77. }
  78. setTimeout(renderStep, DELAY);
  79. break;
  80. }
  81. }
  82. renderStep();
  83. /**
  84. * Add a new datapoint to the graph
  85. */
  86. function addDataPoint() {
  87. // add a new data point to the dataset
  88. var now = vis.moment();
  89. dataset.add({
  90. x: now,
  91. y: y(now / 1000)
  92. });
  93. // remove all data points which are no longer visible
  94. var range = graph2d.getWindow();
  95. var interval = range.end - range.start;
  96. var oldIds = dataset.getIds({
  97. filter: function (item) {
  98. return item.x < range.start - interval; // changed so you don't see them disappearing
  99. }
  100. });
  101. dataset.remove(oldIds);
  102. setTimeout(addDataPoint, DELAY);
  103. }
  104. addDataPoint();
  105. </script>
  106. </body>
  107. </html>