client/annotviz/app/js/annotstimeline.js
author rougeronj
Wed, 21 Jan 2015 12:14:16 +0100
changeset 102 cc7b06bfd574
parent 100 0d7dac03c1a0
child 103 123a611c7903
permissions -rw-r--r--
update to circle representation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
     1
/**
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
     2
* js/annotstimeline
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
     3
*
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
     4
* annotstimeline basic component
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
     5
*
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
     6
*/
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
     7
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
     8
'use strict';
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
     9
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
    10
var PIXI = require('pixi');
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    11
var _ = require('lodash');
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    12
var rgb2hex = require('./utils');
97
545803e685e0 rename components
ymh <ymh.work@gmail.com>
parents: 92
diff changeset
    13
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    14
var defaultOptions = {		
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    15
    logger: undefined,
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    16
    intervalWidth: 10,
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    17
    intervalHeight: 5,
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    18
    maxCellHeight: 200,
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    19
    radius: 300,
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    20
    circleX:500,
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    21
    circleY:500
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    22
};
97
545803e685e0 rename components
ymh <ymh.work@gmail.com>
parents: 92
diff changeset
    23
545803e685e0 rename components
ymh <ymh.work@gmail.com>
parents: 92
diff changeset
    24
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    25
function AnnotsTimeLine(options){
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
    26
    var _this = this;
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    27
    var opts = _(options).defaults(defaultOptions).value();
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
    28
    
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    29
    this.container = new PIXI.DisplayObjectContainer();
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    30
    this.container.x = opts.xInit;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    31
    this.container.y = opts.yInit;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    32
    this.container.width = opts.width;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    33
    this.container.height = opts.height;    
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
    34
    
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    35
    this.timeBegin = opts.timeBegin;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    36
    this.timeEnd = opts.timeEnd;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    37
    this.duration = (this.timeEnd - this.timeBegin)/1000;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    38
    this.width = opts.width;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    39
    this.height = opts.height;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    40
    this.intervalHeight = opts.intervalHeight;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    41
    this.intervalWidth = opts.intervalWidth;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    42
    this.maxCellHeight = opts.maxCellHeight;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    43
    this.annotCategories = opts.annotCategories;
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    44
    
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    45
    this.circleX = opts.circleX;
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    46
    this.circleY = opts.circleY;
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    47
    this.radius = opts.radius;
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    48
    this.perimeter = 2*Math.PI* this.radius;
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    49
    this.intervalDuration = (this.intervalWidth * this.duration / this.perimeter);
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    50
    
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    51
    var totalIndex = Math.floor(this.perimeter/this.intervalWidth);
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    52
    	
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
    53
    this.cells = []
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    54
    for (var i=0; i<(this.perimeter/this.intervalWidth) ; i++){
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    55
    	this.cells[i] = [];
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    56
    	this.cells[i].totalAnnots = 0;
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    57
    	this.cells[i].graphics = new PIXI.Graphics();
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    58
    	this.cells[i].graphics.position.x = this.circleX + this.radius * Math.sin(i*(360/totalIndex)*(Math.PI/180));
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    59
    	this.cells[i].graphics.position.y = this.circleY - this.radius * Math.cos(i*(360/totalIndex)*(Math.PI/180));
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    60
    	this.cells[i].graphics.rotation = (i)*(360/totalIndex)*(Math.PI/180) + (360/(totalIndex*2))*(Math.PI/180);
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    61
    	this.container.addChild(this.cells[i].graphics);
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    62
    	this.cells[i].categories = {};
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    63
    	
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    64
    	for (var category in this.annotCategories[0].colors){
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    65
    		this.cells[i].categories[category] = {
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    66
				"count": 1,
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    67
				"color": this.annotCategories[0].colors[category]
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    68
    		};
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    69
    	}
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    70
    }
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    71
    
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    72
    var ws = opts.ws;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    73
    var stageView = opts.stageView;
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    74
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
    75
    var graphics = new PIXI.Graphics();
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    76
    graphics.lineStyle(1, 0x000000)
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    77
    	.drawCircle(this.circleX, this.circleY, this.radius)
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    78
    	.moveTo(this.circleX, this.circleY)
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    79
    	.lineTo(this.circleX, this.circleY - this.radius - this.maxCellHeight - 30)
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    80
    	.endFill()
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
    81
    this.container.addChild(graphics);
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    82
    
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    83
    stageView.registerComponent(this);
97
545803e685e0 rename components
ymh <ymh.work@gmail.com>
parents: 92
diff changeset
    84
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    85
    //Add Annotation to the TimeLine
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    86
    this.addAnnot = function(data){
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    87
    	if (typeof(this.annotCategories[0].colors[data.content.category.code]) !== 'undefined'){
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    88
    		var annotCode = data.content.category.code;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    89
    	} else {
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    90
    		var annotCode = this.annotCategories[0].order[this.annotCategories[0].order.length -1];
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
    91
    	}
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    92
    	var annotTime = Date.parse(data.ts);
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    93
    	
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    94
    	if (this.timeEnd > Date.parse(data.ts)){
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    95
	    	var i = Math.floor((Date.parse(data.ts)-this.timeBegin)/(1000*this.intervalDuration));
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    96
			this.cells[i].categories[annotCode].count += 1;
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    97
			this.cells[i].totalAnnots +=1;
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
    98
			this.redrawCell(this.cells[i], i);
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
    99
    	}
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   100
    };
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   101
    
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   102
    //Draw the cellule
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   103
    this.redrawCell = function(cell){
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
   104
    	var x = 0; 
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
   105
    	var y = 0;
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   106
    	cell.graphics.clear();
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
   107
    	
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   108
    	//Check if total height is higher than Max Cell Height
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   109
    	if ((cell.totalAnnots*this.intervalHeight) > this.maxCellHeight){
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   110
    		var heightStep = this.maxCellHeight/cell.totalAnnots;
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   111
    	} else {
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   112
    		var heightStep = this.intervalHeight;
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   113
    	}
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   114
    	//Draw the rect depending on the height step calculated
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   115
    	for (var i=0; i< this.annotCategories[0].order.length; i++){
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   116
    		var currentCode = this.annotCategories[0].order[i];
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   117
			cell.graphics.beginFill(cell.categories[currentCode].color.replace("#", "0x"))
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
   118
    			.drawRect(0, y, this.intervalWidth, -cell.categories[currentCode].count * heightStep)
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   119
    			.endFill();
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   120
    		y -= cell.categories[currentCode].count*heightStep;
99
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   121
    	}
9d968fbcaa2a Add General Time Line + minor change in annotsRoll
rougeronj
parents: 92
diff changeset
   122
    }
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   123
    
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   124
    this.init = function() {
97
545803e685e0 rename components
ymh <ymh.work@gmail.com>
parents: 92
diff changeset
   125
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   126
    	ws.message(function(data) {
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   127
            _this.addAnnot(data);
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   128
        });
97
545803e685e0 rename components
ymh <ymh.work@gmail.com>
parents: 92
diff changeset
   129
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   130
    };
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   131
    
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   132
    this.start = function() {
102
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
   133
    	for (var i=0; i<(this.perimeter/this.intervalWidth) ; i++){
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
   134
    		this.redrawCell(this.cells[i]);
cc7b06bfd574 update to circle representation
rougeronj
parents: 100
diff changeset
   135
    	}
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
   136
    };
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   137
    
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   138
    this.refresh = function() {
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   139
    };
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   140
    
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   141
    this.stop = function(){
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   142
    };
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   143
    
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   144
    return this;
91
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
   145
}
09a9074ea7b0 Add general View fonction for main representation
rougeronj
parents:
diff changeset
   146
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   147
module.exports = {
100
0d7dac03c1a0 Merge with 72d767c5142df7fc6387215096321295fbaaa73d
rougeronj
parents: 99 98
diff changeset
   148
		AnnotsTimeLine: AnnotsTimeLine
98
72d767c5142d refactor and make work annotsroll
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   149
};