-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
201 lines (174 loc) · 6.25 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flotsam and Jetsam</title>
<!-- Include Leaflet 1.2.0 Library -->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script>
<!-- Fonts -->
<link href='https://fonts.googleapis.com/css?family=Oswald:300,400,600,700' rel='stylesheet' type='text/css'>
<link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
<!-- Include cartodb.js Library -->
<script src="https://cdn.rawgit.com/CartoDB/cartodb.js/@4.0.0-alpha.28/carto.js"></script>
<style>
* { margin:0; padding:0; }
#timeline {
background: white;
position: absolute;
left: 25%;
padding: 15px;
bottom: 10px;
height: 30px;
width: 600px;
display: flex;
align-items: center;
z-index: 1000000;
border-style: solid;
border-width: 1px;
border-color: #381a00;
font-family: 'Oswald', cursive;
font-size: x-large;
color: #381a00;
}
#timeline input {
flex: 1;
}
#timeline label {
font-size: 12px;
margin-left: 20px;
}
html { box-sizing:border-box; height:100%; }
body { background: lightgray; height:100%; font-family:"Open sans", Helvetica, Arial, sans-serif; }
#container { display:flex; width:100%; height:100%; background: black;}
#map { flex:1; margin:10px; }
#widgets { width:300px; margin:10px 10px 10px 0; }
.widget { background:white; padding:10px; margin-bottom:10px; }
.widget h1 { font-size:1.2em; }
.leaflet-popup-content-wrapper {
background: lightgray
}
.leaflet-popup-content-wrapper .leaflet-popup-content {
background: lightgray;
font-family: 'Oswald', cursive;
font-size: medium;
color: #381a00;
}
.leaflet-popup-tip {
background: lightgray;
}
#map {
border-style: solid;
border-width: 1px;
border-color: #381a00;
}
</style>
</head>
<body>
<div id="container">
<div id="map"></div>
</div>
<div id="timeline">
<input id="timeSlider" type="range" value="0" step="1" />
<label id="dateValue" />
</div>
<script>
// Is this an italian restaurant? Because man, is this some spaghetti code :D
const dateValue = document.querySelector('#dateValue');
const timeSlider = document.querySelector('#timeSlider');
const table = 'large_sample_processed_1';
let timeSeriesData;
function iso(t) {
return t.toISOString().substring(0,10);
}
function countries_at(t) {
var year;
if (t === undefined) {
// Please don't judge me
year = '2017-12-01';
} else {
year = t
}
t = new Date(year);
console.log(t);
var sql = `SELECT * FROM ${table} WHERE date = '${iso(t)}'::date`;
console.log(sql);
return sql
}
function input_date (offset) {
return new Date(timeSeriesData.bins[timeSlider.value].start * 1000);
}
function updateDate (e) {
dateValue.innerHTML = input_date();
}
var source = new carto.source.SQL(countries_at());
function inputChange () {
source.setQuery(countries_at(input_date()));
}
timeSlider.oninput = updateDate;
timeSlider.addEventListener('change', inputChange);
// 1. Setting up the Leaflet Map
var map = L.map('map').setView([38, 145], 6);
L.tileLayer('https://{s}.basemaps.cartocdn.com/rastertiles/dark_all/{z}/{x}/{y}.png', {
maxZoom: 18,
attribution: '©<a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, ©<a href="https://carto.com/attribution">CARTO</a>'
}).addTo(map);
// 2 Defining a carto.Client
var client = new carto.Client({
apiKey: 'fake',
username: 'michellemho-carto'
});
// 3. Displaying on the map
// 3.1 Defining the layer
var fullDataset = new carto.source.Dataset('large_sample_processed_1');
var style = new carto.style.CartoCSS(`
#layer {
marker-width: 5;
marker-fill: ramp([magnitude], colorbrewer(YlGn), quantiles(5));
marker-fill-opacity: 1;
marker-line-color: #FFFFFF;
marker-line-width: 0;
marker-line-opacity: 1;
// marker-type: ellipse;
marker-placement: point;
marker-file: url('http://hockeyhumanitarian.org/wp-content/themes/hha/assets/img/Entypo/arrow-long-right.svg');
marker-allow-overlap: true;
marker-transform: rotate([angle],0,0);
}
`);
var countries = new carto.layer.Layer(source, style, {
featureOverColumns: ['magnitude', 'angle']
});
client.addLayer(countries);
// 3.3. Adding the layers to the map
client.getLeafletLayer().addTo(map);
var popup = L.popup();
countries.on('featureOver', function (featureEvent) {
popup.setLatLng(featureEvent.latLng);
popup.setContent(`Magnitude: ${Math.round(100*featureEvent.data.magnitude)/100} m/s \n Angle: ${Math.round(featureEvent.data.angle)} degrees`);
popup.openOn(map);
});
countries.on('featureOut', function (featureEvent) {
popup.removeFrom(map);
});
// 4 Creating a TimeSeries widget
// 4.1 Defining a TimeSeries dataview, the second parameter is the column we want to aggregate.
var timeSeries = new carto.dataview.TimeSeries(fullDataset, 'date', {
aggregation: 'day'
});
// 4.2 Listening to data changes on the dataview
timeSeries.on('dataChanged', function (newData) {
timeSeriesData = newData;
timeSlider.min = 1;
timeSlider.max = 25;
console.log(timeSlider.max)
updateDate();
});
// Listen to timeSeries status, currently we are showing a console.log but looks like a good place to display a loader!
timeSeries.on('statusChanged', function (newStatus, error) {
});
// 4.3 Adding the dataviews to the client
client.addDataview(timeSeries);
</script>
</body>
</html>