-
Notifications
You must be signed in to change notification settings - Fork 0
/
flights.html
241 lines (212 loc) · 7.95 KB
/
flights.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
<style>
.d3-tip {
line-height: 1;
font-weight: bold;
padding: 12px;
background: rgba(0, 0, 0, 0.8);
color: #fff;
border-radius: 2px;
}
.countries {
fill: #b0d0ab;
stroke: #6cb0e0;
stroke-width: 0.5px;
stroke-linecap: round;
stroke-linejoin: round;
vector-effect: non-scaling-stroke;
}
.airports {
fill: #036;
stroke: #6cb0e0;
stroke-width: 0.5px;
stroke-linecap: round;
stroke-linejoin: round;
vector-effect: non-scaling-stroke;
}
.route {
fill: none;
stroke-width: 1;
stroke: gray;
stroke-dasharray:5, 5;
}
</style>
<body>
<div id="map"></div>
<font size="6">
<text>Current time:</text>
<text class="time"
x="100" y="100"
font-family="sans-serif"
font-size="100px"
text-anchor="middle"
fill="red">22:00</text>
</font>
<script src="d3/d3.v3.min.js"></script>
<script src="http://labratrevenge.com/d3-tip/javascripts/d3.tip.v0.6.3.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/queue.v1.min.js"></script>
<script>
var width = 938;
var height = 620;
var projection = d3.geo
.mercator()
.scale(150)
.translate([width / 2, height / 2]);
var start_time = Date.now();
start_time -= 22 * 60 * 60;
var last = 22 * 60 * 60;
var path = d3.geo
.path()
.pointRadius(2)
.projection(projection);
var svg = d3.select("#map")
.append("svg")
.attr("width", width)
.attr("height", height);
var airportMap = {};
function transition(plane, route, duration) {
var l = route.node().getTotalLength();
plane.transition()
.duration(duration)
.attrTween("transform", delta(plane, route.node()))
.each("end", function() { route.remove(); })
.remove();
}
function delta(plane, path) {
var l = path.getTotalLength();
var plane = plane;
return function(i) {
return function(t) {
var p = path.getPointAtLength(t * l);
var t2 = Math.min(t + 0.05, 1);
var p2 = path.getPointAtLength(t2 * l);
var x = p2.x - p.x;
var y = p2.y - p.y;
var r = 90 - Math.atan2(-y, x) * 180 / Math.PI;
var s = Math.min(Math.sin(Math.PI * t) * 0.7, 0.3);
return "translate(" + p.x + "," + p.y + ") scale(" + s + ") rotate(" + r + ")";
}
}
}
function fly(origin, destination, duration) {
var route = svg.append("path")
.datum({type: "LineString", coordinates: [airportMap[origin], airportMap[destination]]})
.attr("class", "route")
.attr("d", path);
var plane = svg.append("path")
.attr("class", "plane")
.attr("transform", "translate(-100, -100)")
.attr("d", "m25.21488,3.93375c-0.44355,0 -0.84275,0.18332 -1.17933,0.51592c-0.33397,0.33267 -0.61055,0.80884 -0.84275,1.40377c-0.45922,1.18911 -0.74362,2.85964 -0.89755,4.86085c-0.15655,1.99729 -0.18263,4.32223 -0.11741,6.81118c-5.51835,2.26427 -16.7116,6.93857 -17.60916,7.98223c-1.19759,1.38937 -0.81143,2.98095 -0.32874,4.03902l18.39971,-3.74549c0.38616,4.88048 0.94192,9.7138 1.42461,13.50099c-1.80032,0.52703 -5.1609,1.56679 -5.85232,2.21255c-0.95496,0.88711 -0.95496,3.75718 -0.95496,3.75718l7.53,-0.61316c0.17743,1.23545 0.28701,1.95767 0.28701,1.95767l0.01304,0.06557l0.06002,0l0.13829,0l0.0574,0l0.01043,-0.06557c0,0 0.11218,-0.72222 0.28961,-1.95767l7.53164,0.61316c0,0 0,-2.87006 -0.95496,-3.75718c-0.69044,-0.64577 -4.05363,-1.68813 -5.85133,-2.21516c0.48009,-3.77545 1.03061,-8.58921 1.42198,-13.45404l18.18207,3.70115c0.48009,-1.05806 0.86881,-2.64965 -0.32617,-4.03902c-0.88969,-1.03062 -11.81147,-5.60054 -17.39409,-7.89352c0.06524,-2.52287 0.04175,-4.88024 -0.1148,-6.89989l0,-0.00476c-0.15655,-1.99844 -0.44094,-3.6683 -0.90277,-4.8561c-0.22699,-0.59493 -0.50356,-1.07111 -0.83754,-1.40377c-0.33658,-0.3326 -0.73578,-0.51592 -1.18194,-0.51592l0,0l-0.00001,0l0,0z");
transition(plane, route, duration);
}
function loaded(error, countries, airports) {
svg.append("g")
.attr("class", "countries")
.selectAll("path")
.data(topojson.feature(countries, countries.objects.countries).features)
.enter()
.append("path")
.attr("d", path);
var tip = d3.tip()
.attr('class', "d3-tip")
.offset([-10, 0])
.html(function(d) {
return "<strong>Airport:</strong> <span class='values'>" + d.id + "</span><br>"
+ "<strong>In degree:</strong> <span class='values'>" + d.properties.indegree + "</span><br>"
+ "<strong>Out degree:</strong> <span class='values'>" + d.properties.outdegree + "</span><br>"
+ "<strong>Degree:</strong> <span class='values'>" + d.properties.degree + "</span><br>"
+ "<strong>Betweenness Centrality:</strong> <span class='values'>" + d.properties.betweenness + "</span><br>"
+ "<strong>Category:</strong> <span class='values'>" + d.properties.category + "</span><br>";
})
svg.call(tip);
svg.append("g")
.attr("class", "airports")
.selectAll("path")
.data(topojson.feature(airports, airports.objects.airports).features)
.enter()
.append("path")
.attr("id", function(d) {console.log(d); return d.id;})
.attr("d", path)
.style("fill", function(d) {
if (d.properties.category == 'Aviation Hub') return "red";
if (d.properties.category == 'Important Gateway') return "purple";
if (d.properties.category == 'Important Origin & Destination') return "green";
if (d.properties.category == 'Important Destination') return "#191970";
return "#00bfff";
})
.on('mouseover', tip.show)
.on('mouseout', tip.hide);
var geos = topojson.feature(airports, airports.objects.airports).features;
for (i in geos) {
airportMap[geos[i].id] = geos[i].geometry.coordinates;
}
var pairs = [];
d3.tsv("schedule.tsv", function(error, data) {
data.forEach(function(d) {
var arr = [];
var start = 1417315200;
arr.push(d.ORI);
arr.push(d.DES);
arr.push(d.TIME - start);
arr.push(d.ARR - start);
if (arr[2] > 0) {
pairs.push(arr);
}
});
});
var i = 0;
setInterval(function() {
if (i > pairs.length - 1) {
i = 0;
}
var od = pairs[i];
//console.log(od[2]);
setTimeout(function() {
fly(od[0], od[1], od[3] - od[2]);
}, od[2]);
i++;
}, 100);
setInterval(function() {
var interval = Date.now() - start_time;
if (interval - last >= 1800) {
last += 1800;
var hour = Math.floor(interval / 3600);
if (hour >= 24) {
last -= 24 * 60 * 60;
start_time += 24 * 60 * 60;
hour = 0;
}
var min = interval - hour * 3600;
if (min >= 1800) min = 1800;
else min = 0;
var str = "";
if (min == 0) str += hour + ":00";
else str += hour + ":30";
if (str.length == 4) str = "0" + str;
d3.select(".time").text(str);
}
}, 800);
/*
var airport_data = [];
d3.tsv("ordered_metrics.tsv", function(error, data) {
data.forEach(function(d) {
var map = {};
map.Label = d.Label;
map.InDegree = d.InDegree;
map.OutDegree = d.OutDegree;
map.Degree = d.Degree;
map.BetweennessCentrality = d.BetweennessCentrality;
map.Category = d.Category;
// console.log(map.Category);
airport_data.push(map);
});
});
console.log(airport_data);
//console.log(svg.selectAll(".airport").enter().data(airport_data).attr("x", function(d) { console.log(d.Label); return d.Label; }));
*/
}
queue().defer(d3.json, "countries.topo.json")
.defer(d3.json, "airports.topo.json")
.await(loaded);
</script>
</body>