author | rougeronj |
Wed, 21 Jan 2015 20:43:05 +0100 | |
changeset 104 | 685c5ebc59d0 |
parent 103 | 123a611c7903 |
child 105 | 25ac8802c189 |
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, |
103 | 19 |
radius: 300 |
100 | 20 |
}; |
97 | 21 |
|
22 |
||
100 | 23 |
function AnnotsTimeLine(options){ |
91 | 24 |
var _this = this; |
100 | 25 |
var opts = _(options).defaults(defaultOptions).value(); |
91 | 26 |
|
100 | 27 |
this.container = new PIXI.DisplayObjectContainer(); |
28 |
this.container.x = opts.xInit; |
|
29 |
this.container.y = opts.yInit; |
|
30 |
this.container.width = opts.width; |
|
31 |
this.container.height = opts.height; |
|
91 | 32 |
|
100 | 33 |
this.timeBegin = opts.timeBegin; |
34 |
this.timeEnd = opts.timeEnd; |
|
35 |
this.duration = (this.timeEnd - this.timeBegin)/1000; |
|
36 |
this.width = opts.width; |
|
37 |
this.height = opts.height; |
|
38 |
this.intervalHeight = opts.intervalHeight; |
|
39 |
this.intervalWidth = opts.intervalWidth; |
|
40 |
this.maxCellHeight = opts.maxCellHeight; |
|
41 |
this.annotCategories = opts.annotCategories; |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
42 |
|
103 | 43 |
this.circleX = opts.circleX || (this.width/2); |
44 |
this.circleY = opts.circleY || (this.height/2); |
|
102 | 45 |
this.radius = opts.radius; |
46 |
this.perimeter = 2*Math.PI* this.radius; |
|
47 |
this.intervalDuration = (this.intervalWidth * this.duration / this.perimeter); |
|
48 |
|
|
49 |
var totalIndex = Math.floor(this.perimeter/this.intervalWidth); |
|
50 |
|
|
91 | 51 |
this.cells = [] |
102 | 52 |
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
|
53 |
this.cells[i] = []; |
103 | 54 |
this.cells[i].i = i; |
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
55 |
this.cells[i].totalAnnots = 0; |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
56 |
this.cells[i].categories = {}; |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
57 |
|
100 | 58 |
for (var category in this.annotCategories[0].colors){ |
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
59 |
this.cells[i].categories[category] = { |
103 | 60 |
"count": 0, |
100 | 61 |
"color": this.annotCategories[0].colors[category] |
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
62 |
}; |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
63 |
} |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
64 |
} |
100 | 65 |
|
66 |
var ws = opts.ws; |
|
67 |
var stageView = opts.stageView; |
|
102 | 68 |
|
103 | 69 |
//draw the base - circle and line to locate the scene |
91 | 70 |
var graphics = new PIXI.Graphics(); |
102 | 71 |
graphics.lineStyle(1, 0x000000) |
103 | 72 |
.drawCircle(this.circleX, this.circleY, this.radius - 3) |
73 |
.drawCircle(this.circleX, this.circleY, this.radius*2/3) |
|
74 |
.drawCircle(this.circleX, this.circleY, this.radius/3) |
|
102 | 75 |
.moveTo(this.circleX, this.circleY) |
76 |
.lineTo(this.circleX, this.circleY - this.radius - this.maxCellHeight - 30) |
|
77 |
.endFill() |
|
91 | 78 |
this.container.addChild(graphics); |
100 | 79 |
|
80 |
stageView.registerComponent(this); |
|
97 | 81 |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
82 |
//Add Annotation to the TimeLine |
100 | 83 |
this.addAnnot = function(data){ |
84 |
if (typeof(this.annotCategories[0].colors[data.content.category.code]) !== 'undefined'){ |
|
85 |
var annotCode = data.content.category.code; |
|
86 |
} else { |
|
87 |
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
|
88 |
} |
100 | 89 |
var annotTime = Date.parse(data.ts); |
90 |
|
|
91 |
if (this.timeEnd > Date.parse(data.ts)){ |
|
92 |
var i = Math.floor((Date.parse(data.ts)-this.timeBegin)/(1000*this.intervalDuration)); |
|
103 | 93 |
|
100 | 94 |
this.cells[i].categories[annotCode].count += 1; |
95 |
this.cells[i].totalAnnots +=1; |
|
102 | 96 |
this.redrawCell(this.cells[i], i); |
100 | 97 |
} |
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
98 |
}; |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
99 |
|
103 | 100 |
this.initGraphics = function(cell){ |
101 |
cell.graphics = new PIXI.Graphics(); |
|
102 |
cell.graphics.position.x = this.circleX + this.radius * Math.sin(cell.i*(360/totalIndex)*(Math.PI/180)); |
|
103 |
cell.graphics.position.y = this.circleY - this.radius * Math.cos(cell.i*(360/totalIndex)*(Math.PI/180)); |
|
104 |
cell.graphics.rotation = (cell.i)*(360/totalIndex)*(Math.PI/180) + (360/(totalIndex*2))*(Math.PI/180); |
|
105 |
this.container.addChild(cell.graphics); |
|
106 |
} |
|
107 |
|
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
108 |
//Draw the cellule |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
109 |
this.redrawCell = function(cell){ |
103 | 110 |
|
111 |
if (typeof(cell.graphics) === 'undefined'){ |
|
112 |
this.initGraphics(cell); |
|
113 |
} else { |
|
114 |
cell.graphics.clear(); |
|
115 |
} |
|
116 |
|
|
102 | 117 |
var y = 0; |
91 | 118 |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
119 |
//Check if total height is higher than Max Cell Height |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
120 |
if ((cell.totalAnnots*this.intervalHeight) > this.maxCellHeight){ |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
121 |
var heightStep = this.maxCellHeight/cell.totalAnnots; |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
122 |
} else { |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
123 |
var heightStep = this.intervalHeight; |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
124 |
} |
103 | 125 |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
126 |
//Draw the rect depending on the height step calculated |
100 | 127 |
for (var i=0; i< this.annotCategories[0].order.length; i++){ |
128 |
var currentCode = this.annotCategories[0].order[i]; |
|
129 |
cell.graphics.beginFill(cell.categories[currentCode].color.replace("#", "0x")) |
|
103 | 130 |
.drawRect(0, y, this.intervalWidth-1, -cell.categories[currentCode].count * heightStep) |
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
131 |
.endFill(); |
100 | 132 |
y -= cell.categories[currentCode].count*heightStep; |
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
133 |
} |
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
134 |
} |
100 | 135 |
|
136 |
this.init = function() { |
|
97 | 137 |
|
100 | 138 |
ws.message(function(data) { |
139 |
_this.addAnnot(data); |
|
140 |
}); |
|
97 | 141 |
|
100 | 142 |
}; |
143 |
|
|
144 |
this.start = function() { |
|
91 | 145 |
}; |
100 | 146 |
|
147 |
this.refresh = function() { |
|
148 |
}; |
|
149 |
|
|
150 |
this.stop = function(){ |
|
103 | 151 |
console.log(this.cells); |
100 | 152 |
}; |
153 |
|
|
154 |
return this; |
|
91 | 155 |
} |
156 |
||
98 | 157 |
module.exports = { |
103 | 158 |
AnnotsTimeLine: AnnotsTimeLine |
98 | 159 |
}; |