PHP/JavaScript webapp to analyse spending habits
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.

ajax.html 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>Flot Examples</title>
  6. <link href="layout.css" rel="stylesheet" type="text/css"></link>
  7. <!--[if IE]><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. </head>
  11. <body>
  12. <h1>Flot Examples</h1>
  13. <div id="placeholder" style="width:600px;height:300px;"></div>
  14. <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>
  15. <p>The data is fetched over HTTP, in this case directly from text
  16. files. Usually the URL would point to some web server handler
  17. (e.g. a PHP page or Java/.NET/Python/Ruby on Rails handler) that
  18. extracts it from a database and serializes it to JSON.</p>
  19. <p>
  20. <input class="fetchSeries" type="button" value="First dataset"> -
  21. <a href="data-eu-gdp-growth.json">data</a> -
  22. <span></span>
  23. </p>
  24. <p>
  25. <input class="fetchSeries" type="button" value="Second dataset"> -
  26. <a href="data-japan-gdp-growth.json">data</a> -
  27. <span></span>
  28. </p>
  29. <p>
  30. <input class="fetchSeries" type="button" value="Third dataset"> -
  31. <a href="data-usa-gdp-growth.json">data</a> -
  32. <span></span>
  33. </p>
  34. <p>If you combine AJAX with setTimeout, you can poll the server
  35. for new data.</p>
  36. <p>
  37. <input class="dataUpdate" type="button" value="Poll for data">
  38. </p>
  39. <script id="source" language="javascript" type="text/javascript">
  40. $(function () {
  41. var options = {
  42. lines: { show: true },
  43. points: { show: true },
  44. xaxis: { tickDecimals: 0, tickSize: 1 }
  45. };
  46. var data = [];
  47. var placeholder = $("#placeholder");
  48. $.plot(placeholder, data, options);
  49. // fetch one series, adding to what we got
  50. var alreadyFetched = {};
  51. $("input.fetchSeries").click(function () {
  52. var button = $(this);
  53. // find the URL in the link right next to us
  54. var dataurl = button.siblings('a').attr('href');
  55. // then fetch the data with jQuery
  56. function onDataReceived(series) {
  57. // extract the first coordinate pair so you can see that
  58. // data is now an ordinary Javascript object
  59. var firstcoordinate = '(' + series.data[0][0] + ', ' + series.data[0][1] + ')';
  60. button.siblings('span').text('Fetched ' + series.label + ', first point: ' + firstcoordinate);
  61. // let's add it to our current data
  62. if (!alreadyFetched[series.label]) {
  63. alreadyFetched[series.label] = true;
  64. data.push(series);
  65. }
  66. // and plot all we got
  67. $.plot(placeholder, data, options);
  68. }
  69. $.ajax({
  70. url: dataurl,
  71. method: 'GET',
  72. dataType: 'json',
  73. success: onDataReceived
  74. });
  75. });
  76. // initiate a recurring data update
  77. $("input.dataUpdate").click(function () {
  78. // reset data
  79. data = [];
  80. alreadyFetched = {};
  81. $.plot(placeholder, data, options);
  82. var iteration = 0;
  83. function fetchData() {
  84. ++iteration;
  85. function onDataReceived(series) {
  86. // we get all the data in one go, if we only got partial
  87. // data, we could merge it with what we already got
  88. data = [ series ];
  89. $.plot($("#placeholder"), data, options);
  90. }
  91. $.ajax({
  92. // usually, we'll just call the same URL, a script
  93. // connected to a database, but in this case we only
  94. // have static example files so we need to modify the
  95. // URL
  96. url: "data-eu-gdp-growth-" + iteration + ".json",
  97. method: 'GET',
  98. dataType: 'json',
  99. success: onDataReceived
  100. });
  101. if (iteration < 5)
  102. setTimeout(fetchData, 1000);
  103. else {
  104. data = [];
  105. alreadyFetched = {};
  106. }
  107. }
  108. setTimeout(fetchData, 1000);
  109. });
  110. });
  111. </script>
  112. </body>
  113. </html>