forked from kuanb/dataCanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract.html
804 lines (752 loc) · 30.3 KB
/
abstract.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
<!DOCTYPE html>
<html>
<head>
<title>Data Canvas Entry - Kuan</title>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="new_weather.css"> <!-- CodePen source: http://codepen.io/weaintplastic/pen/qEMZbx -->
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" /> <!-- Leaflet -->
<style>
html, body{
background-color: #5AC2E7;
height: 100%;
width: 100%;
overflow: hidden;
font-family: 'Open Sans', sans-serif;
font-size: 10px;
}
#map {
position:fixed;
width:50%;
height:100%;
z-index:0;
background-color: #FFF;
margin: -25px -50px -25px -25px;
}
#gradient {
height: 150px;
margin: -25px -50px -125px -25px;
background: -webkit-linear-gradient(bottom, rgba(255,255,255,0), rgba(255,255,255,0.6), rgba(255,255,255,1)); /* For Safari 5.1 to 6.0 */
background: -o-linear-gradient( top, rgba(255,255,255,0), rgba(255,255,255,0.6), rgba(255,255,255,1)); /* For Opera 11.1 to 12.0 */
background: -moz-linear-gradient( top, rgba(255,255,255,0), rgba(255,255,255,0.6), rgba(255,255,255,1)); /* For Firefox 3.6 to 15 */
background: linear-gradient(to top, rgba(255,255,255,0), rgba(255,255,255,0.6), rgba(255,255,255,1)); /* Standard syntax (must be last) */
}
#loader {
display: none;
position: fixed;
left: 25%;
zoom: 10;
-moz-transform: scale(3);
}
#city_name {
position: absolute;
padding: 25px 50px 25px 50px;
color: white;
}
#city_name h1 {
font-weight: 700;
}
#all {
height: 100vh;
}
#info {
height: 100%;
padding: 25px 50px 25px 25px;
color: #5AC2E7;
background-color: #FFF;
box-shadow: 15px 15px 15px 15px rgba(0,0,0,0.25);
}
svg path {
transition: fill 1s ease;
}
svg:hover path.hex_layers {
fill: #282828 !important;
}
svg:hover path.hex_arrows {
stroke-width: 1px !important;
}
svg:hover path.hex_arrow_labels {
visibility: visible;
}
</style>
</head>
<body>
<div class="row" id="all">
<div class="col-sm-6">
<div id="city_name">
<h1>Bangalore</h1>
</div>
<div id="diagram">
</div>
<div class="preloader loading" id="loader">
<span class="slice"></span>
<span class="slice"></span>
<span class="slice"></span>
<span class="slice"></span>
<span class="slice"></span>
<span class="slice"></span>
</div>
</div>
<div class="col-sm-6" id="info">
<!-- Leaflet map goes here -->
<div id="map"></div>
<div class="row">
<div class="col-sm-12">
<div id="gradient"></div>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<h2>Checking the weather in a "Smart City."</h2>
<div class="input-group">
<div class="input-group-btn">
<div class="btn-group">
<button type="button" class="btn btn-info dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
City Selector <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" id="city_list">
<li><a href="#">Bangalore</a></li>
<li><a href="#">Boston</a></li>
<li><a href="#">Geneva</a></li>
<li><a href="#">Rio de Janeiro</a></li>
<li><a href="#">San Francisco</a></li>
<li><a href="#">Shanghai</a></li>
<li><a href="#">Singapore</a></li>
</ul>
</div>
</div>
<input type="text" class="form-control" aria-label="city_selected" id="city_selected" value="Bangalore">
</div>
</div>
</div>
</div>
</div>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<script src="src/leaflet-heat.js"></script>
<script type="text/javascript">
// globals
var original_temp;
var original_desc;
var latlngs = {
'Bangalore': [12.9667, 77.5667],
'Boston': [42.364255, -71.071577],
'Geneva': [46.203188, 6.137050],
'Rio de Janeiro': [-22.904355, -43.175893],
'San Francisco': [37.790129, -122.408418],
'Shanghai': [31.244311, 121.479499],
'Singapore': [1.296472, 103.852962],
}
// set up leaflet
var map = L.map('map', {zoomControl: false}).setView(latlngs.Bangalore, 12);
L.tileLayer('http://{s}.tiles.mapbox.com/v3/kuanb.ipfm8n84/{z}/{x}/{y}.png', {
attribution: 'Map data <a href="http://openstreetmap.org">OpenStreetMap</a>, Imagery <a href="http://mapbox.com">Mapbox</a>',
maxZoom: 18,
opacity: 0.5,
}).addTo(map);
var heat = L.heatLayer();
all_sensors = {
'San Francisco': {
'Urban Launchpad': {
'id': 'ci4yfbbdb000d03zzoq8kjdl0'
},
'GlenParklifeLogger': {
'id': 'ci4yhy9yy000f03zznho5nm7c'
},
'ClimateNinja9000': {
'id': 'ci4yyrdqi000j03zz8ylornqd'
},
'Exploratorium': {
'id': 'ci4vy1tfy000m02s7v29jkkx4'
},
'Datavore': {
'id': 'ci4lnqzte000002xpokc9d25v'
},
'Grand Theater': {
'id': 'ci4usvy81000302s7whpk8qlp'
},
'mapsense': {
'id': 'ci4usvryz000202s7llxjafaf'
},
'GehlData': {
'id': 'ci4xcxxgc000n02tci92gpvi6'
},
'a-streetcar-named-data-sensor': {
'id': 'ci4usss1t000102s7hkg0rpqg'
},
'AlleyCat': {
'id': 'ci4tmxpz8000002w7au38un50'
},
'DataDonut': {
'id': 'ci4yf50s5000c03zzt4h2tnsq'
},
'grapealope': {
'id': 'ci4ut5zu5000402s7g6nihdn0'
},
},
'Bangalore': {
'MODseeed': {
'id': 'ci53tm6va0001032pzez19tgz'
},
'openbangalore': {
'id': 'ci4v3czb7000502s7lnln7ztr'
},
'WRI': {
'id': 'ci4yfgz37000e03zzg1a6o6vy'
},
'Nextdrop': {
'id': 'ci59onqz2000103z5f90u4k78'
},
'DataBrigade': {
'id': 'ci4yae86r000903zzsna9zh30'
},
'Laddoo': {
'id': 'ci4xvuiqf000503zz1bah2he8'
},
'Palantir': {
'id': 'ci4xs6uhw000403zz9za2guib'
},
'Claptrap': {
'id': 'ci541fl410002032pbifg5o7a'
},
'sycsmlp1': {
'id': 'ci4zftqww0002032zeskt2yuc'
},
'Geekstorm': {
'id': 'ci4x8lumr000l02tco6291y1u'
},
},
'Boston': {
'soldering defect': {
'id': 'ci4x0rtb9000h02tcfa5qov33'
},
'Pineapple': {
'id': 'ci4ooqbyw0001021o7p4qiedw'
},
'Sproutuino': {
'id': 'ci4xird28000003zzz1soh9fj'
},
'monalisa': {
'id': 'ci4ue1845000102w7ni64j7pl'
},
'SeeedSensor': {
'id': 'ci4w1npi3000p02s7a43zws7q'
},
'badgersense': {
'id': 'ci4vzm23c000o02s76ezwdgxe'
},
'Sparkling': {
'id': 'ci4x1uh3q000j02tcnehaazvw'
},
'Euclid': {
'id': 'ci5a6lluy000303z5d02xla24'
},
'firehazard': {
'id': 'ci530o426000003v9a6uxvc2l'
},
'Home1': {
'id': 'ci4rb6392000102wddchkqctq'
},
'theperch': {
'id': 'ci4qaiat7000002wdidwagmmb'
},
'Richard': {
'id': 'ci4vv79v9000k02s7n4avp69i'
},
'Gabe Sensor': {
'id': 'ci4w3emre000002tcnpko08o3'
},
},
'Geneva': {
'Geneva16': {
'id': 'ci4lr75ve000c02ypnfy0qt8n'
},
'Geneva6': {
'id': 'ci4lr75ok000302yp9dowz3rm'
},
'Geneva7': {
'id': 'ci4lr75sm000902ypns4q30xy'
},
"Laconn'air": 'ci4lr75sf000602ypyfkxnua3',
'OpiOneKenopi': {
'id': 'ci4lr75sl000802ypo4qrcjda'
},
'Orion': {
'id': 'ci4lr75ob000102yphtjj75f5'
},
'metabolo': {
'id': 'ci4lr75o6000002yp6eolj0rm'
},
'makegeneve': {
'id': 'ci4lr75oi000202ypmtgrudhs'
},
'Geneva11': {
'id': 'ci4lr75vd000b02ypdlm6qbly'
},
'LEClife': {
'id': 'ci4lr75v6000a02ypa256zigk'
},
'Geneva14': {
'id': 'ci4lr75se000502ypjgleg6kj'
},
'Geneva15': {
'id': 'ci4lr75wi000e02yp5ek72zcr'
},
},
'Rio de Janeiro': {
'Crisp Cookie': {
'id': 'ci4vxorfp000l02s7nv4mlefu'
},
'Rio40': {
'id': 'ci4vjer3i000e02s7r2cj23gs'
},
'PadulaNiteroiRJ': {
'id': 'ci4vye225000n02s7rxjdfxa1'
},
'coppentt': {
'id': 'ci5a2vxxf000203z5tenrtp4g'
},
'BarraDaTijucaCanvas': {
'id': 'ci4q0adco000002t9qu491siy'
},
'Kennyduino': {
'id': 'ci4xkmrkk000103zzm94zdsu4'
},
'swissnexbrazil': {
'id': 'ci4oethyi000302ymejc2wc2j'
},
'OurHomeMakerSpace DataCanvas@Rio': {
'id': 'ci4sn1zfb000102wetn54eux9'
},
'BarmaCanvas': {
'id': 'ci4symwpg000402wejp5hagiv'
},
'GaveaDataCanvas': {
'id': 'ci4wsj9if000c02tcrquen3bl'
},
'valeriab': {
'id': 'ci4yf41wg000b03zz1sg9c2yb'
},
'Cavalo': {
'id': 'ci50huz75000003whywxrn4wx'
},
'Olabi': {
'id': 'ci4x6nzle000k02tckylmop2g'
},
'nico': {
'id': 'ci4q2f9cr000102t9di6kx27u'
}
},
'Shanghai': {
'james': {
'id': 'ci5c3vgzv000003v4deetujpe'
},
'penguin': {
'id': 'ci4vk908n000h02s7pqy6aud3'
},
'WGQ': {
'id': 'ci4w9izto000302tcgj9hmy9m'
},
'frogsh': {
'id': 'ci4wmzegn000702tcc6dn993o'
},
'fraserxu': {
'id': 'ci4s0caqw000002wey2s695ph'
},
'SASPudong': {
'id': 'ci4xdbcwd000o02tcoi729rb7'
},
'transistor': {
'id': 'ci4xrkkxy000203zz2qz5x2qt'
},
'JiadingSensor': {
'id': 'ci4z8a9d10001032zk4mh5qow'
},
'xinchejian': {
'id': 'ci521nmr600020347n4s107yh'
},
"Simon's All Knowing Box": 'ci525hppb00030347n3qo265c',
'TheMarms': {
'id': 'ci527ripa000403471yii8wim'
},
'chinotto': {
'id': 'ci4yd6rfl000a03zzycxw5inl'
}
},
'Singapore': {
'SECSensor': {
'id': 'ci4v5vrcu000602s7g2cur4b2'
},
'HackerspaceSGSeeed': {
'id': 'ci4y9ght2000803zzrj61j1nl'
},
'Durianz': {
'id': 'ci599v4yf000003z5b4ovspob'
},
'sg_hougang_61': {
'id': 'ci4v67rh8000902s7vcxxq6wt'
},
'The ThunderBolt': {
'id': 'ci4y4ohu3000703zzy0fxkd5n'
},
'NorthStarOne': {
'id': 'ci4vejnju000b02s70yzzonp9'
},
'Aislinnn': {
'id': 'ci5lssf4x000003x8j2karv47'
},
'ghimmohyandao': {
'id': 'ci4wyckhv000f02tcyojovuvr'
},
'mm@alexandra': {
'id': 'ci4wq5cf6000a02tci6u38edj'
},
'Gecko': {
'id': 'ci52039km00010347y1k3z1u4'
},
'swissnexSG': {
'id': 'ci4wg4xti000502tccs34dvk4'
},
'HPSG730864': {
'id': 'ci4w8gd6f000102tcnp415mz5'
},
'reversehaven': {
'id': 'ci516u6u8000203wht0ml6o9c'
},
'rpbaltazar@bukit-timah': {
'id': 'ci4v5wwl4000702s7t1u7rz4a'
},
},
}
var selectedCity = "Bangalore";
// add markers to leaflet
var all_sensor_locations = {};
for (var sensor in all_sensors) {
for (var sensorID in all_sensors[sensor]) {
var sensorURL = "http://sensor-api.localdata.com/api/v1/sources/" + all_sensors[sensor][sensorID]['id'] + "/entries?" +
"from=2015-03-05T00:00:00-0800" +
"&before=2015-03-10T00:00:00-0800";
$.ajax({url:sensorURL})
.success( function(data) {
if (data.data.length > 0) {
var location = data.data[0]['data']['location'];
var lat = location[1];
var lng = location[0];
var circle = L.circle([lat, lng], 250, {
weight: 0,
fillColor: '#5AC2E7',
fillOpacity: 0.5,
}).addTo(map);
var loc = { 'lat': lat, 'lng': lng };
var source = data.data[0]['source'];
for (var city in all_sensors){
for (var sensNAME in all_sensors[city]){
if(all_sensors[city][sensNAME].id == source){
all_sensors[city][sensNAME]['location'] = loc;
}
}
}
}
});
}
}
$('#city_list li').on('click', function(){
var chosen = $(this).text();
$('#city_selected').val(chosen);
updateData(chosen);
});
updateData = function(city) {
selectedCity = city;
// initial action
$('#loader').show();
setTimeout( function () {
$('#diagram').empty();
map.panTo(new L.LatLng(latlngs[city][0], latlngs[city][1]));
// run update
getDataForHeatmap(city);
createHex(city);
setTimeout( function () {
// final action
$('#city_name h1').text(city)
$('#loader').fadeOut();
}, 500)
}, 500)
}
getDataForHeatmap = function(city){
// date information
var now = new Date();
var year = now.getUTCFullYear(),
month = now.getUTCMonth() + 1,
day = now.getUTCDate(),
hour = now.getUTCHours(),
minute = now.getUTCMinutes(),
from_minute = (minute < 5) ? 59 + minute - 4 : minute - 5;
// clean up date figures
if (month < 10) { month = "0" + String(month)};
if (day < 10) { day = "0" + String(day)};
if (hour < 10) { hour = "0" + String(hour)};
if (minute < 10) { minute = "0" + String(minute)};
if(from_minute < 10) { from_minute = "0" + String(from_minute)};
var from_date = year + '-' + month + '-' + day + 'T' + hour + ':' + from_minute + ':00.000Z';
var before_date = year + '-' + month + '-' + day + 'T' + hour + ':' + minute + ':00.000Z';
var from = from_date,
before = before_date,
resolution = '5m',
fields = ['temperature','light','airquality_raw','sound','humidity','dust'].join(),
sensorIDs = [];
for (var sensor in all_sensors[city]) {
sensorIDs.push(all_sensors[city][sensor]["id"]);
};
sensorIDsString = sensorIDs.join();
url = 'http://sensor-api.localdata.com/api/v1/aggregations?each.sources=' +sensorIDsString + '&from=' + from + '&before=' + before + '&resolution=' + resolution + '&fields=' + fields + '&op=mean';
$.ajax({url: url})
.success( function(data) {
var dataArray = data;
for (var i = 0; i < dataArray.data.length; i++) {
var each = dataArray.data[i];
var source = each.source;
for (var sensor in all_sensors[city]) {
if(all_sensors[city][sensor]["id"] == source){
all_sensors[city][sensor]['temperature'] = each.temperature;
all_sensors[city][sensor]['light'] = each.light;
all_sensors[city][sensor]['airquality_raw'] = each.airquality_raw;
all_sensors[city][sensor]['sound'] = each.sound;
all_sensors[city][sensor]['humidity'] = each.humidity;
all_sensors[city][sensor]['dust'] = each.dust;
}
};
}
});
}
createHex = function(city) {
// date information
var now = new Date();
var year = now.getUTCFullYear(),
month = now.getUTCMonth() + 1,
from_day = now.getUTCDate() - 1,
day = now.getUTCDate(),
hour = now.getUTCHours(),
minute = now.getUTCMinutes(),
milliseconds = now.getUTCMilliseconds();
// clean up date figures
if (month < 10) { month = "0" + String(month)};
if (from_day < 10) { from_day = "0" + String(from_day)};
if (day < 10) { day = "0" + String(day)};
if (hour < 10) { hour = "0" + String(hour)};
if (minute < 10) { minute = "0" + String(minute)};
if (milliseconds < 1000) { milliseconds = "0" + String(milliseconds)};
var from_date = year + '-' + month + '-' + from_day + 'T' + hour + ':00:00.000Z';
var before_date = year + '-' + month + '-' + day + 'T' + hour + ':00:00.000Z';
var from = from_date,
before = before_date,
resolution = '1h',
fields = ['temperature','light','airquality_raw','sound','humidity','dust'].join(),
url = 'http://sensor-api.localdata.com/api/v1/aggregations?op=mean&over.city=' + city + '&from=' + from + '&before=' + before + '&resolution=' + resolution + '&fields=' + fields;
$.ajax({url: url})
.success( function(data) {
var dataArray = data;
var svgContainer = d3.select("#diagram") //create container
.append("svg")
.attr("id", "hex")
.attr("width", $(window).width())
.attr("height", $(window).height());
var ratios = {
temp: { val: 1, arr: [], },
light: { val: 1, arr: [], },
air: { val: 1, arr: [], },
sound: { val: 1, arr: [], },
humid: { val: 1, arr: [], },
dust: { val: 1, arr: [], },
};
for (var i = 0; i < dataArray.data.length; i++) {
var each = dataArray.data[i];
ratios.temp.arr.push(each.temperature);
ratios.light.arr.push(each.light);
ratios.air.arr.push(each.airquality_raw);
ratios.sound.arr.push(each.sound);
ratios.humid.arr.push(each.humidity);
ratios.dust.arr.push(each.dust);
}
for (attr in ratios) {
var diff = Math.max(ratios[attr].arr) - Math.min(ratios[attr].arr);
var sum = ratios[attr].arr.reduce(function(a, b) { return a + b; });
ratios[attr].val = 100/(sum / ratios[attr].arr.length);
};
for (var i = dataArray.data.length-1; i >= 0 ; i--) {
var each = dataArray.data[i];
var temp = Math.min(200, each.temperature *ratios.temp.val + 50),
light = Math.min(200, each.light *ratios.light.val + 50),
air = Math.min(200, each.airquality_raw *ratios.air.val + 50),
sound = Math.min(200, each.sound *ratios.sound.val + 50),
humid = Math.min(200, each.humidity *ratios.humid.val + 50),
dust = Math.min(200, each.dust *ratios.dust.val + 50);
var _s32 = (Math.sqrt(3)/2);
var xDiff = $(window).width()/4;
var yDiff = $(window).height()/2;
var pointData = [
// X VAR // Y VAR
[temp+xDiff, 0+yDiff], // temperature, right
[(air/2)+xDiff, air*_s32+yDiff], // air quality
[-(sound/2)+xDiff, sound*_s32+yDiff], // sound
[-humid+xDiff, 0+yDiff], // humidity
[-(dust/2)+xDiff, -dust*_s32+yDiff], // dust
[(light/2)+xDiff, -light*_s32+yDiff], // sound
[temp+xDiff, 0+yDiff],
];
var enterElement = svgContainer.selectAll("path.area") //draw elements
.data([pointData]).enter().append("path")
.style("fill", "#FFFFFF")
.style("fill-opacity", 0.05)
.style("stroke", "#FFFFFF")
.style("stroke-width", "0.15px")
.attr("d", d3.svg.line())
.attr("class", "hex_layers");
if (i < 1) {
enterElement.style("stroke", "#FFFFFF")
enterElement.style("stroke-width", "2px")
var names = ['temperature','airquality_raw','sound','humidity','dust','light','temperature'];
// now add circles for each edge
for (var corner = 0; corner < pointData.length; corner++) {
var corner_name = names[corner];
var endpoint_x, endpoint_y;
var x_ratio = (pointData[corner][0]-xDiff)/(pointData[corner][1]-yDiff);
var y_ratio = (pointData[corner][1]-yDiff)/(pointData[corner][0]-xDiff);
if (pointData[corner][1]-yDiff == 0) {
x_ratio = 1;
y_ratio = 0;
}
if (pointData[corner][0] >= xDiff && pointData[corner][1] >= yDiff) {
endpoint_x = pointData[corner][0]+(x_ratio*35);
endpoint_y = pointData[corner][1]+(y_ratio*25);
}
if (pointData[corner][0] <= xDiff && pointData[corner][1] <= yDiff) {
endpoint_x = pointData[corner][0]-(x_ratio*35);
endpoint_y = pointData[corner][1]-(y_ratio*25);
}
if (pointData[corner][0] > xDiff && pointData[corner][1] < yDiff) {
endpoint_x = pointData[corner][0]-(x_ratio*35);
endpoint_y = pointData[corner][1]+(y_ratio*25);
}
if (pointData[corner][0] < xDiff && pointData[corner][1] > yDiff) {
endpoint_x = pointData[corner][0]+(x_ratio*35);
endpoint_y = pointData[corner][1]-(y_ratio*25);
}
var branch = [
[ pointData[corner][0], pointData[corner][1] ],
[ endpoint_x, endpoint_y ]
];
svgContainer.selectAll("path.area")
.data([branch]).enter().append("path")
.style("fill", "#FFFFFF")
.style("fill-opacity", 0.05)
.style("stroke", "#FFFFFF")
.style("stroke-width", "0px")
.attr("d", d3.svg.line())
.attr("class", "hex_arrows");
svgContainer.selectAll("path.area")
.data([[]]).enter().append("text")
.attr("id", corner_name + "text")
.attr("x", endpoint_x )
.attr("y", endpoint_y )
.attr("class", "hex_arrow_labels")
.style("text-anchor", "middle")
.style("fill", "#fff")
.text( function () { return corner_name });
svgContainer.selectAll("path.area")
.data([[]]).enter().append("circle")
.attr("id", corner_name)
.attr("r", 5 )
.attr("cx", pointData[corner][0] )
.attr("cy", pointData[corner][1] )
.style("fill", "#5AC2E7")
.style("fill-opacity", 0.5)
.style("stroke", "#FFFFFF")
.style("stroke-width", "2px")
.on({
mouseenter: function() {
toggleInfo(this, each)
},
mouseleave: function() {
toggleInfo()
}
});
}
// Add the SVG Text Element to the svgContainer
svgContainer.selectAll("path.area")
.data([[]]).enter().append("text")
.attr("id", "title")
.attr("x", $(window).width()/4)
.attr("y", $(window).height()/2 + 20)
.attr("text-anchor", "middle")
.text( Math.round(each.temperature) )
.attr("font-family", "sans-serif")
.attr("font-size", "100px")
.attr("fill", "white")
svgContainer.selectAll("path.area")
.data([[]]).enter().append("text")
.attr("id", "subtitle")
.attr("x", $(window).width()/4)
.attr("y", $(window).height()/2 + 50)
.attr("text-anchor", "middle")
.text( "temperature" )
.attr("font-family", "sans-serif")
.attr("font-size", "20px")
.attr("fill", "white")
original_temp = Math.round(each.temperature);
original_desc = "temperature";
}
};
// Get the weather
var weather_url = "http://api.openweathermap.org/data/2.5/weather?q=" + city
$.ajax({ url: weather_url, dataType: 'jsonp', })
.success( function(data) {
// set globals
original_temp = Math.round( data.main.temp - 273.15 );
original_desc = data.weather[0].description;
})
});
}
toggleHeatMap = function(dataId){
if(dataId){
var dataPoints = [];
for (var sensor in all_sensors[selectedCity]){
if(all_sensors[selectedCity][sensor].hasOwnProperty("location")){
var lat = all_sensors[selectedCity][sensor]["location"]["lat"];
var lng = all_sensors[selectedCity][sensor]["location"]["lng"];
if(all_sensors[selectedCity][sensor].hasOwnProperty(dataId)){
var data = all_sensors[selectedCity][sensor][dataId];
var each = [];
each.push(lat);
each.push(lng);
each.push(data);
dataPoints.push(each);
}
}
}
heat = L.heatLayer(dataPoints,{
radius: 100,
blur: 20,
}).addTo(map);
} else {
map.removeLayer(heat);
}
}
toggleInfo = function(elem, data) {
if (elem) {
$('#title')[0].innerHTML = Math.round(data[elem.id]);
$('#subtitle')[0].innerHTML = elem.id;
toggleHeatMap(elem.id);
}
else {
$('#title')[0].innerHTML = original_temp;
$('#subtitle')[0].innerHTML = original_desc;
toggleHeatMap();
}
}
// Preloading heatmap data for faster firs rendering.
getDataForHeatmap(selectedCity);
createHex(selectedCity);
</script>
</body>
</html>