-
Notifications
You must be signed in to change notification settings - Fork 6
/
igsv-gvizcharts.js
126 lines (119 loc) · 4.45 KB
/
igsv-gvizcharts.js
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
/**
* Inline Google Spreadsheet Viewer's Google Chart API integrations.
*
* @file Loads and draws Google Chart API visualizations.
* @license GPL-3.0
*/
(function () { // start immediately-invoked function expresion (IIFE)
google.load('visualization', '1.0', {
'packages' : [
'corechart',
'annotatedtimeline',
'annotationchart',
'gauge',
'geochart',
'timeline'
]
});
jQuery(document).ready(function () {
jQuery('.igsv-chart').each(function () {
var chart_id = jQuery(this).attr('id'),
query = new google.visualization.Query(jQuery(this).data('datasource-href')),
options = lowerFirstCharInKeys(stripPrefixInKeys('chart', jQuery(this).data()));
// Special-case chart options.
options.title = jQuery(this).attr('title');
delete options.type; // handled elsewhere
for (x in options) {
switch (x) {
case 'colors':
options.colors = jQuery(this).data('chart-colors').split(' ');
break;
case 'dimensions':
options.is3D = (3) ? true : false;
delete options.dimensions;
break;
}
}
query.send(function (response) {
if (response.isError()) {
return; // bail, let Google handle displaying errors
}
var data = response.getDataTable(),
chart_el = document.getElementById(chart_id),
chart;
switch (jQuery('#' + chart_id).data('chart-type').toLowerCase()) {
case 'annotatedtimeline':
chart = new google.visualization.AnnotatedTimeLine(chart_el)
break;
case 'annotation':
chart = new google.visualization.AnnotationChart(chart_el)
break;
case 'area':
chart = new google.visualization.AreaChart(chart_el);
break;
case 'bar':
chart = new google.visualization.BarChart(chart_el);
break;
case 'bubble':
chart = new google.visualization.BubbleChart(chart_el);
break;
case 'candlestick':
chart = new google.visualization.CandlestickChart(chart_el);
break;
case 'column':
default:
chart = new google.visualization.ColumnChart(chart_el);
break;
case 'combo':
chart = new google.visualization.ComboChart(chart_el);
break;
case 'gauge':
chart = new google.visualization.Gauge(chart_el);
break;
case 'geo':
chart = new google.visualization.GeoChart(chart_el);
break;
case 'histogram':
chart = new google.visualization.Histogram(chart_el);
break;
case 'line':
chart = new google.visualization.LineChart(chart_el);
break;
case 'pie':
chart = new google.visualization.PieChart(chart_el);
break;
case 'scatter':
chart = new google.visualization.ScatterChart(chart_el);
break;
case 'stepped':
case 'steppedarea':
chart = new google.visualization.SteppedAreaChart(chart_el);
break;
case 'timeline':
chart = new google.visualization.Timeline(chart_el);
break;
}
chart.draw(data, options);
});
// Chart helpers.
function stripPrefixInKeys (prefix, obj) {
var new_obj = {};
for (x in obj) {
if (0 === x.indexOf(prefix)) {
new_obj[x.slice(prefix.length)] = obj[x];
} else {
new_obj[x] = obj[x];
}
}
return new_obj;
}
function lowerFirstCharInKeys (obj) {
var new_obj = {};
for (x in obj) {
new_obj[x.charAt(0).toLowerCase() + x.slice(1)] = obj[x];
}
return new_obj;
}
});
});
})(); // end IIFE