-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle_stacked_chart.js
292 lines (250 loc) · 8.77 KB
/
google_stacked_chart.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
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
google.charts.load('45', {packages: ['corechart']});
google.charts.setOnLoadCallback(queryData);
var data;
var options;
function queryData() {
var query = new google.visualization.Query(
"https://docs.google.com/spreadsheets/d/1gof3Gy7DKKGuMAVSFN2NFMuQ8UTJovGsRzeXfTSOSV4/gviz/tq?gid=935543198&headers=2&range=C:M"
/*"https://docs.google.com/spreadsheets/d/1QG0spehL2ofBCEtkZLcpZwkIczHHd0w9vaifp3lzHa8/gviz/tq?gid=1511124293&headers=2&range=A:K"
*/
);
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
// GET DATA FROM GOOGLE SPREADSHEET
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var responseData = response.getDataTable();
data = new google.visualization.DataTable();
// data.addColumn('date', 'Date');
data.addColumn('string', 'title');
var colCount = responseData.getNumberOfColumns();
for (var i = 2; i< colCount; i++) {
//NOTE: tooltips in stacked charts follow the column of the data
data.addColumn('number', responseData.getColumnLabel(i).split("$")[1]);
data.addColumn({'type': 'string', 'role': 'tooltip', 'p': {'html': true}});
}
// var pvaData = new google.visualization.DataTable();
// pvaData.addColumn('string', 'Date');
// pvaData.addColumn('number', 'PVA Score');
// pvaData.addColumn({'type': 'string', 'role': 'tooltip', 'p': {'html': true}});
var rowCount = responseData.getNumberOfRows();
var ticks = [];
for (var j = 0; j < rowCount; j++) {
var date = responseData.getValue(j, 0);
var title = responseData.getValue(j, 1);
//var speech = responseData.getValue(j, 4); // + " - " + date.toLocaleString();
//var tooltip = date.toLocaleDateString() + " - " + responseData.getValue(j, 2);
// var tooltip = "<div class='point-hover'><b>" + responseData.getValue(j, 2)
// + "</b><br/><font size='smaller'><i>" + date.toLocaleDateString() + "</i> (click for details)</font></div>";
// var tooltip = "<div class='point-hover'><i>" + date.toLocaleDateString() +"</i><br/><b>" + responseData.getValue(j, 2)
// + "</b><br/>" + responseData.getValue(j, 3) + "</div>";
// var tickValue = responseData.getValue(j, 2);
// var tickMark = {v: date, f: tickValue};
// ticks[j] = tickMark;
//data.addRow([date, title, two, three, four]);
//var pvaRow = [moment(date).format("MMM D, YYYY")];
var row = [moment(date).format("MMM D, YYYY")];
var total = 0;
for (var i = 1, k = 2; k < colCount; i += 2, k++) {
var value = responseData.getValue(j,k);
row[i] = value;
total += value;
row[i+1] = "<div class='point-hover'> <i>" + moment(date).format("MMM D, YYYY") + "<br/>" + title + "</i><br/>"
+ "<b>" + data.getColumnLabel(i) + ": " + value + "</b><br/>"
+ "" + responseData.getColumnLabel(k).split("$")[0] + "</div>";
// pvaRow[1] = total;
//
// pvaRow[2] = "<div class='point-hover'> <i>" + responseData.getValue(j,1) + "</i><br/>"
// + "<b>PVA Score: " + total + "</b></div>";
}
if (total > 0) {//simple check to weed out incomplete data
data.addRow(row);
// pvaData.addRow(pvaRow);
}
}
// var csv = google.visualization.dataTableToCsv(data);
// console.log(csv);
options = {
isStacked: 'true',
backgroundColor: 'transparent',
colors:['#C8CFC5', '#ffbb78','#9FB8C3', '#B95352', '#EFDD7B', "#e377c2", "#98df8a", "#ff9896", "#6D81F5"],
height: 530,
orientation: 'horizontal',
bar: {
groupWidth: '6'
},
legend: {
position: 'top',
maxLines: 3},
// omit width, since we set this in CSS
chartArea: {
width: '93%',
top: 100,
left: 50
},
dataOpacity: 0.8, // opacity of points
crosshair: {
trigger: 'selection',
color: '#B95352'
},
pointSize: 4,
hAxis: {
//title: "Date of Speech",
gridlines: {
color: '#333', count: 10
},
textStyle: {
fontSize: '11'
},
titleTextStyle: {
color: '#000000',
fontSize: '12',
italic: false
},
slantedText: true,
slantedTextAngle: "45",
maxTextLines: 2,
maxAlternation: 1
// ,
// ticks: ticks
},
tooltip: {
trigger: 'hover',
isHtml: true,
ignoreBounds: false
},
vAxis: {
gridlines: {
color: '#d7d7d7', count: 9
},
textStyle: {
fontSize: '10'
},
titleTextStyle: {
color: '#000000',
fontSize: '10',
italic: false
},
maxTextLines: 2,
format:'#%'
},
explorer: {
axis: 'horizontal',
actions: ['dragToZoom', 'rightClickToReset'],
keepInBounds: false // set to false to re-enable panning
},
annotations: {
textStyle: {
fontName: 'Arial',
fontSize: 13,
bold: false,
italic: false,
// The color of the text.
color: '#333',
// The color of the text outline.
auraColor: '#fff',
// The transparency of the text.
opacity: 0.8
}
}
};
// var pvaOptions = {
// backgroundColor: 'transparent',
// colors:['#C8CFC5', '#ffbb78','#9FB8C3', '#B95352', '#EFDD7B', "#e377c2", "#98df8a", "#ff9896", "#6D81F5"],
// height: 300,
// legend: {
// position: 'top',
// maxLines: 3},
// // omit width, since we set this in CSS
// chartArea: {
// width: '93%',
// top: 50,
// left: 50
// },
// dataOpacity: 0.8, // opacity of points
// crosshair: {
// trigger: 'selection',
// color: '#B95352'
// },
// pointSize: 4,
// hAxis: {
// //title: "Date of Speech",
// gridlines: {
// color: '#333', count: 10
// },
// textStyle: {
// fontSize: '11'
// },
// titleTextStyle: {
// color: '#000000',
// fontSize: '12',
// italic: false
// },
// slantedText: true,
// slantedTextAngle: "45",
// maxTextLines: 2,
// maxAlternation: 1
// // ,
// // ticks: ticks
// },
// tooltip: {
// trigger: 'hover',
// isHtml: true,
// ignoreBounds: false
// },
// vAxis: {
// gridlines: {
// color: '#d7d7d7', count: 9
// },
// textStyle: {
// fontSize: '10'
// },
// titleTextStyle: {
// color: '#000000',
// fontSize: '10',
// italic: false
// },
// maxTextLines: 2
// },
// // explorer: {
// // axis: 'horizontal',
// // actions: ['dragToZoom', 'rightClickToReset'],
// // keepInBounds: false // set to false to re-enable panning
// // },
// annotations: {
// textStyle: {
// fontName: 'Arial',
// fontSize: 13,
// bold: false,
// italic: false,
// // The color of the text.
// color: '#333',
// // The color of the text outline.
// auraColor: '#fff',
// // The transparency of the text.
// opacity: 0.8
// }
// }
//
// };
updateChart();
// var chartPva = new google.visualization.LineChart(document.getElementById('chart2_div'));
// chartPva.draw(pvaData, pvaOptions);
}
function updateChart(){
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
$(window).resize(function() {
if(this.resizeTO) clearTimeout(this.resizeTO);
this.resizeTO = setTimeout(function() {
$(this).trigger('resizeEnd');
}, 500);
});
$(window).on('resizeEnd', function() {
$('chart_div').empty();
updateChart();
});