-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
211 lines (179 loc) · 6.42 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
<html>
<head>
<style>
body {
font-family: Helvetica, Arial, sans-serif;
background-color: LightGray;
}
h1 {
background-color: #2a5599;
color:white;
padding: 5px;
}
div.tooltip {
position: absolute;
text-align: center;
width: auto;
height: auto;
padding: 2px;
font: 12px sans-serif;
background: AliceBlue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
.legend { list-style: none; }
.legend li { float: right; margin-right: 10px; }
.legend span { border: 1px solid #ccc; float: left; width: 12px; height: 12px; margin: 2px; }
.legend .donated { background-color: #2a5599; }
.legend .received { background-color: #FF7F50; }
svg{
/*border: 1px solid black;*/
}
.mainview{
display:flex;
}
</style>
<script src="d3.js"></script>
</head>
<body>
<h1>Questions</h1>
<p> a) Do the countries that donate the most tend to cluster around specific geographical areas of the world?</br>
<ul>
<li>We can see from the below map that countries in West Europe generally donate more than they receive.</li> </ul>
b) Do the countries that receive the most tend to cluster around specific geographical areas of the world?
<ul>
<li>The countries receiving the most are spread out across the world.</li></ul>
c) Are there neighboring countries that have radically different patterns in terms of how much they receive vs. how much they donate?
<ul>
<li>We can see this relation between Germany and Poland or between Japan and Korea.</li>
</ul>
</p>
<div class="mainView">
<div>
<h2>Donations and Received</h2>
<div style="width:1350px;height:700px;border:1px solid #000;">
<svg id="Map"></svg>
<div>
<ul class="legend">
<li><span class="donated"></span> Amount Donated in Billions</li>
<li><span class="received"></span> Amount Received in Billions</li>
</ul>
</div>
</div>
<p>
For drawing above plot, I modified the aggregated csv.
<ul>
<li>Added columns of received and donated amount in billions.</li>
<li>Added Latitude and Longitude for each country to draw circles.</li>
</ul>
Radius of circle is defined as - square root of amounts in billions scaled to 1.5. Used this to overcome the problem of overlapping of circles.
</br>You can hover over each country to get it's details.
</p>
</div>
</div>
</body>
<script>
let store = {}
function loadData() {
return Promise.all([
d3.csv("cleaned_dataset_fixed.csv"),
d3.json("countries.geo.json"),
]).then(datasets => {
store.aid = datasets[0];
store.geoJSON = datasets[1];
return store;
})
}
function showData() {
let aid = store.aid
console.log(aid)
drawMap(store.geoJSON)
drawCircles(store.aid)
}
loadData().then(showData);
function getMapConfig(){
let width = 1200;
let height = 650;
let container = d3.select("#Map")
container
.attr("width", width)
.attr("height",height)
.call(d3.zoom().scaleExtent([1 / 2, 8]).on("zoom", function () {
container.attr("transform", d3.event.transform)
}))
return {width, height, container}
}
function getMapProjection(config) {
let {width, height} = config;
let projection = d3.geoMercator()
projection.scale(180)
.translate([width / 2, height / 2 + 20])
store.mapProjection = projection;
return projection;
}
function drawBaseMap(container, countries, projection){
let path = d3.geoPath()
.projection(projection)
container.selectAll("path").data(countries)
.enter().append("path")
.attr("d",path)
.attr("stroke", "#ccc")
.attr("fill", "#eee")
}
function drawMap(geoJeon) {
let config = getMapConfig();
let projection = getMapProjection(config)
drawBaseMap(config.container, geoJeon.features, projection)
}
function findColorMin(arr){
if(parseFloat(arr.donatedBillions) < parseFloat(arr.receivedBillions))
return "#2a5599"
else
return "#FF7F50"
}
function findColorMax(arr){
if(parseFloat(arr.donatedBillions) > parseFloat(arr.receivedBillions))
return "#2a5599"
else
return "#FF7F50"
}
function drawCircles(countries) {
let config = getMapConfig();
let projection = getMapProjection(config)
let container = config.container;
var formatDecimal = d3.format(".5f")
var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
var circles = container.selectAll("g")
.data(countries)
.enter()
.append("g");
//add bigger circle
circles.append("circle")
.attr("cx",d=>projection([+d.Longitude,+d.Latitude])[0])
.attr("cy",d=>projection([+d.Longitude,+d.Latitude])[1])
.attr("r",function(d) { return Math.max(Math.sqrt(d.donatedBillions),Math.sqrt(d.receivedBillions))*1.6})
.style("fill", function(d){return findColorMax(d)})
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div .html("<p>"+d.country+"<br/>"+"Donated = $"+formatDecimal(d.donatedBillions) + " Bn<br/>" + "Received = $"+formatDecimal(d.receivedBillions)+" Bn")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
//add smaller circle
circles.append("circle")
.attr("cx",d=>projection([+d.Longitude,+d.Latitude])[0])
.attr("cy",d=>projection([+d.Longitude,+d.Latitude])[1])
.attr("r",function(d) { return Math.min(Math.sqrt(d.donatedBillions),Math.sqrt(d.receivedBillions) *1.6)})
.style("fill", function(d){return findColorMin(d)})
}
</script>