-
Notifications
You must be signed in to change notification settings - Fork 0
/
flights.html
163 lines (136 loc) · 4.43 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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
background: #deebf7;
}
.land {
fill: #75ab1f;
fill-opacity: 0.5;
}
.boundary {
fill: none;
stroke: #fff;
stroke-width: .5px;
stroke-linejoin: round;
stroke-linecap: round;
}
.arc {
fill: none;
stroke: #ff7f00;
stroke-width: 1.5px;
stroke-linecap: round;
stroke-opacity: .75;
}
.mapdot_from, .mapdot_to {
opacity: .75;
fill: #ff7f00;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<script>
var width = 960,
height = 500;
var projection = d3.geo.equirectangular()
.scale(153)
.rotate([115, 0])
.translate([width / 2, height / 2])
.precision(.1);
var geo_path = d3.geo.path()
.projection(projection);
function transition(path) {
path.transition().duration(1000)
.attrTween("stroke-dasharray", tweenDash)
.styleTween("opacity", tweenOpacity)
.transition().delay(1050).duration(50)
.style("opacity", 0)
.remove();
}
function tweenDash() {
var l = this.getTotalLength(),
i = d3.interpolateString("0," + l, l + "," + l);
return function(t) { return i(t); };
}
function tweenOpacity() {
return function(t) { return 1; };
}
var svg3 = d3.select("body").append("svg")
.attr("viewBox", "0 0 " + width + " " + height)
.attr("preserveAspectRatio", "xMinYMin meet");
var current_pairs = [];
d3.json("world-50m.json", function(error, topology) {
if (error) throw error;
svg3.append("path")
.datum(topojson.feature(topology, topology.objects.land))
.attr("d", geo_path)
.attr("class", "land");
svg3.append("path")
.datum(topojson.mesh(topology,
topology.objects.countries,
function(a, b) {
return a !== b && (a.id / 1000 | 0) === (b.id / 1000 | 0);
}))
.attr("d", geo_path)
.attr("class", "boundary");
d3.csv("passenger_trips.csv?121", function(error, pairs) {
if (error) throw error;
i = 0;
setInterval(function(){
var r = Math.random();
//var new_k = pairs.length-1;
//for (var k=0; k<pairs.length; k++){
//if (k == Math.round(r*pairs.length,0)){
////if (r < Math.random()){//+pairs[k].cumsum_proportion) {
//new_k = k;
//break;
//}
//}
var new_pair = pairs[Math.round(r*pairs.length,0)]
//var new_pair = pairs[new_k];
new_pair.i = i;
current_pairs.push(new_pair);
if (current_pairs.length >= 50) { current_pairs.shift(); }
var linestring_data = [];
current_pairs.forEach(function(d){
linestring_data.push({type: "LineString",
coordinates: [[d.ORIGIN_Longitude, d.ORIGIN_Latitude],
[d.DEST_Longitude, d.DEST_Latitude]],
i: d.i})
})
var arcs = svg3.selectAll(".arc")
.data(linestring_data, function(d){return d.i;});
arcs.enter().append("path")
.attr("class", "arc")
.style("opacity", 0)
.attr("d", geo_path)
.call(transition);
var mapdot_from = svg3.selectAll(".mapdot_from")
.data(current_pairs, function(d){return d.i;});
mapdot_from.enter().append("circle")
.attr("class", "mapdot_from")
.attr("cx", function(d) { return projection([d.ORIGIN_Longitude, d.ORIGIN_Latitude])[0]; })
.attr("cy", function(d) { return projection([d.ORIGIN_Longitude, d.ORIGIN_Latitude])[1]; })
.attr("r", 2)
.transition().delay(1050).duration(50)
.style("opacity", 0)
.remove();
var mapdot_to = svg3.selectAll(".mapdot_to")
.data(current_pairs, function(d){return d.i;});
mapdot_to.enter().append("circle")
.attr("class", "mapdot_to")
.style('opacity', 1e-6)
.attr("cx", function(d) { return projection([d.DEST_Longitude, d.DEST_Latitude])[0]; })
.attr("cy", function(d) { return projection([d.DEST_Longitude, d.DEST_Latitude])[1]; })
.attr("r", 2)
.transition().delay(500).duration(750)
.style('opacity', 1)
.transition().delay(1050).duration(50)
.style("opacity", 0)
.remove();
i += 1;
}, 10);
});
});
</script>