alcatel/static/js/streamgraphdoc.js
author obledc
Tue, 10 Sep 2013 13:28:30 +0200
changeset 44 3648b6dea2cc
permissions -rw-r--r--
new
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44
obledc
parents:
diff changeset
     1
function Streamgraph($selector, data,docId,user/*, data2*/) 
obledc
parents:
diff changeset
     2
{
obledc
parents:
diff changeset
     3
    
obledc
parents:
diff changeset
     4
    /* Constants */
obledc
parents:
diff changeset
     5
 
obledc
parents:
diff changeset
     6
    var VMARGIN = 3,
obledc
parents:
diff changeset
     7
        YEARSHEIGHT = 20,
obledc
parents:
diff changeset
     8
        STARTTIME = new Date(data.from_date),
obledc
parents:
diff changeset
     9
        ENDTIME = new Date(data.to_date),
obledc
parents:
diff changeset
    10
        DATASTART = new Date(data.from_date),
obledc
parents:
diff changeset
    11
        DATAEND = new Date(data.to_date),
obledc
parents:
diff changeset
    12
        CURVE = .25,
obledc
parents:
diff changeset
    13
        DATEPADDING = 10,
obledc
parents:
diff changeset
    14
        COLORS = [ "#51585E", "#12161C", "#457DAD", "#899DAA", "#0781BD" ],
obledc
parents:
diff changeset
    15
		QUERYID = data.query_id,
obledc
parents:
diff changeset
    16
        SELECTEDCOLOR = "#c51810"; 
obledc
parents:
diff changeset
    17
    	
obledc
parents:
diff changeset
    18
    /* Calculating scales and positions */
obledc
parents:
diff changeset
    19
  
obledc
parents:
diff changeset
    20
    var width = $selector.width(),
obledc
parents:
diff changeset
    21
        height = $selector.height(),
obledc
parents:
diff changeset
    22
        transp = _.zip.apply( _, _(data.clusters).pluck("volumes") ),
obledc
parents:
diff changeset
    23
        cumulative = _(transp).map(function(column) {
obledc
parents:
diff changeset
    24
            var total = 0;
obledc
parents:
diff changeset
    25
            return _(column).map(function(point) {
obledc
parents:
diff changeset
    26
                return total += point;
obledc
parents:
diff changeset
    27
            });
obledc
parents:
diff changeset
    28
        }),
obledc
parents:
diff changeset
    29
        sums = _(cumulative).map(function(column) {
obledc
parents:
diff changeset
    30
            return _(column).last();
obledc
parents:
diff changeset
    31
        })
obledc
parents:
diff changeset
    32
        maxcol = _(sums).max(),
obledc
parents:
diff changeset
    33
        streamheight = height - YEARSHEIGHT,
obledc
parents:
diff changeset
    34
        streamwidth = width * (DATAEND - DATASTART) / (ENDTIME - STARTTIME),
obledc
parents:
diff changeset
    35
        yscale = (streamheight - 2 * VMARGIN) / maxcol,
obledc
parents:
diff changeset
    36
        centery = streamheight / 2,
obledc
parents:
diff changeset
    37
        xscale = streamwidth / (transp.length - 1),
obledc
parents:
diff changeset
    38
        txscale = width / (ENDTIME - STARTTIME),
obledc
parents:
diff changeset
    39
        startx = txscale * (DATASTART - STARTTIME),
obledc
parents:
diff changeset
    40
        endx = txscale * (DATAEND - STARTTIME),
obledc
parents:
diff changeset
    41
        coords = _(data.clusters).map(function(line, lineindex) {
obledc
parents:
diff changeset
    42
            return {
obledc
parents:
diff changeset
    43
                points : _(line.volumes).map(function(point, colindex) {
obledc
parents:
diff changeset
    44
                    var lowercumul = lineindex ? cumulative[colindex][lineindex - 1] : 0,
obledc
parents:
diff changeset
    45
                        uppercumul = cumulative[colindex][lineindex];
obledc
parents:
diff changeset
    46
                    return {
obledc
parents:
diff changeset
    47
                        data: point,
obledc
parents:
diff changeset
    48
                        x: startx + xscale * colindex,
obledc
parents:
diff changeset
    49
                        lowery: centery + yscale * ( ( sums[colindex] / 2 ) - lowercumul ),
obledc
parents:
diff changeset
    50
                        uppery: centery + yscale * ( ( sums[colindex] / 2 ) - uppercumul ),
obledc
parents:
diff changeset
    51
                    }
obledc
parents:
diff changeset
    52
                }),
obledc
parents:
diff changeset
    53
                id : line.id,
obledc
parents:
diff changeset
    54
                title: line.title
obledc
parents:
diff changeset
    55
            }
obledc
parents:
diff changeset
    56
        }),
obledc
parents:
diff changeset
    57
        _(coords).each(function(line) {
obledc
parents:
diff changeset
    58
            var lowerline = _(line.points).reduce(function(path, point, colindex) {
obledc
parents:
diff changeset
    59
                var res = path;
obledc
parents:
diff changeset
    60
                if (colindex) {
obledc
parents:
diff changeset
    61
                    res += "," + (point.x - CURVE * xscale) + "," + point.lowery + "," + point.x + "," + point.lowery;
obledc
parents:
diff changeset
    62
                } else {
obledc
parents:
diff changeset
    63
                    res += "M" + point.x + "," + point.lowery;
obledc
parents:
diff changeset
    64
                }
obledc
parents:
diff changeset
    65
                if (colindex < line.points.length - 1) {
obledc
parents:
diff changeset
    66
                    res += "C" + (point.x + CURVE * xscale) + "," + point.lowery;
obledc
parents:
diff changeset
    67
                }
obledc
parents:
diff changeset
    68
                return res;
obledc
parents:
diff changeset
    69
            }, "");
obledc
parents:
diff changeset
    70
            var upperline = _(line.points).reduceRight(function(path, point, colindex) {
obledc
parents:
diff changeset
    71
                var res = path;
obledc
parents:
diff changeset
    72
                if (colindex < line.points.length - 1) {
obledc
parents:
diff changeset
    73
                    res += "," + (point.x + CURVE * xscale) + "," + point.uppery + "," + point.x + "," + point.uppery;
obledc
parents:
diff changeset
    74
                } else {
obledc
parents:
diff changeset
    75
                    res += "L" + point.x + "," + point.uppery;
obledc
parents:
diff changeset
    76
                }
obledc
parents:
diff changeset
    77
                if (colindex) {
obledc
parents:
diff changeset
    78
                    res += "C" + (point.x - CURVE * xscale) + "," + point.uppery;
obledc
parents:
diff changeset
    79
                }
obledc
parents:
diff changeset
    80
                return res;
obledc
parents:
diff changeset
    81
            }, "");
obledc
parents:
diff changeset
    82
            line.path = lowerline + upperline;
obledc
parents:
diff changeset
    83
        });
obledc
parents:
diff changeset
    84
    
obledc
parents:
diff changeset
    85
    /* Drawing streamgraph*/
obledc
parents:
diff changeset
    86
   
obledc
parents:
diff changeset
    87
    $selector.empty();
obledc
parents:
diff changeset
    88
   
obledc
parents:
diff changeset
    89
    var paper = new Raphael($selector[0]);
obledc
parents:
diff changeset
    90
    
obledc
parents:
diff changeset
    91
    paper.path("M0 " + (1+centery) + "L" + width + " " + (1+centery)).attr({
obledc
parents:
diff changeset
    92
        stroke: "#000"
obledc
parents:
diff changeset
    93
    })
obledc
parents:
diff changeset
    94
    
obledc
parents:
diff changeset
    95
    _(coords).each(function(line, index) {
obledc
parents:
diff changeset
    96
        line.color = COLORS[index % COLORS.length];
obledc
parents:
diff changeset
    97
        //var hue = (parseInt(line.id)%6)/6;
obledc
parents:
diff changeset
    98
        //line.color = Raphael.hsl( hue, 1, .8 );
obledc
parents:
diff changeset
    99
        //line.highlightColor = Raphael.hsl( hue, 1, .4 );
obledc
parents:
diff changeset
   100
        line.surface = paper.path(line.path);
obledc
parents:
diff changeset
   101
        line.surface.attr({
obledc
parents:
diff changeset
   102
            stroke: "#ffffff",
obledc
parents:
diff changeset
   103
            "stroke-width": .25,
obledc
parents:
diff changeset
   104
            fill: line.color
obledc
parents:
diff changeset
   105
        });
obledc
parents:
diff changeset
   106
    });
obledc
parents:
diff changeset
   107
    
obledc
parents:
diff changeset
   108
    /* Drawing years */
obledc
parents:
diff changeset
   109
   
obledc
parents:
diff changeset
   110
    paper.path("M0," + (height - YEARSHEIGHT) + "," + width + "," + (height - YEARSHEIGHT))
obledc
parents:
diff changeset
   111
    var lastyear = ENDTIME.getFullYear();
obledc
parents:
diff changeset
   112
    for (var i = STARTTIME.getFullYear(); i <= lastyear; i++) {
obledc
parents:
diff changeset
   113
        var x = txscale * (new Date(i,0,1) - STARTTIME);
obledc
parents:
diff changeset
   114
        paper.path("M" + x + ",0," + x + "," + height);
obledc
parents:
diff changeset
   115
        var x = txscale * (new Date(i,6,1) - STARTTIME);
obledc
parents:
diff changeset
   116
        paper.text(x, height - .5 * YEARSHEIGHT, i)
obledc
parents:
diff changeset
   117
            .attr({
obledc
parents:
diff changeset
   118
                "text-anchor": "middle",
obledc
parents:
diff changeset
   119
                "font-family": "Times New Roman, serif",
obledc
parents:
diff changeset
   120
                "font-size": "14px"
obledc
parents:
diff changeset
   121
            });
obledc
parents:
diff changeset
   122
    }
obledc
parents:
diff changeset
   123
    
obledc
parents:
diff changeset
   124
    /* Drawing range window */
obledc
parents:
diff changeset
   125
    
obledc
parents:
diff changeset
   126
    var carregauche = paper.rect(width,-1,width,(2+height)),
obledc
parents:
diff changeset
   127
        carredroite = paper.rect(-width,-1,width,(2+height)),
obledc
parents:
diff changeset
   128
        attrcarres = {
obledc
parents:
diff changeset
   129
            fill: "#333333",
obledc
parents:
diff changeset
   130
            "fill-opacity": .5,
obledc
parents:
diff changeset
   131
            stroke: "#c51810"
obledc
parents:
diff changeset
   132
        };
obledc
parents:
diff changeset
   133
    carregauche.attr(attrcarres);
obledc
parents:
diff changeset
   134
    carredroite.attr(attrcarres);
obledc
parents:
diff changeset
   135
    
obledc
parents:
diff changeset
   136
    var rangerect = paper.rect(0, (height - YEARSHEIGHT), width, YEARSHEIGHT);
obledc
parents:
diff changeset
   137
    rangerect.attr({
obledc
parents:
diff changeset
   138
        fill: "#c51810",
obledc
parents:
diff changeset
   139
        stroke: "none"
obledc
parents:
diff changeset
   140
    });
obledc
parents:
diff changeset
   141
    
obledc
parents:
diff changeset
   142
    function datetext(date) {
obledc
parents:
diff changeset
   143
        var d = new Date(date),
obledc
parents:
diff changeset
   144
            m = 1+d.getMonth(),
obledc
parents:
diff changeset
   145
            y = d.getFullYear();
obledc
parents:
diff changeset
   146
        return ((m < 10 ? "0" : "") + m + "/" + y);
obledc
parents:
diff changeset
   147
    }
obledc
parents:
diff changeset
   148
    
obledc
parents:
diff changeset
   149
    var startdate = paper.text(DATEPADDING, height - .5 * YEARSHEIGHT, datetext(STARTTIME));
obledc
parents:
diff changeset
   150
    startdate.attr({
obledc
parents:
diff changeset
   151
        fill: "#ffffff",
obledc
parents:
diff changeset
   152
        "text-anchor": "start"
obledc
parents:
diff changeset
   153
    });
obledc
parents:
diff changeset
   154
    var enddate = paper.text(width - DATEPADDING, height - .5 * YEARSHEIGHT, datetext(ENDTIME));
obledc
parents:
diff changeset
   155
    enddate.attr({
obledc
parents:
diff changeset
   156
        fill: "#ffffff",
obledc
parents:
diff changeset
   157
        "text-anchor": "end"
obledc
parents:
diff changeset
   158
    });
obledc
parents:
diff changeset
   159
    
obledc
parents:
diff changeset
   160
    /* Redrawing time slices for rollover effect */
obledc
parents:
diff changeset
   161
   var mem = '';
obledc
parents:
diff changeset
   162
    _(coords).each(function(line, index) {
obledc
parents:
diff changeset
   163
		line.mousesurface = paper.path(line.path);
obledc
parents:
diff changeset
   164
		mem += '<li><a href="http://localhost:8000/documentary_files/'+user+'/'+line.id+'/0/12/'+docId+'" title="Afficher le cluster" data-cluster-id="' + line.id + '">' + line.title + '</a></li>';
obledc
parents:
diff changeset
   165
        line.mousesurface.attr({
obledc
parents:
diff changeset
   166
			stroke: "none",
obledc
parents:
diff changeset
   167
            fill: line.color,
obledc
parents:
diff changeset
   168
            opacity: .01,
obledc
parents:
diff changeset
   169
            title: line.title,
obledc
parents:
diff changeset
   170
			href: "http://localhost:8000/documentary_files/"+user+"/"+line.id+"/0/12/"+docId
obledc
parents:
diff changeset
   171
        }).mouseover(function() {
obledc
parents:
diff changeset
   172
			//alert('mousse select');
obledc
parents:
diff changeset
   173
            $("body").trigger("select-cluster", line.id);
obledc
parents:
diff changeset
   174
        }).mouseout(function() {
obledc
parents:
diff changeset
   175
			 //alert('mousse unselect');
obledc
parents:
diff changeset
   176
            $("body").trigger("unselect-cluster", line.id);
obledc
parents:
diff changeset
   177
obledc
parents:
diff changeset
   178
        });
obledc
parents:
diff changeset
   179
       // $(line.mousesurface.node).attr("data-cluster-id", line.id).parent().attr("data-cluster-id", line.id);
obledc
parents:
diff changeset
   180
    });
obledc
parents:
diff changeset
   181
obledc
parents:
diff changeset
   182
	  $(".cluster").html(mem) ;
obledc
parents:
diff changeset
   183
     /*  if (typeof (data2) != 'undefined')
obledc
parents:
diff changeset
   184
		  {
obledc
parents:
diff changeset
   185
			//  alert('dat2not null');
obledc
parents:
diff changeset
   186
			  
obledc
parents:
diff changeset
   187
			  
obledc
parents:
diff changeset
   188
			  
obledc
parents:
diff changeset
   189
			  var line = _(coords).find(function(line) 
obledc
parents:
diff changeset
   190
			  {
obledc
parents:
diff changeset
   191
           		 return line.id == 1;
obledc
parents:
diff changeset
   192
        		});
obledc
parents:
diff changeset
   193
				//alert(line.id);
obledc
parents:
diff changeset
   194
        	if (line) 
obledc
parents:
diff changeset
   195
			{
obledc
parents:
diff changeset
   196
				alert(line.id);
obledc
parents:
diff changeset
   197
            	line.surface.attr({  fill: SELECTEDCOLOR  });
obledc
parents:
diff changeset
   198
			}
obledc
parents:
diff changeset
   199
			
obledc
parents:
diff changeset
   200
			  
obledc
parents:
diff changeset
   201
		  }
obledc
parents:
diff changeset
   202
		  else
obledc
parents:
diff changeset
   203
		  {
obledc
parents:
diff changeset
   204
			  alert('dat2 null');
obledc
parents:
diff changeset
   205
		  }*/
obledc
parents:
diff changeset
   206
	  $(".actu, .cluster a, .article").hover
obledc
parents:
diff changeset
   207
	  (
obledc
parents:
diff changeset
   208
			function() {			
obledc
parents:
diff changeset
   209
				$("body").trigger("select-cluster", $(this).attr("data-cluster-id"));
obledc
parents:
diff changeset
   210
			},
obledc
parents:
diff changeset
   211
			function() {		   
obledc
parents:
diff changeset
   212
			   $("body").trigger("unselect-cluster", $(this).attr("data-cluster-id"));
obledc
parents:
diff changeset
   213
			}
obledc
parents:
diff changeset
   214
       )
obledc
parents:
diff changeset
   215
		
obledc
parents:
diff changeset
   216
    $("body").on("unselect-cluster", function(e, clusterid) {
obledc
parents:
diff changeset
   217
		$(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").removeClass("selected");
obledc
parents:
diff changeset
   218
        var line = _(coords).find(function(line) {
obledc
parents:
diff changeset
   219
            return line.id == clusterid;
obledc
parents:
diff changeset
   220
        });
obledc
parents:
diff changeset
   221
        if (line) {
obledc
parents:
diff changeset
   222
            line.surface.attr({
obledc
parents:
diff changeset
   223
                fill: line.color
obledc
parents:
diff changeset
   224
            });
obledc
parents:
diff changeset
   225
        }
obledc
parents:
diff changeset
   226
    });
obledc
parents:
diff changeset
   227
	
obledc
parents:
diff changeset
   228
	
obledc
parents:
diff changeset
   229
		
obledc
parents:
diff changeset
   230
    $("body").on("select-cluster", function(e, clusterid) {
obledc
parents:
diff changeset
   231
   	    $(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").addClass("selected");
obledc
parents:
diff changeset
   232
        var line = _(coords).find(function(line) {
obledc
parents:
diff changeset
   233
            return line.id == clusterid;
obledc
parents:
diff changeset
   234
        });
obledc
parents:
diff changeset
   235
        if (line) {
obledc
parents:
diff changeset
   236
            line.surface.attr({
obledc
parents:
diff changeset
   237
                fill: SELECTEDCOLOR //line.highlightColor
obledc
parents:
diff changeset
   238
            });
obledc
parents:
diff changeset
   239
        }
obledc
parents:
diff changeset
   240
    });
obledc
parents:
diff changeset
   241
	
obledc
parents:
diff changeset
   242
	/*$("body").on("select-cluster", function(e, clusterid)
obledc
parents:
diff changeset
   243
		 {
obledc
parents:
diff changeset
   244
            $(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").addClass("selected");
obledc
parents:
diff changeset
   245
        });
obledc
parents:
diff changeset
   246
        $("body").on("unselect-cluster", function(e, clusterid) {
obledc
parents:
diff changeset
   247
            $(".actu[data-cluster-id='" + clusterid + "'], .cluster a[data-cluster-id='" + clusterid + "'], .article[data-cluster-id='" + clusterid + "']").removeClass("selected");
obledc
parents:
diff changeset
   248
        });*/
obledc
parents:
diff changeset
   249
    
obledc
parents:
diff changeset
   250
    /* Returning a handler for slide value change */
obledc
parents:
diff changeset
   251
    
obledc
parents:
diff changeset
   252
    this.slidevalues = function(left, right) {
obledc
parents:
diff changeset
   253
        left = left || 0;
obledc
parents:
diff changeset
   254
        right = right || width;
obledc
parents:
diff changeset
   255
        carregauche.attr({x: left - width});
obledc
parents:
diff changeset
   256
        carredroite.attr({x: right});
obledc
parents:
diff changeset
   257
        startdate.attr({
obledc
parents:
diff changeset
   258
            x: DATEPADDING + left,
obledc
parents:
diff changeset
   259
            text: datetext(STARTTIME.valueOf() + left / txscale)
obledc
parents:
diff changeset
   260
        });
obledc
parents:
diff changeset
   261
        enddate.attr({
obledc
parents:
diff changeset
   262
            x: right - DATEPADDING,
obledc
parents:
diff changeset
   263
            text: datetext(STARTTIME.valueOf() + right / txscale)
obledc
parents:
diff changeset
   264
        });
obledc
parents:
diff changeset
   265
        rangerect.attr({
obledc
parents:
diff changeset
   266
            x: left,
obledc
parents:
diff changeset
   267
            width: right - left
obledc
parents:
diff changeset
   268
        });
obledc
parents:
diff changeset
   269
    }
obledc
parents:
diff changeset
   270
    
obledc
parents:
diff changeset
   271
    $("#slider-range").dragslider("values", [startx, endx]);
obledc
parents:
diff changeset
   272
    this.slidevalues(startx, endx);
obledc
parents:
diff changeset
   273
obledc
parents:
diff changeset
   274
}
obledc
parents:
diff changeset
   275
obledc
parents:
diff changeset
   276
function loadStreamgraphDoc(data,docId,user/*,data2*/) 
obledc
parents:
diff changeset
   277
{
obledc
parents:
diff changeset
   278
	$(".streamgraph").empty();
obledc
parents:
diff changeset
   279
    delete window.streamgraph;
obledc
parents:
diff changeset
   280
	;
obledc
parents:
diff changeset
   281
    //$.getJSON(url, function(data) {
obledc
parents:
diff changeset
   282
        window.streamgraph = new Streamgraph($(".streamgraph"), data,docId,user/*,data2*/);
obledc
parents:
diff changeset
   283
		 streamgraph.slidevalues.apply(streamgraph,$("#slider-range").dragslider("values"));
obledc
parents:
diff changeset
   284
obledc
parents:
diff changeset
   285
    //});
obledc
parents:
diff changeset
   286
}
obledc
parents:
diff changeset
   287
obledc
parents:
diff changeset
   288
/*$(function() {
obledc
parents:
diff changeset
   289
    loadStreamgraph("data/json_streamgraph.json");
obledc
parents:
diff changeset
   290
})*/