-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
148 lines (118 loc) · 3.82 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="svg.css">
<script src="d3.v3.min.js"></script>
<script src="topojson.v0.min.js"></script>
<script src="jquery-1.9.1.min.js"></script>
<script src="21-03-13-12pm-truncated.js"></script>
</head>
<body>
<div id="chart">
</div>
<script type="text/javascript">
//// SVG AND D3 STUFF
var svg = d3.select("#chart").append("svg")
.attr("width", 500)
.attr("height", 620);
var projection = d3.geo.albers()
.center([0, 55.4])
.rotate([4.4, 0])
.parallels([50, 60])
.scale(3800)
.translate([250, 250]);
var path = d3.geo.path()
.projection(projection);
// var colourScale = d3.scale.linear()
// .domain([-30, -20, -16, -10, 0, 10, 12, 16, 18, 22, 28, 36])
// .range(['#FFFFFF', '#828282', '#5D1879', '#00197C', '#00FFFF', '#00C44E', '#00FA57', '#FFFA56', '#F1E98D', '#E5AB58', '#FF0000', '#810000']);
var lines = [];
function lineAnimate(selection) {
selection
.attr({
x2: function(d) {return d.x0},
y2: function(d) {return d.y0}
})
.style('opacity', 0)
.transition()
.ease('linear')
.duration(function(d) {return d.duration;})
.delay(function(d) {return d.delay;})
.attr({
x2: function(d) {return d.x1},
y2: function(d) {return d.y1}
})
.style('opacity', 0.8)
.transition()
.duration(1000)
.style('opacity', 0.1)
.each('end', function() {d3.select(this).call(lineAnimate)});
}
//// MATH FUNCTIONS
function toRad(deg) {return deg * Math.PI / 180;}
function toDeg(rad) {return rad * 180 / Math.PI;}
function lonLatFromLonLatDistanceAndBearing(lonLat, d, brng) {
// Formulae from http://www.movable-type.co.uk/scripts/latlong.html
// brg in radians, d in km
var R = 6371; // Earth's radius in km
var lon1 = toRad(lonLat[0]), lat1 = toRad(lonLat[1]);
var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));
return [toDeg(lon2), toDeg(lat2)];
}
//// INITIALISATION
var cardinalToBearing = {};
function init() {
var i, cardinalPoints = ['S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE'];
// Calculate cardinal point to bearing mapping (wind direction is where the wind is coming *from*!)
for(i = 0; i < cardinalPoints.length; i++)
cardinalToBearing[cardinalPoints[i]] = i * Math.PI / 8;
// Prepare line co-ordinates
var windData = weather.SiteRep.DV.Location;
for(i = 0; i < windData.length; i++) {
var d = windData[i];
var speed = d.Period.Rep.S;
var feelsLikeTemperature = d.Period.Rep.F;
var lonLat0 = [d.lon, d.lat];
// Scale line length proportionally to speed
var lonLat1 = lonLatFromLonLatDistanceAndBearing(lonLat0, 1.2 * speed, cardinalToBearing[d.Period.Rep.D]);
var x0y0 = projection(lonLat0);
var x1y1 = projection(lonLat1);
var line = {
x0: x0y0[0],
y0: x0y0[1],
x1: x1y1[0],
y1: x1y1[1],
s: speed,
// f: feelsLikeTemperature,
duration: 8000 / speed, /* pre-compute duration */
delay: Math.random() * 1000 /* pre-compute delay */
};
// console.log(line);
lines.push(line);
}
}
//// The app
d3.json("uk.json", function(error, uk) {
// UK map drawing courtesy of http://bost.ocks.org/mike/map/
var subunits = topojson.object(uk, uk.objects.subunits);
svg.append("path")
.datum(subunits)
.attr("d", path);
// Draw the lines
svg.selectAll('line')
.data(lines)
.enter()
.append("line")
.attr({
x1: function(d) {return d.x0},
y1: function(d) {return d.y0}
})
// .style('stroke', function(d) {return colourScale(d.f);})
.call(lineAnimate);
});
init();
</script>
</body>
</html>