Using the Guardian UK university 2014 rankings to demonstrate, here’s how to mashup a few bits and pieces to visualize them. This is pretty generalized, so can be applied to any data set with fairly minimal changes.
- Google Visualization Views and Charts – to retrieve, organize and present the data
- jQuery promises – to orchestrate asynchronous data access
- Exponential backoff – to avoid failures when Google API is too busy
You can find it here. The data is spread across many Google Docs Spreadsheets so it takes a while to load.
Loading the data
1 2 3 4 5 6 7 8 |
function getSheetInfo(light) { // note that spreadsheet must be published before you can do this as json // this will return the spreadsheet schema info var key = "0At2ExLh4POiZdFd0YUhpZVRPUGxFcW85X2xkMm1vY2c"; var url = "https://spreadsheets.google.com/feeds/worksheets/" + key + "/public/basic?alt=json"; var proxy; return getPromiseData(url,proxy,light); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function getSheetData(package) { // we have the schema, get all the data // this will create an array of promises, one for each worksheet var entries = package.info.feed.entry, sheetPromises = []; for ( var i= 0 ; i < entries.length ;i++) { var p = getTheDocsData(entries[i]); sheetPromises.push(p); p.done (function (data) { package.sheets.push (data); }); } return sheetPromises; } |
1 2 3 4 5 6 7 8 |
function findVizLinkEntry (links) { // there are several versions of the link for each worksheet // we'll use the google vizualization one. for (var i=0; i < links.length; i++ ) { if (links[i].rel.match(/visualizationApi/)) return links[i].href; } alert ("cant find viz link"); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function getTheDocsData ( entry , light) { var deferred = $.Deferred(); var h = findVizLinkEntry(entry.link); var query = new google.visualization.Query(h); // get all columns query.setQuery('SELECT * options no_format'); query.send(function (response) { if (response.isError()) { deferred.reject('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage()); if (light) makeLight(light,'failed'); } else { deferred.resolve( { entry:entry, data:response.getDataTable(), name: entry.title['$t'].replace(/\d*\s*/,"") }); if (light) makeLight(light,'idle'); } }); return deferred.promise(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function getTheData() { var massage = $.Deferred(); var package = {}; // override any default options here package.options = {}; // all the sheets will end up here package.sheets = []; getSheetInfo("schema").done( function (info) { package.info = info; makeLight("sheets","busy"); var allSheetsPromise = getSheetData(package); $.when.apply($,allSheetsPromise) .done(function() { // do any data massagin here before resolving package.sheets.sort(function(a,b) { return a.name < b.name ? -1 : (a.name===b.name ? 0 : 1) ; }); massage.resolve(package); makeLight("sheets","idle"); }) .fail ( function(error) { $('#status').html(error); massage.reject(error); makeLight("sheets","failed"); }); }); return massage.promise(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// general deferred handler for getting json data and creating promise function getPromiseData(url,proxyUrl,light){ var deferred = $.Deferred(); var u = url; if (proxyUrl ) { u = proxyUrl+"?url="+encodeURIComponent(url); } else { // if not proxied, needs to be jSONp u = u + "&callback=?" } backOffJson (deferred,u,light); return deferred.promise(); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
function backOffJson (defer,u, light,tries) { // uses exponential back off tries = tries || 0; if(light)makeLight(light,"busy"); $.getJSON(u, null, function (data, textStatus, jqXHR) { if (data.error) { if (data.error.code == 503 || data.error.code == 429) { // a 503 error - so i'll back off tries ++; if (tries <= 5) { // try again var hangAroundFor = (Math.pow(2,tries)*1000) + (Math.round(Math.random() * 1000)); if (light)makeLight(light,"lemon"); setTimeout(function() { backOffJson (defer,u,light,tries); },hangAroundFor); } else { defer.reject(data); } } else { defer.reject(data); } } else { defer.resolve(data); } }) .error(function(res, status, err) { defer.reject("error " + err + " for " + u); }); defer.promise() .done (function () { if (light) makeLight(light,"idle"); }) .fail( function () { if(light)makeLight(light,"failed"); }); return defer; } |
The visualization
One benefit of retrieving the data as google Vizualization DataTables, is the that the visualization (a scatter chart) is a breeze
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
function doTheViz(control) { // create a dataview of the selected columns for scatter plot control.xAxisSelected = control.yAxisSelected = -1; manageOptionsSubjects(control.options.select.subject.element,control.package.sheets) .val(control.options.select.subject.default); var chart = new google.visualization.ScatterChart(document.getElementById(control.divName)); google.visualization.events.addListener(chart, 'ready', function () { control.options.selectorElement.css("display","block"); }); changeTheView(); function changeTheView () { var select = control.options.select; var sheet = findTheData ( control, select.subject.element.val()); var view = new google.visualization.DataView(sheet.data); // because we want to retain the choice from previous sheets control.xAxisSelected = control.xAxisSelected == -1 ? findColumnNumber(select.xAxis.default,sheet) : control.xAxisSelected ; control.yAxisSelected = control.yAxisSelected == -1 ? findColumnNumber(select.yAxis.default,sheet) : control.yAxisSelected ; // generate new options manageOptions(select.yAxis.element, sheet).val(control.yAxisSelected); manageOptions(select.xAxis.element, sheet).val(control.xAxisSelected); select.yAxis.element.change(function() { updateChartOptions() ; }); select.xAxis.element.change(function() { updateChartOptions() ; }); select.subject.element.change(function() { changeTheView(); }); updateChartOptions(); function updateChartOptions(){ control.options.selectorElement.css("display","none"); // TODO an enhancement to tooltip could be to find the performance data // for the instituion in the institution sheet control.xAxisSelected = +control.options.select.xAxis.element.val(); control.yAxisSelected = +control.options.select.yAxis.element.val(); view.setColumns([control.xAxisSelected,control.yAxisSelected, {sourceColumn:findColumnNumber (control.options.namecolumn, sheet),role:'tooltip'}]); var options = { title: control.options.select.subject.element.val(), vAxis: { title : view.getColumnLabel(1) } , hAxis: { title : view.getColumnLabel(0) }, legend : { position : "none" }, animation : { duration : control.options.animationDuration , easing: control.options.animationEasing }, height: control.options.height, width: control.options.width }; chart.draw(view,options); } } } |
Simply use a selector with values that correspond to the column number in the source table, and create a view with those selected columns from the underlying datatable. Since each sheet is treated separately, there is no need for them to be in the same format or even have the same columns – the selectors and viz are completely data driven.
The code
Here is the whole thing.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>2014 UK Guardian university rankings - ramblings.mcpher.com</title> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <link rel="stylesheet" type="text/css" href="http://xliberation.com/cdn/css/d3flights.css"> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript" src="http://d3js.org/d3.v3.min.js"></script> <script type="text/javascript"> (function () { google.load("visualization", "1", {packages: ["corechart","table"]}); google.load("jquery", "1"); google.setOnLoadCallback(function() { makeLight("uni","busy"); initialize().done ( function (control) { doTheViz(control); makeLight("uni","idle"); } ) .fail (function(error) { alert (JSON.stringify(error)); makeLight("uni","failed"); }) }); function manageOptions(sel,sheet) { // generate new options for regular data var o = $('option',sel ); if(o)o.remove(); for (var i = 0; i < sheet.data.getNumberOfColumns(); i ++) { if( sheet.data.getColumnType(i) == 'number' ) { sel.append( $("<option>", { value: i, text: sheet.data.getColumnLabel(i) } )); } } return sel; } function manageOptionsSubjects(sel,sheets) { // generate new options for filtering sheets var o = $('option',sel ); if(o)o.remove(); for (var i = 0; i < sheets.length; i ++) { // for the moment just ignore the institutions and specialist tabs as menu options if (sheets[i].name != 'Institutions' && sheets[i].name != 'Specialists') { sel.append( $("<option>", { value: sheets[i].name, text: sheets[i].name } )); } } return sel; } function findColumnNumber (label, sheet) { for (var i = 0; i < sheet.data.getNumberOfColumns(); i ++) { if (sheet.data.getColumnLabel(i) == label) return(i); } alert ('couldnt find label ' + label); // just return the first one return 0; } function doTheViz(control) { // create a dataview of the selected columns for scatter plot control.xAxisSelected = control.yAxisSelected = -1; manageOptionsSubjects(control.options.select.subject.element,control.package.sheets) .val(control.options.select.subject.default); var chart = new google.visualization.ScatterChart(document.getElementById(control.divName)); google.visualization.events.addListener(chart, 'ready', function () { control.options.selectorElement.css("display","block"); }); changeTheView(); function changeTheView () { var select = control.options.select; var sheet = findTheData ( control, select.subject.element.val()); var view = new google.visualization.DataView(sheet.data); // because we want to retain the choice from previous sheets control.xAxisSelected = control.xAxisSelected == -1 ? findColumnNumber(select.xAxis.default,sheet) : control.xAxisSelected ; control.yAxisSelected = control.yAxisSelected == -1 ? findColumnNumber(select.yAxis.default,sheet) : control.yAxisSelected ; // generate new options manageOptions(select.yAxis.element, sheet).val(control.yAxisSelected); manageOptions(select.xAxis.element, sheet).val(control.xAxisSelected); select.yAxis.element.change(function() { updateChartOptions() ; }); select.xAxis.element.change(function() { updateChartOptions() ; }); select.subject.element.change(function() { changeTheView(); }); updateChartOptions(); function updateChartOptions(){ control.options.selectorElement.css("display","none"); // TODO an enhancement to tooltip could be to find the performance data // for the instituion in the institution sheet control.xAxisSelected = +control.options.select.xAxis.element.val(); control.yAxisSelected = +control.options.select.yAxis.element.val(); view.setColumns([control.xAxisSelected,control.yAxisSelected, {sourceColumn:findColumnNumber (control.options.namecolumn, sheet),role:'tooltip'}]); var options = { title: control.options.select.subject.element.val(), vAxis: { title : view.getColumnLabel(1) } , hAxis: { title : view.getColumnLabel(0) }, legend : { position : "none" }, animation : { duration : control.options.animationDuration , easing: control.options.animationEasing }, height: control.options.height, width: control.options.width }; chart.draw(view,options); } } } function findTheData(control,title) { var sheets = control.package.sheets; for (var i= 0; i < sheets.length; i++) { if (title == sheets[i].name) return sheets[i]; } alert ('couldnt find sheet ' + title); } function initialize () { var initPromise = $.Deferred(); getTheData().done( function (package) { var control = {}; control.package = package; control.divName = "chart"; // set some default options control.options = $.extend({ width : $('#'+control.divName).width(), height : 600, namecolumn: 'Name of Institution', barWidth:$(control.barName).width()/2, animationDuration: 1000, animationEasing: 'inAndOut', selectorElement: $('#'+'selector'), select: { yAxis: { element: $('#'+'yaxis'), default: 'Guardian score/100'}, xAxis: { element: $('#'+'xaxis'), default: 'Average Entry Tariff'}, subject: { element: $('#'+'subject'), default: '1 Med'} } }, package.options); initPromise.resolve(control); }) .fail( function (error) { initPromise.reject(error); }); return initPromise.promise(); } function getTheData() { var massage = $.Deferred(); var package = {}; // override any default options here package.options = {}; // all the sheets will end up here package.sheets = []; getSheetInfo("schema").done( function (info) { package.info = info; makeLight("sheets","busy"); var allSheetsPromise = getSheetData(package); $.when.apply($,allSheetsPromise) .done(function() { // do any data massagin here before resolving package.sheets.sort(function(a,b) { return a.name < b.name ? -1 : (a.name===b.name ? 0 : 1) ; }); massage.resolve(package); makeLight("sheets","idle"); }) .fail ( function(error) { $('#status').html(error); massage.reject(error); makeLight("sheets","failed"); }); }); return massage.promise(); } function getTheDocsData ( entry , light) { var deferred = $.Deferred(); var h = findVizLinkEntry(entry.link); var query = new google.visualization.Query(h); // get all columns query.setQuery('SELECT * options no_format'); query.send(function (response) { if (response.isError()) { deferred.reject('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage()); if (light) makeLight(light,'failed'); } else { deferred.resolve( { entry:entry, data:response.getDataTable(), name: entry.title['$t'].replace(/\d*\s*/,"") }); if (light) makeLight(light,'idle'); } }); return deferred.promise(); } function makeLight (what,img) { // updates the trafci light status d3.select('#'+what+'busy') .attr('src', "//xliberation.com/cdn/img/" +img + ".gif"); } function getSheetInfo(light) { // note that spreadsheet must be published before you can do this as json // this will return the spreadsheet schema info var key = "0At2ExLh4POiZdFd0YUhpZVRPUGxFcW85X2xkMm1vY2c"; var url = "https://spreadsheets.google.com/feeds/worksheets/" + key + "/public/basic?alt=json"; var proxy; return getPromiseData(url,proxy,light); } function getSheetData(package) { // we have the schema, get all the data // this will create an array of promises, one for each worksheet var entries = package.info.feed.entry, sheetPromises = []; for ( var i= 0 ; i < entries.length ;i++) { var p = getTheDocsData(entries[i]); sheetPromises.push(p); p.done (function (data) { package.sheets.push (data); }); } return sheetPromises; } function findVizLinkEntry (links) { // there are several versions of the link for each worksheet // we'll use the google vizualization one. for (var i=0; i < links.length; i++ ) { if (links[i].rel.match(/visualizationApi/)) return links[i].href; } alert ("cant find viz link"); } // general deferred handler for getting json data and creating promise function getPromiseData(url,proxyUrl,light){ var deferred = $.Deferred(); var u = url; if (proxyUrl ) { u = proxyUrl+"?url="+encodeURIComponent(url); } else { // if not proxied, needs to be jSONp u = u + "&callback=?" } backOffJson (deferred,u,light); return deferred.promise(); } function backOffJson (defer,u, light,tries) { // uses exponential back off tries = tries || 0; if(light)makeLight(light,"busy"); $.getJSON(u, null, function (data, textStatus, jqXHR) { if (data.error) { if (data.error.code == 503 || data.error.code == 429) { // a 503 error - so i'll back off tries ++; if (tries <= 5) { // try again var hangAroundFor = (Math.pow(2,tries)*1000) + (Math.round(Math.random() * 1000)); if (light)makeLight(light,"lemon"); setTimeout(function() { backOffJson (defer,u,light,tries); },hangAroundFor); } else { defer.reject(data); } } else { defer.reject(data); } } else { defer.resolve(data); } }) .error(function(res, status, err) { defer.reject("error " + err + " for " + u); }); defer.promise() .done (function () { if (light) makeLight(light,"idle"); }) .fail( function () { if(light)makeLight(light,"failed"); }); return defer; } })() ; </script></head> <body> <div style = "height:180px;"> <div style="width:30%;float:left;"> <div class="infobar" style="width:100%;float:left"> <img class="creditimage" src="http://lh3.googleusercontent.com/-MjqAZOIzLvk/AAAAAAAAAAI/AAAAAAAAAAA/HceWILx6kw4/s49-c/photo.jpg"></img> <div class="creditintro"> <strong>Bruce's comments</strong> <br>This pulls the Guardian UK 2014 University Rankings from Google Docs for basic Google Vizualizations. </div> <div class="creditbody">Ackowledgements<br> <a href='http://blog.ouseful.info/'>Tony Hirst for the idea</a><br> <a href='http://www.guardian.co.uk/news/datablog/2013/jun/04/university-guide-2014-table-rankings'>The Guardian for the data</a><br> <a href='https://ramblings.mcpher.com'><strong>For more stuff like this visit Excel Liberation</strong></a><br> </div> <div class="dataprogress"> A lemon below means Google is too busy - it retries<br> <strong>getting Guardian Uni Data</strong> <img id="unibusy" class="busy" src="//xliberation.com/cdn/img/idle.gif"></img><br> schema<img id="schemabusy" class="busy" src="//xliberation.com/cdn/img/lemon.gif"></img> sheets<img id="sheetsbusy" class="busy" src="//xliberation.com/cdn/img/lemon.gif"></img> </div> <div id = "status" class= "status"> </div> <div class="social"> <a href="https://twitter.com/share" class="twitter-share-button" data-text="Google Viz University 2014 rankings" data-via="brucemcpherson" data-count="none" data-hashtags="d3js">Tweet</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> <a href="https://twitter.com/brucemcpherson" class="twitter-follow-button" data-show-count="false" data-show-screen-name="false">Follow @brucemcpherson</a> <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> <!-- Place this tag where you want the +1 button to render. --> <div class="g-plusone" data-size="small" data-annotation="inline" data-width="200"></div> <!-- Place this tag after the last +1 button tag. --> <script type="text/javascript"> (function() { var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true; po.src = 'https://apis.google.com/js/plusone.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s); })(); </script> </div> </div> </div> <div style="float:right:width:60%;text-align: center;"> <H1>Guardian 2014 UK University rankings</H1> </div> </div> <br> <div id="chart" style="width:100%;"> </div> <div id="selector" style="display:none;">Subject <select id = "subject"></select> yAxis <select id = "yaxis"></select> xAxis <select id = "xaxis"></select></div> </body> </html> |
For help and more information join our forum,follow the blog or follow me on Twitter .