index.html (4506B)
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 12 $(function() { 13 14 var options = { 15 lines: { 16 show: true 17 }, 18 points: { 19 show: true 20 }, 21 xaxis: { 22 tickDecimals: 0, 23 tickSize: 1 24 } 25 }; 26 27 var data = []; 28 29 $.plot("#placeholder", data, options); 30 31 // Fetch one series, adding to what we already have 32 33 var alreadyFetched = {}; 34 35 $("button.fetchSeries").click(function () { 36 37 var button = $(this); 38 39 // Find the URL in the link right next to us, then fetch the data 40 41 var dataurl = button.siblings("a").attr("href"); 42 43 function onDataReceived(series) { 44 45 // Extract the first coordinate pair; jQuery has parsed it, so 46 // the data is now just an ordinary JavaScript object 47 48 var firstcoordinate = "(" + series.data[0][0] + ", " + series.data[0][1] + ")"; 49 button.siblings("span").text("Fetched " + series.label + ", first point: " + firstcoordinate); 50 51 // Push the new data onto our existing data array 52 53 if (!alreadyFetched[series.label]) { 54 alreadyFetched[series.label] = true; 55 data.push(series); 56 } 57 58 $.plot("#placeholder", data, options); 59 } 60 61 $.ajax({ 62 url: dataurl, 63 type: "GET", 64 dataType: "json", 65 success: onDataReceived 66 }); 67 }); 68 69 // Initiate a recurring data update 70 71 $("button.dataUpdate").click(function () { 72 73 data = []; 74 alreadyFetched = {}; 75 76 $.plot("#placeholder", data, options); 77 78 var iteration = 0; 79 80 function fetchData() { 81 82 ++iteration; 83 84 function onDataReceived(series) { 85 86 // Load all the data in one pass; if we only got partial 87 // data we could merge it with what we already have. 88 89 data = [ series ]; 90 $.plot("#placeholder", data, options); 91 } 92 93 // Normally we call the same URL - a script connected to a 94 // database - but in this case we only have static example 95 // files, so we need to modify the URL. 96 97 $.ajax({ 98 url: "data-eu-gdp-growth-" + iteration + ".json", 99 type: "GET", 100 dataType: "json", 101 success: onDataReceived 102 }); 103 104 if (iteration < 5) { 105 setTimeout(fetchData, 1000); 106 } else { 107 data = []; 108 alreadyFetched = {}; 109 } 110 } 111 112 setTimeout(fetchData, 1000); 113 }); 114 115 // Load the first series by default, so we don't have an empty plot 116 117 $("button.fetchSeries:first").click(); 118 119 // Add the Flot version string to the footer 120 121 $("#footer").prepend("Flot " + $.plot.version + " – "); 122 }); 123 124 </script> 125 </head> 126 <body> 127 128 <div id="header"> 129 <h2>AJAX</h2> 130 </div> 131 132 <div id="content"> 133 134 <div class="demo-container"> 135 <div id="placeholder" class="demo-placeholder"></div> 136 </div> 137 138 <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> 139 140 <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> 141 142 <p> 143 <button class="fetchSeries">First dataset</button> 144 [ <a href="data-eu-gdp-growth.json">see data</a> ] 145 <span></span> 146 </p> 147 148 <p> 149 <button class="fetchSeries">Second dataset</button> 150 [ <a href="data-japan-gdp-growth.json">see data</a> ] 151 <span></span> 152 </p> 153 154 <p> 155 <button class="fetchSeries">Third dataset</button> 156 [ <a href="data-usa-gdp-growth.json">see data</a> ] 157 <span></span> 158 </p> 159 160 <p>If you combine AJAX with setTimeout, you can poll the server for new data.</p> 161 162 <p> 163 <button class="dataUpdate">Poll for data</button> 164 </p> 165 166 </div> 167 168 <div id="footer"> 169 Copyright © 2007 - 2013 IOLA and Ole Laursen 170 </div> 171 172 </body> 173 </html>