-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchoropleth.js
368 lines (320 loc) · 14.1 KB
/
choropleth.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// set dates
n = new Date();
y = n.getFullYear();
m = String(n.getMonth() + 1).padStart(2,'0');
sd = n.getDate();
ed = sd + 8;
document.getElementById("date_cal_select").min = y + '-' + m + '-' + sd
document.getElementById("date_cal_select").max = y + '-' + m + '-' + ed
document.getElementById("date_cal_select").value = y + '-' + m + '-' + sd
var height = 550;
var width = 800;
//projection and path of map
var projection = d3.geoMercator()
.center([-120.574951, 47.361153])
.scale(5000)
.translate([(width) * .51, (height)*.44]);
var path = d3.geoPath()
.projection(projection);
Promise.all([
d3.json("data/map.geojson"),
d3.csv("data/daily_weather.csv"),
d3.csv("data/wa-zip-code.csv")
]).then(function(values){
var fixed = values[0].features.map(function(f) {
return turf.rewind(f,{reverse:true});
})
ready(null, fixed, values[1],values[2])
}
);
function ready(error, wa_geo, weather_data, lat_long) {
function refreshGraphics() {
var data_selected = document.getElementById("data_select").value
var month_selected = String(Number(document.getElementById("date_cal_select").value.split("-")[1]))
var date_selected = String(Number(document.getElementById("date_cal_select").value.split("-")[2]))
createMapAndLegend(wa_geo,weather_data,data_selected,month_selected,date_selected,lat_long)
}
refreshGraphics()
document.getElementById("date_cal_select").onchange = refreshGraphics;
document.getElementById("data_select").onchange = refreshGraphics;
}
function makeToolTip(svg, zip, pop, feels, cloud, wind, lat, long, icon, cond_desc){
// body of tooltip
svg.append('rect')
.attr('x', 5)
.attr('y', 25)
.attr('width', 280)
.attr('height', 310)
.attr('rx', 50)
.attr("id","container_card");
// zip code header
svg.append("text")
.attr("y", 70)
.attr("x", 145)
.text('Zip Code: ' + zip)
.attr("text-anchor", "middle")
.attr("class", "tool_text tip_header")
// icon
svg.append("svg:image")
.attr('x', 78)
.attr('y', 53)
.attr('width', 120)
.attr('height', 120)
.attr("xlink:href", 'http://openweathermap.org/img/wn/'+icon+'@2x.png')
console.log(cond_desc)
svg.append("text")
.attr("y", 165)
.attr("x", 138)
.text(cond_desc)
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_desc")
// precip
svg.append("text")
.attr("y", 225)
.attr("x", 70)
.text('Chance of')
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_desc")
svg.append("text")
.attr("y", 245)
.attr("x", 70)
.text('Precipitation')
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_desc")
svg.append("text")
.attr("y", 205)
.attr("x", 70)
.text(pop)
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_info")
// feels like
svg.append("text")
.attr("y", 225)
.attr("x", 210)
.text('Feels Like')
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_desc")
svg.append("text")
.attr("y", 205)
.attr("x", 210)
.text(feels)
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_info")
// cloud cover
svg.append("text")
.attr("y", 310)
.attr("x", 70)
.text('Cloud Cover')
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_desc")
svg.append("text")
.attr("y", 290)
.attr("x", 70)
.text(cloud)
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_info")
// wind
svg.append("text")
.attr("y", 310)
.attr("x", 210)
.text('Wind Speeds')
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_desc")
svg.append("text")
.attr("y", 290)
.attr("x", 210)
.text(wind)
.attr("text-anchor", "middle")
.attr("class", "tool_text cat_info")
// link bar
svg.append("svg:image")
.attr('x', 90)
.attr('y', 340)
.attr('width', 45)
.attr('height', 45)
.attr("href", "images/Google Maps.png")
.on("click", function() {
window.open('https://maps.google.com/?q='+lat+','+long+'&ll='+lat+','+long+'&z=8')
.focus();
})
.style("cursor", "pointer");
svg.append("text")
.attr("y",395)
.attr("x", 112)
.text("Google")
.attr("text-anchor", "middle")
.attr("class", "link_text")
svg.append("text")
.attr("y",410)
.attr("x", 112)
.text("Maps")
.attr("text-anchor", "middle")
.attr("class", "link_text")
svg.append("svg:image")
.attr('x', 165)
.attr('y', 345)
.attr('width', 35)
.attr('height', 35)
.attr("href", "images/All Trails.png")
.on("click", function() {
alert('The map will be very zoomed in, scroll out to see nearby trails.')
window.open('https://www.alltrails.com/explore?b_tl_lat='+lat+'&b_tl_lng='+long+'&b_br_lat='+lat+'&b_br_lng='+long)
.focus();
})
.style("cursor", "pointer");
svg.append("text")
.attr("y",396)
.attr("x", 182)
.text("All")
.attr("text-anchor", "middle")
.attr("class", "link_text")
svg.append("text")
.attr("y",411)
.attr("x", 182)
.text("Trails")
.attr("text-anchor", "middle")
.attr("class", "link_text")
}
function createMapAndLegend(wa_geo, weather_data, data_selected, month, day,lat_long) {
var selected = -1
d3.select("#choropleth")
.selectAll("*")
.remove();
// append new svg
var svg = d3.select("#choropleth")
.append("svg")
.attr("width", width)
.attr("height", height)
.append("g");
var color_range={'wind_speed':["#E3E3E3","#3F007D"],
'clouds':["#f9d71c","#BEBEBE"],
'average_feel':['#0096c7','#ededed','#e5383b'],
'pop':['#E5E5E5','#0096c7']}
var color_domain=[0,100]
if (data_selected==='average_feel') {
// calculate [low, median, high] to give color range balance
var low_high=d3.extent(weather_data, function(d){
if (d.month===month && d.day===day){
return +d[data_selected];
}})
var mid=d3.median(weather_data, function(d){
if (d.month===month && d.day===day){
return +d[data_selected];
}})
color_domain=[low_high[0], mid, low_high[1]]
} else if (data_selected==='wind_speed') {
color_domain=[0,d3.max(weather_data, function(d){
if (d.month===month && d.day===day){
return +d[data_selected];
}})]
}
var color = d3.scaleLinear()
.domain(color_domain)
.range(color_range[data_selected])
// create legend
svg.append("g")
.attr("class", "legendLinear")
.attr("transform", "translate("+width*.25+","+height*.92+")");
var legend = d3.legendColor()
.labelFormat(d3.format(".0f"))
.orient('horizontal')
.cells(10)
.shapeWidth(40)
.shapePadding(.5)
.scale(color);
svg.select(".legendLinear")
.call(legend);
var proper_titles={'average_feel':'Feels Like (Temperature - F)',
'clouds':'Cloud Cover (%)',
'wind_speed':'Wind Speeds (mph)',
'pop':'Chance of Precipitation (%)'}
svg.append('text')
.attr('transform', 'translate(' + (width*.5) + ', ' + (height*.9) + ')')
.text(proper_titles[document.getElementById("data_select").value])
.style('font-size', '16px')
.style('text-anchor','middle')
.style('font-weight',1000)
.attr('class','legendLinear')
var svg_tip = d3.select("#choropleth")
.append("svg")
.attr("width", 290)
.attr("height", 450)
.attr("transform", "translate(-25,-70)")
.append("g");
// draw map and color
svg.selectAll("path")
.data(wa_geo)
.enter()
.append("path")
.attr("d", path)
.style("cursor", "pointer")
.attr('id', function(d){
var weather_record = weather_data.filter(function(indiv){
return (indiv.zip_code===d.id && indiv.month===month && indiv.day===day)
})
const point_value = weather_record[0][data_selected]
return color(+point_value)
})
.style('fill', function(d){
return d3.select(this).attr('id')
})
.style('stroke', function(d){
return d3.select(this).attr('id')
})
.on('mouseover', function(d) {
if (selected == -1){
d3.select(this)
.style("fill", "black")
.style("stroke", "black")
// records for tool tip
var req_weather = weather_data.filter(function(indiv){
return (indiv.zip_code===d.id && indiv.month===month && indiv.day===day)
})
var req_coords = lat_long.filter(function(indiv){
return (indiv.Zip===d.id)
})
// values for tool tip
if (d.id.includes('Unzipped')){
var zip = 'N/A';
}
else {
var zip = d.id;
}
var pop = Math.round(req_weather[0]['pop']) + '%';
var feels = parseFloat(req_weather[0]['average_feel']).toFixed(0)+'° F';
var cloud = parseFloat(req_weather[0]['clouds'])+'%';
var wind = parseFloat(req_weather[0]['wind_speed']).toFixed(0)+' mph';
var lat = req_coords[0]['Latitude'];
var long = req_coords[0]['Longitude'];
var icon = req_weather[0]['condition_icon'];
var cond_desc = req_weather[0]['condition_description'];
makeToolTip(svg_tip, zip, pop, feels, cloud, wind, lat, long, icon, cond_desc)
}})
.on('mouseout', function(d){
if (selected == -1){
d3.select(this)
.style('fill', function(d){
return d3.select(this).attr('id')
})
.style('stroke', function(d){
return d3.select(this).attr('id')
})
svg_tip.selectAll("*").remove();
}
})
.on('click', function(d){
if (selected == -1){
selected=d3.select(this)
}
else {
selected.style('fill', function(d){
return selected.attr('id')
})
.style('stroke', function(d){
return selected.attr('id')
})
selected = -1
svg_tip.selectAll("*").remove();
}
})
}