integration/js/treemap.js
author veltr
Tue, 22 Jan 2013 14:42:36 +0100
changeset 24 bd6d2bdbc47a
parent 21 c2dd00471b2d
child 25 94073a2af6e1
permissions -rw-r--r--
Tests of treemap with real data
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
     1
$(function(){
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
     2
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
     3
    var IDEALRATIO = 1.25;
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
     4
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
     5
    function cuttree(data, x, y, w, h, cut, ratio, callback) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
     6
        
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
     7
        function f(subdata, subx, suby, subw, subh) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
     8
            if (subdata.length == 1) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
     9
                subdata[0].x = subx;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    10
                subdata[0].y = suby;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    11
                subdata[0].w = subw;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    12
                subdata[0].h = subh;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    13
            } else {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    14
                callback(subdata, subx, suby, subw, subh)
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    15
            }
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
        var first = _(data).first(cut), rest = _(data).rest(cut);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    19
        if (!first.length || !rest.length) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    20
            return;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    21
        }
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    22
        if (w/h > IDEALRATIO) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    23
            var leftw = w * ratio;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    24
            f(first, x, y, leftw, h);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    25
            f(rest, x + leftw, y, w - leftw, h);
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
    26
        } else {
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    27
            var toph = h * ratio;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    28
            f(first, x, y, w, toph);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    29
            f(rest, x, y + toph, w, h - toph);
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
    30
        }
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
    31
    }
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    32
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    33
    function pivot(data, x, y, w, h) {
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    34
        if (data.length == 1) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    35
            data[0].x = x;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    36
            data[0].y = x;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    37
            data[0].w = w;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    38
            data[0].h = h;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    39
            return;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    40
        }
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    41
        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
    42
        for (var i = 0; i < data.length - 1; i++) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    43
            cumul += data[i].value;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    44
            var delta = Math.abs(cumul - total/2);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    45
            if (delta < bestcut) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    46
                bestcut = delta;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    47
                bestcumul = cumul;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    48
                cut = i+1;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    49
            } else {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    50
                break;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    51
            }
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
        cuttree(data, x, y, w, h, cut, bestcumul / total, pivot);
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
    54
    }
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
    55
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    56
    function squarify(data, x, y, w, h) {
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    57
        if (data.length == 1) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    58
            data[0].x = x;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    59
            data[0].y = x;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    60
            data[0].w = w;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    61
            data[0].h = h;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    62
            return;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    63
        }
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    64
        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
    65
        for (var i = 0; i < data.length - 1; i++) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    66
            cumul += data[i].value;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    67
            cuttree(data, x, y, w, h, i+1, cumul / total, pivot);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    68
            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
    69
            if (ratio < bestcut) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    70
                bestcut = ratio;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    71
                bestcumul = cumul;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    72
                cut = i+1;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    73
            } else {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    74
                break;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    75
            }
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
    76
        }
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
    77
        cuttree(data, x, y, w, h, cut, bestcumul / total, squarify);
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
    78
    }
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    79
    
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    80
    /* SOURCE : http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript */
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    81
    
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    82
    /**
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    83
     * Converts an HSV color value to RGB. Conversion formula
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    84
     * adapted from http://en.wikipedia.org/wiki/HSV_color_space.
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    85
     * Assumes h, s, and v are contained in the set [0, 1] and
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    86
     * returns r, g, and b in the set [0, 255].
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    87
     *
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    88
     * @param   Number  h       The hue
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    89
     * @param   Number  s       The saturation
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    90
     * @param   Number  v       The value
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    91
     * @return  Array           The RGB representation
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    92
     */
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    93
    function hsvToRgb(h, s, v){
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    94
        var r, g, b;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    95
    
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    96
        var i = Math.floor(h * 6);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    97
        var f = h * 6 - i;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    98
        var p = v * (1 - s);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
    99
        var q = v * (1 - f * s);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   100
        var t = v * (1 - (1 - f) * s);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   101
    
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   102
        switch(i % 6){
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   103
            case 0: r = v, g = t, b = p; break;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   104
            case 1: r = q, g = v, b = p; break;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   105
            case 2: r = p, g = v, b = t; break;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   106
            case 3: r = p, g = q, b = v; break;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   107
            case 4: r = t, g = p, b = v; break;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   108
            case 5: r = v, g = p, b = q; break;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   109
        }
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   110
    
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   111
        return _([r * 255, g * 255, b * 255]).map(Math.floor);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   112
    }
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   113
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   114
    /* Templates des éléments à insérer */
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   115
   
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   116
    var articleTemplate = _(
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   117
        '<div class="cluster-article" style="position: absolute; left: <%=x%>px; top: <%=y%>px; width: <%=w%>px; height: <%=h%>px; background: <%=color%>">'+
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   118
            '<%=label%>'+
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   119
        '</div>'
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   120
    ).template();
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   121
    var clusterTemplate = _( 
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   122
    '<div class="actu" style="left: <%=x%>px; top: <%=y%>px; width: <%=w%>px; height: <%=h%>px; background: <%=color%>">'+
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   123
        '<a href="#">'+
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   124
//            '<img src="img/home-visuel-<%-i%>.jpg" alt="" />'+
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   125
        '<%=articles%>'+
20
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   126
        '<div class="voile"></div>'+
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   127
        '</a>'+
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   128
        '<div class="inner-actu">'+
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   129
            '<h2><a href="#"><%-label%></a></h2>'+
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   130
            '<div class="links">'+
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   131
                '<ul>'+
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   132
                    '<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
   133
                '</ul>'+
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   134
            '</div>'+
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   135
        '</div>'+
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   136
    '</div>'
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   137
    ).template();
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   138
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   139
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   140
    var hTreemap = 600;//à définir
