From 89e7318805b09cf32c4f919f2b09f522830fb9ec Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Mon, 12 Aug 2013 11:57:27 +0100 Subject: Add a page with experimental statistics on public bodies The statistics on the status of the requests to a particular public body are too slow to calculate on-the-fly, so this commit adds: * Extra columns on public_bodies to store counts of the successful, not held, and overdue request counts for each public body. * A rake task which should be run periodically to update the overdue request count column. If Javascript is not available, the summary statistics are shown as tables. If Javascript is available, graphs are drawn with Flot. --- public/javascripts/stats-graphs.js | 82 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 public/javascripts/stats-graphs.js (limited to 'public/javascripts/stats-graphs.js') diff --git a/public/javascripts/stats-graphs.js b/public/javascripts/stats-graphs.js new file mode 100644 index 000000000..5d3d52152 --- /dev/null +++ b/public/javascripts/stats-graphs.js @@ -0,0 +1,82 @@ +/* From http://stackoverflow.com/a/10284006/223092 */ +function zip(arrays) { + return arrays[0].map(function(_,i){ + return arrays.map(function(array){return array[i]}) + }); +} + +$(document).ready(function() { + $.each(graphs_data, function(index, graph_data) { + var graph_id = graph_data['id'], + dataset, + plot, + graph_data, + graph_div = $('#' + graph_id); + + graph_div.css('width', '700px'); + graph_div.css('height', '400px'); + + dataset = [ + {'color': 'orange', + 'bars': { + 'show': true, + 'barWidth': 0.5, + 'align': 'center' + }, + 'data': zip([graph_data['x_values'], + graph_data['y_values']]) + } + ] + + if (graph_data['errorbars']) { + dataset.push({ + 'color': 'orange', + 'points': { + // Don't show these, just draw error bars: + 'radius': 0, + 'errorbars': 'y', + 'yerr': { + 'asymmetric': true, + 'show': true, + 'upperCap': "-", + 'lowerCap': "-", + 'radius': 5 + } + }, + 'data': zip([graph_data['x_values'], + graph_data['y_values'], + graph_data['cis_below'], + graph_data['cis_above']]) + }); + } + + options = { + 'xaxis': { + 'ticks': graph_data['x_ticks'], + }, + 'yaxis': { + 'min': 0, + 'max': graph_data['y_max'] + }, + 'xaxes': [{ + 'axisLabel': graph_data['x_axis'], + 'axisLabelPadding': 20, + 'axisLabelColour': 'black' + }], + 'yaxes': [{ + 'axisLabel': graph_data['y_axis'], + 'axisLabelPadding': 20, + 'axisLabelColour': 'black' + }], + 'series': { + 'lines': { + 'show': false + } + }, + } + + plot = $.plot(graph_div, + dataset, + options); + }); +}); -- cgit v1.2.3 From c8220c5592528463de2f553091d9c0678f12d509 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Mon, 19 Aug 2013 12:42:22 +0100 Subject: Refer to Javascript object members more idiomatically Crockford / JSLint suggests using dot notation for referring to members of objects where possible. --- public/javascripts/stats-graphs.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'public/javascripts/stats-graphs.js') diff --git a/public/javascripts/stats-graphs.js b/public/javascripts/stats-graphs.js index 5d3d52152..9d9ac5b9d 100644 --- a/public/javascripts/stats-graphs.js +++ b/public/javascripts/stats-graphs.js @@ -7,7 +7,7 @@ function zip(arrays) { $(document).ready(function() { $.each(graphs_data, function(index, graph_data) { - var graph_id = graph_data['id'], + var graph_id = graph_data.id, dataset, plot, graph_data, @@ -23,12 +23,12 @@ $(document).ready(function() { 'barWidth': 0.5, 'align': 'center' }, - 'data': zip([graph_data['x_values'], - graph_data['y_values']]) + 'data': zip([graph_data.x_values, + graph_data.y_values]) } ] - if (graph_data['errorbars']) { + if (graph_data.errorbars) { dataset.push({ 'color': 'orange', 'points': { @@ -43,28 +43,28 @@ $(document).ready(function() { 'radius': 5 } }, - 'data': zip([graph_data['x_values'], - graph_data['y_values'], - graph_data['cis_below'], - graph_data['cis_above']]) + 'data': zip([graph_data.x_values, + graph_data.y_values, + graph_data.cis_below, + graph_data.cis_above]) }); } options = { 'xaxis': { - 'ticks': graph_data['x_ticks'], + 'ticks': graph_data.x_ticks, }, 'yaxis': { 'min': 0, - 'max': graph_data['y_max'] + 'max': graph_data.y_max }, 'xaxes': [{ - 'axisLabel': graph_data['x_axis'], + 'axisLabel': graph_data.x_axis, 'axisLabelPadding': 20, 'axisLabelColour': 'black' }], 'yaxes': [{ - 'axisLabel': graph_data['y_axis'], + 'axisLabel': graph_data.y_axis, 'axisLabelPadding': 20, 'axisLabelColour': 'black' }], -- cgit v1.2.3 From 493c1a270b02bf1cd6030e56babefcca89a81595 Mon Sep 17 00:00:00 2001 From: Mark Longair Date: Mon, 19 Aug 2013 12:43:16 +0100 Subject: Refactor calculation of statistics Move the calculation of statistics on public bodies into the PublicBody model, so that there's less logic in the controller. --- public/javascripts/stats-graphs.js | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'public/javascripts/stats-graphs.js') diff --git a/public/javascripts/stats-graphs.js b/public/javascripts/stats-graphs.js index 9d9ac5b9d..73e19a6fc 100644 --- a/public/javascripts/stats-graphs.js +++ b/public/javascripts/stats-graphs.js @@ -13,6 +13,11 @@ $(document).ready(function() { graph_data, graph_div = $('#' + graph_id); + if (!graph_data.x_values) { + /* Then there's no data for this graph */ + return true; + } + graph_div.css('width', '700px'); graph_div.css('height', '400px'); -- cgit v1.2.3