forked from gembree1/d3-slider-map
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
141 lines (115 loc) · 3.8 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style>
body{
font-family: 'Helvetica Neue', Helvetica, sans-serif;
}
p{
font-size: 10pt;
margin-left:20px;
}
.countries{
stroke: #fff;
}
.legend rect{
stroke: #000;
}
.legend, .legend_title text{
font-size:10pt;
}
.barlabel {
font-size:8pt;
text-anchor: middle;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v4.min.js"></script>
<script src="https://d3js.org/topojson.v2.min.js"></script>
<script src="https://d3js.org/d3-scale-chromatic.v1.min.js"></script>
<script src="https://d3js.org/d3-geo-projection.v1.min.js"></script>
<script src="functions.js"></script>
<script>
/// params
var color_na = d3.rgb("#d4d4d4");
// only works if array.length-1 is between 3 and 9 (d3 color scheme)
var quantiles = [0, 0.2, 0.4, 0.6, 0.8, 1];
var init_year = 1990;
var headline = "Number of deaths caused by storms in ";
/// main
// slider
d3.select("body").insert("p", ":first-child").append("input")
.attr("type", "range")
.attr("min", "1990")
.attr("max", "2016")
.attr("value", init_year)
.attr("id", "year");
d3.select("body").insert("h2", ":first-child").text(headline + init_year);
// init map container, projection
var width = 960, height = 425;
var svg_map = d3.select("body").insert("svg")
.attr("id", "map")
.attr("height", height)
.attr("width", width);
var path = d3.geoPath(d3.geoRobinson());
// init legend container
svg_map.append("g")
.attr("class", "legend");
svg_map.append("g")
.attr("class", "legend_title")
.append("text");
// init bars container
var margin = {top: 50, right:10, bottom:50, left:30};
var svgBarsWidth = 960 - margin.left - margin.right,
svgBarsHeight = 200 - margin.top - margin.bottom;
var x = d3.scaleBand()
.rangeRound([0, svgBarsWidth])
.padding(.05);
var y = d3.scaleLinear().range([svgBarsHeight, 0]);
var svg_bars = d3.select("body")
.append("svg")
.attr("id", "bars")
.attr("width", svgBarsWidth + margin.left + margin.right)
.attr("height", svgBarsHeight + margin.top + margin.bottom)
.append("g")
.attr("class", "bars")
.attr("transform", "translate(" + margin.left + ", " + margin.top + ")");
// load data
d3.json("data/emdat_data.json", function(error, d) {
if (error) throw error;
let data_all = d['Storm'];
let data = data_all[init_year];
let color = calcColorScale(data);
// load map data and render it
d3.json("data/world.json", function(error, worldmap) {
if (error) throw error;
// init map
svg_map.append("g")
.attr("class", "countries")
.selectAll("path")
.data(topojson.feature(worldmap, worldmap.objects.world).features)
.enter().append("path")
.attr("d", path)
.attr("id", function(d) { return d.id; })
.call(fillMap, color, data)
.append("title")
.call(setPathTitle, data);
// init legend
renderLegend(color, data);
renderBars(color, data);
}); // map data
// was the slider used?
d3.select("#year").on("input", function() {
let upd_color = calcColorScale(data_all[this.value]);
updateMap(upd_color, data_all[this.value]);
renderLegend(upd_color, data_all[this.value]);
renderBars(upd_color, data_all[this.value]);
});
}); // disaster data
</script>
</body>
</html>