-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.js
162 lines (127 loc) · 4.17 KB
/
map.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
(function () {
// Calls the resize function
d3.select(window).on("resize", throttle);
var windowWidth = window.innerWidth;
// Set the height and width
var padding = 5;
var scaleSetting = 1.6;
var mapHeightWidthRatio = 0.602;
var width = document.getElementById('map').offsetWidth-padding;
var height = width * mapHeightWidthRatio;
var activeCountries, topo, borders, coastline, projection, path, svg, map;
var tooltip = d3.select("#map").append("div").attr("class", "tooltip hidden");
var mapZoom = d3.zoom()
.scaleExtent([1, 4])
.translateExtent([[0,0], [width, height]])
.extent([[0, 0], [width, height]])
.on("zoom", freeZoom);
function freeZoom() {
map.attr("transform", d3.event.transform);
}
d3.select("#zoom_in").on("click", function() {
mapZoom.scaleBy(svg.transition().duration(500), 1.1);
});
d3.select("#zoom_out").on("click", function() {
mapZoom.scaleBy(svg.transition().duration(500), 0.9);
});
function removeLoadingScreen() {
var loadingScreenEl = document.getElementById('loading');
if (!loadingScreenEl) {
return;
}
if(typeof loadingScreenEl.style['transition'] !== 'undefined') {
loadingScreenEl && loadingScreenEl.addEventListener('transitionend', function () {
loadingScreenEl.parentNode.removeChild(loadingScreenEl);
});
loadingScreenEl.style.opacity = '0';
}
else {
loadingScreenEl.parentNode.removeChild(loadingScreenEl);
}
}
setup(width,height);
//initial setup
function setup(width,height){
//Try d3.geoWinkel3() / d3.geoMercator() / d3.geoNaturalEarth1() / d3.geoTimes()
projection = d3.geoWinkel3()
.translate([(width/2), (height/scaleSetting)])
.scale(width / scaleSetting / Math.PI)
.rotate([-11,0]);
path = d3.geoPath().projection(projection);
svg = d3.select("#map")
.append("svg")
.attr("width", width)
.attr("height", height)
.call(mapZoom)
.on("wheel.zoom", null); // disable scroll zoom
map = svg.append("g");
}
//load data
var promises = [];
promises.push(d3.json("data/world-topo.json?cachebust="+(+new Date())));
promises.push(d3.csv("data/active.csv?cachebust="+(+new Date())));
Promise.all(promises)
.then(ready)
.catch(function(err) {
throw err;
});
function ready(data) {
var world = data[0];
var countries = topojson.feature(world, world.objects.world).features;
topo = countries;
activeCountries = data[1];
coastline = topojson.mesh(world, world.objects.world, function(a, b) { return a === b });
topo.forEach(function(d, i) {
activeCountries.forEach(function(e, j) {
if (d.id === e.id) {
e.geometry = d.geometry;
e.type = d.type;
}
});
});
draw(topo, activeCountries, coastline);
removeLoadingScreen();
}
function draw(topo, activeCountries, coastline) {
var activeCountry = map.selectAll(".activeCountry").data(activeCountries);
map.selectAll(".country")
.data(topo)
.enter().append("path")
.attr("class", "country")
.attr("id", function(d) { return d.id; })
.attr("d", path);
map.insert("path", ".graticule")
.datum(coastline)
.attr("class","coastline")
.attr("d", path);
activeCountry.enter().append("path")
.attr("class", "member")
.attr("id", function(d) { return d.id; })
.attr("d", path);
//ofsets plus width/height of transform, plus 20 px of padding, plus 20 extra for tooltip offset off mouse
var offsetL = document.getElementById('map').offsetLeft+(width/60);
var offsetT =document.getElementById('map').offsetTop+(height/60);
if (windowWidth > 752) {
//when you click on a country go to the member page
activeCountry.on('click', function(d){
window.location = d.url;
console.log ("Country clicked");
});
}
}
function redraw() {
windowWidth = window.innerWidth;
width = document.getElementById('map').offsetWidth-padding;
height = width / scaleSetting;
d3.select('svg').remove();
setup(width,height);
draw(topo, activeCountries, coastline);
}
var throttleTimer;
function throttle() {
window.clearTimeout(throttleTimer);
throttleTimer = window.setTimeout(function() {
redraw();
}, 200);
}
})();