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