| author | veltr |
| Mon, 29 Apr 2013 18:37:06 +0200 | |
| changeset 54 | faf1e584551c |
| parent 53 | 88666afffe6b |
| child 55 | c2e76eb7be88 |
| permissions | -rw-r--r-- |
| 54 | 1 |
var SECONDS = 1000, |
2 |
MINUTES = 60 * SECONDS, |
|
3 |
HOURS = 60 * MINUTES; |
|
| 30 | 4 |
|
| 54 | 5 |
var config = { |
6 |
partnerCode: 'B00015838755', |
|
7 |
originalDuration: 24 * HOURS, |
|
8 |
vizDuration: 10 * MINUTES, |
|
| 53 | 9 |
colorIds: { |
10 |
"-2.0": 2, |
|
11 |
"0.5": 0, "1.0": 0, "1.5": 0, "2.0": 0, |
|
12 |
"2.5": 1, "3.0": 1, "3.5": 1, |
|
13 |
"4.0": 2, "4.5": 2, "5.0": 2 |
|
14 |
}, |
|
| 54 | 15 |
movieCount: 10, |
16 |
opinionsPerPage: 100, |
|
17 |
colors: [ "#c00000", "#ffd000", "#008000"], |
|
18 |
refreshRate: 50 |
|
| 28 | 19 |
} |
20 |
||
| 54 | 21 |
$(function() { |
22 |
|
|
23 |
var originalStart = new Date().valueOf() - config.originalDuration, |
|
24 |
timeScale = config.vizDuration / config.originalDuration, |
|
25 |
movies, |
|
26 |
moviesToLoad, |
|
27 |
opinions = [], |
|
28 |
loadTime = new Date().valueOf(), |
|
29 |
clockInterval; |
|
30 |
|
|
31 |
function resizeTimer() { |
|
32 |
var w = Math.max(80, $(window).width() - 170), |
|
33 |
n = Math.min(12, Math.floor(w / 80)); |
|
34 |
$('.timer-bar').width(w); |
|
35 |
var html = ''; |
|
36 |
for (var k = 0; k < n + 1; k++) { |
|
37 |
html += '<li style="left:' |
|
38 |
+ Math.floor(k * w / n) |
|
39 |
+ 'px;">' |
|
40 |
+ new Date(originalStart + k * config.originalDuration / n).toLocaleTimeString().substr(0,5) |
|
41 |
+ '</li>'; |
|
42 |
} |
|
43 |
$('.hours').html(html); |
|
44 |
} |
|
45 |
|
|
46 |
resizeTimer(); |
|
47 |
$(window).resize(resizeTimer); |
|
48 |
|
|
49 |
var vsHeight = $(window).height() - $('footer').height() - $('header').height() - 300, |
|
50 |
vsWidth = config.movieCount * 160; |
|
51 |
|
|
52 |
$("#data-viz").css('height', vsHeight); |
|
53 |
$('.posters').css('width', vsWidth); |
|
54 |
|
|
55 |
function startViz() { |
|
56 |
|
|
57 |
var barChart = undefined; |
|
58 |
|
|
59 |
opinions.sort(function(a,b) { |
|
60 |
return a._timestamp > b._timestamp; |
|
61 |
}); |
|
62 |
$("#data-viz").html("").css("text-align","left"); |
|
63 |
var barChartSettings = { |
|
64 |
width : vsWidth, |
|
65 |
height : vsHeight, |
|
66 |
chart : { |
|
67 |
spacer : 5, |
|
68 |
y: 100, |
|
69 |
height: vsHeight - 100 |
|
70 |
}, |
|
71 |
data : { |
|
72 |
model : movies.map(function(movie) { |
|
73 |
return { |
|
74 |
label: movie.title |
|
75 |
}; |
|
76 |
}), |
|
77 |
strata : function() { |
|
78 |
|
|
79 |
if (typeof barChart === "undefined") { |
|
80 |
var flocculated = []; |
|
81 |
_(function() { |
|
82 |
_.range(3).map(function(colorId) { |
|
83 |
$(".layer_"+colorId).css("fill", config.colors[colorId]); |
|
84 |
}); |
|
85 |
}).defer(); |
|
86 |
} else { |
|
87 |
var flocculated = barChart.selectAll("state",2); |
|
88 |
} |
|
89 |
|
|
90 |
var res = movies.map(function(movie) { |
|
91 |
var movieTokens = flocculated.filter(function(t) { |
|
92 |
return t.attr("movie") === movie.id; |
|
93 |
}); |
|
94 |
return _.range(3).map(function(colorId) { |
|
95 |
var stratumTokens = movieTokens.filter(function(t) { |
|
96 |
return t.attr("colorId") === colorId; |
|
97 |
}).length; |
|
98 |
return { |
|
99 |
value: function() { |
|
100 |
return stratumTokens |
|
101 |
} |
|
102 |
}; |
|
103 |
}); |
|
104 |
}); |
|
105 |
|
|
106 |
return res; |
|
107 |
}, |
|
108 |
stream : { |
|
109 |
provider : "tokens", |
|
110 |
refresh: config.refreshRate, |
|
111 |
now : 0 |
|
112 |
}, |
|
113 |
tokens: opinions.map(function(opinion) { |
|
114 |
return { |
|
115 |
category: opinion._column, |
|
116 |
datetime: opinion.datetime, |
|
117 |
movie: opinion._movie, |
|
118 |
colorId: opinion._colorId, |
|
119 |
fillStyle: opinion._color, |
|
120 |
strokeStyle: "#555555", |
|
121 |
t: Math.floor((opinion._timestamp - originalStart) * timeScale / config.refreshRate), |
|
122 |
shape:{ |
|
123 |
type:'box', |
|
124 |
width:74, |
|
125 |
height:5 |
|
126 |
}, |
|
127 |
callback:{ |
|
128 |
suspension: function(token) { |
|
129 |
setTimeout(function() { |
|
130 |
token.flocculate(); |
|
131 |
barChart.strata.update(barChart); |
|
132 |
}, 5000); |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
}) |
|
137 |
}, |
|
138 |
sedimentation: { |
|
139 |
token: { |
|
140 |
size: { |
|
141 |
original:12, |
|
142 |
minimum:0 |
|
143 |
} |
|
144 |
}, |
|
145 |
aggregation:{ |
|
146 |
height: vsHeight - 100, |
|
147 |
maxData: Math.max.apply(Math, movies.map(function(m) { return m.opinions.length; })) |
|
148 |
}, |
|
149 |
suspension:{ |
|
150 |
decay:{ |
|
151 |
power:1.001 |
|
152 |
} |
|
153 |
} |
|
154 |
} |
|
155 |
} |
|
156 |
barChart = $("#data-viz").vs(barChartSettings).data('visualSedimentation'); |
|
157 |
var startOfViz = new Date(); |
|
158 |
clearInterval(clockInterval); |
|
159 |
$(".timer-bar-value").width(0); |
|
160 |
clockInterval = setInterval(function() { |
|
161 |
$(".timer-bar-value").width($('.timer-bar').width() * Math.min(1, (new Date() - startOfViz) / config.vizDuration)); |
|
162 |
}, |
|
163 |
100); |
|
164 |
} |
|
165 |
|
|
166 |
function loadMovie(movie) { |
|
167 |
$.getJSON( |
|
168 |
"http://api.allocine.fr/rest/v3/opinionlist", |
|
169 |
{ |
|
170 |
partner: config.partnerCode, |
|
171 |
format: "json", |
|
172 |
subject: "movie:" + movie.id, |
|
173 |
count: config.opinionsPerPage, |
|
174 |
refresh: refreshtoken(), |
|
175 |
page: movie.opinionPage |
|
176 |
}, |
|
177 |
function(d) { |
|
178 |
if (d.feed && d.feed.activity) { |
|
179 |
var tokens = d.feed.activity.filter(function(a) { |
|
180 |
return a.action.type !== "notinterested" && a.action.value !== "-3.0"; |
|
181 |
}); |
|
182 |
} else { |
|
183 |
var tokens = []; |
|
184 |
} |
|
185 |
tokens.forEach(function(a) { |
|
186 |
a._colorId = config.colorIds[a.action.value]; |
|
187 |
a._color = config.colors[a._colorId]; |
|
188 |
a._timestamp = new Date(a.datetime).valueOf(); |
|
189 |
a._movie = movie.id; |
|
190 |
a._column = movie.column; |
|
191 |
}); |
|
192 |
var tokcount = tokens.length; |
|
193 |
tokens = tokens.filter(function(a) { |
|
194 |
return a._timestamp >= originalStart; |
|
195 |
}); |
|
196 |
movie.opinions = movie.opinions.concat(tokens); |
|
197 |
opinions = opinions.concat(tokens); |
|
198 |
$("#data-viz").append("."); |
|
199 |
if (tokens.length === tokcount) { |
|
200 |
console.log("Page " + movie.opinionPage + " of '" + movie.title + "' loaded"); |
|
201 |
movie.opinionPage ++; |
|
202 |
loadMovie(movie); |
|
203 |
} else { |
|
204 |
console.log("Page " + movie.opinionPage + " of '" + movie.title + "' -- total : " + movie.opinions.length + " opinions loaded"); |
|
205 |
moviesToLoad--; |
|
206 |
if (!moviesToLoad) { |
|
207 |
console.log("**** Everything is loaded, in " + (new Date().valueOf() - loadTime) / 1000 + " seconds"); |
|
208 |
startViz(); |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
); |
|
213 |
} |
|
214 |
|
|
215 |
$.getJSON( |
|
216 |
"http://api.allocine.fr/rest/v3/movielist", |
|
217 |
{ |
|
218 |
partner: config.partnerCode, |
|
219 |
format: "json", |
|
220 |
filter: "top:week", |
|
221 |
count: config.movieCount, |
|
222 |
refresh: refreshtoken() |
|
223 |
}, |
|
224 |
function(d) { |
|
225 |
$("#data-viz").html("Chargement des flux d'opinions<br />"); |
|
226 |
console.log("Movie List Loaded"); |
|
227 |
movies = d.feed.movie.map(function(movie, i) { |
|
228 |
return { |
|
229 |
id: movie.code, |
|
230 |
column: i, |
|
231 |
title: movie.title, |
|
232 |
poster: movie.poster.href, |
|
233 |
opinions: [], |
|
234 |
opinionPage: 1 |
|
235 |
} |
|
236 |
}); |
|
237 |
moviesToLoad = movies.length; |
|
238 |
movies.forEach(loadMovie); |
|
239 |
$('.posters').html(movies.map(function(movie) { |
|
240 |
return '<li><img width="150" height="200" src="' |
|
241 |
+ movie.poster |
|
242 |
+ '" alt="' |
|
243 |
+ movie.title |
|
244 |
+'" /></li>'; |
|
245 |
}).join("")); |
|
246 |
} |
|
247 |
); |
|
248 |
|
|
249 |
function refreshtoken() { |
|
250 |
return Math.floor(0x100000000 * Math.random()).toString(16) + "-" + (new Date().valueOf() % 0x100000000).toString(16); |
|
251 |
} |
|
252 |
|
|
253 |
$(".rejouer").click(function() { |
|
254 |
startViz(); |
|
255 |
return false; |
|
256 |
}) |
|
|
49
531a593a0294
update buzz : integration crew comment arrow and half star (rating)
Anthony Ly <anthonyly.com@gmail.com>
parents:
42
diff
changeset
|
257 |
|
| 54 | 258 |
}); |