-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogle-mobility.js
382 lines (345 loc) · 11.2 KB
/
google-mobility.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
function buildVis1(data) {
const categories = [
'residential_percent_change_from_baseline',
'grocery_and_pharmacy_percent_change_from_baseline',
'parks_percent_change_from_baseline',
'transit_stations_percent_change_from_baseline',
'retail_and_recreation_percent_change_from_baseline',
'workplaces_percent_change_from_baseline']
// dimensions
var chartWidth = 400,
chartHeight = 100;
var margin = {
top: 20,
right: 150,
bottom: 20,
left: 50
}
var captionWidth = 100;
var sourceWidth = chartWidth,
sourceHeight = chartHeight;
var parseTime = d3.timeParse('%Y-%m-%d');
var formatTime = d3.timeFormat('%m/%d/%Y');
var bisect = d3.bisector(d => d.date).left;
// define axis range
var xScale = d3.scaleTime()
.domain([new Date(2020, 1, 15), new Date(2020, 3, 11)])
.range([0, chartWidth]);
var yScale = d3.scaleLinear()
.domain([-100, 100])
.range([chartHeight, 0]);
var colorScale = d3.scaleOrdinal()
.domain(categories)
.range(['red', 'brown', 'green', 'orange', 'blue', 'gray'])
var textScale = d3.scaleOrdinal()
.domain(categories)
.range(['Residential', 'Groceries & Pharmacies', 'Parks', 'Public Transit', 'Retail & Recreation', 'Workplaces'])
var pathMap = function(cat) {
return d3.line()
.x(d => xScale(d.date))
.y(d => yScale(d[cat]));
}
// parse string to int values
data = data.map(d => {
d.date = parseTime(d.date);
categories.forEach(cat => {
d[cat] = parseInt(d[cat])
})
return d;
})
// limit to top 5 countries
var countryToShow = ['United States', 'United Kingdom', 'Spain', 'Italy', 'Brazil']
data = data.filter(d => countryToShow.includes(d.country_region));
// aggregate entries only
data = data.filter(d => d.sub_region_1 === '');
// countryToShow.forEach(country => {
// data.push(zeroIndex(country))
// })
// nest data by country
var nested = d3.nest()
.key(d => d.country_region)
.sortKeys((a,b) => {
return countryToShow.indexOf(a) - countryToShow.indexOf(b)
})
.entries(data);
console.log(nested)
var svg = d3.select('#mobility-vis')
// .append('g')
// .attr('class', 'chartWrapper')
.selectAll('svg')
.data(nested)
.enter()
.append('svg')
.attr('width', chartWidth + margin.left + margin.right + captionWidth)
// .attr('height', height + captionWidth + margin.top + margin.bottom)
.attr('height', chartHeight + margin.top + margin.bottom)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
var cursorDate = svg.append('g')
.append('text')
.attr('class', 'cursorDate')
.attr('y', chartHeight + 12)
.attr('font-size', '10px')
.attr('text-anchor', 'left');
var cursorLine = svg.append('line')
.style('stroke', 'black')
.style('stroke-width', 1)
// .attr('x1', xScale(new Date(2020, 2, 11)))
// .attr('x2', xScale(new Date(2020, 2, 11)))
.attr('y1', 0)
.attr('y2', chartHeight)
.attr('opacity', 0)
.style('pointer-events', 'none');
var cursorCaption = svg.append('g')
.attr('class', 'captionBox')
// .attr('transform', 'translate(0,' + (height + margin.bottom) + ')')
.attr('transform', 'translate(' + (chartWidth + 20) + ',' + margin.top + ')')
.append('rect')
// .attr('class', 'captionBox')
.attr('width', captionWidth)
.attr('height', chartHeight)
.attr('opacity', 0);
var cursorText = svg.append('text')
.attr('class', 'cursorText')
.attr('x', xScale(new Date(2020, 2, 11)) + 5)
// .attr('x', width + margin.left)
.attr('y', 15)
.attr('font-size', '12px')
.text('WHO declares COVID-19 a pandemic')
.attr('opacity', 0)
// .attr('text-anchor', 'middle')
// .style('pointer-events', 'none')
// .attr('border', 'black');
categories.forEach((cat,i) => {
d3.selectAll('.captionBox').append('text')
.attr('class', 'caption_'+cat)
// .text(d => {
// console.log(''.concat(textScale(cat),':',d.values[idx][cat]))
// return ''.concat(textScale(cat),':',d.values[idx][cat])
// })
.attr('x', 0)
.attr('y', i*15)
.attr('font-size', '12px')
.attr('fill', colorScale(cat))
.text(textScale(cat)+':')
})
//TODO
function mouseover() {
cursorLine.attr('opacity', 1)
d3.selectAll('.x-axis')
.call(d3.axisBottom(xScale).ticks(0))
return mousemove.call(this)
}
function mousemove() {
var date = xScale.invert(d3.mouse(this)[0]);
var idx = 0;
if (formatTime(date) === '03/11/2020') {
cursorText.attr('opacity', 1)
} else {
cursorText.attr('opacity', 0)
}
cursorDate
.attr('x', xScale(date))
.text(formatTime(date))
cursorLine
.attr('x1', xScale(date))
.attr('x2', xScale(date))
// .attr('cy', d => {
// idx = bisect(d.values, date, 0, d.values.length-1)
// return yScale(d.values[idx]['residential_percent_change_from_baseline'])
// })
// cursorCaption.attr('x', xScale(date))
// categories.forEach((cat,i) => {
// d3.select('.captionBox').append('text')
// .text(d => {
// console.log(''.concat(textScale(cat),':',d.values[idx][cat]))
// return ''.concat(textScale(cat),':',d.values[idx][cat])
// })
// .attr('x', xScale(date))
// .attr('y', i*20)
// .attr('font-size', '10px')
// })
categories.forEach((cat,i) => {
d3.selectAll('.caption_'+cat)
// .attr('x', xScale(date))
.text(d => {
idx = bisect(d.values, date, 0, d.values.length-1)
return ''.concat(textScale(cat),': ',d.values[idx][cat],'%')
})
})
// cursorText.attr('opacity', () => {
// return (formatTime(date) === '03/11/2020' ? 1 : 0)
// })
// if (formatTime(date) === '03/11/2020') {
// .attr('opacity', 0)
// } else {
// cursorText.text('')
// }
}
function mouseout() {
cursorDate.text('')
cursorLine.attr('opacity', 0)
cursorText.attr('opacity', 0)
d3.selectAll('.x-axis')
.call(d3.axisBottom(xScale).ticks(2))
// d3.selectAll('.captionBox').selectAll('text').text(textScale(cat)+':')
}
// // add interaction
// svg.append('rect')
// .attr('class', 'hoverbox')
// .attr('width', chartWidth)
// .attr('height', chartHeight)
// .style('pointer-events', 'all')
// .style('fill', 'gray')
// .style('opacity', 0.1)
// .on('mouseover', mouseover)
// .on('mousemove', mousemove)
// .on('mouseout', mouseout);
// add axes
svg.append('g')
.attr('class','x-axis')
.attr('transform', 'translate(0,' + chartHeight + ')')
.call(d3.axisBottom(xScale).ticks(2));
svg.append('g')
.call(d3.axisLeft(yScale).ticks(5).tickFormat(x => x>0 ? '+'+x+'%' : x+'%'));
// additional info line
// var info = d3.select('.chartWrapper').append('g')
// .attr('class', 'info-pandemic');
svg.append('line')
.style('stroke', 'black')
.style('stroke-width', 1)
.style('stroke-dasharray', ('3, 3'))
.attr('x1', xScale(new Date(2020, 2, 11)))
.attr('x2', xScale(new Date(2020, 2, 11)))
.attr('y1', 0)
.attr('y2', chartHeight)
// .on('mouseover', function() {
// cursorText.attr('opacity', 1)
// })
// .on('mouseout', function() {
// cursorText.attr('opacity', 0)
// })
// info.append('text')
// .attr('class', 'info-pandemic-text')
// .attr('text-anchor', 'middle')
// .attr('x', xScale(new Date(2020, 2, 11)) + margin.left + 10)
// .attr('y', margin.top)
// // .attr('dy', -5)
// .style('font-size', '12px')
// .text('WHO declares COVID-19 a pandemic')
// draw line graphs
categories.forEach(cat => {
svg.append('g')
.attr('class', cat)
.append('path')
.attr('fill', 'none')
.attr('stroke', colorScale(cat))
.attr('stroke-width', 1.5)
.attr('d', d => pathMap(cat)(d.values))
})
svg.append('text')
.attr('class', 'country')
.attr('text-anchor', 'middle')
.attr('x', chartWidth/2)
.attr('y', 0)
.attr('dy', -margin.top/2)
.style('font-size', '12px')
.text(d => d.key)
// add interaction
svg.append('rect')
.attr('class', 'hoverbox')
.attr('width', chartWidth)
.attr('height', chartHeight)
.style('pointer-events', 'all')
.style('fill', 'gray')
.style('opacity', 0.1)
.on('mouseover', mouseover)
.on('mousemove', mousemove)
.on('mouseout', mouseout);
// add legend
// d3.select('#mobility-vis')
// .append('svg')
// .attr('class', 'legend')
// .attr('transform', 'translate(' + 4*width + ',0)')
// .selectAll('dot')
// .data(categories)
// .enter()
// .append('circle')
// .attr('cx', 20)
// .attr('cy', (d,i) => 20+i*20)
// .attr('r', 5)
// .style('fill', d => colorScale(d))
// d3.select('.legend')
// .selectAll('text')
// .data(categories)
// .enter()
// .append('text')
// .attr('x', 40)
// .attr('y', (d,i) => 20+i*20)
// .attr('text-anchor', 'left')
// .style('fill', d => colorScale(d))
// .style('font-size', '12px')
// .style('alignment-baseline', 'middle')
// .text(d => textScale(d))
// data source
var dataSource = d3.select('#mobility-vis').append('div')
.attr('class', 'gm-data-source')
// .attr('width', sourceWidth)
// .attr('height', sourceHeight)
.append('g')
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')')
dataSource.append('text')
.attr('id', 'data-source-q')
.attr('font-color', 'blue')
.style('font-size', '10px')
.style('cursor', 'pointer')
.text('Why don\'t I see my country on this graph?')
// .on('mouseover', function() {
// d3.select('#data-source-q').style('font-weight', 'bold')
// d3.select('#data-source').style('opacity', 1)
// })
// .on('mouseout', function() {
// d3.select('#data-source-q').style('font-weight', 'normal')
// d3.select('#data-source').style('opacity', 0)
// })
.on('click', function() {
var d = d3.select(this)
d.classed('source-on', !d.classed('source-on'))
d.style('font-weight', function () {
return (d.classed('source-on') ? 'bold' : 'normal')
})
d3.select('#data-source').style('opacity', function() {
return (d.classed('source-on') ? 1 : 0)
})
})
dataSource.append('div')
.attr('id', 'data-source')
.style('opacity', 0)
.attr('width', sourceWidth)
.attr('height', sourceHeight)
.html('<p id="source-tag">This data was collected by Google using their Location History feature. <br/>Countries with insufficient data, including China, Iran, and Russia, do not show up in this dataset.<br/><br/> Read more at: <a href="https://www.google.com/covid19/mobility/data_documentation.html?hl=en">Google LLC "Google COVID-19 Community Mobility Reports".</a></p>')
// .append('rect')
// .attr('width', sourceWidth)
// .attr('height', sourceHeight)
// .style('display', 'none')
// .append('text')
// .attr('x', 0)
// .attr('y', 30)
// .attr('fill', 'gray')
// .style('font-size', '10px')
// .text('This data was collected by Google using their Location History feature. Countries with insufficient data, including China, Iran, and Russia, do not show up in this dataset.')
// d3.select('#source-tag')
// .attr('font-size', '10px')
// dataSource.append('text')
// .attr('id', 'data-source')
// .attr('fill', 'black')
// .style('font-size', '10px')
// .text('hellooooooo')
// .style('opacity', 0)
}
// todo possible additions
// choose top 10 by number of confirmed cases / total population / etc
// "why don't I see china on this map?" button to explain where the data is coming from
// finish hover tooltip
// info lines for major events
// hover over category for explanation, click to "activate" and show on map