-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap.html
830 lines (686 loc) · 19.4 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
<!DOCTYPE html>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300|Josefin+Slab|Arvo|Lato|Vollkorn|Abril+Fatface|Old+Standard+TT|Droid+Sans|Lobster|Inconsolata|Montserrat|Playfair+Display|Karla|Alegreya|Libre+Baskerville|Merriweather|Lora|Archivo+Narrow|Neuton|Signika|Questrial|Fjalla+One|Bitter|Varela+Round);
.container{
display: flex;
}
#leftdiv{
width: 700px;
height: 500px;
position: relative;
background-color: red;
}
.rightdiv{
flex-grow: 1;
}
.background {
fill: #eee;
pointer-events: all;
}
.map-layer {
fill: #fff;
stroke: #aaa;
}
text{
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
font-weight: 300;
}
text.big-text{
font-size: 30px;
font-weight: 400;
}
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
.slidecontainer {
position: relative;
}
#vis {
float: left;
margin-left: 20px;
margin-top: 8px;
}
#play-button {
margin-left: 100px;
top: 3px;
background: #f08080;
padding-right: 26px;
padding-left: 50px;
border-radius: 3px;
border: none;
color: white;
margin: 0;
padding: 0 12px;
width: 60px;
cursor: pointer;
height: 30px;
}
#play-button:hover {
background-color: #696969;
}
#timestring {
float: left;
margin-left: 29%;
}
.slider {
-webkit-appearance: none;
float: left;
width: 540px;
height: 5px;
border-radius: 5px;
background: #d3d3d3;
outline: none;
opacity: 0.7;
-webkit-transition: .2s;
transition: opacity .2s;
margin-right: 30px;
margin-left: 43px;
margin-top: 6px;
margin-bottom: 6px;
}
.slider::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 15px;
height: 15px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
.slider::-moz-range-thumb {
width: 25px;
height: 25px;
border-radius: 50%;
background: #4CAF50;
cursor: pointer;
}
.stop-left {
stop-color: #00ff00; /* Indigo */
}
.stop-right {
stop-color: #ff0000; /* Teal */
}
.filled {
fill: url(#mainGradient);
}
p {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
form {
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
}
</style>
<body>
<div class="container">
<div id="leftdiv">
<svg id="mapclass"></svg>
<div id="uncertaintyid">
<form>
<input type="checkbox" onclick="changeUncertainty()"> Alter Opacity Based On Uncertainty <br>
</form>
</div>
</div>
<div class="rightdiv">
<div class="slidecontainer">
<div id="playtime">
<div id="timestring">
<p>Time: <span id="demo"></span></p>
</div>
<div id="vis">
<button id="play-button">Play</button>
</div>
</div>
<div id="slidelevel">
<input type="range" min="0" max="719" value="0" class="slider" id="myRange">
</div>
</div>
<div id="side_graphs"></div>
</div>
</div>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/math.min.js"></script>
<script>
var moving = false;
var currentValue = 0;
var targetValue = 719;
var day = 6;
var hour = 0;
var minute = 0;
var wholeDay = 0;
var time = 0;
var currentDate;
var dateString;
const weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
var totalUncertainty = []
var totalRadiation = []
var totalPosition = []
var slider = document.getElementById("myRange");
var playButton = d3.select("#play-button").on("click", function() {
// Toggle playing boolean
moving = !moving;
if (moving) {
this.innerHTML = "Pause";
} else {
this.innerHTML = "Play";
}
if (moving) {
animateElements();
}
});
function animateElements() {
slider.stepUp();
time = slider.value;
if (time == 719) {
moving = false;
console.log('ye');
d3.select("#play-button").innerHTML = "Play";
}
currentDate = calculateDate();
output.innerHTML = convertDateString(currentDate);
updateData(slider.value);
if (moving) {
setTimeout(animateElements, 100);
}
}
function calculateDate() {
minute = (time * 10) % 60;
hour = (Math.floor((time * 10) / 60)) % 24;
wholeDay = Math.floor((time * 10) / 1440);
day = 6 + wholeDay;
return new Date(2020, 3, day, hour, minute, 0);
}
function convertDateString(currDate) {
var dateString = currDate.toString();
return dateString.slice(0, 21);
}
var output = document.getElementById("demo");
currentDate = calculateDate()
output.innerHTML = convertDateString(currentDate); // Display the default slider value
// Update the current slider value (each time you drag the slider handle)
slider.oninput = function() {
time = this.value;
currentDate = calculateDate();
output.innerHTML = convertDateString(currentDate);
updateData(this.value);
}
var width = 650,
height = 600,
centered;
// Define color scale
var color = d3.scale.linear()
.clamp(true)
.range(['#00ff00', '#ff0000']);
var projection = d3.geo.mercator()
.scale(100000)
// Center the Map in Colombia
.center([-119.85, .1])
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
// Set svg width & height
var svg = d3.select('svg')
.attr('width', width)
.attr('height', height);
// Add background
rect = svg.append('rect')
.attr('class', 'background')
.attr('width', width)
.attr('height', height)
.on('click', clicked)
var g = svg.append('g');
var mapLayer = g.append('g')
.classed('map-layer', true);
var bigText = g.append('text')
.classed('big-text', true)
.attr('x', 20)
.attr('y', 45);
var radiationText = g.append('text')
.attr('x', 20)
.attr('y', 70)
var uncertaintyText = g.append('text')
.attr('x', 20)
.attr('y', 85)
var tooltipRect = g.append('rect')
.attr('fill', 'white')
.attr('opacity', 0)
.attr('width', '145px')
.attr('height', '60px')
var tooltip1 = g.append('text')
.attr('x', 0)
.attr('y', 0)
var tooltip2 = g.append('text')
.attr('x', 0)
.attr('y', 0)
var tooltip3 = g.append('text')
.attr('x', 0)
.attr('y', 0)
var globalData = {}
var globalUncertainty = {}
var globalPosition = {}
var currMouseover = null
var globalTime = 0
var uncertainty = false
var maxUncertainty = 0
initData()
function initData() {
d3.json("radiation.json", function(d) {
d3.json("uncertainty.json", function(u) {
d3.json("position.json", function(p){
console.log(p);
totalUncertainty = JSON.parse(u)
totalRadiation = JSON.parse(d)
totalPosition = JSON.parse(p)
var uncertaintyData = totalUncertainty[globalTime]
var data = totalRadiation[globalTime];
var positionData = totalPosition[globalTime]
drawMap(data, uncertaintyData, positionData, totalRadiation)
})
});
});
}
function rawValuesFromData(entireData, mapData) {
rawValues = []
var features = mapData.features;
for (i = 0; i < entireData.length; i++){
dataForTimestamp = entireData[i]
for (j = 0; j < features.length; j++) {
if (dataForTimestamp[nameFn(features[j])] != null){
rawValues.push(dataForTimestamp[nameFn(features[j])])
}
}
}
return rawValues
}
function drawMap(data, uncertaintyData, positionData, totalRadiation) {
globalData = data
globalUncertainty = uncertaintyData
globalPosition = positionData
var geoJson = d3.json('StHimarkGeo.json', function(error, mapData) {
var features = mapData.features;
rawValuesRadiation = rawValuesFromData(totalRadiation, mapData)
stdDevRad = math.std(rawValuesRadiation)
medianRad = math.median(rawValuesRadiation)
maxScaleRad = medianRad + .1 * stdDevRad
rawValuesUncertainty = rawValuesFromData(totalUncertainty, mapData)
stdDevUnc = math.std(rawValuesUncertainty)
meanUnc = math.mean(rawValuesUncertainty)
maxUncertainty = meanUnc + stdDevUnc
// Update color scale domain based on data
color.domain([0, maxScaleRad]);
// Draw each province as a path
mapLayer.selectAll('path')
.data(features)
.enter().append('path')
.attr('d', path)
.attr('vector-effect', 'non-scaling-stroke')
.style('fill', fillFn)
.on('mouseover', mouseover)
.on('mouseout', mouseout)
.on('click', clicked);
for (const key of Object.keys(positionData)){
mapLayer.selectAll("circle")
.data(Object.keys(positionData)).enter()
.append("circle")
.attr("cx", d => positionData[d]['point'][0])
.attr("cy", d => positionData[d]['point'][1])
.attr("r", "4px")
.attr("fill", d => positionData[d]['type'] == 'mobile' ? "yellow" : "blue")
.on("mouseover", d => sensorTooltip(d))
.on("mouseout", d => tooltipOut(d))
}
});
}
//Read in radiation data and parse into a javascript object where the neighborhoods are the keys and the values are the values
//Calls drawMap after processing is complete
function updateData(time) {
globalTime = time;
var uncertaintyData = totalUncertainty[time];
var data = totalRadiation[time];
var positionData = totalPosition[time];
updateMap(data, uncertaintyData, positionData)
}
function updateMap(data, uncertaintyData, positionData){
globalData = data
globalUncertainty = uncertaintyData
globalPosition = positionData
var geoJson = d3.json('StHimarkGeo.json', function(error, mapData) {
var features = mapData.features;
if (currMouseover != null) {displayData(currMouseover)}
// Draw each province as a path
mapLayer.selectAll('path')
.style('fill', function(d){return fillFn(d);})
});
mapLayer.selectAll('circle').remove()
for (const key of Object.keys(positionData)){
mapLayer.selectAll("circle")
.data(Object.keys(positionData)).enter()
.append("circle")
.attr("cx", function(d){return positionData[d]['point'][0]})
.attr("cy", function(d){return positionData[d]['point'][1]})
.attr("r", "4px")
.attr("fill", function(d){return positionData[d]['type'] == 'mobile' ? "yellow" : "blue"})
.on("mouseover", d => sensorTooltip(d))
.on("mouseout", d => tooltipOut(d))
}
}
function changeUncertainty(){
uncertainty = !uncertainty
updateData(time)
}
// Get province name
function nameFn(d){
return d && d.properties ? d.properties.Nbrhood : null;
}
function convertHex(hex,alpha){
hex = hex.replace('#','');
a = Math.round(alpha * 255).toString(16);
console.log("alpha", a)
return "#" + hex + a;
}
// Get province color
function fillFn(d){
var rawColor = color(globalData[nameFn(d)])
var newColor = rawColor
if (uncertainty) {
if (globalUncertainty[nameFn(d)] < maxUncertainty) {
opacity = 1 - globalUncertainty[nameFn(d)] / maxUncertainty + .01
newColor = convertHex(rawColor, opacity)
}
else {
newColor = convertHex(rawColor, 0.1)
console.log(globalUncertainty[nameFn(d)])
}
}
return newColor;
}
// When clicked, zoom in
function clicked(d) {
var x, y, k;
// Compute centroid of the selected path
if (d && centered !== d) {
var centroid = path.centroid(d);
x = centroid[0];
y = centroid[1];
k = 4;
centered = d;
} else {
x = width / 2;
y = height / 2;
k = 1;
centered = null;
}
// Zoom
g.transition()
.duration(750)
.attr('transform', 'translate(' + width / 2 + ',' + height / 2 + ')scale(' + k + ')translate(' + -x + ',' + -y + ')');
}
d3.selection.prototype.moveToFront = function() {
return this.each(function(){
this.parentNode.appendChild(this);
});
};
d3.selection.prototype.moveToBack = function() {
return this.each(function() {
var firstChild = this.parentNode.firstChild;
if (firstChild) {
this.parentNode.insertBefore(this, firstChild);
}
});
};
function mouseover(d){
// Highlight hovered province
//d3.select(this).style('fill', 'orange');
d3.select(this).moveToFront();
d3.select(this).style("stroke", "blue");
currMouseover = nameFn(d)
// Draw effects
textArt(currMouseover);
displayData(currMouseover);
updateData(globalTime)
}
function mouseout(d){
d3.select(this).style("stroke", "steelblue");
// Reset province color
mapLayer.selectAll('path')
.style('fill', function(d){return fillFn(d);});
currMouseover = null
// Clear province name
bigText.text('');
radiationText.text('');
uncertaintyText.text('');
d3.select(this).moveToBack();
}
var BASE_FONT = "'Helvetica Neue', Helvetica, Arial, sans-serif";
var FONTS = [
"Open Sans",
"Josefin Slab",
"Arvo",
"Lato",
"Vollkorn",
"Abril Fatface",
"Old StandardTT",
"Droid+Sans",
"Lobster",
"Inconsolata",
"Montserrat",
"Playfair Display",
"Karla",
"Alegreya",
"Libre Baskerville",
"Merriweather",
"Lora",
"Archivo Narrow",
"Neuton",
"Signika",
"Questrial",
"Fjalla One",
"Bitter",
"Varela Round"
];
function sensorTooltip(d) {
tooltip1
.attr("x", globalPosition[d].point[0])
.attr("y", globalPosition[d].point[1] - 45 )
.text("Sensor Id: " + globalPosition[d].id)
tooltip2
.attr("x", globalPosition[d].point[0])
.attr("y", globalPosition[d].point[1] - 25)
.text("Sensor type: " + globalPosition[d].type)
uncertaintyFloat = Math.round(globalPosition[d].uncertainty * 1000) / 1000
tooltip3
.attr("x", globalPosition[d].point[0])
.attr("y", globalPosition[d].point[1] - 5)
.text("Uncertainty: " + uncertaintyFloat)
tooltipRect
.attr("x", globalPosition[d].point[0])
.attr("y", globalPosition[d].point[1] - 60)
.attr("opacity", 1)
}
function tooltipOut(d) {
tooltip1.text('')
tooltip2.text('')
tooltip3.text('')
tooltipRect
.attr("opacity", 0)
}
function textArt(text){
// Use random font
var fontIndex = Math.round(Math.random() * FONTS.length);
var fontFamily = FONTS[fontIndex] + ', ' + BASE_FONT;
bigText
.style('font-family', fontFamily)
.text(text);
}
function displayData(text) {
var fontIndex = Math.round(Math.random() * FONTS.length);
var fontFamily = FONTS[fontIndex] + ', ' + BASE_FONT;
var radiation = globalData[text]
var uncertainty = globalUncertainty[text]
var displayRadiation = radiation != null ? radiation.toFixed(2) : "No Data"
var displayUncertainty = uncertainty != null ? uncertainty.toFixed(2) : "No Data"
radiationText
.style('fontFamily', fontFamily)
.text("Radiation Level: " + displayRadiation)
uncertaintyText
.style('fontFamily', fontFamily)
.text("Uncertainty: " + displayUncertainty)
}
//Add key
// Create the svg:defs element and the main gradient definition.
var svgDefs = svg.append('defs');
var mainGradient = svgDefs.append('linearGradient')
.attr('id', 'mainGradient');
// Create the stops of the main gradient. Each stop will be assigned
// a class to style the stop using CSS.
mainGradient.append('stop')
.attr('class', 'stop-left')
.attr('offset', '0');
mainGradient.append('stop')
.attr('class', 'stop-right')
.attr('offset', '1');
// Use the gradient to set the shape fill, via CSS.
svg.append('rect')
.classed('filled', true)
.attr('x', 20)
.attr('y', 525)
.attr('width', 300)
.attr('height', 50);
svg.append('text')
.text('Radiation Level')
.attr('x', 120)
.attr('y', 520)
svg.append('text')
.text('Low')
.attr('x', 20)
.attr('y', 520)
svg.append('text')
.text('High')
.attr('x', 290)
.attr('y', 520)
</script>
<!-- Mobile Average Line Graph -->
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse the date / time
// var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%SZ").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.timestamp); })
.y(function(d) { return y(d.Average); });
// Adds the svg canvas
var mobileline = d3.select("#side_graphs")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Get the data
d3.csv("mobileaverage.csv", function(error, data) {
data.forEach(function(d) {
d.timestamp = new Date(d.timestamp);
d.Average = +d.Average;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.timestamp; }));
y.domain([0, d3.max(data, function(d) { return d.Average; })]);
// Add the valueline path.
mobileline.append("path")
.attr("class", "line")
.attr("d", valueline(data));
// Add the X Axis
mobileline.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
mobileline.append("g")
.attr("class", "y axis")
.call(yAxis);
mobileline.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Average Mobile Sensor Radiation");
});
</script>
<!-- Static Average Line Graph -->
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 30, right: 20, bottom: 30, left: 50},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse the date / time
// var parseDate = d3.time.format("%Y-%m-%dT%H:%M:%SZ").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x)
.orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y)
.orient("left").ticks(5);
// Define the line
var valueline = d3.svg.line()
.x(function(d) { return x(d.timestamp); })
.y(function(d) { return y(d.Average); });
// Adds the svg canvas
var staticline = d3.select("#side_graphs")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Get the data
d3.csv("staticaverage.csv", function(error, data) {
data.forEach(function(d) {
d.timestamp = new Date(d.timestamp);
d.Average = +d.Average;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.timestamp; }));
y.domain([0, d3.max(data, function(d) { return d.Average; })]);
// Add the valueline path.
staticline.append("path")
.attr("class", "line")
.attr("d", valueline(data));
// Add the X Axis
staticline.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
staticline.append("g")
.attr("class", "y axis")
.call(yAxis);
staticline.append("text")
.attr("x", (width / 2))
.attr("y", 0 - (margin.top / 2))
.attr("text-anchor", "middle")
.style("font-size", "16px")
.style("text-decoration", "underline")
.text("Average Static Sensor Radiation");
});
</script>