-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccsdPoverty.html
351 lines (307 loc) · 10.5 KB
/
ccsdPoverty.html
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
345
346
347
348
349
350
351
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Poverty In Contra Costa County School Districts 2010 - 2015</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<!-- latest version of CARTO.js -->
<script src="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/cartodb.js"></script>
<!-- latest version of simple statistics -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/simple-statistics/1.0.1/simple_statistics.js"></script>
<!-- style sheet for CARTO.js -->
<link rel="stylesheet" href="https://cartodb-libs.global.ssl.fastly.net/cartodb.js/v3/3.15/themes/css/cartodb.css" />
<!-- style sheet for google fonts -->
<link href='//fonts.googleapis.com/css?family=Vollkorn' rel='stylesheet' type='text/css'>
<style>
body {
margin: 0;
padding: 0;
background: whitesmoke;
font-family: "Vollkorn";
color: #0D0000;
}
header {
padding: 6px 10%;
}
#side-panel {
position: absolute;
left: 15px;
width: 300px;
margin: 20px auto;
padding: 0 15px;
background: rgba(256, 256, 256, .8);
border: 1px solid grey;
border-radius: 3px;
z-index: 800;
}
#map {
position: absolute;
width: 100%;
top: 0;
bottom: 0;
}
.legend {
padding: 6px 8px;
font-size: 1em;
background: rgba(255, 255, 255,0.8);
color: #3d3d3d;
box-shadow: 0 0 15px rgba(0,0,0,2);
border: 1px solid;
border-radius: 5px;
width: 300px;
}
.legend h3 {
font-size: 1.1em;
font-weight: bold;
line-height: 1em;
margin: 0;
}
.legend span {
width: 20px;
height: 20px;
float: left;
margin: 0 10px 4px 0;
display: block;
margin-right: 1em;
}
.legend label:after {
content: '';
display: block;
clear: both;
}
#ui-control {
width: 176px;
padding: 8px 25px 8px 15px;
background: rgba(75,75,75,0.8);
box-shadow: 0 0 15px rgba(0,0,0,2);
border-radius: 5px;
color: whitesmoke;
}
#ui-control .min {
float: left;
}
#ui-control .max {
float: right;
margin-right: -15px;
}
.year-slider {
width: 100%;
}
.label {
font-size: 1.1em;
font-weight: bold;
}
.info {
padding: 6px 8px;
width: auto;
font-size: 1em;
background: rgba(25,25,25,0.8);
box-shadow: 0 0 15px rgba(0,0,0,0.2);
border-radius: 5px;
text-align: left;
color: whitesmoke;
}
.info h3 {
margin: 0;
}
</style>
</head>
<body>
<div id='map'></div>
<section id="side-panel">
<h2>Child Poverty In Contra Costa County School Districts 2010 - 2015</h2>
<p>This time series chloropleth map shows the changes in child poverty in Contra Costa County school districts for K-8 schools from 2010 to 2015.</p>
<p>Poverty data comes from the <a href="http://www.census.gov/did/www/saipe/" target="_blank">Small Area Income & Poverty Estimates (SAIPE)</a> for School Districts, Counties, and States.</p>
<p>Map authored <a href="https://bazini627.github.io" target="_blank">Matthew Bacinskas</a>.</p>
</section>
<div id="ui-control">
<label><span class="min">2010</span><span class="max">2015</span>
<input type="range" min="2010", max="2015",
value= "2010", step="1", class="year-slider">
</label>
</div>
<script>
// Object to hold our map options
var options = {
center: [37.9200, -121.89],
zoom: 10,
minZoom: 10,
maxZoom: 10,
zoomControl: false,
},
currentYear = '2010';
map = L.map('map', options); // Create map object and pass it our options object from above
// Load map tiles from CARTO and add to map
var tiles = L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> © <a href="http://cartodb.com/attributions">CartoDB</a> | Map Authored by <a href="https://bazini627.github.io/" target="blank">Matthew Bacinskas</a>',
subdomains: 'abcd',
maxZoom: 19
}).addTo(map);
// Use jquery to get geosjos from CARTO
$.getJSON("https://mjbmaps.carto.com/api/v2/sql?format=GeoJSON&q=SELECT * FROM mjbmaps.ccsd_poverty_2010_2015 where lograde = 'KG'", function(data) {
dataLayer = L.geoJson(data, {
style: function(feature) {
return {
color: 'black',
weight: 1,
fillOpacity: 1,
fillColor: '#1f78b4'
};
}
}).addTo(map);
drawLegend();
drawMap();
drawInfo();
createSliderUI();
});
// Function to draw our map
function drawMap() {
var breaks = getClassBreaks();
// Loop through our layer and set style
dataLayer.eachLayer(function(layer) {
layer.setStyle({
fillColor: getColor(layer.feature.properties['_' + currentYear + 'pr5to17'], breaks)
});
layer.on('mouseover', function() {
// Pass 'this'(layer/district moused over) to highlightFeature
highlightFeature(this);
updateInfo(this);
})
layer.on('mouseout', function() {
unHighlightFeature(this);
})
var popup = "<b>" + layer.feature.properties.name + " County</b>" + "<br>" + "<b>" + currentYear +" Poverty Rate:</b> " + layer.feature.properties['_' + currentYear + 'pr5to17'].toFixed(2).toLocaleString() + "%";
layer.bindPopup(popup);
});
// when mousing over the map
dataLayer.on('mouseover', function() {
$(".info").show(); // display the info panel
});
// when mousing off the map
dataLayer.on('mouseout', function() {
$(".info").hide(); // hide the info panel
});
updateLegend(breaks);
}
// Function to create class breaks for our cholorpleth
function getClassBreaks() {
// Empty array for current year poverty values
var values = [];
dataLayer.eachLayer(function(layer) {
var value = (layer.feature.properties['_' + currentYear + 'pr5to17']).toFixed(2).toLocaleString();
// Push the value for each year onto the values array
values.push(Number(value));
});
var breaks = ss.quantile(values, [0, 0.2, 0.4, 0.6, 0.8, 1]);
return breaks;
}
// Function to set up the color for our breaks
function getColor(d, breaks) {
return d <= breaks[0] ? '#f1eef6':
d <= breaks[1] ? '#d0d1e6':
d <= breaks[2] ? '#a6bddb':
d <= breaks[3] ? '#74a9cf':
d <= breaks[4] ? '#2b8cbe':
'#045a8d'
}
// Function to draw the legend
function drawLegend() {
// Leaflet control object for our legend
var mapLegend = L.control( {position: 'bottomright'} );
// Anonymous function to create html elements for legend
mapLegend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'legend');
return div;
};
mapLegend.addTo(map);
}
// Function to update the legend
function updateLegend(breaks) {
var html = "<h3><span>2010</span> Poverty Rate For School-Age Children</h3><br>";
// Select legend with jQuery
var legend = $('.legend').html(html);
// Loop through our breaks array
for (var i = 0; i < breaks.length - 1; i++) {
// Get the color for each break point
var color = getColor(breaks[i + 1], breaks);
legend.append('<span style="background:' + color + '"></span>' + '<label>' + breaks[i] + ' — ' + breaks[i + 1] + "%" + '</label>');
}
}
// Function to highlight county to be used with a mouseover
function highlightFeature(layer) {
layer.setStyle({
weight: 2,
color: 'yellow',
fillOpacity: 1
});
layer.bringToFront();
}
// Function to unhighlight our highlighted county
function unHighlightFeature(layer) {
layer.setStyle({
weight: 1,
color: 'black',
});
}
// function to create the UI
function createSliderUI(breaks) {
// create a Leaflet control object and store a reference to it in a variable
var sliderControl = L.control({ position: 'bottomleft'} );
// when we add this control object to the map
sliderControl.onAdd = function(map) {
// select an existing DOM element with an id of "ui-control"
var slider = L.DomUtil.get("ui-control");
// when the user clicks on the slider element
L.DomEvent.addListener(slider, 'mousedown', function(e) {
// prevent the click event from bubbling up to the map
L.DomEvent.stopPropagation(e);
});
// return the slider from the onAdd method
return slider;
}
// add the control object containing our slider element to the map
sliderControl.addTo(map);
// select the year slider with jQuery
$('.year-slider')
// attach an event listener
.on("input change", function() {
// jqury val method gives us the current value of the value attribute
currentYear = $(this).val();
//console.log(currentYear);
drawMap(breaks, currentYear);
$('.legend h3 span').html(currentYear);
$('.leaflet-popup-content').html();
});
}
// Function to draw infowindow
function drawInfo() {
// create a new Leaflet control object and position top right
var info = L.control({position: 'topright'});
// when adding control to map
info.onAdd = function(map) {
// create a new div with class of info
var div = L.DomUtil.create('div', 'info');
// return div to control object
return div;
}
// add the empty info div to the map
info.addTo(map);
// select the info panel and hide it
$(".info").hide();
}
// Function to update infowindow
function updateInfo(layer) {
// create shortcut for accessing properties
var props = layer.feature.properties;
// build an HTML string of relevant information about that county
var html = "<h3>"+ props['name'] + "</h3>";
for(var i = 2010; i <= 2015; i++) {
var year = String(i);
html+= year + ": " + props['_' + year + 'pr5to17'].toFixed(2).toLocaleString() + "%<br>"
}
// insert HTML content into empty info panel
$(".info").html(html);
}
</script>
</body>
</html>