diff -r bad0e6c60b63 -r 3848e1813a30 alcatel/static/js/treemap.js --- a/alcatel/static/js/treemap.js Mon Jul 22 14:56:35 2013 +0200 +++ b/alcatel/static/js/treemap.js Wed Aug 14 16:36:41 2013 +0200 @@ -1,146 +1,384 @@ -/* Génération de données aléatoires */ +//$(function(){ -var data = [], - startcolor = [ 0, 0, 255 ], - endcolor = [ 255, 255, 0 ] - elementcount = 8; + var IDEALRATIO = 1.33; + + function cuttree(data, x, y, w, h, cut, ratio, callback) { + + function f(subdata, subx, suby, subw, subh) { + if (subdata.length == 1) { + subdata[0].x = subx; + subdata[0].y = suby; + subdata[0].w = subw; + subdata[0].h = subh; + } else { + callback(subdata, subx, suby, subw, subh) + } + } + + var first = _(data).first(cut), rest = _(data).rest(cut); + if (!first.length || !rest.length) { + return; + } + if (w/h > IDEALRATIO) { + var leftw = w * ratio; + f(first, x, y, leftw, h); + f(rest, x + leftw, y, w - leftw, h); + } else { + var toph = h * ratio; + f(first, x, y, w, toph); + f(rest, x, y + toph, w, h - toph); + } + } -for (var i = 0; i < elementcount; i++) { - var r = i/elementcount, - col = _(endcolor).map(function(e,i) { - var s = startcolor[i] - return Math.floor(r*e + (1-r)*s) - }); - data.push({ - label: "Cluster " + (1+i), - i: i+1, - color: "rgb("+col.join(",")+")", - value: 1+Math.pow(Math.random(),2)*5 - }); -} - -/* Génération du Treemap */ + function pivot(data, x, y, w, h) { + if (data.length == 1) { + data[0].x = x; + data[0].y = x; + data[0].w = w; + data[0].h = h; + return; + } + var cut = 1, cumul = 0, bestcumul = 0, total = _(data).reduce(function(a,b){return a+b.value},0), bestcut = Infinity; + for (var i = 0; i < data.length - 1; i++) { + cumul += data[i].value; + var delta = Math.abs(cumul - total/2); + if (delta < bestcut) { + bestcut = delta; + bestcumul = cumul; + cut = i+1; + } else { + break; + } + } + cuttree(data, x, y, w, h, cut, bestcumul / total, pivot); + } -data = _(data).sortBy(function(d) { return -d.value; }); - -var IDEALRATIO = 1.25; - -function cuttree(data, x, y, w, h, cut, ratio, callback) { - - function f(subdata, subx, suby, subw, subh) { - if (subdata.length == 1) { - subdata[0].x = subx; - subdata[0].y = suby; - subdata[0].w = subw; - subdata[0].h = subh; - } else { - callback(subdata, subx, suby, subw, subh) + function squarify(data, x, y, w, h) { + if (data.length == 1) { + data[0].x = x; + data[0].y = x; + data[0].w = w; + data[0].h = h; + return; } + var cut = 1, cumul = 0, bestcumul = 0, total = _(data).reduce(function(a,b){return a+b.value},0), bestcut = Infinity; + for (var i = 0; i < data.length - 1; i++) { + cumul += data[i].value; + cuttree(data, x, y, w, h, i+1, cumul / total, pivot); + var ratio = Math.abs(Math.log(IDEALRATIO*data[0].h/data[0].w)); + if (ratio < bestcut) { + bestcut = ratio; + bestcumul = cumul; + cut = i+1; + } else { + break; + } + } + cuttree(data, x, y, w, h, cut, bestcumul / total, squarify); } - var first = _(data).first(cut), rest = _(data).rest(cut); - if (!first.length || !rest.length) { - return; - } - if (w/h > IDEALRATIO) { - var leftw = w * ratio; - f(first, x, y, leftw, h); - f(rest, x + leftw, y, w - leftw, h); - } else { - var toph = h * ratio; - f(first, x, y, w, toph); - f(rest, x, y + toph, w, h - toph); - } -} -function pivot(data, x, y, w, h) { - var cut = 1, cumul = 0, bestcumul = 0, total = _(data).reduce(function(a,b){return a+b.value},0), bestcut = Infinity; - for (var i = 0; i < data.length - 1; i++) { - cumul += data[i].value; - var delta = Math.abs(cumul - total/2); - if (delta < bestcut) { - bestcut = delta; - bestcumul = cumul; - cut = i+1; - } else { - break; - } - } - cuttree(data, x, y, w, h, cut, bestcumul / total, pivot); -} - -function squarify(data, x, y, w, h) { - var cut = 1, cumul = 0, bestcumul = 0, total = _(data).reduce(function(a,b){return a+b.value},0), bestcut = Infinity; - for (var i = 0; i < data.length - 1; i++) { - cumul += data[i].value; - cuttree(data, x, y, w, h, i+1, cumul / total, pivot); - var ratio = Math.abs(Math.log(IDEALRATIO*data[0].h/data[0].w)); - if (ratio < bestcut) { - bestcut = ratio; - bestcumul = cumul; - cut = i+1; - } else { - break; - } - } - cuttree(data, x, y, w, h, cut, bestcumul / total, squarify); -} - -/* Template des éléments à insérer */ -var actu = - '
'+ - ''+ - ''+ - ''+ + /* Templates des éléments à insérer */ + + var articleTemplate = _( + '
'+ + '' + + '
' + ).template(); + + var clusterTemplate = _( + '
'+ + '<%=articles%>'+ // Pour l'image composite + '' + // Pour l'image de cluster + '
'+ '
'+ - '

