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.

97 lines
2.7 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</option>
  24. <option value="discrete">Discrete</option>
  25. </select>
  26. </p>
  27. <div id="visualization"></div>
  28. <script type="text/javascript">
  29. var strategy = document.getElementById('strategy');
  30. // create a graph2d with an (currently empty) dataset
  31. var container = document.getElementById('visualization');
  32. var dataset = new vis.DataSet();
  33. var options = {
  34. start: vis.moment().add(-60, 'seconds'),
  35. end: vis.moment()
  36. };
  37. var graph2d = new vis.Graph2d(container, dataset, options);
  38. // a function to generate data points
  39. function y(x) {
  40. return (Math.sin(x / 2) + Math.cos(x / 4)) * 5;
  41. }
  42. /**
  43. * Append a new data point to the graph
  44. * @param {Date} x
  45. * @param {Number} y
  46. */
  47. function add (x, y) {
  48. // add a new data point to the dataset
  49. dataset.add({x: x, y: y});
  50. // move the window (you can think of different strategies).
  51. var now = vis.moment();
  52. var range = graph2d.getWindow();
  53. var interval = range.end - range.start;
  54. switch (strategy.value) {
  55. case 'continuous':
  56. // continuously move the window
  57. graph2d.setWindow(now - interval, now, {animate: false});
  58. break;
  59. default: // 'discrete'
  60. // move the window 90% to the left when now is larger than the end of the window
  61. if (now > range.end) {
  62. graph2d.setWindow(now - 0.1 * interval, now + 0.9 * interval);
  63. }
  64. break;
  65. }
  66. // remove all data points which are no longer visible
  67. var oldIds = dataset.getIds({
  68. filter: function (item) {
  69. return item.x < range.start;
  70. }
  71. });
  72. dataset.remove(oldIds);
  73. }
  74. var delay = 500; // delay in ms
  75. function next() {
  76. // add a new data point
  77. var now = vis.moment();
  78. add(now, y(now / 1000));
  79. setTimeout(next, delay);
  80. }
  81. next();
  82. </script>
  83. </body>
  84. </html>