Repository navigation
Expand file tree
/
Copy pathviz3.js
More file actions
249 lines (204 loc) · 7.49 KB
/
Copy pathviz3.js
File metadata and controls
249 lines (204 loc) · 7.49 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
function set_values(timelineData,dateParsed){
var mapWidth = window.innerWidth * 0.5
var mapHeight = window.innerHeight * 0.8
var tweetLeftMargin = (mapWidth + 80) + 'px'
var tweetTopMargin = (-(window.innerHeight)+33)+'px'
d3.select("#tweet")
.style("margin-left",tweetLeftMargin)
.style("margin-top",tweetTopMargin)
d3.select('#map')
.attr('width',mapWidth)
.attr('height',mapHeight)
// Create timeline
//console.log(["create_timeline",create_timeline]);
create_timeline(timelineData,mapWidth,dateParsed);
}
function addPoints(x,map){
if (x.value.hasOwnProperty("tweets")){
x.value.tweets.features.forEach(function(d,i){
var circle = L.circle([d.geometry.coordinate[1],d.geometry.coordinate[0]], 500, {
color: 'red',
fillColor: '#f03',
fillOpacity: 0.2,
className: "tweet_location_pre_data"
}).addTo(map);
var circleData = d3.selectAll(".tweet_location_pre_data")
.classed("tweet_location_pre_data",false)
.classed("tweet_location",true)
.attr("id","tweetID_"+d.properties.tweet.split('/')[5])
.attr("media",d.properties.link)
.attr("tweet",d.properties.tweet)
.attr("user",d.properties.user_id);
})
};
}
function build_map(){
/*var mapboxTiles = L.tileLayer('https://{s}.tiles.mapbox.com/v3/examples.map-zr0njcqy/{z}/{x}/{y}.png', {
attribution: '<a href="http://www.mapbox.com/about/maps/" target="_blank">Terms & Feedback</a>'
});
*/
//var map = L.map('map')
//.addLayer(mapboxTiles)
//.setView([40.0274,-105.2519], 10);
var map = L.map('map').setView([40.0274,-105.2519], 10);
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};
var baseLayers = {"stamen": stamen, "toolserver-mapnik":toolserver};
L.control.layers(baseLayers).addTo(map);
return map;
}
function create_timeline_data(dataArray){
var data = {};
dataArray.forEach(function(d,i){
// d.key = date
// d.value.tweets.featurs = ARRAY
// ARRAY.properties = {body,link,tweet,user_id}
if (d.value.hasOwnProperty("tweets")){
data[d.key] = d.value.tweets.features.length
}else{
data[d.key] = 0
}
})
return d3.entries(data)
}
function create_timeline(data,mapWidth,dateParsed){
// remove old timeline
d3.select("#timeline").selectAll("*").remove();
var windowHeight = window.innerHeight
var margin = {top: windowHeight*0.1, right: 0, bottom: windowHeight*0.1, left: 50},
width = mapWidth,
height = windowHeight*0.3 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y-%m-%d %H:%M:%S").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.ticks(Math.max(width/200,2))
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.ticks(Math.max(height/50,2))
.orient("left");
var line = d3.svg.line()
.x(function(d) { return x(d.key); })
.y(function(d) { return y(d.value); });
var svg = d3.select("#timeline")
.classed("timeline",true)
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
data.forEach(function(d) {
if (!dateParsed){
d.key = parseDate(d.key);
}
d.value = +d.value;
});
x.domain(d3.extent(data, function(d) { return d.key; }));
y.domain(d3.extent(data, function(d) { return d.value; }));
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
if (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");
};
svg.append("path")
.datum(data)
.attr(&qu
8000
ot;class", "line")
.attr("d", line);
}
function add_fore_directed(data){
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom,
padding = 1, // separation between nodes
radius = 6;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var svg = d3.select("body").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 + ")");
var controls = d3.select("body").append("label")
.attr("id", "controls");
var checkbox = controls.append("input")
.attr("id", "collisiondetection")
.attr("type", "checkbox");
controls.append("span")
.text("Collision detection");
var force = d3.layout.force()
.nodes(data)
.size([width, height])
.on("tick", tick)
.charge(-1)
.gravity(0)
.chargeDistance(20);
}
d3.json("data/BoulderFlood_viewer.json", function(collection) {
console.log(["collection:",collection])
// Enventuall: render photos
/* d3.json("http://api.instagram.com/oembed?url=http://instagr.am/p/fA9uwTtkSN/",function(error, json) {
if (error) return console.warn(error);
data = json;
console.log(data)
});
*/
// build map and creat an svg.
var map = build_map();
// Transform object to array
var dataArray = d3.entries(collection.time_series.interval_data);
var timelineData = create_timeline_data(dataArray)
var dateParsed = false
// Initialize map & tweet orientation.
set_values(timelineData,dateParsed);
// For each tweet, add a circle to map.
dataArray.map(function(d){addPoints(d,map)});
// Adjust map & tweet orientation when window is resized.
window.addEventListener('resize', function(event){
var dateParsed = true
set_values(timelineData,dateParsed);
});
// Mouseover event that produces embeded tweet
var circles = d3.selectAll(".tweet_location")
circles.on("mouseover",function(event){
d3.select("#tweet").selectAll("*").remove();
twttr.widgets.createTweet(
d3.select(this).attr('tweet').split('/')[5],
document.getElementById('tweet'))
});
/*
var svg = d3.select(map.getPanes().overlayPane).append("svg");
var startDate = collection.time_series.metadata.time_start;
var endDate = collection.time_series.metadata.time_end;
var interval = collection.time_series.metadata.interval_minutes;
*/
/* var tester = L.circle([40.0274,-105.2519], 500, {
color: 'yellow',
fillColor: '#f03',
fillOpacity: 0.2,
className: "practice_point"
}).addTo(map);
*/
});