forked from urbanlaunchpad/dataCanvas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract.html
209 lines (182 loc) · 8.45 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
<!DOCTYPE html>
<html>
<head>
<title>Example 02.05 - Custom geometry</title>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<style>
html, body{
background-color: #5AC2E7;
height: 100%;
width: 100%;
overflow: hidden;
font-family: 'Open Sans', sans-serif;
font-size: 30px;
}
#all {
height: 100vh;
}
#info {
height: 100%;
padding: 25px;
color: #5AC2E7;
background-color: #FFF;
}
</style>
</head>
<body>
<div class="row" id="all">
<div class="col-sm-6" id="diagram">
</div>
<div class="col-sm-6" id="info">
<h1>Explore a different city.</h1>
<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">
<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="...">
</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>
<!-- Javascript code that runs our Three.js examples -->
<script type="text/javascript">
var allCities = ['Bangalore', 'Boston', 'Geneva', 'Rio de Janeiro', 'San Francisco', 'Shanghai', 'Singapore'];
createHex = function(city) {
// date information
var now = new Date();
var year = now.getFullYear(),
month = now.getMonth() + 1,
from_day = now.getDate() - 1,
day = now.getDate(),
hour = now.getHours(),
minute = now.getMinutes(),
milliseconds = now.getMilliseconds();
// 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-0800';
var before_date = year + '-' + month + '-' + day + 'T' + hour + ':00:00-0800'; console.log(before_date, '2015-03-04T00:00:00-0800');
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 = 150/(sum / ratios[attr].arr.length);
console.log(150/(sum / ratios[attr].arr.length));
};
for (var i = 0; i < dataArray.data.length; i++) {
var each = dataArray.data[i];
var temp = Math.min(200, each.temperature *ratios.temp.val),
light = Math.min(200, each.light *ratios.light.val),
air = Math.min(200, each.airquality_raw *ratios.air.val),
sound = Math.min(200, each.sound *ratios.sound.val),
humid = Math.min(200, each.humidity *ratios.humid.val),
dust = Math.min(200, each.dust *ratios.dust.val);
ratios.temp.arr.push(temp);
ratios.light.arr.push(light);
ratios.air.arr.push(air);
ratios.sound.arr.push(sound);
ratios.humid.arr.push(humid);
ratios.dust.arr.push(dust);
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());
if (i < 1) {
enterElement.style("stroke", "#FFFFFF")
.style("stroke-width", "2px")
}
};
// Get the weather
var weather_url = "http://api.openweathermap.org/data/2.5/weather?q=" + city
$.ajax({url: weather_url})
.success( function(data) { console.log(data);
// Add the SVG Text Element to the svgContainer
svgContainer.selectAll("path.area")
.data([[]]).enter().append("text")
.attr("x", $(window).width()/4)
.attr("y", $(window).height()/2 + 20)
.attr("text-anchor", "middle")
.text( parseInt(data.weather[0].icon, 10) )
.attr("font-family", "sans-serif")
.attr("font-size", "100px")
.attr("fill", "white")
svgContainer.selectAll("path.area")
.data([[]]).enter().append("text")
.attr("x", $(window).width()/4)
.attr("y", $(window).height()/2 + 50)
.attr("text-anchor", "middle")
.text( data.weather[0].description )
.attr("font-family", "sans-serif")
.attr("font-size", "20px")
.attr("fill", "white")
})
});
}
createHex('Bangalore');
</script>
</body>
</html>