-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
132 lines (123 loc) · 4.13 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
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css">
<script src="https://d3js.org/d3.v6.js"></script>
<script src="/citizenship.js"></script>
</head>
<body>
<h1>Average time it takes to become a Swedish citizen</h1>
<p>The average time it took for Swedish citizenship applications to be processed by Migrationsverket.</p>
<p>Click on a year and move the mouse over a country of interest to see more info</p>
<p>All data is derived from <a href="https://www.migrationsverket.se/Om-Migrationsverket/Statistik/Svenskt-medborgarskap.html">Migrationsverket</a></p>
<div id="buttons"></div>
<div id="tooltip"></div>
<svg id="playground" width="1600" height="1000"></svg>
<a href="https://github.com/kisabaka/migrationtime">Clone on Github</a>
<script>
let currentYear,
citizenshipData,
hint,
worldMap;
const metric = "AvgProcessingTime";
const svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height");
// Map and projection
const path = d3.geoPath();
const projection = d3.geoEqualEarth()
.scale(250)
.center([30,30])
.translate([width / 2, height / 4]);
Promise.all([
d3.json("https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/world.geojson"),
CitizenshipData.loadCsv("/data/citizenship.csv")
]).then(function(loadData){
worldMap = loadData[0];
citizenshipData = loadData[1];
// Tooltip.
hint = d3.select("#tooltip");
// The map.
svg.append("g")
.attr("class", "countries")
.selectAll("path")
.data(worldMap.features)
.join("path")
.attr("d", d3.geoPath().projection(projection))
.attr("fill", '#ccc')
.on('mousemove', (ev, d) => showHint(ev, d))
.on('mouseout', () => hideHint());
// The legend.
const legend = svg.append("g")
.attr("class", "legend")
.selectAll("path")
.data(citizenshipData.labels(metric))
.enter();
legend
.append("circle")
.attr("r", 7)
.attr("cx", 200)
.attr("cy", (d, i) => 450 + i * 20)
.style("fill", (d) => d[1]);
legend
.append("text")
.attr("x", 220)
.attr("y", (d, i) => 455 + i * 20)
.text(d => d[0]);
// And the buttons.
d3.select('#buttons')
.selectAll('button')
.data(citizenshipData.getYears())
.enter()
.append('button')
.text((year) => year)
.on('click', (ev, year) => {
selectYear(year);
});
});
function selectYear(year) {
currentYear = year;
svg.select("g.countries")
.selectAll("path")
.data(worldMap.features)
.join("path")
.attr("fill", function (d) {
const region = citizenshipData.forGeoInYear(d.id, year);
return citizenshipData.colorScale(metric)(region.get(metric) || 0);
});
}
function showHint(ev, d) {
if (!currentYear) {
hideHint();
return;
}
hint.transition()
.duration(100)
.style('opacity', 0.8)
.style('display', 'block');
const geo = citizenshipData.forGeoInYear(d.id, currentYear);
const avgTime = geo.avgProcessingTime();
let text;
if (avgTime) {
text = `Citizens of ${d.properties.name} waited on average ${avgTime} days in ` +
` ${currentYear}. ${geo.granted()} were granted citizenship` +
` while ${geo.refused()} were refused. There were ${geo.incoming()} applications ` +
` piling up from citizens of this country in ${currentYear}`;
} else {
text = `${d.properties.name} had no waiting applicants in ${currentYear}! ` +
` or I'm just missing data from this year`;
}
hint.text(text)
.style('left', `${ev.pageX - hint.property('offsetWidth') / 4}px`)
.style('top', `${ev.pageY + 20}px`);
}
function hideHint() {
if (hint) {
hint.transition()
.duration(100)
.style('opacity', 0)
.style('display', 'none');
}
}
</script>
</body>