aboutsummaryrefslogtreecommitdiffstats
path: root/public/javascripts/stats-graphs.js
blob: ff3d3a11ad86562c20cc9306232574dd05ffcad3 (plain)
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
/* 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),
            previousPoint = null;

        if (!graph_data.x_values) {
            /* Then there's no data for this graph */
            return true;
        }

        graph_div.css('width', '700px');
        graph_div.css('height', '600px');

        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 = {
            'grid': { 'hoverable': true, 'clickable': true },
            'xaxis': {
                'ticks': graph_data.x_ticks,
                'rotateTicks': 90
            },
            '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);

        graph_div.bind("plotclick", function(event, pos, item) {
            var i, pb, url, name;
            if (item) {
                i = item.dataIndex;
                pb = graph_data.public_bodies[i];
                url = pb.url;
                name = pb.name;
                window.location.href = url;
            }
        });

        /* This code is adapted from:
           http://www.flotcharts.org/flot/examples/interacting/ */

        function showTooltip(x, y, contents) {
            $('<div id="flot-tooltip">' + contents + '</div>').css({
                'position': 'absolute',
                'display': 'none',
                'top': y + 10,
                'left': x + 10,
                'border': '1px solid #fdd',
                'padding': '2px',
                'background-color': '#fee',
                'opacity': 0.80
            }).appendTo("body").fadeIn(200);
        }

        graph_div.bind("plothover", function (event, pos, item) {
            var escapedName, x, y;
            if (item) {
                if (previousPoint != item.dataIndex) {
                    previousPoint = item.dataIndex;
                    $("#flot-tooltip").remove();
                    escapedName = $('<div />').text(
                        graph_data.tooltips[item.dataIndex]).html();
                    showTooltip(item.pageX,
                                item.pageY,
                                escapedName);
                }
            } else {
                $("#flot-tooltip").remove();
                previousPoint = null;
            }
        });
    });
});