2
|
1 |
PATH_MODE = false; |
|
2 |
|
1
|
3 |
$(function() { |
|
4 |
|
2
|
5 |
var colorset_pastel = ["#FBB4AE", "#B3CDE3", "#CCEBC5", "#DECBE4", "#FED9A6", "#E5D8BD", "#FDDAEC"]; |
|
6 |
var colorset_vivid = ["#E41A1C", "#377EB8", "#4DAF4A", "#984EA3", "#FF7F00", "#A65628", "#F781BF"]; |
|
7 |
|
|
8 |
$(".topics-block").draggable({axis:"y"}) |
1
|
9 |
|
|
10 |
$.getJSON("fakedata/data.json", function(data) { |
2
|
11 |
|
|
12 |
function secsToString(seconds) { |
|
13 |
var hours = Math.floor(seconds/3600), |
|
14 |
minutes = Math.floor(seconds/60) % 60, |
|
15 |
secs = Math.floor(seconds % 60); |
|
16 |
function pad(n) { |
|
17 |
var r = n.toString(); |
|
18 |
while (r.length < 2) { |
|
19 |
r = "0" + r; |
|
20 |
} |
|
21 |
return r; |
|
22 |
} |
|
23 |
return (hours ? (hours + ":") : "") + pad(minutes) + ":" + pad(secs); |
|
24 |
} |
|
25 |
|
|
26 |
$(".duration").text(secsToString(data.duration)); |
|
27 |
|
|
28 |
$(".topwords-block").on("click", "li", function() { |
|
29 |
var el = $(this).toggleClass("selected"); |
|
30 |
wordFilter(); |
|
31 |
}); |
1
|
32 |
|
|
33 |
var nmmso = data.segments.length; |
|
34 |
|
|
35 |
data.topics.forEach(function(topic) { |
|
36 |
topic.score = 0; |
2
|
37 |
topic.weights = []; |
|
38 |
topic.scores = []; |
1
|
39 |
for (var i = 0; i < nmmso; i++) { |
2
|
40 |
topic.weights.push(0); |
|
41 |
topic.scores.push(0); |
1
|
42 |
} |
|
43 |
}); |
|
44 |
|
|
45 |
data.segments.forEach(function(mmso, i) { |
|
46 |
mmso.topics.forEach(function(t) { |
|
47 |
var score = t.weight * mmso.tweet_count; |
2
|
48 |
data.topics[t.topic].weights[i] = t.weight; |
|
49 |
data.topics[t.topic].scores[i] = score; |
1
|
50 |
data.topics[t.topic].score += score; |
|
51 |
}); |
2
|
52 |
mmso.tweet_rate = mmso.tweet_count / mmso.duration; |
1
|
53 |
}); |
|
54 |
|
2
|
55 |
var sortedTopics = data.topics.slice().sort(function(a,b) { |
1
|
56 |
return b.score - a.score; |
|
57 |
}); |
|
58 |
|
2
|
59 |
var selectedWords = []; |
|
60 |
|
|
61 |
function wordFilter() { |
|
62 |
var selectedLis = $(".topwords-block li.selected"); |
|
63 |
selectedWords = []; |
|
64 |
selectedLis.each(function() { |
|
65 |
selectedWords.push($(this).text().trim()); |
|
66 |
}); |
|
67 |
console.log(selectedWords); |
|
68 |
if (selectedWords.length) { |
|
69 |
showTopics(data.topics.filter(function(topic) { |
|
70 |
var foundWords = selectedWords.map(function() { return false }); |
|
71 |
topic.words.forEach(function(topicword) { |
|
72 |
selectedWords.forEach(function(selectedword, k) { |
|
73 |
if (!foundWords[k] && selectedword === topicword.word) { |
|
74 |
foundWords[k] = true; |
|
75 |
} |
|
76 |
}) |
|
77 |
}); |
|
78 |
return foundWords.reduce(function(mem, w) { return mem && w}, true); |
|
79 |
}).sort(function(a,b) { |
|
80 |
return b.score - a.score; |
|
81 |
})); |
|
82 |
} else { |
|
83 |
showTopics(sortedTopics); |
|
84 |
} |
|
85 |
} |
1
|
86 |
|
2
|
87 |
function showTopics(topiclist) { |
|
88 |
var topicHtmls = ["", "", ""], |
|
89 |
globwords = {}, |
|
90 |
topwords = []; |
|
91 |
topiclist.forEach(function(topic,i) { |
|
92 |
var wordsToShow = topic.words.slice(0,5), |
|
93 |
max = wordsToShow[0].weight, |
|
94 |
min = Math.min(wordsToShow[wordsToShow.length - 1].weight, max - .01), |
|
95 |
scale = 10 / (max - min); |
|
96 |
var li = '<li class="shadow-block topic" data-topic-id="' |
|
97 |
+ topic.index |
|
98 |
+ '" data-viz-color="' |
|
99 |
+ Raphael.hsl(topic.index / data.topics.length, .8, .5) //colorset_vivid[topic.index % colorset_vivid.length] //colorset_vivid[i % colorset_vivid.length] |
|
100 |
+ '"" data-li-color="' |
|
101 |
+ Raphael.hsl(topic.index / data.topics.length, .7, .8) //colorset_pastel[topic.index % colorset_pastel.length] //colorset_pastel[i % colorset_pastel.length] |
|
102 |
+ '"><ul class="topic-words">' |
|
103 |
+ wordsToShow.reduce(function(memwords, word) { |
|
104 |
return memwords |
|
105 |
+ '<li style="font-size: ' |
|
106 |
+ ( 10 + scale * (word.weight - min) ) |
|
107 |
+ 'px;">' |
|
108 |
+ word.word |
|
109 |
+ '</li>'; |
|
110 |
},"") |
|
111 |
+ '</ul></li>'; |
|
112 |
topicHtmls[i % 3] += li; |
|
113 |
topic.words.forEach(function(word) { |
|
114 |
globwords[word.word] = word.weight + (globwords[word.word] || 0) |
|
115 |
}); |
|
116 |
}); |
|
117 |
var tb = $(".topics-block"); |
|
118 |
tb.html(topicHtmls.reduce(function(mem,html) { |
|
119 |
return mem + '<ul class="topic-column">' + html + '</ul>' |
|
120 |
},"")); |
|
121 |
tb.css("top",0); |
|
122 |
|
|
123 |
for (var w in globwords) { |
|
124 |
topwords.push({ |
|
125 |
word : w, |
|
126 |
weight : globwords[w] |
|
127 |
}); |
|
128 |
} |
|
129 |
topwords.sort(function(a, b) { |
|
130 |
return b.weight - a.weight; |
|
131 |
}); |
|
132 |
|
|
133 |
var wordsToShow = topwords.slice(0,30), |
|
134 |
max = wordsToShow[0].weight, |
|
135 |
min = Math.min(wordsToShow[wordsToShow.length - 1].weight, max - .01), |
|
136 |
scale = 10 / (max - min); |
|
137 |
|
|
138 |
$(".topwords-block").html(wordsToShow.reduce(function(mem, d) { |
|
139 |
return mem |
|
140 |
+ '<li style="font-size: ' |
|
141 |
+ ( 10 + scale * (d.weight - min) ) |
|
142 |
+ 'px;"' |
|
143 |
+ (selectedWords.indexOf(d.word) !== -1 ? ' class="selected"' : '') |
|
144 |
+ '>' |
|
145 |
+ d.word |
|
146 |
+ '</li>' |
|
147 |
},"")); |
|
148 |
|
|
149 |
showTopicViz(); |
|
150 |
} |
1
|
151 |
|
2
|
152 |
function showTopicViz() { |
|
153 |
var selectedBlocks = $(".topic.selected, .topic.hover"), |
|
154 |
sbl = selectedBlocks.length; |
|
155 |
if (!sbl) { |
|
156 |
selectedBlocks = $(".topic"); |
|
157 |
sbl = selectedBlocks.length; |
|
158 |
} |
|
159 |
if (sbl) { |
|
160 |
$(".topic").css("background",""); |
|
161 |
selectedBlocks.each(function() { |
|
162 |
var el = $(this); |
|
163 |
el.css("background",el.attr("data-li-color")); |
|
164 |
}); |
|
165 |
} else { |
|
166 |
$(".topic").each(function() { |
|
167 |
var el = $(this); |
|
168 |
el.css("background",el.attr("data-li-color")); |
|
169 |
}); |
|
170 |
} |
|
171 |
var topicsAndColors = selectedBlocks.map(function() { |
|
172 |
var el = $(this), |
|
173 |
topicid = parseInt(el.attr("data-topic-id")), |
|
174 |
vizcolor = el.attr("data-viz-color"); |
|
175 |
return { |
|
176 |
topic: data.topics[topicid], |
|
177 |
id: topicid, |
|
178 |
color: vizcolor |
|
179 |
}; |
|
180 |
}); |
|
181 |
if (PATH_MODE) { |
|
182 |
topicpaths.forEach(function(p) { |
|
183 |
p.hide(); |
|
184 |
}); |
|
185 |
topicsAndColors.each(function(j, t) { |
|
186 |
var p = topicpaths[t.id]; |
|
187 |
p.attr({ |
|
188 |
stroke: t.color, |
|
189 |
fill: t.color, |
|
190 |
"fill-opacity": .25 |
|
191 |
}); |
|
192 |
p.show(); |
|
193 |
}); |
|
194 |
if (sbl > 1 && sbl < data.topics.length) { |
|
195 |
var d = "M0,0L" + data.segments.map(function(mmso, i) { |
|
196 |
var tweets = 0; |
|
197 |
for (var j = 0; j < sbl; j++) { |
|
198 |
tweets += topicsAndColors[j].topic.scores[i] |
|
199 |
} |
|
200 |
x = xscale * tweets / mmso.duration; |
|
201 |
return x |
|
202 |
+ "," |
|
203 |
+ yscale * (mmso.start + mmso.duration / 3) |
|
204 |
+ "L" |
|
205 |
+ x |
|
206 |
+ "," |
|
207 |
+ yscale * (mmso.start + 2 * mmso.duration / 3); |
|
208 |
}).join("L") + "L0,"+(yscale * data.duration); |
3
|
209 |
sumpatha.attr({ path: d }); |
|
210 |
sumpathb.attr({ path: d }); |
|
211 |
sumpatha.show(); |
|
212 |
sumpathb.show(); |
2
|
213 |
} else { |
3
|
214 |
sumpatha.hide(); |
|
215 |
sumpathb.hide(); |
2
|
216 |
} |
|
217 |
} else { |
|
218 |
for (var i = 0; i < nmmso; i++) { |
|
219 |
var opacity = 0, |
|
220 |
rgb = [0,0,0]; |
|
221 |
for (var j = 0; j < sbl; j++) { |
|
222 |
var c = Raphael.getRGB(topicsAndColors[j].color), |
|
223 |
o = topicsAndColors[j].topic.weights[i]; |
|
224 |
rgb[0] += c.r * o; |
|
225 |
rgb[1] += c.g * o; |
|
226 |
rgb[2] += c.b * o; |
|
227 |
opacity += o; |
|
228 |
} |
|
229 |
if (opacity) { |
|
230 |
color = Raphael.rgb.apply(Raphael, rgb.map(function(c) { |
|
231 |
return c/opacity; |
|
232 |
})); |
|
233 |
segmentrects[i].show(); |
|
234 |
segmentrects[i].attr({ |
|
235 |
fill: color, |
|
236 |
opacity: .2 + .8 * opacity |
|
237 |
}); |
|
238 |
} else { |
|
239 |
segmentrects[i].hide(); |
|
240 |
} |
1
|
241 |
} |
|
242 |
} |
2
|
243 |
|
|
244 |
} |
|
245 |
|
|
246 |
var jqsvg = $(".start-svg"); |
|
247 |
paper = new Raphael(jqsvg[0]), |
|
248 |
ph = jqsvg.height(), |
|
249 |
pw = jqsvg.width(), |
|
250 |
yscale = ph / data.duration, |
|
251 |
mx = Math.max.apply(Math, data.segments.map(function(s) { return s.tweet_rate})), |
|
252 |
xscale = (pw - 10)/mx; |
|
253 |
|
|
254 |
if (PATH_MODE) { |
3
|
255 |
var sumpatha = paper.path(); |
|
256 |
sumpatha.attr({ |
2
|
257 |
fill: "#f0f0f0", |
3
|
258 |
stroke: "none" |
2
|
259 |
}); |
|
260 |
var topicpaths = data.topics.map(function(topic) { |
|
261 |
var d = "M0,0L" + topic.scores.map(function(s, i) { |
|
262 |
var mmso = data.segments[i], |
|
263 |
x = xscale * s / mmso.duration; |
|
264 |
return x |
|
265 |
+ "," |
|
266 |
+ yscale * (mmso.start + mmso.duration / 3) |
|
267 |
+ "L" |
|
268 |
+ x |
|
269 |
+ "," |
|
270 |
+ yscale * (mmso.start + 2 * mmso.duration / 3); |
|
271 |
}).join("L") + "L0,"+(yscale * data.duration); |
|
272 |
return paper.path(d); |
|
273 |
}); |
3
|
274 |
var sumpathb = paper.path(); |
|
275 |
sumpathb.attr({ |
|
276 |
stroke: "#666666", |
|
277 |
"stroke-width": 2 |
|
278 |
}); |
2
|
279 |
} else { |
|
280 |
var segmentrects = data.segments.map(function(mmso) { |
|
281 |
var rect = paper.rect(0, yscale * mmso.start, pw - 50, yscale * mmso.duration); |
|
282 |
rect.attr({stroke: "none"}); |
|
283 |
return rect; |
|
284 |
}); |
|
285 |
} |
|
286 |
|
|
287 |
var d = "M" + data.segments.map(function(s) { |
|
288 |
var x = xscale * s.tweet_rate; |
|
289 |
return x |
|
290 |
+ "," |
|
291 |
+ yscale * (s.start + s.duration / 3) |
|
292 |
+ "L" |
|
293 |
+ x |
|
294 |
+ "," |
|
295 |
+ yscale * (s.start + 2 * s.duration / 3); |
|
296 |
}).join("L"); |
|
297 |
|
|
298 |
paper.path(d); |
|
299 |
|
|
300 |
|
|
301 |
for (var i=0; i < data.duration; i += 1800) { |
|
302 |
var y = yscale * i; |
|
303 |
paper.path("M0," + y + "L" + pw + "," + y).attr({ |
|
304 |
stroke: "#666" |
|
305 |
}); |
|
306 |
paper.text(pw - 2, y + 6, secsToString(i)).attr({ |
|
307 |
"text-anchor": "end" |
|
308 |
}); |
|
309 |
} |
|
310 |
paper.text(pw-2, ph-6, secsToString(data.duration)).attr({ |
|
311 |
"text-anchor": "end" |
|
312 |
}); |
|
313 |
|
|
314 |
wordFilter(); |
|
315 |
|
|
316 |
$(".topics-block").on("mouseenter", ".topic", function() { |
|
317 |
var topicid = parseInt($(this).attr("data-topic-id")), |
|
318 |
color = $(this).attr("data-viz-color"), |
|
319 |
topic = data.topics[topicid]; |
|
320 |
$(this).addClass("hover"); |
|
321 |
showTopicViz(); |
|
322 |
}).on("mouseleave", ".topic", function() { |
|
323 |
$(this).removeClass("hover"); |
|
324 |
showTopicViz(); |
|
325 |
}).on("click", ".topic", function() { |
|
326 |
$(this).toggleClass("selected"); |
|
327 |
showTopicViz(); |
|
328 |
}); |
|
329 |
|
1
|
330 |
|
|
331 |
}); |
|
332 |
}); |