-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopening.html
296 lines (240 loc) · 8.73 KB
/
opening.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3: Global Net Imports/Exports of the largest 3 traders</title>
<script type="text/javascript" src="http://d3js.org/d3.v4.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<style type="text/css">
.label {
font-weight: bold;
text-anchor: middle;
cursor: default;
}
.legendlabel {
font-weight: bold;
text-anchor: middle;
cursor: default;
}
h1 {
margin-bottom: 25px;
font-size: 24px;
font-weight: bold;
}
h2 {
margin-top: 30px;
margin-left: 20px;
font-size: 18px;
}
#container {
width: 1000px;
margin: 25px auto 25px auto;
padding: 50px 50px 50px 50px;
background-color: lightgray;
box-shadow: 0 0 20px #ccc;
}
#footer p {
margin-top: 50px;
margin-bottom: 0;
font-size: 11px;
line-height: 14px;
color: black;
}
</style>
</head>
<body>
<div id="container">
<h1>In 2016, only three countries made up nearly 25% of the global volume of imports and exports</h1>
<div id="chart">
<h2>Total Volume of Exports (Import + Export) for 2016</h2>
</div>
<div style="display:inline-block;" id="legend"></div>
<p>Despite many global trading partners, it's clear that the US, China, and (suprisingly) Germany drive a quarter of the volume of global trade.</p>
<div id="footer">
<p>Data available at:<a href="https://comtrade.un.org/data/">the UN Comtrade Website.</a> Missing or negligible data is colored gray.
</div>
</div>
<script type="text/javascript">
//Width and height
var w = 1000;
var h = 500;
//Color scheme for the choropleth
var color = d3.scaleQuantize()
.range(["rgb(237,248,233)", "rgb(186,228,179)",
"rgb(116,196,118)", "rgb(49,163,84)", "rgb(0,109,44)"]);
//Map projection and path generation
var projection = d3.geoMercator()
.translate([w / 2, h / 2]);
var path = d3.geoPath()
.projection(projection);
//Create SVG (should be linked up to #chart above for proper placement)
var svg = d3.select("#chart")
.append("svg")
.attr("width", w)
.attr("height", h)
.append("g");
//Saved for later as a note to allow labels to appear
//var formatAsPercent = d3.format(",.1%");
//Labels array for reading into the "Text" labels below
var labels = [
{
x: 100,
y: 150,
text: 'US: 10.7%'
},
{
x: 450,
y: 50,
text: 'Germany: 8.4%'
},
{
x: 900,
y: 200,
text: 'China: 13.4%'
},
{
x: 133,
y: 309,
text: 'Indicates higher trade volume'
},
{
x: 130,
y: 349,
text: 'Indicates lower trade volume'
}
]
//Lines array for reading into the "Line" below, these will point to the countries
var lines = [
{
x1: 140,
x2: 200,
y1: 140,
y2: 130
},
{
x1: 450,
x2: 520,
y1: 60,
y2: 90
},
{
x1: 800,
x2: 890,
y1: 170,
y2: 180
}
]
//Simple text for legend labels
var legendLabels = [
{
text: "Indicates higher trade volume",
x: 300,
y: 300
},
{
text: "Indicates lower trade volume",
x: 100,
y: 100
}
]
d3.csv("Global_Imp_Exp_Volumes.csv", function (data) {
//Set input domain for color scale
color.domain([
d3.min(data, function (d) { return d.value; }),
d3.max(data, function (d) { return d.value; })
]);
//Load in GeoJSON data
d3.json("world_countries.json", function (json) {
//Merge the Trade data and GeoJSON data
for (var i = 0; i < data.length; i++) {
//Get each country name
var dataCountry = data[i].country;
//Convert text variable to float variable for quantitative comparisons
var dataValue = parseFloat(data[i].value);
//Find corresponding country inside the GeoJSON
for (var j = 0; j < json.features.length; j++) {
var jsonCountry = json.features[j].properties.name;
if (dataCountry == jsonCountry) {
//Copy the data value into the JSON to match up the country
json.features[j].properties.value = dataValue;
//Stop looking through the JSON
break;
}
}
}
//Create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.style("fill", function (d) {
var value = d.properties.value;
if (value) {
return color(value);
} else {
//Make it gray if undefined
return "#ccc";
}
});
//This group adds the labels
svg.selectAll("text")
.data(labels)
.enter()
.append("text")
.attr("class", "label")
.attr("x", function (d) { return d.x;})
.attr("y", function (d) { return d.y;})
.text(function (d) {
return d.text;
});
//Create color legend based on the color scheme above
var legend = svg.selectAll('rect')
.data(color.range().reverse())
.enter()
.append('rect')
.attr("x", 20)
.attr("y", function (d, i) {
return i*10+300;
})
.attr("width", 10)
.attr("height", 10)
.style("fill", function (d, i) {
return d;
});
//This group adds the legend labels
svg.selectAll("text")
.data(legendLabels)
.enter()
.append("text")
.attr("class", "legendLabel")
.attr("x", function (d) { return d.x; })
.attr("y", function (d) { return d.y; })
.text(function (d) {
return d.text;
});
//Create the lines that point from the annotations to the countries
svg.selectAll("line")
.data(lines)
.enter()
.append("line")
.attr("x1", function (d) {
return d.x1;
})
.attr("x2", function (d) {
return d.x2;
})
.attr("y1", function (d) {
return d.y1;
})
.attr("y2", function (d) {
return d.y2;
})
.attr("stroke-width", 2)
.attr("stroke", "black");
});
});
</script>
</body>
</html>