index.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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: AJAX</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. var options = {
  13. lines: {
  14. show: true
  15. },
  16. points: {
  17. show: true
  18. },
  19. xaxis: {
  20. tickDecimals: 0,
  21. tickSize: 1
  22. }
  23. };
  24. var data = [];
  25. $.plot("#placeholder", data, options);
  26. // Fetch one series, adding to what we already have
  27. var alreadyFetched = {};
  28. $("button.fetchSeries").click(function () {
  29. var button = $(this);
  30. // Find the URL in the link right next to us, then fetch the data
  31. var dataurl = button.siblings("a").attr("href");
  32. function onDataReceived(series) {
  33. // Extract the first coordinate pair; jQuery has parsed it, so
  34. // the data is now just an ordinary JavaScript object
  35. var firstcoordinate = "(" + series.data[0][0] + ", " + series.data[0][1] + ")";
  36. button.siblings("span").text("Fetched " + series.label + ", first point: " + firstcoordinate);
  37. // Push the new data onto our existing data array
  38. if (!alreadyFetched[series.label]) {
  39. alreadyFetched[series.label] = true;
  40. data.push(series);
  41. }
  42. $.plot("#placeholder", data, options);
  43. }
  44. $.ajax({
  45. url: dataurl,
  46. type: "GET",
  47. dataType: "json",
  48. success: onDataReceived
  49. });
  50. });
  51. // Initiate a recurring data update
  52. $("button.dataUpdate").click(function () {
  53. data = [];
  54. alreadyFetched = {};
  55. $.plot("#placeholder", data, options);
  56. var iteration = 0;
  57. function fetchData() {
  58. ++iteration;
  59. function onDataReceived(series) {
  60. // Load all the data in one pass; if we only got partial
  61. // data we could merge it with what we already have.
  62. data = [ series ];
  63. $.plot("#placeholder", data, options);
  64. }
  65. // Normally we call the same URL - a script connected to a
  66. // database - but in this case we only have static example
  67. // files, so we need to modify the URL.
  68. $.ajax({
  69. url: "data-eu-gdp-growth-" + iteration + ".json",
  70. type: "GET",
  71. dataType: "json",
  72. success: onDataReceived
  73. });
  74. if (iteration < 5) {
  75. setTimeout(fetchData, 1000);
  76. } else {
  77. data = [];
  78. alreadyFetched = {};
  79. }
  80. }
  81. setTimeout(fetchData, 1000);
  82. });
  83. // Load the first series by default, so we don't have an empty plot
  84. $("button.fetchSeries:first").click();
  85. // Add the Flot version string to the footer
  86. $("#footer").prepend("Flot " + $.plot.version + " &ndash; ");
  87. });
  88. </script>
  89. </head>
  90. <body>
  91. <div id="header">
  92. <h2>AJAX</h2>
  93. </div>
  94. <div id="content">
  95. <div class="demo-container">
  96. <div id="placeholder" class="demo-placeholder"></div>
  97. </div>
  98. <p>Example of loading data dynamically with AJAX. Percentage change in GDP (source: <a href="http://epp.eurostat.ec.europa.eu/tgm/table.do?tab=table&init=1&plugin=1&language=en&pcode=tsieb020">Eurostat</a>). Click the buttons below:</p>
  99. <p>The data is fetched over HTTP, in this case directly from text files. Usually the URL would point to some web server handler (e.g. a PHP page or Java/.NET/Python/Ruby on Rails handler) that extracts it from a database and serializes it to JSON.</p>
  100. <p>
  101. <button class="fetchSeries">First dataset</button>
  102. [ <a href="data-eu-gdp-growth.json">see data</a> ]
  103. <span></span>
  104. </p>
  105. <p>
  106. <button class="fetchSeries">Second dataset</button>
  107. [ <a href="data-japan-gdp-growth.json">see data</a> ]
  108. <span></span>
  109. </p>
  110. <p>
  111. <button class="fetchSeries">Third dataset</button>
  112. [ <a href="data-usa-gdp-growth.json">see data</a> ]
  113. <span></span>
  114. </p>
  115. <p>If you combine AJAX with setTimeout, you can poll the server for new data.</p>
  116. <p>
  117. <button class="dataUpdate">Poll for data</button>
  118. </p>
  119. </div>
  120. <div id="footer">
  121. Copyright &copy; 2007 - 2013 IOLA and Ole Laursen
  122. </div>
  123. </body>
  124. </html>