-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
151 lines (124 loc) · 4.44 KB
/
main.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
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
/**
* Global Variables
*/
//Text that explains the selected data
var explanation = [];
explanation.goods = "<p> The indicator shows the volume of goods transported in Europe (in tonnes), broken down by country and by year.</p>" +
"<p>The data covers the total volume of freight and mail loaded/unloaded.";
explanation.pax = "<p>All passengers on a particular flight (with one flight number) counted once only and not repeatedly on each individual stage of that flight.</p>" +
"<p>They are all revenue and non-revenue passengers whose journey begins or terminates at the reporting airport " +
"and transfer passengers joining or leaving the flight at the reporting airport.</p>" +
"<p>Direct transit passengers are excluded.</p>";
//Map size
var mapWidth = 650,
mapHeight = 600;
//Projection config of the map
var projection = d3.geo.mercator()
.center([9, 55])
.scale(460)
.translate([mapWidth / 2, mapHeight / 2]);
//D3 Number format
var formatNumber = d3.format('>-,');
//D3 SVG path
var path = d3.geo.path()
.projection(projection)
.pointRadius(2);
// json object with map and statistical data
var dataJson = {};
function drawMap() {
$('.map').empty();
svg = d3.select('.map').append('svg')
.attr('width', mapWidth)
.attr('height', mapHeight);
d3.json('build/eu_data.json', function (error, eu) {
if (error) return console.error(error);
dataJson = eu;
svg.append('path')
.datum(topojson.feature(eu, dataJson.objects.countries))
.attr('d', path);
//add classes
svg.selectAll('.subunit')
.data(topojson.feature(eu, dataJson.objects.countries).features)
.enter().append('path')
.attr('class', function (d) {
if (d.id != -99) {
return 'subunit ' + d.id;
}
return 'subunit NO';
})
.attr('d', path);
// Add Label
svg.selectAll('.subunit-label')
.data(topojson.feature(eu, dataJson.objects.countries).features)
.enter().append('text')
.attr('class', function (d) {
if (d.id != -99) {
return 'subunit-label ' + d.id;
}
return 'subunit-label NO';
})
.attr('transform', function (d) {
return 'translate(' + path.centroid(d) + ')';
})
.attr('dy', '.35em')
.text(function (d) {
return d.properties.name;
});
drawBubbles();
});
}
function drawBubbles() {
var year = $('#choosedYear').val(),
type = $("input.typeRadio:checked").val();
var zoomRange = [0, 300000000];
if (type === 'goods') {
zoomRange = [0, 4300000];
}
var radius = d3.scale.sqrt()
.domain(zoomRange)
.range([0, 30]);
$('.bubble').empty();
$('.legend').empty();
$('#explanation').html(explanation[type])
var bubbleClass = 'bubble ' + type;
var dataField = type + year;
svg.append('g')
.attr('class', bubbleClass)
.selectAll('circle')
.data(topojson.feature(dataJson, dataJson.objects.countries).features
.sort(function (a, b) {
return b.properties[dataField] - a.properties[dataField];
}))
.enter().append('circle')
.attr('transform', function (d) {
return 'translate(' + path.centroid(d) + ')';
})
.attr('r', function (d) {
if (isNaN(d.properties[dataField])){
return 0;
}
return radius(d.properties[dataField]);
})
.append('title')
.text(function (d) {
return d.properties.name
+ '\n' + type + ' in ' + year + ': ' + formatNumber(d.properties[dataField]);
}).attr('class', 'bubbletext');
var legend = svg.append('g')
.attr('class', 'legend')
.attr('transform', 'translate(' + (mapWidth - 50) + ',' + (mapHeight - 20) + ')')
.selectAll('g')
.data([zoomRange[1] / 10, zoomRange[1] / 2, zoomRange[1]])
.enter().append('g');
legend.append('circle')
.attr('cy', function (d) {
return -radius(d);
})
.attr('r', radius);
legend.append('text')
.attr('y', function (d) {
return -2 * radius(d);
})
.attr('dy', '1.3em')
.text(d3.format('.1s'));
}