Repository navigation
Expand file tree
/
Copy pathviz6.js
More file actions
363 lines (297 loc) · 11.6 KB
/
Copy pathviz6.js
File metadata and controls
363 lines (297 loc) · 11.6 KB
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
function build_map(){
// uses leaflet.js to build a zoomable map
var map = L.map('map').setView([40.0274,-105.2519],13);
var stamen = L.tileLayer('http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png', {attribution: 'dev.'}).addTo(map);
var toolserver = L.tileLayer('http://{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png');
var baseLayers = {"stamen": stamen, "toolserver-mapnik":toolserver};
L.control.layers(baseLayers).addTo(map);
return map;
}
function set_sizes(){
console.log('setting sizes')
// keep all window sizes in scope
var windowHeight = +window.innerHeight;
var windowWidth = +window.innerWidth;
// map sizes
var margin = {top: windowHeight*0.01, right: 0, bottom: windowHeight*0.1, left: windowWidth*0.10};
var sizes = {
mapWidth: (0.40 * windowWidth)
, mapHeight: (0.40 * windowHeight)
, mapLeft: d3.min([(0.02 * windowWidth),20])
, mapTop: d3.min([(0.05 * windowHeight),30])
, timelineWidth: 0.36 * windowWidth
, timelineHeight: 0.20 * windowHeight
, timelineMarginLeft: (windowWidth * 0.02)+35
, photoHeight: 0.40 * windowHeight
, windowHeight: windowHeight
, windowWidth: windowWidth
}
sizes["timelineMarginTop"] = (sizes.mapHeight)+ d3.min([(sizes.windowHeight * 0.10),60]);
sizes["tweetMarginLeft"] = ( sizes.mapWidth + sizes.mapLeft + d3.min([sizes.windowWidth*0.1,20]));
sizes["photoMarginLeft"] = sizes.tweetMarginLeft;
sizes["tweetTop"] = sizes.mapTop;
// ISSUE: need to determine how to get the highe of the tweet
if (d3.select("#tweet iframe")[0][0] == null){
console.log("tweetWidgetHeight: 0")
var tweetWidgetHeight = 0;
}else{
console.log(+d3.select("#tweet iframe")[0][0].height)
var tweetWidgetHeight = +d3.select("#tweet iframe")[0][0].height;
}
// ISSUE: remove the 150 and replace with `tweetWidgetHeight`
sizes["photoTop"] = sizes.tweetTop + 155 + d3.min([sizes.windowWidth*0.1,20]);
// adjust map
d3.select('#map')
.style('left',sizes.mapLeft + 'px')
.style('top',sizes.mapTop + 'px')
.style('width',sizes.mapWidth + 'px')
.style('height',sizes.mapHeight + 'px');
// adjust timeline svg
d3.select(".timeline")
.attr("width", sizes.windowWidth)
.attr("height", sizes.windowHeight)
// adjust timeline volume plot
d3.select('.volume')
.style('height',sizes.timelineHeight)
.attr("transform", "translate(" + (sizes.timelineMarginLeft) + "," + (sizes.timelineMarginTop) + ")");
// adjust tweet
d3.select('#tweet')
.style('margin-left',sizes.tweetMarginLeft + 'px')
.style('top',sizes.tweetTop+'px');
// adjust photo
d3.select('#photo')
.style('margin-left',sizes.tweetMarginLeft + 'px')
.style('top',sizes.photoTop + 'px')
.style('border-radius','20px')
.style('height',sizes.photoHeight + 'px');
var scales = update_scales(sizes);
return sizes;
}
function update_scales(sizes){
// function to create x scale
var x = d3.time.scale()
.range([0, sizes.timelineWidth]);
// function to create y scale
var y = d3.scale.linear()
.range([sizes.timelineHeight, 0]);
// https://github.com/mbostock/d3/wiki/Time-Formatting
// function to build xAxis options
var xAxis = d3.svg.axis()
.scale(x)
.ticks(Math.max(sizes.timelineWidth/200,2))
.tickFormat(d3.time.format("%b %d"))
.orient("bottom");
// function to build yAxis options
var yAxis = d3.svg.axis()
.scale(y)
.ticks(Math.max(sizes.timelineHeight/50,2))
.orient("left");
return {x:x, y:y, xAxis:xAxis, yAxis:yAxis}
}
function create_timeline2(data, sizes){
d3.select(".volume").remove();
// get scales
var scales = update_scales(sizes);
var x = scales.x
, y = scales.y
, xAxis = scales.xAxis
, yAxis = scales.yAxis;
// function to draw a line given across coordinates (x,y).
var line = d3.svg.line()
.x(function(d) { return x(d.key); })
.y(function(d) { return y(d.value); });
// create a container inside a pre-existing "id=timeline" element.
var svg = d3.select("#timeline")
.classed("timeline",true)
.attr("width", sizes.windowWidth)
.attr("height", sizes.windowHeight)
.append("g")
.classed("volume",true)
.attr("transform", "translate(" + (sizes.timelineMarginLeft) + "," + (sizes.timelineMarginTop) + ")");
var brush = d3.svg.brush()
.x(x)
.on("brush", brushed);
var brushElement = svg.append("g")
.attr("class", "brushElement")
.call(brush)
.selectAll("rect")
.attr("y", 0)
.attr("height", sizes.timelineHeight);
// add domain to scales
x.domain(d3.extent(data, function(d) { return d.key; }));
y.domain(d3.extent(data, function(d) { return d.value; }));
// create line
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line);
// create x axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + sizes.timelineHeight+ ")")
.call(xAxis);
// create conditional y axis
if (sizes.windowHeight > 600){
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Tweets");
};
function brushed(){
//console.log(brush.extent())
d3.selectAll(".tweet_location")
.each(function(d) {
var checkDate = new Date(this.getAttribute("timeStamp"));
var lowExtent = new Date(brush.extent()[0]),
highExtent = new Date(brush.extent()[1]);
if (lowExtent <= checkDate && checkDate <= highExtent) {
d3.select(this).style({fill: "red", stroke: "red"});
} else {
d3.select(this).style({fill: "steelblue", stroke: "steelblue"});
}
})
}
}
function add_features_to_map(timeStamp,features,map){
// add features to the map
features.forEach(function(feature,i){
//var feature["timeStamp"] = timeStamp;
//var feature["timeStamp_str"] = timeStamp.toString();
// add points to map try to do this directly w/ geoJson
L.geoJson(feature).addTo(map);
});
}
function add_longlat_to_map(timeStamp,features,map,geoLayer){
// add features to the map
features.forEach(function(feature,i){
//var feature["timeStamp"] = timeStamp;
//var feature["timeStamp_str"] = timeStamp.toString();
// add points to map try to do this directly w/ geoJson
geoLayer.addData(feature);
});
}
function convert_to_array(data,map){
// to iterate, we transform the object to array
var dataArray = d3.entries(data.time_series.interval_data);
// function for parsing dates
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
// create empty layer to add points
var geoLayer = L.geoJson().addTo(map)
// parse dates and coerce values
function iterate_(dataArray){
parsedDataArray = [];
dataArray.forEach(function(ts) {
//console.log(ts.value.tweets_geo.features)
//add_features_to_map(ts.key,ts.value.tweets_geo.features,map);
//add_longlat_to_map(ts.key,ts.value.tweets_geo.features,map,geoLayer);
// create timeline data
parsedDataArray.push({
key:parseDate(ts.key)
, value:+ts.value.stats.tweets_geo_all
});
});
return parsedDataArray.sort(function(a,b){
return b.key-a.key
});
}
return iterate_(dataArray);
}
function decimal_count(num) {
return (num.split('.')[1] || []).length;
}
function add_points_to_map(data,map){
var dateArray = Object.keys(data.time_series.interval_data);
var point_counts = {};
dateArray.forEach(function(ts){
data.time_series.interval_data[ts].tweets_geo.features.forEach(function(feature){
// get geo
var longitude = feature.geometry.coordinates[0];
var latitude = feature.geometry.coordinates[1];
var tweetUrl = feature.properties.tweet_url;
var tweetID = feature.properties.tweet_url.split('/')[5];
// remove this if block once the geo is corrected
if (decimal_count(latitude.toString())>4 & decimal_count(longitude.toString())>4){
// Use leaflet to add circles to the map
L.circle([+latitude,+longitude],130,{
color: 'steelblue',
fillColor: 'steelblue',
fillOpacity: 0.2,
className: "tweet_location_pre_data"
}).addTo(map);
// add data to circles
d3.select(".tweet_location_pre_data")
.classed("tweet_location_pre_data",false)
.classed("tweet_location",true)
.classed("tweetID_"+tweetID,true)
.attr("tweet_url",tweetUrl)
.attr("tweetID",tweetID)
.attr("timeStamp",ts)
.attr("timeStampTag",ts.split(":")[0]);
if (feature.properties.hasOwnProperty("media")){
// add media to circle
d3.select(".tweetID_"+tweetID)
.attr('media',feature.properties.media)
}
}
});
})
// sort points
var sorted_keys = Object.keys(point_counts).sort(function(a,b){return point_counts[b]-point_counts[a]})
//output={};
output=[];
sorted_keys.forEach(function(name){
output.push({name:point_counts[name]})
//output[d]=point_counts[d]
})
console.log(output)
}
//d3.json("data/BoulderFlood_viewer.json", function(collection) {
d3.json("data/event_viewer.json", function(collection) {
console.log(["collection:",collection])
// all sizes
var sizes = set_sizes();
// build map and creat an svg.
var map = build_map();
// build timeline
var timelineData = convert_to_array(collection,map);
console.log(timelineData);
create_timeline2(timelineData,sizes);
// add point to map
add_points_to_map(collection,map);
// Adjust map & tweet orientation when window is resized.
window.addEventListener('resize', function(event){
//set_values(timelineData,dateParsed);
var sizes = set_sizes();
create_timeline2(timelineData,sizes);
});
// Mouseover event that produces embeded tweet
var circles = d3.selectAll(".tweet_location")
circles.on("mouseover",function(event){
// removes old embeded tweet
var element = d3.select(this)
d3.select("#tweet").selectAll("*").remove();
// adds new embeded tweet
twttr.widgets.createTweet(
element.attr('tweetID'),
document.getElementById('tweet')
);
// remove old photo
d3.select('#photo').remove();
// add new photo
if (element.attr("media") != null){
if (element.attr("media").match("instagram.com") != null){
// embeds instagram photo
d3.select("body").append('img').attr('id','photo')
.attr('src','http://instagram.com/p/' + element.attr("media").split('/')[4] +'/media/?size=l')
.attr('border','2px solid')
.attr('border-radius','25px');
}
}
var sizes = set_sizes();
});
});