|
1 function Streamgraph($selector, data,docId,user/*, data2*/) |
|
2 { |
|
3 |
|
4 /* Constants */ |
|
5 |
|
6 var VMARGIN = 3, |
|
7 YEARSHEIGHT = 20, |
|
8 STARTTIME = new Date(data.from_date), |
|
9 ENDTIME = new Date(data.to_date), |
|
10 DATASTART = new Date(data.from_date), |
|
11 DATAEND = new Date(data.to_date), |
|
12 CURVE = .25, |
|
13 DATEPADDING = 10, |
|
14 COLORS = [ "#51585E", "#12161C", "#457DAD", "#899DAA", "#0781BD" ], |
|
15 QUERYID = data.query_id, |
|
16 SELECTEDCOLOR = "#c51810"; |
|
17 |
|
18 /* Calculating scales and positions */ |
|
19 |
|
20 var width = $selector.width(), |
|
21 height = $selector.height(), |
|
22 transp = _.zip.apply( _, _(data.clusters).pluck("volumes") ), |
|
23 cumulative = _(transp).map(function(column) { |
|
24 var total = 0; |
|
25 return _(column).map(function(point) { |
|
26 return total += point; |
|
27 }); |
|
28 }), |
|
29 sums = _(cumulative).map(function(column) { |
|
30 return _(column).last(); |
|
31 }) |
|
32 maxcol = _(sums).max(), |
|
33 streamheight = height - YEARSHEIGHT, |
|
34 streamwidth = width * (DATAEND - DATASTART) / (ENDTIME - STARTTIME), |
|
35 yscale = (streamheight - 2 * VMARGIN) / maxcol, |
|
36 centery = streamheight / 2, |
|
37 xscale = streamwidth / (transp.length - 1), |
|
38 txscale = width / (ENDTIME - STARTTIME), |
|
39 startx = txscale * (DATASTART - STARTTIME), |
|
40 endx = txscale * (DATAEND - STARTTIME), |
|
41 coords = _(data.clusters).map(function(line, lineindex) { |
|
42 return { |
|
43 points : _(line.volumes).map(function(point, colindex) { |
|
44 var lowercumul = lineindex ? cumulative[colindex][lineindex - 1] : 0, |
|
45 uppercumul = cumulative[colindex][lineindex]; |
|
46 return { |
|
47 data: point, |
|
48 x: startx + xscale * colindex, |
|
49 lowery: centery + yscale * ( ( sums[colindex] / 2 ) - lowercumul ), |
|
50 uppery: centery + yscale * ( ( sums[colindex] / 2 ) - uppercumul ), |
|
51 } |
|
52 }), |
|
53 id : line.id, |
|
54 title: line.title |
|
55 } |
|
56 }), |
|
57 _(coords).each(function(line) { |
|
58 var lowerline = _(line.points).reduce(function(path, point, colindex) { |
|
59 var res = path; |
|
60 if (colindex) { |
|
61 res += "," + (point.x - CURVE * xscale) + "," + point.lowery + "," + point.x + "," + point.lowery; |
|
62 } else { |
|
63 res += "M" + point.x + "," + point.lowery; |
|
64 } |
|
65 if (colindex < line.points.length - 1) { |
|
66 res += "C" + (point.x + CURVE * xscale) + "," + point.lowery; |
|
67 } |
|
68 return res; |
|
69 }, ""); |
|
70 var upperline = _(line.points).reduceRight(function(path, point, colindex) { |
|
71 var res = path; |
|
72 if (colindex < line.points.length - 1) { |
|
73 res += "," + (point.x + CURVE * xscale) + "," + point.uppery + "," + point.x + "," + point.uppery; |
|
74 } else { |
|
75 res += "L" + point.x + "," + point.uppery; |
|
76 } |
|
77 if (colindex) { |
|
78 res += "C" + (point.x - CURVE * xscale) + "," + point.uppery; |
|
79 } |
|
80 return res; |
|
81 }, ""); |
|
82 line.path = lowerline + upperline; |
|
83 }); |
|
84 |
|
85 /* Drawing streamgraph*/ |
|
86 |
|
87 $selector.empty(); |
|
88 |
|
89 var paper = new Raphael($selector[0]); |
|
90 |
|
91 paper.path("M0 " + (1+centery) + "L" + width + " " + (1+centery)).attr({ |
|
92 stroke: "#000" |
|
93 }) |
|
94 |
|
95 _(coords).each(function(line, index) { |
|
96 line.color = COLORS[index % COLORS.length]; |
|
97 //var hue = (parseInt(line.id)%6)/6; |
|
98 //line.color = Raphael.hsl( hue, 1, .8 ); |
|
99 //line.highlightColor = Raphael.hsl( hue, 1, .4 ); |
|
100 line.surface = paper.path(line.path); |
|
101 line.surface.attr({ |
|
102 stroke: "#ffffff", |
|
103 "stroke-width": .25, |
|
104 fill: line.color |
|
105 }); |
|
106 }); |
|
107 |
|
108 /* Drawing years */ |
|
109 |
|
110 paper.path("M0," + (height - YEARSHEIGHT) + "," + width + "," + (height - YEARSHEIGHT)) |
|
111 var lastyear = ENDTIME.getFullYear(); |
|
112 for (var i = STARTTIME.getFullYear(); i <= lastyear; i++) { |
|
113 var x = txscale * (new Date(i,0,1) - STARTTIME); |
|
114 paper.path("M" + x + ",0," + x + "," + height); |
|
115 var x = txscale * (new Date(i,6,1) - STARTTIME); |
|
116 paper.text(x, height - .5 * YEARSHEIGHT, i) |
|
117 .attr({ |
|
118 "text-anchor": "middle", |
|
119 "font-family": "Times New Roman, serif", |
|
120 "font-size": "14px" |
|
121 }); |
|
122 } |
|
123 |
|
124 /* Drawing range window */ |
|
125 |
|
126 var carregauche = paper.rect(width,-1,width,(2+height)), |
|
127 carredroite = paper.rect(-width,-1,width,(2+height)), |
|
128 attrcarres = { |
|
129 fill: "#333333", |
|
130 "fill-opacity": .5, |
|
131 stroke: "#c51810" |
|
132 }; |
|
133 carregauche.attr(attrcarres); |
|
134 carredroite.attr(attrcarres); |
|
135 |
|
136 var rangerect = paper.rect(0, (height - YEARSHEIGHT), width, YEARSHEIGHT); |
|
137 rangerect.attr({ |
|
138 fill: "#c51810", |
|
139 stroke: "none" |
|
140 }); |
|
141 |
|
142 function datetext(date) { |
|
143 var d = new Date(date), |
|
144 m = 1+d.getMonth(), |
|
145 y = d.getFullYear(); |
|
146 return ((m < 10 ? "0" : "") + m + "/" + y); |
|
147 } |
|
148 |
|
149 var startdate = paper.text(DATEPADDING, height - .5 * YEARSHEIGHT, datetext(STARTTIME)); |
|
150 startdate.attr({ |
|
151 fill: "#ffffff", |
|
152 "text-anchor": "start" |
|
153 }); |
|
154 var enddate = paper.text(width - DATEPADDING, height - .5 * YEARSHEIGHT, datetext(ENDTIME)); |
|
155 enddate.attr({ |
|
156 fill: "#ffffff", |
|
157 "text-anchor": "end" |
|
158 }); |
|
159 |
|
160 /* Redrawing time slices for rollover effect */ |
|
161 var mem = ''; |
|
162 _(coords).each(function(line, index) { |
|
163 line.mousesurface = paper.path(line.path); |
|
164 mem += '<li><a href="http://localhost:8000/documentary_files/'+user+'/'+line.id+'/0/12/'+docId+'" title="Afficher le cluster" data-cluster-id="' + line.id + '">' + line.title + '</a></li>'; |
|
165 line.mousesurface.attr({ |
|
166 stroke: "none", |
|
167 fill: line.color, |
|
168 opacity: .01, |
|
169 title: line.title, |
|
170 href: "http://localhost:8000/documentary_files/"+user+"/"+line.id+"/0/12/"+docId |
|
171 }).mouseover(function() { |
|
172 //alert('mousse select'); |
|
173 $("body").trigger("select-cluster", line.id); |
|
174 }).mouseout(function() { |
|
175 //alert('mousse unselect'); |
|
176 $("body").trigger("unselect-cluster", line.id); |
|
177 |
|
178 }); |
|
179 // $(line.mousesurface.node).attr("data-cluster-id", line.id).parent().attr("data-cluster-id", line.id); |
|
180 }); |
|
181 |
|
182 $(".cluster").html(mem) ; |
|
183 /* if (typeof (data2) != 'undefined') |
|
184 { |
|
185 // alert('dat2not null'); |
|
186 |
|
187 |
|
188 |
|
189 var line = _(coords).find(function(line) |
|
190 { |
|
191 return line.id == 1; |
|
192 }); |
|
193 //alert(line.id); |
|
194 if (line) |
|
195 { |
|
196 alert(line.id); |
|
197 line.surface.attr({ fill: SELECTEDCOLOR }); |
|
198 } |
|
199 |
|
200 |
|
201 } |
|
202 else |
|
203 { |
|
204 alert('dat2 null'); |
|
205 }*/ |
|
206 $(".actu, .cluster a, .article").hover |
|
207 ( |
|
208 function() { |
|
209 $("body").trigger("select-cluster", $(this).attr("data-cluster-id")); |
|
210 }, |
|
211 function() { |
|
212 $("body").trigger("unselect-cluster", $(this).attr("data-cluster-id")); |
|
213 } |
|
214 ) |
|
215 |
|
216 $("body").on("unselect-cluster", function(e, clusterid) { |
|
217 $(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").removeClass("selected"); |
|
218 var line = _(coords).find(function(line) { |
|
219 return line.id == clusterid; |
|
220 }); |
|
221 if (line) { |
|
222 line.surface.attr({ |
|
223 fill: line.color |
|
224 }); |
|
225 } |
|
226 }); |
|
227 |
|
228 |
|
229 |
|
230 $("body").on("select-cluster", function(e, clusterid) { |
|
231 $(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").addClass("selected"); |
|
232 var line = _(coords).find(function(line) { |
|
233 return line.id == clusterid; |
|
234 }); |
|
235 if (line) { |
|
236 line.surface.attr({ |
|
237 fill: SELECTEDCOLOR //line.highlightColor |
|
238 }); |
|
239 } |
|
240 }); |
|
241 |
|
242 /*$("body").on("select-cluster", function(e, clusterid) |
|
243 { |
|
244 $(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").addClass("selected"); |
|
245 }); |
|
246 $("body").on("unselect-cluster", function(e, clusterid) { |
|
247 $(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").removeClass("selected"); |
|
248 });*/ |
|
249 |
|
250 /* Returning a handler for slide value change */ |
|
251 |
|
252 this.slidevalues = function(left, right) { |
|
253 left = left || 0; |
|
254 right = right || width; |
|
255 carregauche.attr({x: left - width}); |
|
256 carredroite.attr({x: right}); |
|
257 startdate.attr({ |
|
258 x: DATEPADDING + left, |
|
259 text: datetext(STARTTIME.valueOf() + left / txscale) |
|
260 }); |
|
261 enddate.attr({ |
|
262 x: right - DATEPADDING, |
|
263 text: datetext(STARTTIME.valueOf() + right / txscale) |
|
264 }); |
|
265 rangerect.attr({ |
|
266 x: left, |
|
267 width: right - left |
|
268 }); |
|
269 } |
|
270 |
|
271 $("#slider-range").dragslider("values", [startx, endx]); |
|
272 this.slidevalues(startx, endx); |
|
273 |
|
274 } |
|
275 |
|
276 function loadStreamgraphDoc(data,docId,user/*,data2*/) |
|
277 { |
|
278 $(".streamgraph").empty(); |
|
279 delete window.streamgraph; |
|
280 ; |
|
281 //$.getJSON(url, function(data) { |
|
282 window.streamgraph = new Streamgraph($(".streamgraph"), data,docId,user/*,data2*/); |
|
283 streamgraph.slidevalues.apply(streamgraph,$("#slider-range").dragslider("values")); |
|
284 |
|
285 //}); |
|
286 } |
|
287 |
|
288 /*$(function() { |
|
289 loadStreamgraph("data/json_streamgraph.json"); |
|
290 })*/ |