20
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   141
    $('#treemap').height(hTreemap);
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   142
    
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   143
    function showResults(results) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   144
        var data = _(results.clusters).map(function(cluster, i) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   145
            var hue = (i%6)/6
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   146
            return {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   147
                label: cluster.title,
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   148
                value: parseFloat(cluster.weight),
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   149
                color: "rgb(" + hsvToRgb(hue,.8,.8).join(",") + ")",
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   150
                annotation_count: cluster.annotations.length,
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   151
                articles: _(cluster.documents).map(function(article, j) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   152
                    return {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   153
                        label: article.title,
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   154
                        value: parseFloat(article.weight),
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   155
                        color: "rgb(" + hsvToRgb(hue,.8,.9-.1*(j%6)).join(",") + ")"
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   156
                    }
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
        data _(data).sortBy(function(d) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   161
            return -d.value;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   162
        });
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   163
        squarify(data,0,0,760,hTreemap);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   164
        _(data).each(function(cluster) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   165
            squarify(cluster.articles, 0, 0, cluster.w, cluster.h);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   166
            _(cluster.articles).sortBy(function(d) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   167
                return -d.value;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   168
            });
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   169
            cluster.articles = _(cluster.articles).reduce(function(mem, a) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   170
                return mem + articleTemplate(a);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   171
            }, "");
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
        var treemapHtml = _(data).reduce(function(mem, d) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   174
            return mem + clusterTemplate(d);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   175
        },"");
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   176
        $('#treemap #actus').html(treemapHtml);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   177
    }
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   178
    
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   179
    function renderJson(url) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   180
        $.getJSON(url, showResults);
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   181
    }
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   182
    
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   183
    renderJson("data/requete.json");
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   184
    
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   185
    $(".header-menu a").click(function() {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   186
        if ($(this).text() == "INTERNATIONAL") {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   187
            renderJson("data/requete_filtre_international.json");
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   188
        }
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   189
        if ($(this).text() == "FRANCE") {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   190
            renderJson("data/requete_filtre_france.json");
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   191
        }
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   192
        if ($(this).hasClass("home")) {
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   193
            renderJson("data/requete.json")
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   194
        }
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   195
        return false;
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   196
    });
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   197
    
20
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   198
    //redimensionnement d'image
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   199
/*
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   200
     $(".actu").each(function(k,v){
20
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   201
        var wActu = $(this).width();
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   202
        var hActu = $(this).height();
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   203
        var img = $(this).find('img');
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   204
        img.load(function(){
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   205
            var img = $(this);
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   206
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   207
            var wImg = img.width();
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   208
            var hImg = img.height();
20
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   209
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   210
            var ratioImg = wImg/hImg;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   211
            img.css('height',hActu);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   212
            img.css('width',hActu*ratioImg);
20
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   213
            wImg = img.width();
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   214
            hImg = img.height();
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   215
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   216
            if(wActu>wImg){
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   217
                var ratioImg = hImg/wImg;
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   218
                img.css('width', wActu);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   219
                img.css('height',wActu*ratioImg);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   220
                wImg = img.width();
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   221
                hImg = img.height();
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   222
            }
19
81a605180745 ajout algo treemap
Anthony Ly <anthonyly.com@gmail.com>
parents:
diff changeset
   223
21
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   224
            if (wImg<wActu) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   225
                img.css('margin-left',(wActu-wImg)/2);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   226
            }else{
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   227
                img.css('margin-left',-(wImg-wActu)/2);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   228
            }
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   229
            if (hImg<hActu) {
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   230
                img.css('margin-top',(hActu-hImg)/2);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   231
            }else{
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   232
                img.css('margin-top',-(hImg-hActu)/2);
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   233
            }
c2dd00471b2d maj retour.
Anthony Ly <anthonyly.com@gmail.com>
parents: 20
diff changeset
   234
        });
20
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   235
    });
24
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   236
*/
bd6d2bdbc47a Tests of treemap with real data
veltr
parents: 21
diff changeset
   237
20
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   238
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   239
    $("#liste").hide();
c86141a8570d maj retours / modifications
Anthony Ly <anthonyly.com@gmail.com>
parents: 19
diff changeset
   240
})