<%-label%>

'+ + '

<%-label%>

'+ ''+ + '
'+ + '

<%= abstract %>

'+ + '
' + ).template(); + + /* Templates pour la vue liste */ + + var clusterListeTemplate = _( + '
'+ + '
'+ + '

<%= title %> »

'+ + '

'+ + '<%= documents_number %> articles | <%= annotations.length %> annotations '+ + ''+ + '

'+ + '
'+ + ''+ + ''+ + ''+ + ''+ + '
'+ + '
'+ + '

<%= abstract %>

'+ + ''+ + '
'+ + '
'+ + '
' + ).template(); + + var clusterListeTemplateDossierDoc = _( + '
'+ + '
'+ + '

<%= title %> »

'+ + '

'+ + '<%= documents_number %> articles | <%= annotations.length %> annotations '+ + ''+ + '

'+ + '
'+ + ''+ + ''+ + ''+ + ''+ + '
'+ + '
'+ + '

<%= abstract %>

'+ + ''+ + '
'+ + '
'+ + '
' + ).template(); + + var clusterTemplateDossierDoc = _( + '
'+ + '<%=articles%>'+ // Pour l'image composite + '' + // Pour l'image de cluster + '
'+ + '
'+ + '

<%-label%>

'+ + ''+ '
'+ - '
'; -var tmpl = _.template(actu); - -squarify(data,0,0,760,358); - -document.getElementById('treemap1').innerHTML = _(data).reduce(function(mem, d) { - return mem + tmpl(d); -},""); + '

<%= abstract %>

'+ + '
' + ).template(); + /* var clusterListeTemplate = _( + '
'+ + '
'+ + '

<%= title %> »

'+ + '

'+ + '<%= documents_number %> articles | <%= annotations.length %> annotations '+ + ''+ + '

'+ + '
'+ + ''+ + ''+ + ''+ + ''+ + '
'+ + '
'+ + '

<%= abstract %>

