-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
236 lines (193 loc) · 8.08 KB
/
index.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
<html>
<head>
<meta charset="utf-8">
<title>Project 2: COVID-19</title>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="https://d3js.org/topojson.v3.min.js"></script>
<link rel="stylesheet" href="index.css"></link>
</head>
<body>
<h1>Project 2: COVID-19</h1>
<div class="page_full">
<div class="label_map_container">
<div class="country_label_container">
<svg id="country_label" width="200" height="80"></svg>
</div>
<div class="map_container">
<svg id="svg_map" width="1200" height="550"></svg>
</div>
<div class="slider_container">
<input id="slider" type="range" min="0" max="15.6" step="1">
<svg id="time_scale" height="30" width="1000"></svg>
</div>
</div>
</div>
<script>
let svg = d3.select("svg#svg_map");
const width = +svg.attr("width");
const height = +svg.attr("height");
const margin = { "top": 5, "right": 10, "bottom": 5, "left": 10 };
const requestData = async function ()
{
// LOADING COVID-19 IMPACT DATA
// Gathered from Our World in Data's repository found here:
// https://github.com/owid/covid-19-data/tree/master/public/data
// world_impact: Feb. 24, 2020 to Apr. 20, 2021
const world_impact = await d3.csv("data/cov19/data_fixed.csv");
console.log(world_impact)
var time_parser = d3.timeParse("%Y-%m-%d")
for (row of world_impact) row['date'] = time_parser(row['date'])
const impact_by_country =
await d3.csv("data/cov19/country_wise_latest.csv");
// LOADING VACCINE DATA
// vax_by_country: Dec. 22, 2020 to Feb. 22, 2021
const vax_by_country =
await d3.csv(
"data/country_vaccination/country_vaccinations.csv"
);
// vax_by_manu: Dec. 24, 2020 to Apr. 14, 2021
const vax_by_manu =
await d3.csv(
"data/country_vaccination/country_vaccinations_by_manufacturer.csv"
);
// Finding the extent of dates
let impact_dates = []
for (let entry of world_impact) impact_dates.push(entry['date'])
var date_ext = d3.extent(impact_dates)
console.log(impact_dates)
// Create a bottom axis object
var scale_time = d3.scaleTime()
.domain(date_ext)
.range([0, 990]);
let time_axis = d3.axisBottom(scale_time)
.ticks(16)
.tickFormat(d3.timeFormat('%b %y'));
time = d3.select('svg#time_scale')
.append('g')
.attr("transform", "translate(" + 5 + ", 0)")
.call(time_axis)
.selectAll('text')
.attr('text-anchor', 'start');;
// Append a label for the country name
let country_label = d3.select('svg#country_label');
let label_width = country_label.attr('width');
let label_height = country_label.attr('height');
country_label.append('text')
.attr('class', 'country_label')
.attr('y', 40)
.text('')
.attr('text-anchor', 'middle')
.attr('doinant-baseline', 'auto')
.attr('x', label_width)
// Append the choropleth
world_map = svg.append("g")
.attr("width", width - margin.left - margin.right)
.attr("height", height - margin.top - margin.bottom)
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
let map_width = +world_map.attr('width');
let map_height = +world_map.attr('height');
// TopoJson file courtesy of Mike Bostock
const world = await d3.json('world_topo.json');
let countries = topojson.feature(world, world.objects.countries);
let countriesMesh = topojson.mesh(world, world.objects.countries);
// Renders the map based on the given date
function render_map(date) {
console.log(date)
// extracting relevant data
rel_data = world_impact.filter(function(row) {
return row['date'] === date
}
);
ext1 = d3.extent(rel_data, d => d['total_cases_per_million'])
console.log(ext1);
color_scale = d3.scaleSequential(d3.interpolateInferno)
.domain(ext1)
let projection = d3.geoEquirectangular().fitSize([map_width, map_height], countries)
let path = d3.geoPath().projection(projection);
// Adding a graticule
var graticule = d3.geoGraticule10();
// Adding country paths
world_map.selectAll('path.country').data(countries.features)
.join('path')
.attr('class', 'country')
.attr('fill', d => color_scale(d.total_cases_per_million))
.attr('note', d => d.id)
.attr('d', path)
// .on('mouseover', function (e, d)
// {
// name_len = d.properties.name.length
// // COUNTRY --> RED
// let country = d3.select(this)
// // unclicked countries should turn red on mouseover
// if (!country.attr('class').includes('clicked'))
// {
// country.attr('class', 'country filled')
// }
// // LABEL
// d3.select('text.country_label')
// .text(d.properties.name)
// })
// .on('mouseout', function (e, d)
// {
// // Get rid of colored countries
// let country = d3.select(this)
// if (!country.attr('class').includes('clicked'))
// {
// country.attr('class', 'country')
// }
// // LABEL
// d3.select('text.country_label')
// .text('')
// })
// .on('click', function (e, d)
// {
// country = d3.select(this)
// if(country.attr('class').includes('clicked'))
// {
// country.attr('class', 'country')
// }
// else {
// d3.selectAll('path.clicked')
// .attr('class', 'country')
// d3.select(this)
// .attr('class', 'clicked')
// }
// render_on([90, 0])
// })
world_map.join('path').datum(countriesMesh)
.attr('class', 'outline')
.attr('d', path)
}
// re-render world map on slider move
d3.select("input#slider")
.on("input", function ()
{
let date = d3.select(this).property("value");
render_map(date);
})
render_map(world_impact[60].date);
// var projection = d3.geoMercator().fitSize([width, height], counties)
// var path = d3.geoPath().projection(projection)
// diagram.selectAll("path.counties").data(counties.features)
// .join("path")
// .attr("class", "county")
// // .attr("fill", d => colorScale(d.properties.Total_Biodiversity))
// .attr("d", path)
// Checking to see how many countries there are
// There are 187 out of 195, so pretty good
// countries_list = []
// for (let country of impact_by_country) countries_list.push(country['Country/Region'])
// cov_countries = new Set(countries_list)
// console.log(cov_countries)
// Checking to see how many countries there are
// There are 177 out of 195, so not terrible
// vax_countries_list = []
// for (let vax_country of vax_by_country) vax_countries_list.push(vax_country.country)
// vax_countries = new Set(vax_countries_list)
// console.log(vax_countries)
// Rendeding slider functionality
}
requestData();
</script>
</body>
</html>