-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2.html
287 lines (221 loc) · 7.05 KB
/
index2.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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
background-color: #eee;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
#leftdiv {
float: left;
width: 50%;
}
#rightDiv {
float: right;
width: 50%;
}
#bottomDiv {
float: left;
width: 100%;
}
.thumbimg {
max-height: 200px;
max-width: 200px;
}
</style>
<head>
<link rel="stylesheet" type="text/css" href="css/collage.css">
</head>
<body>
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="http://d3js.org/d3.v3.js" charset="utf-8"></script>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="d3-cloud/d3.layout.cloud.js"></script>
<div id="leftdiv" class="a"></div>
<div id="rightDiv" class="b"></div>
<div id="bottomDiv" class="collage"></div>
<script>
(function() {
var countTotal = 1;
var n = 243,
duration = 750,
now = new Date(Date.now() - duration), //is set to 0.75 seconds behind now
count = 0,
data = d3.range(n).map(function() { return 0; }); // Create an empty data set e.g. 0,0,0,0, etc
var margin = {top: 6, right: 0, bottom: 20, left: 50},
width = 600 - margin.right,
height = 500 - margin.top - margin.bottom;
// X axis
var x = d3.time.scale()
.domain([now - (n - 2) * duration, now - duration]) // current time - .75 s - (486) * 750, current time
.range([0, width]);
// Y axis
var y = d3.scale.linear()
.range([height, 0]);
var line = d3.svg.line()
.interpolate("basis")
.x(function(d, i) { return x(now - (n - 1 - i) * duration); })
.y(function(d, i) { return y(d); });
var svg = d3.select("div.a").append("p").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("margin-left", -margin.left + "px")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
svg.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
// X Axis
var axis = svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(x.axis = d3.svg.axis().scale(x).orient("bottom"));
// Y Axis
var yaxsis = svg.append("g")
.attr("class", "y axis")
.call(d3.svg.axis().scale(y).ticks(10).orient("left"));
// Line
var path = svg.append("g")
.attr("clip-path", "url(#clip)")
.append("path")
.data([data])
.attr("class", "line");
// Y axis label
yaxsis.append("text")
.attr("transform", "rotate(-90),translate(-250,-40)")
.text("Interactions Per Second");
this.tick = (function() {
// update the domains
now = new Date();
// your data minimum and maximum e.g. domain([0, 20])
// now - 0.75 seconds - (245) * 750
x.domain([now - (n - 2) * duration, now - duration]);
y.domain([0, d3.max(data)]);
// push the accumulated count onto the back, and reset the count
data.push(Math.min(100, count));
count = 0;
// redraw the line
svg.select(".line")
.attr("d", line)
.attr("transform", null);
// slide the x-axis left
axis.transition()
.duration(duration)
.ease("linear")
.call(x.axis);
// slide the line left
path.transition()
.duration(duration)
.ease("linear")
.attr("transform", "translate(" + x(now - (n - 1) * duration) + ")")
.each("end", tick);
// Y Axis
yaxsis.transition()
.attr("class", "y axis")
.ease("linear")
.call(d3.svg.axis().scale(y).ticks(10).orient("left"));
// pop the old data point off the front
data.shift();
})
this.tick();
// words code
var words = {};
var fill = d3.scale.category20();
//what range of font sizes do we want, we will scale the word counts
var fontSize = d3.scale.log().range([50, 200]);
//create my cloud object
var mycloud = d3.layout.cloud().size([600, 500])
.words([])
.padding(2)
.rotate(function() { return ~~(Math.random() * 2) * 0; })
// .rotate(function() { return 0; })
.font("Impact")
.fontSize(function(d) { return fontSize(d.size); })
.on("end", draw);
//render the cloud with animations
function draw(words) {
//fade existing tag cloud out
d3.select("div.b").selectAll("svg").selectAll("g")
.transition()
.duration(1000)
.style("opacity", 1e-6)
.remove();
//render new tag cloud
d3.select("div.b").selectAll("svg")
.append("g")
.attr("transform", "translate(300,250)")
.selectAll("text")
.data(words)
.enter().append("text")
.style("font-size", function(d) { return ((d.size)* 1) + "px"; })
.style("font-family", "Impact")
.style("fill", function(d, i) { return fill(i); })
.style("opacity", 1e-6)
.attr("text-anchor", "middle")
.attr("transform", function(d) { return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")"; })
.transition()
.duration(1000)
.style("opacity", 1)
.text(function(d) { return d.text; });
}
function get_words() {
var words_array = [];
for (key in words){
words_array.push({text: key, size: words[key]})
}
if(Object.keys(words).length >10){
words = {};
}
//render cloud
mycloud.stop().words(words_array).start();
};
//create SVG container
d3.select("div.b").append("svg")
.attr("width", 600)
.attr("height", 500);
//render first cloud
get_words();
//start streaming
var interval = setInterval(function(){get_words()}, 2000);
//var socket = io.connect('http://localhost:9001');
var socket = io.connect('http://sociamvm-app-001.ecs.soton.ac.uk:9001');
socket.on('news', function (data) {
// update graph data
console.log(data)
++count;
// update words
if(data.wikipedia_page_name != undefined){
console.log(data.wikipedia_page_name);
words[data.wikipedia_page_name]= 1;
}
// images
if (data.image_url && data.image_url != "") {
var div = $("#bottomDiv");
var img = new Image(data.image_url);
img.onload = function () {
div.append($("<a href='#'> <img class='thumbimg' src='"+data.image_url+"'> </a>"));
if (div.children().length > 12) {
div.children()[0].remove();
}
};
img.src = data.image_url;
}
});
})();
</script>
</body>