author | rougeronj |
Wed, 21 Jan 2015 12:14:16 +0100 | |
changeset 102 | cc7b06bfd574 |
parent 100 | 0d7dac03c1a0 |
child 103 | 123a611c7903 |
permissions | -rw-r--r-- |
91 | 1 |
/** |
100 | 2 |
* js/annotstimeline |
91 | 3 |
* |
100 | 4 |
* annotstimeline basic component |
91 | 5 |
* |
6 |
*/ |
|
7 |
||
8 |
'use strict'; |
|
9 |
||
10 |
var PIXI = require('pixi'); |
|
100 | 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 | 13 |
|
100 | 14 |
var defaultOptions = { |
15 |
logger: undefined, |
|
16 |
intervalWidth: 10, |
|
17 |
intervalHeight: 5, |
|
102 | 18 |
maxCellHeight: 200, |
19 |
radius: 300, |
|
20 |
circleX:500, |
|
21 |
circleY:500 |
|
100 | 22 |
}; |
97 | 23 |
|
24 |
||
100 | 25 |
function AnnotsTimeLine(options){ |
91 | 26 |
var _this = this; |
100 | 27 |
var opts = _(options).defaults(defaultOptions).value(); |
91 | 28 |
|
100 | 29 |
this.container = new PIXI.DisplayObjectContainer(); |
30 |
this.container.x = opts.xInit; |
|
31 |
this.container.y = opts.yInit; |
|
32 |
this.container.width = opts.width; |
|
33 |
this.container.height = opts.height; |
|
91 | 34 |
|
100 | 35 |
this.timeBegin = opts.timeBegin; |
36 |
this.timeEnd = opts.timeEnd; |
|
37 |
this.duration = (this.timeEnd - this.timeBegin)/1000; |
|
38 |
this.width = opts.width; |
|
39 |
this.height = opts.height; |
|
40 |
this.intervalHeight = opts.intervalHeight; |
|
41 |
this.intervalWidth = opts.intervalWidth; |
|
42 |
this.maxCellHeight = opts.maxCellHeight; |
|
43 |
this.annotCategories = opts.annotCategories; |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
44 |
|
102 | 45 |
this.circleX = opts.circleX; |
46 |
this.circleY = opts.circleY; |
|
47 |
this.radius = opts.radius; |
|
48 |
this.perimeter = 2*Math.PI* this.radius; |
|
49 |
this.intervalDuration = (this.intervalWidth * this.duration / this.perimeter); |
|
50 |
|
|
51 |
var totalIndex = Math.floor(this.perimeter/this.intervalWidth); |
|
52 |
|
|
91 | 53 |
this.cells = [] |
102 | 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 | 58 |
this.cells[i].graphics.position.x = this.circleX + this.radius * Math.sin(i*(360/totalIndex)*(Math.PI/180)); |
59 |
this.cells[i].graphics.position.y = this.circleY - this.radius * Math.cos(i*(360/totalIndex)*(Math.PI/180)); |
|
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 | 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 | 66 |
"count": 1, |
100 | 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 | 71 |
|
72 |
var ws = opts.ws; |
|
73 |
var stageView = opts.stageView; |
|
102 | 74 |
|
91 | 75 |
var graphics = new PIXI.Graphics(); |
102 | 76 |
graphics.lineStyle(1, 0x000000) |
77 |
.drawCircle(this.circleX, this.circleY, this.radius) |
|
78 |
.moveTo(this.circleX, this.circleY) |
|
79 |
.lineTo(this.circleX, this.circleY - this.radius - this.maxCellHeight - 30) |
|
80 |
.endFill() |
|
91 | 81 |
this.container.addChild(graphics); |
100 | 82 |
|
83 |
stageView.registerComponent(this); |
|
97 | 84 |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
85 |
//Add Annotation to the TimeLine |
100 | 86 |
this.addAnnot = function(data){ |
87 |
if (typeof(this.annotCategories[0].colors[data.content.category.code]) !== 'undefined'){ |
|
88 |
var annotCode = data.content.category.code; |
|
89 |
} else { |
|
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 | 92 |
var annotTime = Date.parse(data.ts); |
93 |
|
|
94 |
if (this.timeEnd > Date.parse(data.ts)){ |
|
95 |
var i = Math.floor((Date.parse(data.ts)-this.timeBegin)/(1000*this.intervalDuration)); |
|
96 |
this.cells[i].categories[annotCode].count += 1; |
|
97 |
this.cells[i].totalAnnots +=1; |
|
102 | 98 |
this.redrawCell(this.cells[i], i); |
100 | 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 | 104 |
var x = 0; |
105 |
var y = 0; |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
106 |
cell.graphics.clear(); |
91 | 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 | 115 |
for (var i=0; i< this.annotCategories[0].order.length; i++){ |
116 |
var currentCode = this.annotCategories[0].order[i]; |
|
117 |
cell.graphics.beginFill(cell.categories[currentCode].color.replace("#", "0x")) |
|
102 | 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 | 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 | 123 |
|
124 |
this.init = function() { |
|
97 | 125 |
|
100 | 126 |
ws.message(function(data) { |
127 |
_this.addAnnot(data); |
|
128 |
}); |
|
97 | 129 |
|
100 | 130 |
}; |
131 |
|
|
132 |
this.start = function() { |
|
102 | 133 |
for (var i=0; i<(this.perimeter/this.intervalWidth) ; i++){ |
134 |
this.redrawCell(this.cells[i]); |
|
135 |
} |
|
91 | 136 |
}; |
100 | 137 |
|
138 |
this.refresh = function() { |
|
139 |
}; |
|
140 |
|
|
141 |
this.stop = function(){ |
|
142 |
}; |
|
143 |
|
|
144 |
return this; |
|
91 | 145 |
} |
146 |
||
98 | 147 |
module.exports = { |
100 | 148 |
AnnotsTimeLine: AnnotsTimeLine |
98 | 149 |
}; |