-
Notifications
You must be signed in to change notification settings - Fork 0
/
Site3.html
265 lines (215 loc) · 6.44 KB
/
Site3.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
<html>
<script src='https://d3js.org/d3.v5.min.js'></script>
<head>
<style>
h1 { text-align:center}
svg {display: block; margin: auto;}
.button {
background-color: #003f5c;
border: none;
color: white;
padding: 15px 15px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: auto;
cursor: pointer;
}
select{font-size: 20px; font-weight: bold;}
#dropdown{margin:0 auto;width:100px;}
.source {
text-align: center;
font-size: 10px;}
.intro {
width:800px;
margin: auto;
font-size: 18px;}
}
</style>
</head>
<body onload='init()'>
<h1>U.S. Immigrants Admitted by Age Group</h1>
<div class="intro">
<p>Besides nationality, age and sex are factors that affect immigration.
From the chart below, young adults tend to have the best capacity to migrate regardless of the year selected.
An interesting insight is that females were the dominant sex migrating into the U.S. for most years and age groups.
</p>
</div>
<div id="dropdown">
<select id="year">
<option value="1986" selected="selected">1986</option>
<option value="1987">1987</option>
<option value="1988">1988</option>
<option value="1989">1989</option>
<option value="1990">1990</option>
<option value="1991">1991</option>
<option value="1992">1992</option>
<option value="1993">1993</option>
<option value="1994">1994</option>
<option value="1995">1995</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
</select>
</div>
<svg width=1000 height=500 ></svg>
<div style="float: left;">
<button class="button" onclick="location.href='http://jhuamani.github.io/Site2.html'" type="button">Top 10 Countries</button>
</div>
<div style="float: right;">
<button class="button" onclick="location.href='http://jhuamani.github.io/Site1.html'" type="button">Immigrants Admitted</button>
</div>
<p class="source">Source: "Yearbook of Immigration Statistics" U.S. Department of Homeland Security,
https://www.dhs.gov/immigration-statistics/yearbook </p>
<script>
async function init() {
Fdata = await d3.csv("https://jhuamani.github.io/FemalesbyAge.csv");
Mdata = await d3.csv("https://jhuamani.github.io/MalesbyAge.csv");
//Tooltip
var tooltip = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity",0)
.style("position","absolute")
.style("background-color", "white")
.style("padding", "5px");
var currentYear = document.getElementById("year").value
var margin = 100;
var height = 550 - 2*margin;
var width = 1000-2*margin;
//y-axis
var y = d3.scaleBand().domain(Fdata.map(function(d) {return d.AgeGroup;}))
.range([height,0]).padding(0.2);
yaxis = d3.select("svg").append("g")
.attr("transform","translate("+margin+","+margin+")")
.call(d3.axisLeft(y));
//x-axis
var ticks = [1000,10000,100000,300000];
var x = d3.scaleLog().domain([1000,300000]).range([0,width]);
xaxis = d3.select("svg").append("g")
.attr("transform","translate("+margin+","+(height+margin)+")")
.call(d3.axisBottom(x).tickFormat(d3.format(",")).tickValues(ticks));
//Bars for barchart
var Frects = d3.select("svg").append("g")
.attr("id","FemaleBars")
.attr("transform","translate("+margin+","+margin+")")
.selectAll("rect").data(Fdata).enter().append("rect")
.attr("x", 0)
.attr("y", function(d) {return y(d.AgeGroup);})
.attr("height", y.bandwidth()/2)
.attr("width", function(d) {return x(d[currentYear]);})
.attr("fill", "#a05195");
var Mrects = d3.select("svg").append("g")
.attr("id","MaleBars")
.attr("transform","translate("+margin+","+margin+")")
.selectAll("rect").data(Mdata).enter().append("rect")
.attr("x", 0)
.attr("y", function(d) {return (y(d.AgeGroup)+y.bandwidth()/2);})
.attr("height", y.bandwidth()/2)
.attr("width", function(d) {return x(d["1986"]);})
.attr("fill", "#ff7c43")
Frects
.on("mouseover",function(d){
tooltip
.style("opacity",1)
d3.select(this)
.attr("fill", "#ffc401");
Mrects
.style("opacity",0.2)
;})
.on("mousemove",function(d){
tooltip
.style("left", (d3.event.pageX + 60) + "px")
.style("top", (d3.event.pageY) + "px")
.html("Female Immigrants: " + d[currentYear])
;})
.on("mouseout",function () {
tooltip.style("opacity",0)
d3.select(this)
.attr("fill", "#a05195")
Mrects
.style("opacity",1)
;});
Mrects
.on("mouseover",function(d){
tooltip
.style("opacity",1)
d3.select(this)
.attr("fill", "#ffc401");
Frects
.style("opacity",0.2)
;})
.on("mousemove",function(d){
tooltip
.style("left", (d3.event.pageX + 60) + "px")
.style("top", (d3.event.pageY) + "px")
.html("Male Immigrants: " + d[currentYear])
;})
.on("mouseout",function () {
tooltip.style("opacity",0)
d3.select(this)
.attr("fill", "#ff7c43")
Frects
.style("opacity",1)
;});
//legend
var legx = 800;
d3.select("svg").append("circle")
.attr("cx",legx)
.attr("cy",margin)
.attr("r", 6)
.style("fill", "#a05195")
d3.select("svg").append("circle")
.attr("cx",legx)
.attr("cy",margin+30)
.attr("r", 6)
.style("fill", "#ff7c43")
d3.select("svg")
.append("text")
.attr("x", legx + 20)
.attr("y", margin)
.text("Females")
.style("font-size", "15px")
.attr("alignment-baseline","middle")
d3.select("svg").append("text")
.attr("x", legx + 20)
.attr("y", margin+30)
.text("Males")
.style("font-size", "15px")
.attr("alignment-baseline","middle")
function rebar(year){
Frects
.transition().duration(1000)
.attr("width", function(d) {return x(d[year]);});
Mrects
.transition().duration(1000)
.attr("width", function(d) {return x(d[year]);})
};
d3.select("#year").on("change", function(d){
currentYear = this.value
rebar(currentYear)
});
}
</script>
</body>
</html>