forked from HarlanH/code-for-dc-edu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschool-postload.js
184 lines (160 loc) · 5.99 KB
/
school-postload.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
// set up global variables (uh oh) here
var map = L.map('map', {minZoom:11, maxZoom:14}).setView([38.895111, -77.036667], 12);
var $select = $('#schoolsList');
var schoolmarker = new Array();
var geojson;
var school_lines = new Array();
var infobox = L.control();
var legend = L.control({position: 'bottomleft'});
// the active attribute doesn't change until after onclick is resolved so it's easier to manually track button state
var button_onoff = {
"charter":1,
"public":1,
"elementary":1,
"middle":1,
"high":1,
"allwards":1,
"w1":0,
"w2":0,
"w3":0,
"w4":0,
"w5":0,
"w7":0,
"w8":0
};
$('.btn-group').on('click', 'button', function(e){
var selected = $(this).attr('value');
// console.log(selected);
if($(this).hasClass("active")){
$(this).removeClass("btn-primary");
//$(this).addClass("btn-inverse");
// console.log("deactivating "+selected);
}else{
button_onoff[selected] = 1;
$(this).addClass("btn-primary");
//$(this).removeClass("btn-inverse");
// console.log("activating "+selected);
}
dropdownmenu();
});
$('.btn-group2').on('click', 'button', function(e){
var selected = $(this).attr('value');
// console.log(selected);
button_onoff["allwards"]=0;
button_onoff["w1"]=0;
button_onoff["w2"]=0;
button_onoff["w3"]=0;
button_onoff["w4"]=0;
button_onoff["w5"]=0;
button_onoff["w6"]=0;
button_onoff["w7"]=0;
button_onoff["w8"]=0;
button_onoff[selected] = 1;
dropdownmenu();
});
function dropdownmenu() {
//clear the current content of the select
$select.html('');
// add instructions
$select.append('<option>Pick a School</option>');
//request the JSON data and parse into the select element
//var schools = getAllSchools();
$.getJSON('data/schools.json', function(data){
//iterate over the data and append a select option
$.each(data, function(key, val){
$select.append('<option id="' + val.school_code + '">' + val.schoolname + '</option>');
})
});
}
// the document's ready, so we can do stuff to it
$(document).ready(function() {
L.tileLayer(tileString, {
maxZoom: 18,
attribution: 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://cloudmade.com">CloudMade</a>'
}).addTo(map);
dropdownmenu();
$.getJSON('clusters.geojson', function(data){
geojson = L.geoJson(data, {style: style, onEachFeature: onEachFeature}).addTo(map);
});
infobox.onAdd = function (map) {
this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
this.update();
return this._div;
};
// method that we will use to update the control based on feature properties passed
infobox.update = function (props) {
this._div.innerHTML = props ? props.NBH_NAMES : "Hover over to learn more";
};
infobox.addTo(map);
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
labels = [];
// loop through our density intervals and generate a label with a colored square for each interval
for (var i = 0; i < grades.length; i++) {
div.innerHTML +=
'<i style="background:' + getColor(grades[i] + 1) + '"></i> ' +
grades[i] + (grades[i + 1] ? '–' + grades[i + 1] + '<br>' : '+');
}
return div;
};
legend.addTo(map);
});
// function defs below
function onEachFeature(feature, layer) {
layer.on({
mouseover: highlightNC,
mouseout: resetNC
//click: displaySchools
});
}
function schoolListSelected() {
var myselect = document.getElementById("schoolsList");
var schoolID = myselect.options[myselect.selectedIndex].id;
var school_name = myselect.options[myselect.selectedIndex].value;
var clusters = getClusters(schoolID);
// clusters[1].lat and .lon should work
//console.log(clusters);
// wipe any old school lines
while (school_lines.length > 0) {
map.removeLayer(school_lines.pop());
}
// change the colors of the NCs
// draw lines between the school and the NC centers
// iterate, plot the points and lines
for (i = 0; i < clusters.length; i++) {
// if the _school_ has a location
if (i in clusters && typeof clusters[i].lat === 'number'){
var cluster_id = clusters[i].id
var lineseg = L.polyline([[clusters[i].lat, clusters[i].lon],
[nc_centers.lat_ctr[cluster_id-1], nc_centers.lon_ctr[cluster_id-1]]],
{
weight: 3+((clusters[i].count<10)?0:Math.sqrt(clusters[i].count/4.0)),
orig_weight: 3+((clusters[i].count<10)?0:Math.sqrt(clusters[i].count/4.0)),
opacity: line_opacity(clusters[i].count),
orig_opacity: line_opacity(clusters[i].count),
color: getColor(clusters[i].count),
orig_color: getColor(clusters[i].count),
txt: nc_centers.names[cluster_id-1] + " -> " + school_name + ": " + ((clusters[i].count<10)?"few":clusters[i].count) + " students"
});
lineseg.addTo(map);
lineseg.on({ mouseover: highlightLine, mouseout: resetLine });
school_lines.push(lineseg);
//lineseg.bindPopup(schools[i].school_name + ": " + ((schools[i].count<10)?"few":schools[i].count) + " students")
} // TODO: log that we've got a school iwth no location!
}
// drop a pushpin on the school
// var first_cluster = clusters[Object.keys(clusters)[0]];
// if(schoolmarker.length > 0){
// map.removeLayer(nclayer);
// }
// for(i=0;i<schoolmarker.length;i++) {
// map.removeLayer(schoolmarker[i]);
// schoolmarker.splice(i,1);
// }
// displayClusters(schoolID);
// //console.log(first_cluster);
// var TempMarker = new L.marker([first_cluster.lat, first_cluster.lon]);
// schoolmarker.push(TempMarker);
// //schoolmarker[0].bindPopup(schoolID);
// map.addLayer(schoolmarker[0]);
}