forked from vcccaat/visualization-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
225 lines (198 loc) · 6.76 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
<!DOCTYPE html>
<html>
<head>
<script src="https://d3js.org/d3.v7.min.js"></script>
<script src="https://d3js.org/topojson.v3.min.js"></script>
<script src="https://d3js.org/d3-collection.v1.min.js"></script>
<script src="legend.js"></script>
<!-- <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;500;700&display=swap" rel="stylesheet"> -->
<style>
.country {
fill: #edf9fe;
}
.outline {
stroke: black;
stroke-width: 1px;
fill: none;
}
.slidecontainer {
margin: auto;
width: 20%;
text-align: center;
}
.slider {
-webkit-appearance: none;
width: 100%;
height: 15px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: 0.2s;
transition: opacity 0.2s;
}
.container {
text-align: center;
width: 50%;
}
</style>
</head>
<body>
<div class="container">
<div>
<h2>WORLD HAPPINESS INDEX MAP</h2>
</div>
<div class="slidecontainer">
<input type="range" min="2015" max="2020" value="1" class="slider" id="sliderRange" />
<p>Year: <span id="sliderYear"></span></p>
</div>
<div id="mapCanvas">
<svg id="map" width="800" height="500"></svg>
</div>
<div>
<svg id="colorLegend" height="80" width="500" style="background: #fff; margin-top: 30px"></svg>
</div>
</div>
<script>
var slider = document.getElementById('sliderRange');
var output = document.getElementById('sliderYear');
output.innerHTML = slider.value;
slider.oninput = function () {
output.innerHTML = this.value;
};
</script>
<script>
const svg = d3.select('svg#map');
const width = parseInt(svg.attr('width'));
const height = parseInt(svg.attr('height'));
const margin = { top: 20, right: 20, bottom: 20, left: 20 };
const mapWidth = width - margin.left - margin.right;
const mapHeight = height - margin.top - margin.bottom;
const map = svg.append('g').attr('transform', 'translate(' + margin.left + ',' + margin.top + ')');
const requestData = async function () {
const happy2015 = await d3.csv('./datasets/2015.csv');
const world = await d3.json('./datasets/world.json');
const countryCodes = await d3.json('./datasets/country-codes.json');
console.log(countryCodes);
var countryHappiness = {};
var CountryIds = [];
var allHappiness = [];
for (let row of happy2015) {
if (!(row.Country in countryHappiness)) {
countryHappiness[row.Country] = Number(row['Happiness Score']);
}
CountryIds.push(getCountryId(row.Country, countryCodes));
allHappiness.push(Number(row['Happiness Score']));
}
console.log(countryHappiness);
var countries = topojson.feature(world, world.objects.countries);
countries.features = countries.features.filter((country) => {
return country.id !== '010';
});
world.objects.countries.geometries = world.objects.countries.geometries.filter((country) => {
return CountryIds.includes(country.id);
});
var countriesMesh = topojson.mesh(world, world.objects.countries);
var projection = d3.geoMercator().fitSize([mapWidth, mapHeight], countries);
var path = d3.geoPath().projection(projection);
map.append('rect').attr('x', -margin.left).attr('y', -margin.top).attr('width', width).attr('height', height).attr('fill-opacity', 0);
map
.selectAll('path.country')
.data(countries.features)
.join('path')
.attr('class', 'country')
.attr('d', path)
.on('mouseover', mouseEntersPlot)
.on('mouseout', mouseLeavesPlot);
map.append('path').datum(countriesMesh).attr('class', 'outline').attr('d', path);
let tooltipWidth = 200;
let tooltipHeight = 80;
let momesh = map.append('path').attr('class', 'mouseover outline').attr('d', '');
let tooltip = map.append('g').attr('class', 'tooltip').attr('visibility', 'hidden');
tooltip
.append('rect')
.attr('fill', 'black')
.attr('opacity', 0.9)
.attr('x', -tooltipWidth / 2.0)
.attr('y', 0)
.attr('width', tooltipWidth)
.attr('height', tooltipHeight);
let txt = tooltip
.append('text')
.attr('class', 'countryName')
.attr('fill', 'white')
.attr('text-anchor', 'middle')
.attr('alignment-baseline', 'hanging')
.attr('x', 0)
.attr('y', 20);
let txt2 = tooltip
.append('text')
.attr('class', 'countryInfo')
.attr('fill', 'white')
.attr('text-anchor', 'middle')
.attr('alignment-baseline', 'hanging')
.attr('x', 0)
.attr('y', 45);
// color scale
const colorScale = d3.scaleSequential(d3.interpolateGnBu).domain(d3.extent(Object.values(allHappiness)));
map.selectAll('.country').style('fill', (d) => {
let countryName = getCountryName(d.id, countryCodes);
if (countryHappiness[countryName]) {
return colorScale(countryHappiness[countryName]);
}
});
const sortedAllHappiness = allHappiness.sort(function (a, b) {
return a - b;
});
drawLegend(sortedAllHappiness, d3.select('#colorLegend'), colorScale);
d3.selectAll('.country').on('mouseenter', mouseEntersPlot);
d3.selectAll('.country').on('mouseout', mouseLeavesPlot);
function mouseEntersPlot() {
let country = d3.select(this);
if (CountryIds.includes(country.datum().id)) {
tooltip.style('visibility', 'visible');
country.attr('stroke', 'black').attr('stroke-width', 3).style('opacity', 0.5);
}
let countryID = country.datum().id;
let countryName = getCountryName(countryID, countryCodes);
txt.text(countryName);
txt2.text('Happiness Score: ' + countryHappiness[countryName]);
let bounds = path.bounds(country.datum());
let xPos = (bounds[0][0] + bounds[1][0]) / 2.0;
xPos = xPos > 580 ? 580 : xPos;
let yPos = bounds[1][1] + 10;
yPos = yPos > 400 ? 400 : yPos;
// console.log(xPos);
tooltip.attr('transform', `translate(${xPos},${yPos})`);
var mo = topojson.mesh(world, world.objects.countries, function (a, b) {
return a.id === countryID || b.id === countryID;
});
momesh.datum(mo).attr('d', path);
}
function mouseLeavesPlot() {
tooltip.style('visibility', 'hidden');
let country = d3.select(this);
if (CountryIds.includes(country.datum().id)) {
country.attr('stroke', 'black').attr('stroke-width', 1).style('opacity', 1);
}
momesh.attr('d', '');
}
function getCountryId(name, countryCodes) {
for (let country of countryCodes) {
if (name === country['name']) {
return country['country-code'];
}
}
}
function getCountryName(id, countryCodes) {
for (let country of countryCodes) {
if (id === country['country-code']) {
return country.name;
}
}
}
};
requestData();
</script>
</body>
</html>