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