-
Notifications
You must be signed in to change notification settings - Fork 0
/
pollutant-trends.html
338 lines (287 loc) · 12.9 KB
/
pollutant-trends.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
<!DOCTYPE html>
<head>
<title></title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://raw.githubusercontent.com/nychealth/EH-dataportal/prod-deploy/scss/theme.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/2.6.11/core.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/7.8.7/polyfill.js"></script>
<script src="https://vega.github.io/vega/assets/promise.min.js"></script>
<script src="https://vega.github.io/vega/assets/symbol.min.js"></script>
<script src="https://vega.github.io/vega/assets/fetch.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vega@5/build-es5/vega.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-lite@4/build-es5/vega-lite.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vega-embed@6/build-es5/vega-embed.js"></script>
<style>
.hide {
display: none;
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
/* Rounded sliders */
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="row">
<div class="col-6">
<p class="fs-sm">Choose pollutant:<br>
<div class="">
<button type="button" class="btn polbtn btn-outline-dark active" value="BC">BC</button>
<button type="button" class="btn polbtn btn-outline-dark" value="NO">NO</button>
<button type="button" class="btn polbtn btn-outline-dark" value="NO2">NO<sub>2</sub></button>
<button type="button" class="btn polbtn btn-outline-dark" value="O3">O<sub>3</sub></button>
<button type="button" class="btn polbtn btn-outline-dark" value="PM2.5">PM<sub>2.5</sub></button>
<button type="button" class="btn polbtn btn-outline-dark" value="SO3">SO<sub>2</sub></button>
</div>
</div>
<div class="col-6">
<p class="fs-sm" style="text-align: right;">Choose emissions source:<br>
<div id="subCont"></div>
</div>
</div>
<div class="row mt-2">
<div class="col-12">
<hr>
<p><span class="fs-lg" style="font-weight: bold;"><span id='polPrint' class="">Pollutant</span> by <span id='sourcePrint'>Source</span></span><br>
<em><span id="unitPrint" class="fs-sm"></span></em>
</p>
<div id="vis" style="width: 100%;"></div>
</div>
</div>
<div class="row hide">
<div class="col">
<button type="button" id="seasonbutton" value="1" onclick="tertile('ssn')" class="bybtn btn btn-sm btn-outline-success active">By season</button>
<button type="button" id="tertilebutton" value="2" onclick="tertile('ttl')" class="bybtn btn btn-sm btn-outline-success">By tertile</button>
</div>
</div>
</div>
<script>
var pollutant
var source
var view; // ssn (season) or ttl (tertile)
var sources = [];
var units
var pollutantName
// arrays of pollutant-specific sources
var bcSources = ['Commercial Cooking', 'Industrial Areas', 'Traffic Density'];
var noSources = ['Boiler Density','Traffic NOx Emissions'];
var no2Sources = ['Traffic Density', 'Building Density'];
var o3Sources = ['Traffic Density'];
var pmSources = ['Boiler PM2.5 Emissions','Commercial Cooking','Traffic Density'];
var so2Sources = ['Boilers Using Residual Oil','Night-Time Population Density'];
// SET DEFAULTS
pollutant = 'BC'
pollutantName = 'Black Carbon'
units = 'micrograms per cubic meter'
sources = bcSources
source = sources[0]
createSourceButtons()
// GET POLLUTANT AND OTHER VARIABLES ON INITIAL BUTTON CLICK
const btns = document.querySelectorAll(".polbtn")
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
pollutant = this.value
// declare sources and units for selected pollutant
if (pollutant === 'BC') {
sources = bcSources
units = 'micrograms per cubic meter'
pollutantName = 'Black Carbon'
} else if (pollutant === 'NO') {
sources = noSources
units = 'parts per billion'
pollutantName = 'Nitric Oxide'
} else if (pollutant === 'NO2') {
sources = no2Sources
units = 'parts per billion'
pollutantName = 'Nitrogen Dioxide'
} else if (pollutant === 'O3') {
sources = o3Sources
units = 'parts per billion'
pollutantName = 'Ozone'
} else if (pollutant === 'PM2.5') {
sources = pmSources
units = 'micrograms per cubic meter'
pollutantName = 'Fine Particles'
} else if (pollutant = 'SO2') {
sources = so2Sources
units = 'parts per billion'
pollutantName = 'Sulfur Dioxide'
}
source = sources[0]
// logMetadata();
// create buttons for the pollutant's sources
createSourceButtons()
drawChart();
});
}
function createSourceButtons() {
// loop through Sources and create buttons
document.getElementById('subCont').innerHTML = '';
for (let i = 0; i < sources.length; i++) {
var thisSource = sources[i]
var newButton = `<button type="button" class="btn btn-sm srcbtn btn-outline-secondary mr-1 float-right">${thisSource}</button>`
document.getElementById('subCont').innerHTML += newButton
};
// loop through newly-added buttons, get first, apply Active state
var srcBtns = document.querySelectorAll('.srcbtn')
srcBtns[0].classList.add('active')
// put listener on source buttons
for (let x = 0; x < srcBtns.length; x++) {
srcBtns[x].addEventListener("click", function() {
// remove "active" from classnames
var activeSource = document.querySelectorAll('.srcbtn.active')
activeSource[0].classList.remove('active')
// apply Active to selected source button
this.className += " active";
source = this.innerHTML;
drawChart();
})
}
}
// re-draw chart on window re-sizing
window.onresize = function(event) {
drawChart();
};
var chartSpec = {};
function drawChart() {
document.getElementById('polPrint').innerHTML = pollutantName
document.getElementById('sourcePrint').innerHTML = source
document.getElementById('unitPrint').innerHTML = units
let width = document.getElementById('vis').offsetWidth / 2.25
chartSpec = {
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"title": null,
"width": width,
"description": "NYCCAS data by season, source, and pollutant",
"data": {
"url": "https://raw.githubusercontent.com/nycehs/nyccas-vis-demo/main/TrendChart.csv"
},
"config": {
"title": {"anchor": "start", "fontSize": 14},
"axisY": {"title": null},
"axisX": {"title": "Year","grid": false},
"style": {
"cell": {"stroke": "transparent"},
"line": {"stroke": "#1696d2", "strokeWidth": 3},
"text": {"fontSize": 11, "fontWeight": "bold", "size": 11,"align": "left"},
"group-title": {"fontWeight": "normal"}
},
"range": {
"category": [
"#810f7c",
"#b3cde3",
"#8c96c6"
]
}
},
"columns": 2,
"transform": [
{
"filter": `datum.Pollutant === '${pollutant}' && datum.Source === '${source}'`
}
],
"mark": {"type": "line", "point": true},
"encoding": {
"facet": {
"field": "Season",
"type": "nominal",
"columns": 2,
"title": null,
"sort": ["Winter", "Spring", "Summer", "Fall"],
"header": {"labelFontSize": 15,"labelAlign":"left","labelAnchor": "start"}
},
"x": {"timeUnit": "year", "field": "Time"},
"y": {"field": "Value", "type": "quantitative"},
"tooltip": [
{"field": "Value", "title": "Value"},
{"field": "Tertile", "title": "Tertile"}
],
"color": {
"field": "Tertile",
"type": "nominal",
"legend": null
}
}
};
vegaEmbed('#vis', chartSpec);
}
function logMetadata() {
console.log('*****METADATA*****')
console.log('pollutant: ' + pollutant)
console.log('sources: ' + sources)
console.log('source: ' + source)
console.log('units: ' + units)
}
function tertile(x) {
view = x;
console.log(view)
if (x === 'ssn') {
document.getElementById('tertilebutton').classList.remove('active')
document.getElementById('seasonbutton').classList.add('active')
// document.getElementById('3up').classList.add('hide')
// document.getElementById('4up').classList.remove('hide')
} else if ( x === 'ttl') {
document.getElementById('tertilebutton').classList.add('active')
document.getElementById('seasonbutton').classList.remove('active')
// document.getElementById('4up').classList.add('hide')
// document.getElementById('3up').classList.remove('hide')
}
}
drawChart();
</script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>