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.

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