client/annotviz/app/js/annotstimeline.js
changeset 100 0d7dac03c1a0
parent 99 9d968fbcaa2a
parent 98 72d767c5142d
child 102 cc7b06bfd574
equal deleted inserted replaced
99:9d968fbcaa2a 100:0d7dac03c1a0
       
     1 /**
       
     2 * js/annotstimeline
       
     3 *
       
     4 * annotstimeline basic component
       
     5 *
       
     6 */
       
     7 
       
     8 'use strict';
       
     9 
       
    10 var PIXI = require('pixi');
       
    11 var _ = require('lodash');
       
    12 var rgb2hex = require('./utils');
       
    13 
       
    14 var defaultOptions = {		
       
    15     logger: undefined,
       
    16     intervalWidth: 10,
       
    17     intervalHeight: 5,
       
    18     maxCellHeight: 20,
       
    19 };
       
    20 
       
    21 
       
    22 function AnnotsTimeLine(options){
       
    23     var _this = this;
       
    24     var opts = _(options).defaults(defaultOptions).value();
       
    25     
       
    26     this.container = new PIXI.DisplayObjectContainer();
       
    27     this.container.x = opts.xInit;
       
    28     this.container.y = opts.yInit;
       
    29     this.container.width = opts.width;
       
    30     this.container.height = opts.height;    
       
    31     
       
    32     this.timeBegin = opts.timeBegin;
       
    33     this.timeEnd = opts.timeEnd;
       
    34     this.duration = (this.timeEnd - this.timeBegin)/1000;
       
    35     this.width = opts.width;
       
    36     this.height = opts.height;
       
    37     this.intervalHeight = opts.intervalHeight;
       
    38     this.intervalWidth = opts.intervalWidth;
       
    39     this.maxCellHeight = opts.maxCellHeight;
       
    40     this.intervalDuration = (this.intervalWidth * this.duration / this.width);
       
    41     this.annotCategories = opts.annotCategories;
       
    42     
       
    43     this.cells = []
       
    44     for (var i=0; i<(this.width/this.intervalWidth) ; i++){
       
    45     	this.cells[i] = [];
       
    46     	this.cells[i].x = i * this.intervalWidth;
       
    47     	this.cells[i].totalAnnots = 0;
       
    48     	this.cells[i].graphics = new PIXI.Graphics();
       
    49     	this.container.addChild(this.cells[i].graphics);
       
    50     	this.cells[i].categories = {};
       
    51     	
       
    52     	for (var category in this.annotCategories[0].colors){
       
    53     		this.cells[i].categories[category] = {
       
    54 				"count": 0,
       
    55 				"color": this.annotCategories[0].colors[category]
       
    56     		};
       
    57     	}
       
    58     }
       
    59     
       
    60     var ws = opts.ws;
       
    61     var stageView = opts.stageView;
       
    62     // draw temp line to locate the middle of the container
       
    63     var graphics = new PIXI.Graphics();
       
    64     graphics.beginFill(0x000000)
       
    65     	.lineStyle(1, 0x000000)
       
    66     	.moveTo(this.container.x, (this.height/2))
       
    67     	.lineTo(this.width, (this.height/2))
       
    68     	.endFill();
       
    69     this.container.addChild(graphics);
       
    70     
       
    71     stageView.registerComponent(this);
       
    72 
       
    73     //Add Annotation to the TimeLine
       
    74     this.addAnnot = function(data){
       
    75     	if (typeof(this.annotCategories[0].colors[data.content.category.code]) !== 'undefined'){
       
    76     		var annotCode = data.content.category.code;
       
    77     	} else {
       
    78     		var annotCode = this.annotCategories[0].order[this.annotCategories[0].order.length -1];
       
    79     	}
       
    80     	var annotTime = Date.parse(data.ts);
       
    81     	
       
    82     	if (this.timeEnd > Date.parse(data.ts)){
       
    83 	    	var i = Math.floor((Date.parse(data.ts)-this.timeBegin)/(1000*this.intervalDuration));
       
    84 			this.cells[i].categories[annotCode].count += 1;
       
    85 			this.cells[i].totalAnnots +=1;
       
    86 			this.redrawCell(this.cells[i]);
       
    87     	}
       
    88     };
       
    89     
       
    90     //Draw the cellule
       
    91     this.redrawCell = function(cell){
       
    92     	var x = cell.x; 
       
    93     	var y = this.height/2;
       
    94     	cell.graphics.clear();
       
    95     	
       
    96     	//Check if total height is higher than Max Cell Height
       
    97     	if ((cell.totalAnnots*this.intervalHeight) > this.maxCellHeight){
       
    98     		var heightStep = this.maxCellHeight/cell.totalAnnots;
       
    99     	} else {
       
   100     		var heightStep = this.intervalHeight;
       
   101     	}
       
   102     	//Draw the rect depending on the height step calculated
       
   103     	for (var i=0; i< this.annotCategories[0].order.length; i++){
       
   104     		var currentCode = this.annotCategories[0].order[i];
       
   105 			cell.graphics.beginFill(cell.categories[currentCode].color.replace("#", "0x"))
       
   106     			.drawRect(x, y, this.intervalWidth, -cell.categories[currentCode].count * heightStep)
       
   107     			.endFill();
       
   108     		y -= cell.categories[currentCode].count*heightStep;
       
   109     	}
       
   110     }
       
   111     
       
   112     this.init = function() {
       
   113 
       
   114     	ws.message(function(data) {
       
   115             _this.addAnnot(data);
       
   116         });
       
   117 
       
   118     };
       
   119     
       
   120     this.start = function() {
       
   121     };
       
   122     
       
   123     this.refresh = function() {
       
   124     };
       
   125     
       
   126     this.stop = function(){
       
   127     };
       
   128     
       
   129     return this;
       
   130 }
       
   131 
       
   132 module.exports = {
       
   133 		AnnotsTimeLine: AnnotsTimeLine
       
   134 };