-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcitizenship.js
125 lines (109 loc) · 3.14 KB
/
citizenship.js
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
const Scales = {
AvgProcessingTime: {
domain: [1, 90, 30 * 6, 30 * 9, 365, 180 + 365, 365 * 2, 365 * 3, 365 * 4],
colorRange: ["#ccc", "#fa0", "#f5e800", "#5fcd59", "#15928d", "#2e6590", "#453182", "#450055", "#000"],
labels: ['undefined', 'under 3 months', 'up to 6 months', 'up to 9 months', 'up to 1 year', 'up to 18 months', 'up to 2 years', 'up to 3 years', 'up to 4 years']
}
};
class GeoRegion {
constructor(alpha3, props) {
this.alpha3 = alpha3;
this.props = props || new Map();
}
get(prop) {
return this.props.get(prop);
}
avgProcessingTime() {
return this.get('AvgProcessingTime');
}
granted() {
return this.get('Granted') || 0;
}
refused() {
return this.get('Refused') || 0;
}
incoming() {
return this.get('Queued') || 0;
}
};
class CitizenshipData {
static loadCsv(url) {
const maximums = new Map();
const minimums = new Map();
const data = new Map();
return d3.csv(url, function(d) {
if (!data.has(d.Year)) {
data.set(d.Year, new Map());
}
const year = data.get(d.Year)
const datum = {
Queued: +d.Queued,
Dequeued: +d.Dequeued,
Granted: +d.Granted,
Refused: +d.Refused,
Canceled: +d.Canceled,
Pending: +d.Pending,
AvgProcessingTime: +d.AvgProcessingTime
};
const datumMap = new Map([['Country', d.Country]]);
Object.keys(datum).forEach((key) => {
// Merge values if there's already a row for the same geo.
let oldValue = 0;
if (year.has(d.Alpha3)) {
oldValue = datumMap.get(key);
}
let newValue = datum[key];
if (oldValue > 0) {
if (key == 'AvgProcessingTime') {
newValue = Math.floor((oldValue + newValue) / 2);
} else {
newValue = oldValue + newValue;
}
}
datumMap.set(key, newValue);
maximums.set(key, Math.max(maximums.get(key) || 0, newValue));
minimums.set(key, Math.max(minimums.get(key) || 0, newValue));
})
year.set(d.Alpha3, datumMap);
}).then(() => {
return new CitizenshipData(data, maximums, minimums);
});
}
constructor(dataMap, maximums, minimums) {
this.data = dataMap;
this.maximums = maximums;
this.minimums = minimums;
this.years = Array.from(dataMap.keys()).sort()
}
forGeoInYear(geo, year) {
const yearData = this.data.get(year);
if (yearData) {
return new GeoRegion(geo, yearData.get(geo));
}
}
colorScale(metric) {
const metricScale = Scales[metric]
if (metricScale) {
return d3.scaleThreshold()
.domain(metricScale.domain)
.range(metricScale.colorRange);
}
return d3.scaleThreshold()
.domain([this.minimums[metric], this.maximums[metric]])
.range(d3.schemeBlues[7]);
}
labels(metric) {
const metricScale = Scales[metric]
if (metricScale) {
const result = [];
for (let i = 0; i < metricScale.labels.length; i++) {
result.push([metricScale.labels[i], metricScale.colorRange[i]]);
}
return result;
}
return [];
}
getYears() {
return this.years;
}
};