index.html 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>Flot Examples: Real-time updates</title>
  6. <link href="../examples.css" rel="stylesheet" type="text/css">
  7. <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../../excanvas.min.js"></script><![endif]-->
  8. <script language="javascript" type="text/javascript" src="../../jquery.js"></script>
  9. <script language="javascript" type="text/javascript" src="../../jquery.flot.js"></script>
  10. <script type="text/javascript">
  11. $(function() {
  12. // We use an inline data source in the example, usually data would
  13. // be fetched from a server
  14. var data = [],
  15. totalPoints = 300;
  16. function getRandomData() {
  17. if (data.length > 0)
  18. data = data.slice(1);
  19. // Do a random walk
  20. while (data.length < totalPoints) {
  21. var prev = data.length > 0 ? data[data.length - 1] : 50,
  22. y = prev + Math.random() * 10 - 5;
  23. if (y < 0) {
  24. y = 0;
  25. } else if (y > 100) {
  26. y = 100;
  27. }
  28. data.push(y);
  29. }
  30. // Zip the generated y values with the x values
  31. var res = [];
  32. for (var i = 0; i < data.length; ++i) {
  33. res.push([i, data[i]])
  34. }
  35. return res;
  36. }
  37. // Set up the control widget
  38. var updateInterval = 30;
  39. $("#updateInterval").val(updateInterval).change(function () {
  40. var v = $(this).val();
  41. if (v && !isNaN(+v)) {
  42. updateInterval = +v;
  43. if (updateInterval < 1) {
  44. updateInterval = 1;
  45. } else if (updateInterval > 2000) {
  46. updateInterval = 2000;
  47. }
  48. $(this).val("" + updateInterval);
  49. }
  50. });
  51. var plot = $.plot("#placeholder", [ getRandomData() ], {
  52. series: {
  53. shadowSize: 0 // Drawing is faster without shadows
  54. },
  55. yaxis: {
  56. min: 0,
  57. max: 100
  58. },
  59. xaxis: {
  60. show: false
  61. }
  62. });
  63. function update() {
  64. plot.setData([getRandomData()]);
  65. // Since the axes don't change, we don't need to call plot.setupGrid()
  66. plot.draw();
  67. setTimeout(update, updateInterval);
  68. }
  69. update();
  70. // Add the Flot version string to the footer
  71. $("#footer").prepend("Flot " + $.plot.version + " &ndash; ");
  72. });
  73. </script>
  74. </head>
  75. <body>
  76. <div id="header">
  77. <h2>Real-time updates</h2>
  78. </div>
  79. <div id="content">
  80. <div class="demo-container">
  81. <div id="placeholder" class="demo-placeholder"></div>
  82. </div>
  83. <p>You can update a chart periodically to get a real-time effect by using a timer to insert the new data in the plot and redraw it.</p>
  84. <p>Time between updates: <input id="updateInterval" type="text" value="" style="text-align: right; width:5em"> milliseconds</p>
  85. </div>
  86. <div id="footer">
  87. Copyright &copy; 2007 - 2014 IOLA and Ole Laursen
  88. </div>
  89. </body>
  90. </html>