-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbubble.html
204 lines (185 loc) · 7.16 KB
/
bubble.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
<html>
<head>
<link href='http://fonts.googleapis.com/css?family=Varela' rel='stylesheet' type='text/css'>
<!-- Load the d3 library. -->
<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<style>
body {
font-family: 'Varela', sans-serif;
}
div {
margin: 30px;
}
svg { border: solid 1px;}
.axis path { fill: none; stroke: #777; }
.axis line { stroke: #777; }
.hidden {
display: none;
}
div.tooltip {
color: #222;
background: #fff;
padding: .5em;
text-shadow: #f5f5f5 0 1px 0;
border-radius: 2px;
box-shadow: 0px 0px 2px 0px #a6a6a6;
opacity: 0.9;
position: absolute;
}
</style>
</head>
<body>
<div id ="explanation2">
The color of each bubble corresponds to the color of the country on the world map and
it represents the income group that country belongs to. In particular, blue - high income,
green - upper middle, dark orange - lower middle, and light orange - low income. The sizes
of circles upon hovering, represent the number of incoming tourists in the most recent year.
<br>
In order to help the user observe the general trend, the circle sizes stay the same after being
hovered over. This design choice makes sure that the user can see the full picture by the end. It
also engages with the user. It simulates a gaming experience and aims to
capture the userÕs attention fully to each and every country.
</div>
<div id="canvas"></div>
<script>
var lower_middle_income;
var upper_middle_income;
var high_income;
var avg;
var numbers;
d3.json("cleandata.json", function (error, data){
var width2 = document.getElementById('canvas').offsetWidth;
var height2 = width2;
var svg2, g2;
var force = d3.layout.force()
.charge(-170)
.linkDistance(50)
.size([width2, height2]);
var margin = {top: 10, right: 10, bottom: 10, left: 10},
width2 = width2 - margin.left - margin.right,
height2 = height2 - margin.top - margin.bottom;
svg2 = d3.select("#canvas").append("svg")
.attr("width", width2)
.attr("height", height2)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
g2 = svg2.append("g");
numbers = data;
maxGDP = d3.max(numbers, function(d) {return d.gdp_percapita});
minGDP = d3.min(numbers, function(d) {return d.gdp_percapita});
sumGDP = d3.sum(numbers, function(d) {return d.gdp_percapita} ) - maxGDP - minGDP;
avg = sumGDP / (numbers.length - 2);
lower_middle_income = avg * 3/4
upper_middle_income = avg
high_income = avg * 1.25
var countryColor = function(income){
if (income == "high"){
return "#1F77B4"; //high income
}
else if (income == "upper") {
return "#2CA02C"; //upper middle income
}
else if (income == "lower") {
return "#FFBB78"; //lower middle income
}
else if (income == "low") {
return "#FF7F0E"; //low income
}
else{
return "#CCC"; //unknown GDP
}
};
function getIncomeGroup(code) {
for (var i = 0; i < numbers.length; i++) {
if (numbers[i].iso3_digit_code == code){
if (numbers[i].gdp_percapita >= high_income) {
return "high";
}
else if (numbers[i].gdp_percapita >= upper_middle_income && numbers[i].gdp_percapita < high_income) {
return "upper";
}
else if (numbers[i].gdp_percapita >= lower_middle_income && numbers[i].gdp_percapita < upper_middle_income) {
return "lower";
}
else if (numbers[i].gdp_percapita < lower_middle_income) {
return "low";
}
else{
return "No Available Data";
}
}
}
return "No Available Data";
}
function getIncome(code) {
for (var i = 0; i < numbers.length; i++){
if (numbers[i].iso3_digit_code == code){
return numbers[i].gdp_percapita;
}
}
return "No Available Data";
}
var points = numbers.map(function (country) {
return {
x: Math.random(),
y: Math.random(),
name: country["country_name"],
incoming: country["incoming"],
id: country["iso3_digit_code"]
};
})
.filter(function (point) {
return ! isNaN(point.x) && ! isNaN(point.y);
});
var xScale = d3.scale.linear().domain(d3.extent(points, function (point) {return point.x;})).range([50, width2 - 50]);
var yScale = d3.scale.linear().domain(d3.extent(points, function (point) {return point.y;})).range([height2 - 50, 50]);
force
.nodes(points)
.start();
var node = svg2.selectAll("circle.node")
.data(points)
.enter()
.append("circle")
.attr("class", "node")
.attr("cx", function(d) { return xScale(d.x); })
.attr("cy", function(d) { return yScale(d.y); })
.attr("r", "20")
.style("fill", function (d) {return countryColor(getIncomeGroup(d.id)); })
.style("opacity", 0.5)
.call(force.drag);
var text = svg2.selectAll('text')
.data(points)
.enter()
.append("svg:text")
.attr("x", function(d) { return xScale(d.x); })
.attr("y", function(d) { return yScale(d.y); })
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) {
return d.name;
})
.attr("font-size", "12px");
node
.on("mouseover", function(d) {
d3.select(this)
.attr("r", function (d) {
if (isNaN(Number(d.incoming))) {
return d3.min(d.incoming);
}else{
return (d.incoming / 500000);
}
})
})
force.on("tick", function() {
text.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
});
</script>
</body>
</html>