-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.html
301 lines (258 loc) · 9.67 KB
/
map.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Create a time slider</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.js"></script>
<link href="https://api.mapbox.com/mapbox-gl-js/v1.12.0/mapbox-gl.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
.map-overlay {
font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
position: absolute;
width: 25%;
right: 10px;
bottom: 10px;
/*top: 0;
left: 0;*/
padding: 10px;
}
.map-overlay .map-overlay-inner {
background-color: #fff;
/*box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);*/
border-radius: 3px;
padding: 10px;
margin-bottom: 10px;
}
.map-overlay h2 {
line-height: 24px;
display: block;
margin: 0 0 10px;
}
.map-overlay .legend .bar {
height: 10px;
width: 100%;
background: linear-gradient(to right, #440154FF, #3B528BFF, #21908CFF, #5DC863FF, #FDE725FF);
}
.map-overlay input {
background-color: transparent;
display: inline-block;
width: 100%;
position: relative;
margin: 0;
cursor: ew-resize;
}
</style>
<div id="map"></div>
<div class="map-overlay top">
<div class="map-overlay-inner">
<h2>Seeding Probability</h2>
<div id='pd'><br>
<p>Hover over a county!</p>
</div>
Day: <label id="day"></label>
<input id="slider" type="range" min="1" max="111" step="1" value="0" />
<!--button id="play-button">Play</button-->
</div>
<div class="map-overlay-inner">
<div id="legend" class="legend">
<div class="bar"></div>
<div><span style="text-align:left;"> 0
<span style="float:right;"> 1
</span></span><br><center>Probability</center></div>
</div>
</div>
</div>
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoiZ3JpZ2hpIiwiYSI6ImNrZjJ2anZ3cDI1Y3Yyc3A5aHE1cnF1bjQifQ.ZXsOWkIVBsLb6s7r4fHztg';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/grighi/ckh254qp11pcj19mpqkui0g18',
//style option: light-v10
//style option: streets-v11
center: [-92.6, 40],
zoom: 2
});
/*
function filterSt(state) {
var filters = ['==', 'STATE', state];
map.setFilter('original', filters);
// Set the label to the day
document.getElementById('day').textContent = state;
}
*/
map.on('load', function () {
/*
d3.json(
'local-counties-small.geojson',
function (err, data) {
if (err) throw err;
*/
//initialize map on day 1
var day = 1;
//set Day: label to 1
document.getElementById('day').textContent = day;
//prepare color scale
function makeColorScale(d) {
return([
'interpolate',
['linear'],
//['get', String(d), ['at', 0, ['get', 'p']]],
['get', 'p' + String(d)],
0, '#440154',
0.04, '#3B528B',
0.08, '#21908C',
0.25, '#5DC863',
0.4, '#FDE725',
1, '#696969'
])};
/*
map.addSource('county-data', {
type: 'geojson',
data: data
});
map.addLayer({
'id': 'original',
'type': 'fill',
'source': 'county-data',
'paint': {
'fill-opacity': 0.3,
'fill-color': makeColorScale(day)
}
}, //'waterway-label' // FOR STYLE light-v10: ensures polygons are rendered above waterway-labels
);
*/
//update overlay box when hovering over a county
map.on('mousemove', function(e) {
var county = map.queryRenderedFeatures(e.point, {
layers: ['original']
});
if (county.length > 0) {
document.getElementById('pd').innerHTML = '<h3><strong>' +
county[0].properties.lsad + '</strong></h3>' +
'<p>Estimated probability of first report: ' +
county[0].properties['p'+String(day)] + '</p>' + '<p>Actual day of first report: ' +
county[0].properties['First_Inf_Date'] + '</p>';
} else {
document.getElementById('pd').innerHTML = '<br><p>Hover over a county!</p>';
}
});
//update map according to slider position
document
.getElementById('slider')
.addEventListener('input', function (e) {
day = parseInt(e.target.value, 10);
map.setPaintProperty('original', 'fill-color', makeColorScale(day));
document.getElementById('day').textContent = day;
});
// });
/*
//wish list: move slider with shift+scroll
*/
/*
//code to potentially add a play button, not working
var moving = false;
var currentValue = 0;
var targetValue = width;
console.log(targetValue);
var slider = d3.select("#slider")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("transform", "translate(" + 0 + "," + height + ")");
var playButton = d3.select("#play-button");
var x = d3.scaleLinear()//建立映射关系
.domain([0, 111])
.range([0, targetValue])
.clamp(true);
slider.append("line")
.attr("class", "track")
.attr("x1", x.range()[0])
.attr("x2", x.range()[1])
.select(function () { return this.parentNode.appendChild(this.cloneNode(true)); })
.attr("class", "track-inset")
.select(function () { return this.parentNode.appendChild(this.cloneNode(true)); })
.attr("class", "track-overlay")
.call(d3.drag()
.on("start.interrupt", function () { slider.interrupt(); })
.on("start drag", function () {
currentValue = d3.event.x;
console.log(currentValue)
update(x.invert(currentValue));
})
);
slider.insert("g", ".track-overlay")
.attr("class", "ticks")
.attr("transform", "translate(0," + 18 + ")")
.selectAll("text")
.data(x.ticks(10))
.enter()
.append("text")
.attr("x", x)
.attr("y", 10)
.attr("text-anchor", "middle")
.text(function (d) {
return (d + 1);
});
var handle = slider.insert("circle", ".track-overlay")
.attr("class", "handle")
.attr("r", 9);
var labels = slider.append("g").append("text")
.attr("class", "labels")
.attr("text-anchor", "middle")
.text('1')
.attr("transform", "translate(0," + (-15) + ")")
.style("fill", "#4F442B")
playButton
.on("click", function () {
var button = d3.select(this);
if (button.text() == "Pause") {
moving = false;
clearInterval(timer);
// timer = 0;
button.text("Play");
} else {
moving = true;
timer = setInterval(step, 1000);//调用interval周期函数
button.text("Pause");
}
console.log("Slider moving: " + moving);//判定是否在运动
})
function step() {
update(x.invert(currentValue));
currentValue = currentValue + (targetValue / (111));
if (currentValue > targetValue) {
moving = false;
currentValue = 0;
clearInterval(timer);
// timer = 0;
playButton.text("Play");
// console.log("Slider moving: " + moving);
}
}
function update(day) {
var day = parseInt(day, 10);
console.log(day)
handle.attr("cx", x(day));
labels
.attr("x", x(day))
.text(day);
map.setPaintProperty('original', 'fill-color', makeColorScale(day));
}
*/
});
map.getCanvas().style.cursor = 'default';
//map.fitBounds(
// [[-170, 50],
// [ -40, 20]]);
</script>
<script type="text/javascript" src="js/iframeResizer.contentWindow.min.js"></script>
</body>
</html>