-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschool.ejs
344 lines (322 loc) · 18.8 KB
/
school.ejs
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
<!DOCTYPE html>
<html>
<head>
<%- partial("partials/_header", { title: "Measure G Spending by School"}) %>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
</script>
<!--Google analytics snippet -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-35908939-11', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<!-- Wrap all page content here -->
<div id="wrap">
<%- partial("partials/_navbar") %>
<!-- Begin page content -->
<div class="center-container">
<div class="center-row">
<div class="container">
<div class="row">
<div class="col-sm-12">
<h2 id="school_name"></h2>
<div id="school_address"></div>
</div>
</div>
<div class="row" id="school_summary" style="display:none">
<div class="col-xs-12 col-sm-6 col-md-8">
<div class="row">
<div class="col-xs-12 col-md-4">
<div class="stat_label">Measure G Spending (<%= latestExpenditureDataYear %>)</div>
<div class="spending" id="total_spending"></div>
</div>
<div class="col-xs-12 col-md-4">
<div class="stat_label">Per Student</div>
<div class= "spending" id="per_student_spending"></div>
</div>
<div class="col-xs-12 col-md-4">
<div class="stat_label">Number of Students</div>
<div class= "stat" id="total_enrollment"></div>
<div>Note: enrollment data from <%= latestEnrollmentYear %></div>
</div>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4">
<div id="school_map"></div>
</div>
</div>
<div id="table_div"></div>
<div class="row">
<div class="mdl-tabs mdl-js-tabs mdl-js-ripple-effect" id="tabs">
<div class="mdl-tabs__tab-bar">
<a href="#all-programs-panel" class="mdl-tabs__tab is-active all-programs-data" id="all-programs-tab">All Programs</a>
<a href="#objects-panel" class="mdl-tabs__tab is-active object-level-data" id="objects-tab" style="display:none"></a>
<a href="#glossary-panel" class="mdl-tabs__tab is-active glossary-level-data" id="glossary-tab" style="display:none"></a>
</div>
<div class="mdl-tabs__panel is-active all-programs-data" id="all-programs-panel">
<div id="program_chart"></div>
</div>
<div class="mdl-tabs__panel object-level-data" id="objects-panel">
<div id="object_chart"></div>
</div>
<div class="mdl-tabs__panel glossary-level-data" id="glossary-panel">
<div id="glossary_info"></div>
<div id="gloss_summary"></div>
</div>
</div>
</div>
</div>
</div>
</div> <!-- end wrap -->
<!-- Place your code in the script tags below -->
<script type="text/javascript">
google.charts.load('current', {'packages':['bar','line']});
window.onload = function() {
var map;
function initMap(center, name) {
map = new google.maps.Map(document.getElementById('school_map'), {
center: center,
zoom: 15,
disableDefaultUI: true,
zoomControl: true
});
var marker = new google.maps.Marker({
position: center,
map: map,
title: name,
icon: "https://maps.google.com/mapfiles/kml/shapes/schools_maps.png"
});
};
function formatSchoolAddress(street,city,state,zip) {
return street + " " + city + ", " + state + " " + zip;
}
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var schoolID = getParameterByName("schoolID");
// global variables in _harp.json
var latestEnrollmentYear = <%= latestEnrollmentYear %>;
var latestExpenditureDataYear = <%= latestExpenditureDataYear %>;
var yearToDisplay = {
2012: "2012-13",
2013: "2013-14",
2014: "2014-15",
2015: "2015-16",
2016: "2016-17",
2017: "2017-18",
2018: "2018-19"
};
// See pretty print here: https://github.com/openoakland/measure-g/issues/50
var sql = "SELECT ll.ousd_site,ll.ncessch,ll.latcod as latitude,ll.loncod as longitude,ll.schnam as school_name,ll.ncessch as nces,ll.lstree as street,ll.lcity as city,ll.lstate as state,ll.lzip as zip,ll.gslo as grade_low,ll.gshi as grade_high,ll.site_type, e.total_enrollment, to_char(mg.ytd_actual, '$999,999') as ytd_actual_money, to_char(round((mg.ytd_actual / e.total_enrollment)::numeric, 2),'$999,999.99') per_student_money FROM measure_g_schools_lat_long ll, <%= enrollmentDataTable %> e,(SELECT site, SUM(m.ytd_actual) as ytd_actual, m.year FROM <%= expenditureDataTable %> m GROUP BY m.site, m.year) mg WHERE ll.ousd_site = %S AND e.ncessch = ll.ncessch AND e.year = %ENROLLMENTYEAR AND mg.site = %S AND mg.year = %MEASUREGYEAR";
sql = sql.replace(/%S/gi, schoolID).replace(/%ENROLLMENTYEAR/gi, latestEnrollmentYear).replace(/%MEASUREGYEAR/gi, latestExpenditureDataYear);
var schoolName = "";
// Call cartodb
$.getJSON('https://jbaldo.cartodb.com/api/v2/sql/?q='+sql, function(data) {
$.each(data.rows, function(key, row) {
schoolName = row.school_name;
$('#school_name').text(schoolName);
$('#school_address').text(formatSchoolAddress(row.street,row.city,row.state,row.zip));
var center = {lat: row.latitude, lng: row.longitude};
initMap(center,row.school_name);
$('#total_spending').text(row.ytd_actual_money);
$('#per_student_spending').text(row.per_student_money);
$('#total_enrollment').text(row.total_enrollment);
$('#school_summary').show();
});
});
var sql2 = "SELECT prog.short as program,a.year, COALESCE(SUM(ytd_actual), 0) as ytd_actual FROM programs prog, <%= expenditureDataTable %> a WHERE prog.id = a.program_id AND a.site = %S GROUP BY program, a.year ORDER BY program, a.year".replace(/%S/gi, schoolID);
var programTotalsJson = [];
var programs_chart;
var programTotalData;
$('#all-programs-tab').click(function(){
$('#object-level-data').removeClass('is-active');
// $('#all-programs-data').addClass('');
});
function drawChart() {
programTotalData = google.visualization.arrayToDataTable(dataByProgram);
var options = {
chart: {
title: 'Click on a program for more detail',
subtitle: ''
},
height: 400,
width: $(window).width() * .8,
vAxis:{
format: '$#,###',
textStyle: {
fontSize: 14
}
},
hAxis: {
textStyle: {
fontSize: 14
}
}
};
programs_chart = new google.charts.Line(document.getElementById('program_chart'));
programs_chart.draw(programTotalData, google.charts.Bar.convertOptions(options));
google.visualization.events.addListener(programs_chart, 'select', selectHandler);
function selectHandler(e) {
var dataByObjects = [];
var selection = programs_chart.getSelection();
if (selection.length > 0){
var programName = programTotalData.getColumnLabel(selection[0].column);
var object_query = "SELECT a.year, a.ytd_actual, o.short as obj_desc FROM <%= expenditureDataTable %> a JOIN programs p ON a.program_id = p.id AND p.short = '%P' LEFT JOIN objects o ON a.object_id = o.id WHERE a.site = 102 AND ytd_actual > 0 ORDER BY obj_desc".replace(/%P/gi, programName);
// TODO: get dynamically from Years table (or set in config)
var supportedYears = [
2012,
2013,
2014,
2015,
2016,
2017,
2018
];
var yearToColumnMapping = {};
var tableHeader = ['Expense Type'];
// Dynamically create Google Visualization DataTable columns based on # of years
for(var i = 0; i < supportedYears.length; i++){
var year = supportedYears[i];
var yearKey= year.toString();
yearToColumnMapping[yearKey] = _.indexOf(supportedYears,year,true);
tableHeader.push(yearToDisplay[yearKey]);
}
$.getJSON('https://jbaldo.cartodb.com/api/v2/sql/?q='+object_query, function(data) {
_.each(_.groupBy(data.rows, 'obj_desc'), function(yearsOfSpending, object){
var objectSpendingByYear = [object];
// Instead of complex SQL joining,
// start with 0 value for each year and replace with spending if it exists
for (var i = 0; i < supportedYears.length; i++) {
objectSpendingByYear.push(0);
}
_.each(yearsOfSpending, function(year){
var column = yearToColumnMapping[year.year] + 1;
objectSpendingByYear[column] = year.ytd_actual;
});
dataByObjects.push(objectSpendingByYear);
});
//Add a header
dataByObjects.unshift(tableHeader);
}).then(function(){
var obj_data = google.visualization.arrayToDataTable(dataByObjects);
obj_data.sort([{column: 1, desc:true}]);
var objectChart = new google.charts.Bar(document.getElementById('object_chart'));
var numBars = dataByObjects.length - 1;
var chartHeight = numBars * 70;
var options = {
chart: {
},
bars: 'horizontal', // Required for Material Bar Charts.
height: chartHeight,
width: $(window).width() * .8,
axes: {
x: {
0: { side: 'top', label: 'Amount spent'} // Top x-axis.
}
},
hAxis:{
format: '$#,###'
}
}
objectChart.draw(obj_data, google.charts.Bar.convertOptions(options));
google.visualization.events.addListener(objectChart, 'select', selectHandler2);
$('.all-programs-data').removeClass('is-active');
$('#objects-tab').text(programName);
$('.object-level-data').addClass('is-active');
$('#objects-tab').show();
$('#objects-tab').click();
function selectHandler2(f) {
var selection2 = objectChart.getSelection();
if (selection2.length > 0){
var objectName = obj_data.getValue(selection2[0].row, 0);
document.getElementById("glossary_info").innerHTML = objectName;
var glossary_query = "SELECT a.short, a.term_def FROM objects a WHERE a.short LIKE '%OBJNAME'".replace(/%OBJNAME/gi, objectName, objectName).replace('&', '%26');
$.getJSON('https://jbaldo.cartodb.com/api/v2/sql/?q='+glossary_query, function(data) {
if(data.rows.length > 0){
$.each(data.rows, function(key, row) {
var glossaryTerm = row.term_def;
if(glossaryTerm) {
$('#gloss_summary').text(glossaryTerm);
$('#gloss_summary').show();
}
});
} else {
$('#gloss_summary').text("Not found");
}
}).then(function(){
$('.object-level-data').removeClass('is-active');
$('#glossary-tab').text(objectName);
$('.glossary-level-data').addClass('is-active');
$('#glossary-tab').show();
$('#glossary-tab').click();
});
}
}
});
}
} //end select handler
}
$(window).resize(function() {
drawChart();
});
var dataByProgram = [];
// TODO: get dynamically from Years table (or set in config)
var supportedYears = [
2012,
2013,
2014,
2015,
2016,
2017,
2018
];
var yearToRowMapping = {};
var tableHeader = ['School Years'];
// Dynamically create Google Visualization DataTable rows based on # of years
for(var i = 0; i < supportedYears.length; i++){
var year = supportedYears[i];
var yearKey = year.toString();
yearToRowMapping[yearKey] = _.indexOf(supportedYears,year,true);
dataByProgram.push([yearToDisplay[year]]);
}
$.getJSON('https://jbaldo.cartodb.com/api/v2/sql/?q='+sql2, function(data) {
var groupedByProgram = _.groupBy(data.rows, 'program');
_.each(groupedByProgram, function(yearsOfSpending, program){
// Dynamically create a row for every program
tableHeader.push(program);
// Instead of complex SQL joining,
// start with 0 value for each program and replace with spending if it exists
for (var i = 0; i < dataByProgram.length; i++) {
dataByProgram[i].push(0);
}
_.each(yearsOfSpending, function(year){
var row = yearToRowMapping[year.year];
var column = tableHeader.length - 1;
dataByProgram[row][column] = year.ytd_actual;
});
});
//Add the table header
dataByProgram.unshift(tableHeader);
}).then(function(){
google.charts.setOnLoadCallback(drawChart);
});
}
var sqlCompare = "SELECT schnam, ousd_site FROM measure_g_schools_lat_long"
</script>
<%- partial("partials/_uservoice.ejs") %>
</body>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCqC5S-8nx5nXnD16I_FzA5KNhyjyFLRgw" async defer></script>
</html>