| author | veltr |
| Tue, 29 Jan 2013 13:29:21 +0100 | |
| changeset 28 | 10a958322a62 |
| parent 26 | 94f586daa623 |
| permissions | -rw-r--r-- |
| 26 | 1 |
|
| 21 | 2 |
$(function(){ |
3 |
||
| 26 | 4 |
var IDEALRATIO = 1.33; |
| 19 | 5 |
|
| 21 | 6 |
function cuttree(data, x, y, w, h, cut, ratio, callback) { |
7 |
|
|
8 |
function f(subdata, subx, suby, subw, subh) { |
|
9 |
if (subdata.length == 1) { |
|
10 |
subdata[0].x = subx; |
|
11 |
subdata[0].y = suby; |
|
12 |
subdata[0].w = subw; |
|
13 |
subdata[0].h = subh; |
|
14 |
} else { |
|
15 |
callback(subdata, subx, suby, subw, subh) |
|
16 |
} |
|
17 |
} |
|
18 |
|
|
19 |
var first = _(data).first(cut), rest = _(data).rest(cut); |
|
20 |
if (!first.length || !rest.length) { |
|
21 |
return; |
|
22 |
} |
|
23 |
if (w/h > IDEALRATIO) { |
|
24 |
var leftw = w * ratio; |
|
25 |
f(first, x, y, leftw, h); |
|
26 |
f(rest, x + leftw, y, w - leftw, h); |
|
| 19 | 27 |
} else { |
| 21 | 28 |
var toph = h * ratio; |
29 |
f(first, x, y, w, toph); |
|
30 |
f(rest, x, y + toph, w, h - toph); |
|
| 19 | 31 |
} |
32 |
} |
|
| 21 | 33 |
|
34 |
function pivot(data, x, y, w, h) { |
|
| 24 | 35 |
if (data.length == 1) { |
36 |
data[0].x = x; |
|
37 |
data[0].y = x; |
|
38 |
data[0].w = w; |
|
39 |
data[0].h = h; |
|
40 |
return; |
|
41 |
} |
|
| 21 | 42 |
var cut = 1, cumul = 0, bestcumul = 0, total = _(data).reduce(function(a,b){return a+b.value},0), bestcut = Infinity; |
43 |
for (var i = 0; i < data.length - 1; i++) { |
|
44 |
cumul += data[i].value; |
|
45 |
var delta = Math.abs(cumul - total/2); |
|
46 |
if (delta < bestcut) { |
|
47 |
bestcut = delta; |
|
48 |
bestcumul = cumul; |
|
49 |
cut = i+1; |
|
50 |
} else { |
|
51 |
break; |
|
52 |
} |
|
53 |
} |
|
54 |
cuttree(data, x, y, w, h, cut, bestcumul / total, pivot); |
|
| 19 | 55 |
} |
56 |
||
| 21 | 57 |
function squarify(data, x, y, w, h) { |
| 24 | 58 |
if (data.length == 1) { |
59 |
data[0].x = x; |
|
60 |
data[0].y = x; |
|
61 |
data[0].w = w; |
|
62 |
data[0].h = h; |
|
63 |
return; |
|
64 |
} |
|
| 21 | 65 |
var cut = 1, cumul = 0, bestcumul = 0, total = _(data).reduce(function(a,b){return a+b.value},0), bestcut = Infinity; |
66 |
for (var i = 0; i < data.length - 1; i++) { |
|
67 |
cumul += data[i].value; |
|
68 |
cuttree(data, x, y, w, h, i+1, cumul / total, pivot); |
|
69 |
var ratio = Math.abs(Math.log(IDEALRATIO*data[0].h/data[0].w)); |
|
70 |
if (ratio < bestcut) { |
|
71 |
bestcut = ratio; |
|
72 |
bestcumul = cumul; |
|
73 |
cut = i+1; |
|
74 |
} else { |
|
75 |
break; |
|
76 |
} |
|
| 19 | 77 |
} |
| 21 | 78 |
cuttree(data, x, y, w, h, cut, bestcumul / total, squarify); |
| 19 | 79 |
} |
| 24 | 80 |
|
| 19 | 81 |
|
| 24 | 82 |
/* Templates des éléments à insérer */ |
83 |
|
|
84 |
var articleTemplate = _( |
|
| 26 | 85 |
'<div class="cluster-article" style="left: <%=x%>px; top: <%=y%>px; width: <%=w%>px; height: <%=h%>px;">'+ |
86 |
'<img src="<%=image_url%>" />' + |
|
| 24 | 87 |
'</div>' |
88 |
).template(); |
|
89 |
var clusterTemplate = _( |
|
| 26 | 90 |
'<div class="actu" style="left: <%=x%>px; top: <%=y%>px; width: <%=w - 1%>px; height: <%=h - 1%>px; background: #ffffff" data-cluster-id="<%=id%>">'+ |
91 |
'<%=articles%>'+ // Pour l'image composite |
|
92 |
'<img src="<%=image_url%>" />' + // Pour l'image de cluster |
|
|
20
c86141a8570d
maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents:
19
diff
changeset
|
93 |
'<div class="voile"></div>'+ |
| 19 | 94 |
'<div class="inner-actu">'+ |
95 |
'<h2><a href="#"><%-label%></a></h2>'+ |
|
96 |
'<div class="links">'+ |
|
97 |
'<ul>'+ |
|
| 24 | 98 |
'<li><a href="#" title="<%=annotation_count%> Annotations sur ce cluster" class="file"><span><%=annotation_count%></span></a></li>'+ |
| 19 | 99 |
'</ul>'+ |
100 |
'</div>'+ |
|
101 |
'</div>'+ |
|
| 26 | 102 |
'<p class="abstract"><%= abstract %></p>'+ |
| 24 | 103 |
'</div>' |
104 |
).template(); |
|
| 28 | 105 |
|
106 |
/* Templates pour la vue liste */ |
|
107 |
|
|
108 |
var clusterListeTemplate = _( |
|
109 |
'<div class="article" data-cluster-id="<%= id %>">'+ |
|
110 |
'<div class="inner-article clearfix">'+ |
|
111 |
'<h2><a title="Lire l\'article" href="#"> <%= title %> »</a></h2>'+ |
|
112 |
'<p class="number-article">'+ |
|
113 |
'<a href="#"><%= documents_number %> articles | <%= annotations.length %> annotations </a>'+ |
|
114 |
'<a title="ajouter une annotation" class="add-annotation" href="#"></a>'+ |
|
115 |
'</p>'+ |
|
116 |
'<div class="article-annotations">'+ |
|
117 |
'<a title="48 annotations" class="blue" href="#" style="width:20%;"></a>'+ |
|
118 |
'<a title="title" class="green" href="#" style="width:20%;"></a>'+ |
|
119 |
'<a title="title" class="red" href="#" style="width:40%;"></a>'+ |
|
120 |
'<a title="title" class="empty" href="#" style="width:20%;"></a>'+ |
|
121 |
'</div>'+ |
|
122 |
'<div class="article-content">'+ |
|
123 |
'<p class="resume"><%= abstract %></p>'+ |
|
124 |
'<ul class="links">'+ |
|
125 |
'<ul>'+ |
|
126 |
'<% _(documents).each(function(d) { print("<li><a href=\'" + d.url_document + "\'>" + d.title.replace(/(^.{30,60})[\s].+$/m,\'$1…\') + "</a></li>"); }) %>'+ |
|
127 |
'</ul>'+ |
|
128 |
'</ul>'+ |
|
129 |
'</div>'+ |
|
130 |
'</div>'+ |
|
131 |
'</div>' |
|
132 |
).template(); |
|
| 19 | 133 |
|
| 21 | 134 |
|
| 24 | 135 |
var hTreemap = 600;//à définir |
|
20
c86141a8570d
maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents:
19
diff
changeset
|
136 |
$('#treemap').height(hTreemap); |
| 24 | 137 |
|
138 |
function showResults(results) { |
|
| 28 | 139 |
$(".articles").html(_(results.clusters).reduce(function(mem, c) { return mem + clusterListeTemplate(c) },'')); |
140 |
|
|
| 24 | 141 |
var data = _(results.clusters).map(function(cluster, i) { |
| 26 | 142 |
var hue = (parseInt(cluster.id)%6)/6 |
| 24 | 143 |
return { |
| 26 | 144 |
id: cluster.id, |
| 24 | 145 |
label: cluster.title, |
| 26 | 146 |
abstract : cluster.abstract, |
| 24 | 147 |
value: parseFloat(cluster.weight), |
148 |
annotation_count: cluster.annotations.length, |
|
| 26 | 149 |
image_url: cluster.url_image || false, |
150 |
articles: cluster.documents.filter(function(article) { |
|
151 |
return !!article.url_image |
|
152 |
}) |
|
153 |
.map(function(article, j) { |
|
| 24 | 154 |
return { |
155 |
value: parseFloat(article.weight), |
|
| 26 | 156 |
image_url: article.url_image || false |
| 24 | 157 |
} |
158 |
}) |
|
159 |
} |
|
160 |
}); |
|
| 25 | 161 |
data = _(data).sortBy(function(d) { |
| 24 | 162 |
return -d.value; |
163 |
}); |
|
164 |
squarify(data,0,0,760,hTreemap); |
|
165 |
_(data).each(function(cluster) { |
|
| 26 | 166 |
squarify(cluster.articles, 0, 0, cluster.w - 1, cluster.h - 1); |
| 24 | 167 |
_(cluster.articles).sortBy(function(d) { |
168 |
return -d.value; |
|
169 |
}); |
|
170 |
cluster.articles = _(cluster.articles).reduce(function(mem, a) { |
|
171 |
return mem + articleTemplate(a); |
|
172 |
}, ""); |
|
173 |
}); |
|
174 |
var treemapHtml = _(data).reduce(function(mem, d) { |
|
175 |
return mem + clusterTemplate(d); |
|
176 |
},""); |
|
177 |
$('#treemap #actus').html(treemapHtml); |
|
| 26 | 178 |
|
179 |
//redimensionnement d'image |
|
180 |
|
|
181 |
$(".actu img").each(function() { |
|
182 |
var img = $(this), |
|
183 |
div = $(this).parent(); |
|
184 |
img.load(function() { |
|
185 |
var iw = img.width(), |
|
186 |
ih = img.height(), |
|
187 |
dw = div.width(), |
|
188 |
dh = div.height(), |
|
189 |
scale = Math.max(dw/iw, dh/ih), |
|
190 |
niw = iw * scale, |
|
191 |
nih = ih * scale; |
|
192 |
img.css({ |
|
193 |
width: niw, |
|
194 |
height: nih, |
|
195 |
"margin-left": (dw - niw) / 2, |
|
196 |
"margin-top": (dh - nih) / 3 |
|
197 |
}); |
|
198 |
}); |
|
199 |
}); |
|
200 |
|
|
| 28 | 201 |
$(".cluster").html(data.reduce(function(mem, c) { |
202 |
return mem + '<li><a href="#" title="Afficher le cluster" data-cluster-id="' + c.id + '">' + c.label + '</a></li>' |
|
203 |
}, '')); |
|
204 |
|
|
205 |
$(".actu, .cluster a, .article").hover( |
|
| 26 | 206 |
function() { |
207 |
$("body").trigger("select-cluster", $(this).attr("data-cluster-id")); |
|
208 |
}, |
|
209 |
function() { |
|
210 |
$("body").trigger("unselect-cluster", $(this).attr("data-cluster-id")); |
|
211 |
} |
|
212 |
) |
|
213 |
$("body").on("select-cluster", function(e, clusterid) { |
|
| 28 | 214 |
$(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").addClass("selected"); |
| 26 | 215 |
}); |
216 |
$("body").on("unselect-cluster", function(e, clusterid) { |
|
| 28 | 217 |
$(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").removeClass("selected"); |
| 26 | 218 |
}); |
| 24 | 219 |
} |
220 |
|
|
221 |
function renderJson(url) { |
|
222 |
$.getJSON(url, showResults); |
|
223 |
} |
|
224 |
|
|
| 28 | 225 |
renderJson("data/requete_filtre_2008.json"); |
| 24 | 226 |
|
|
20
c86141a8570d
maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents:
19
diff
changeset
|
227 |
|
|
c86141a8570d
maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents:
19
diff
changeset
|
228 |
$("#liste").hide(); |
|
c86141a8570d
maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents:
19
diff
changeset
|
229 |
}) |