'+ + ''+ + '
'+ + '
'+ + '
' + ).template(); + */ + var hTreemap = 600;//à définir + $('#treemap').height(hTreemap); + + function showResults(results) + { + + if (typeof (results.clusters) == 'undefined') + { + $('#create-dossierDoc').hide(); + } + else + { + $('#create-dossierDoc').show(); + } + $(".articles").empty(); + $(".articles").html(_(results.clusters).reduce(function(mem, c) { return mem + clusterListeTemplate(c); },'')); + var data = _(results.clusters).map(function(cluster, i) { + var hue = (parseInt(cluster.id)%6)/6 + return { + id: cluster.id, + query_id: cluster.query_id, + label: cluster.title, + abstract : cluster.abstract, + value: parseFloat(cluster.weight), + annotation_count: cluster.annotations.length, + image_url: cluster.url_image || false, + articles: cluster.documents.filter(function(article) { + return !!article.url_image + }) + .map(function(article, j) { + return { + value: parseFloat(article.weight), + image_url: article.url_image || false + } + }) + } + }); + data = _(data).sortBy(function(d) { + return -d.value; + }); + squarify(data,0,0,760,hTreemap); + _(data).each(function(cluster) { + squarify(cluster.articles, 0, 0, cluster.w - 1, cluster.h - 1); + _(cluster.articles).sortBy(function(d) { + return -d.value; + }); + cluster.articles = _(cluster.articles).reduce(function(mem, a) { + return mem + articleTemplate(a); + }, ""); + }); + var treemapHtml = _(data).reduce(function(mem, d) { + return mem + clusterTemplate(d); + },""); + $('#treemap #actus').empty(); + $('#treemap #actus').html(treemapHtml); -//redimensionnement d'image -$(".actu").each(function(k,v){ - var wActu = $(this).width(); - var hActu = $(this).height(); - var img = $(this).find('img'); - var wImg = img.width(); - var hImg = img.height(); + //redimensionnement d'image + $(".actu img").each(function() + { + + var img = $(this), + div = $(this).parent(); + $(this).load(function() { + var iw = $(this).width(), + ih = $(this).height(), + dw = $(this).parent().width(), + dh = $(this).parent().height(), + scale = Math.max(dw/iw, dh/ih), + niw = iw * scale, + nih = ih * scale; + img.css({ + width: niw, + height: nih, + "margin-left": (dw - niw) / 2, + "margin-top": (dh - nih) / 3 + }); + }); + }); + + /* $(".cluster").html(data.reduce(function(mem, c) { + return mem + '
  • ' + c.label + '
  • ' + }, '')); + + $(".actu, .cluster a, .article").hover + ( + function() + { + $("body").trigger("select-cluster", $(this).attr("data-cluster-id")); + }, + function() + { + $("body").trigger("unselect-cluster", $(this).attr("data-cluster-id")); + } + )*/ + + + } + + function showResultsDossierDoc(results) + { + $(".articles").empty(); + $(".articles").html(_(results.clusters).reduce(function(mem, c) { return mem + clusterListeTemplateDossierDoc(c); },'')); + var data = _(results.clusters).map(function(cluster, i) { + var hue = (parseInt(cluster.id)%6)/6 + return { + id: cluster.id, + doc_id: cluster.doc_id, + user: cluster.user, + label: cluster.title, + abstract : cluster.abstract, + value: parseFloat(cluster.weight), + annotation_count: cluster.annotations.length, + image_url: cluster.url_image || false, + articles: cluster.documents.filter(function(article) { + return !!article.url_image + }) + .map(function(article, j) { + return { + value: parseFloat(article.weight), + image_url: article.url_image || false + } + }) + } + }); + + data = _(data).sortBy(function(d) { + return -d.value; + }); - var ratioImg = wImg/hImg; - img.css('height',hActu); - img.css('width',hActu*ratioImg); - wImg = img.width(); - hImg = img.height(); + squarify(data,0,0,760,hTreemap); + _(data).each(function(cluster) { + squarify(cluster.articles, 0, 0, cluster.w - 1, cluster.h - 1); + _(cluster.articles).sortBy(function(d) { + return -d.value; + }); + cluster.articles = _(cluster.articles).reduce(function(mem, a) { + return mem + articleTemplate(a); + }, ""); + }); + var treemapHtml = _(data).reduce(function(mem, d) { + return mem + clusterTemplateDossierDoc(d); + },""); + $('#treemap #actus').empty(); + $('#treemap #actus').html(treemapHtml); - if(wActu>wImg){ - var ratioImg = hImg/wImg; - img.css('width', wActu); - img.css('height',wActu*ratioImg); - wImg = img.width(); - hImg = img.height(); + //redimensionnement d'image + $(".actu img").each(function() + { + var img = $(this), + div = $(this).parent(); + var loadThisImage = function() { + var iw = img.width(), + ih = img.height(), + dw = div.width(), + dh = div.height(), + scale = Math.max(dw/iw, dh/ih), + niw = iw * scale, + nih = ih * scale; + img.css({ + width: niw, + height: nih, + "margin-left": (dw - niw) / 2, + "margin-top": (dh - nih) / 3 + }); + } + if (this.width) { + loadThisImage(); + } else { + img.load(loadThisImage); + } + }); + + /*$("body").on("select-cluster", function(e, clusterid) { + $(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").addClass("selected"); + }); + $("body").on("unselect-cluster", function(e, clusterid) { + $(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").removeClass("selected"); + });*/ } - if (wImg