author | ymh <ymh.work@gmail.com> |
Fri, 23 Jan 2015 11:28:25 +0100 | |
changeset 137 | 750d9076a4a8 |
parent 135 | d3066fa80a81 |
child 144 | 1762372184ae |
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'); |
|
105
25ac8802c189
Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents:
103
diff
changeset
|
11 |
var Utils = require('./utils.js'); |
100 | 12 |
var _ = require('lodash'); |
97 | 13 |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
14 |
var defaultOptions = { |
100 | 15 |
logger: undefined, |
16 |
intervalWidth: 10, |
|
17 |
intervalHeight: 5, |
|
102 | 18 |
maxCellHeight: 200, |
124 | 19 |
radius: 300, |
20 |
serverUrl: 'http://127.0.0.1:8080', |
|
21 |
channel: 'ANNOT', |
|
22 |
maxPages: 1000, |
|
128
9f2334598088
add archive variable to desactivate the socket listening for the static timeLines
rougeronj
parents:
127
diff
changeset
|
23 |
showClockGraphics: true, |
9f2334598088
add archive variable to desactivate the socket listening for the static timeLines
rougeronj
parents:
127
diff
changeset
|
24 |
archive: false |
100 | 25 |
}; |
97 | 26 |
|
27 |
||
100 | 28 |
function AnnotsTimeLine(options){ |
91 | 29 |
var _this = this; |
100 | 30 |
var opts = _(options).defaults(defaultOptions).value(); |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
31 |
|
100 | 32 |
this.container = new PIXI.DisplayObjectContainer(); |
33 |
this.container.x = opts.xInit; |
|
34 |
this.container.y = opts.yInit; |
|
35 |
this.container.width = opts.width; |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
36 |
this.container.height = opts.height; |
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
37 |
|
100 | 38 |
this.timeBegin = opts.timeBegin; |
39 |
this.timeEnd = opts.timeEnd; |
|
40 |
this.duration = (this.timeEnd - this.timeBegin)/1000; |
|
41 |
this.width = opts.width; |
|
42 |
this.height = opts.height; |
|
43 |
this.intervalHeight = opts.intervalHeight; |
|
44 |
this.intervalWidth = opts.intervalWidth; |
|
45 |
this.maxCellHeight = opts.maxCellHeight; |
|
46 |
this.annotCategories = opts.annotCategories; |
|
131 | 47 |
this.startTs = options.startTs || Date.now(); |
126
13d9a532a0a7
Add timeline of the 2nd day - take out from the clock annotstimeline and put it in annotsvizview - make the clocks graphics optional through showClockGraphics
rougeronj
parents:
123
diff
changeset
|
48 |
this.showClockGraphics = opts.showClockGraphics; |
128
9f2334598088
add archive variable to desactivate the socket listening for the static timeLines
rougeronj
parents:
127
diff
changeset
|
49 |
this.archive = opts.archive; |
131 | 50 |
|
103 | 51 |
this.circleX = opts.circleX || (this.width/2); |
52 |
this.circleY = opts.circleY || (this.height/2); |
|
102 | 53 |
this.radius = opts.radius; |
126
13d9a532a0a7
Add timeline of the 2nd day - take out from the clock annotstimeline and put it in annotsvizview - make the clocks graphics optional through showClockGraphics
rougeronj
parents:
123
diff
changeset
|
54 |
var perimeter = 2*Math.PI* this.radius; |
13d9a532a0a7
Add timeline of the 2nd day - take out from the clock annotstimeline and put it in annotsvizview - make the clocks graphics optional through showClockGraphics
rougeronj
parents:
123
diff
changeset
|
55 |
this.intervalDuration = (this.intervalWidth * this.duration / perimeter); |
131 | 56 |
|
124 | 57 |
var channel = opts.channel; |
58 |
var eventCode = opts.eventCode; |
|
59 |
var serverUrl = opts.serverUrl; |
|
60 |
var maxPages = opts.maxPages; |
|
131 | 61 |
|
126
13d9a532a0a7
Add timeline of the 2nd day - take out from the clock annotstimeline and put it in annotsvizview - make the clocks graphics optional through showClockGraphics
rougeronj
parents:
123
diff
changeset
|
62 |
var totalIndex = Math.floor( perimeter/this.intervalWidth); |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
63 |
|
131 | 64 |
this.cells = []; |
126
13d9a532a0a7
Add timeline of the 2nd day - take out from the clock annotstimeline and put it in annotsvizview - make the clocks graphics optional through showClockGraphics
rougeronj
parents:
123
diff
changeset
|
65 |
for (var i=0; i<(perimeter/this.intervalWidth) ; i++){ |
131 | 66 |
this.cells[i] = []; |
67 |
this.cells[i].i = i; |
|
68 |
this.cells[i].totalAnnots = 0; |
|
69 |
this.cells[i].categories = {}; |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
70 |
} |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
71 |
|
100 | 72 |
var ws = opts.ws; |
73 |
var stageView = opts.stageView; |
|
102 | 74 |
|
103 | 75 |
//draw the base - circle and line to locate the scene |
91 | 76 |
var graphics = new PIXI.Graphics(); |
105
25ac8802c189
Improve interface + Add horizontal pianoroll to annotsvizview
rougeronj
parents:
103
diff
changeset
|
77 |
graphics.lineStyle(2, 0x646464) |
131 | 78 |
.drawCircle(this.circleX, this.circleY, this.radius - 3) |
79 |
.endFill(); |
|
91 | 80 |
this.container.addChild(graphics); |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
81 |
|
100 | 82 |
stageView.registerComponent(this); |
97 | 83 |
|
131 | 84 |
var loadArchives = function() { |
124 | 85 |
//start timeBegin end startTime |
86 |
//query -> need channel + eventCode |
|
87 |
//iterate over data fill cells |
|
88 |
var startTs = _this.timeBegin; |
|
137 | 89 |
var endTs = Math.min(_this.timeEnd,_this.startTs); |
90 |
||
91 |
console.log("START TS",new Date(startTs).toISOString()); |
|
92 |
console.log("END TS",new Date(endTs).toISOString()); |
|
124 | 93 |
|
94 |
var url = serverUrl + '/p/api/v1/annotation'; |
|
95 |
var filters = [ |
|
96 |
{ name: 'ts', op: '>', val: new Date(startTs).toISOString()}, //start |
|
97 |
{ name: 'ts', op: '<=', val: new Date(endTs).toISOString()}, //end |
|
98 |
{ name: 'channel', op: '==', val: channel}, //channel |
|
99 |
{ name: 'event_code', op: '==', val: eventCode} //eventcode |
|
100 |
]; |
|
101 |
||
102 |
url = url + '?q=' + JSON.stringify({filters:filters}); |
|
103 |
||
104 |
var totalPage = 1; |
|
105 |
var currentPage = 1; |
|
106 |
||
107 |
var processResFunction = function(res) { |
|
137 | 108 |
//console.log("RES archive", res); |
124 | 109 |
if(res) { |
110 |
var data = res.target.json; |
|
111 |
/*jshint -W069 */ |
|
112 |
totalPage = Math.min(maxPages,parseInt(data['total_pages'])); |
|
113 |
data.objects.forEach(function(annotation) { |
|
114 |
_this.addAnnot(annotation); |
|
115 |
}); |
|
116 |
} |
|
117 |
if(currentPage <= totalPage) { |
|
118 |
var jsonLoader = new PIXI.JsonLoader(url+'&page='+currentPage, true); |
|
119 |
jsonLoader.on('loaded', processResFunction); |
|
120 |
jsonLoader.load(); |
|
121 |
currentPage++; |
|
122 |
} |
|
123 |
}; |
|
124 |
processResFunction(); |
|
125 |
||
126 |
}; |
|
127 |
||
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
128 |
//Add Annotation to the TimeLine |
100 | 129 |
this.addAnnot = function(data){ |
124 | 130 |
|
131 | 131 |
var ts = Date.parse(data.ts); |
132 |
var colorsDef; |
|
133 |
_(this.annotCategories).eachRight(function(cdef) { |
|
123
d8ef840eaf68
get the correct json category depending on the timestamp
rougeronj
parents:
121
diff
changeset
|
134 |
if(cdef.ts < ts) { |
d8ef840eaf68
get the correct json category depending on the timestamp
rougeronj
parents:
121
diff
changeset
|
135 |
colorsDef = cdef; |
d8ef840eaf68
get the correct json category depending on the timestamp
rougeronj
parents:
121
diff
changeset
|
136 |
return false; |
d8ef840eaf68
get the correct json category depending on the timestamp
rougeronj
parents:
121
diff
changeset
|
137 |
} |
d8ef840eaf68
get the correct json category depending on the timestamp
rougeronj
parents:
121
diff
changeset
|
138 |
}); |
124 | 139 |
|
137 | 140 |
if(!colorsDef) { |
141 |
return; |
|
142 |
} |
|
143 |
||
131 | 144 |
if (this.timeEnd > ts){ |
145 |
var i = Math.floor((ts - this.timeBegin)/(1000*this.intervalDuration)); |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
146 |
|
131 | 147 |
if (typeof(this.cells[i].graphics) === 'undefined'){ |
148 |
this.initCell(this.cells[i], colorsDef); |
|
149 |
} |
|
124 | 150 |
|
131 | 151 |
var annotCode; |
152 |
if (typeof(colorsDef.colors[data.content.category.code]) !== 'undefined'){ |
|
153 |
annotCode = data.content.category.code; |
|
154 |
} else { |
|
155 |
annotCode = 'default'; |
|
156 |
} |
|
124 | 157 |
|
131 | 158 |
this.cells[i].categories[annotCode].count += 1; |
159 |
this.cells[i].totalAnnots +=1; |
|
160 |
this.redrawCell(this.cells[i], colorsDef); |
|
161 |
} |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
162 |
}; |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
163 |
|
126
13d9a532a0a7
Add timeline of the 2nd day - take out from the clock annotstimeline and put it in annotsvizview - make the clocks graphics optional through showClockGraphics
rougeronj
parents:
123
diff
changeset
|
164 |
this.initClockGraphics = function() { |
131 | 165 |
var tBeg = new PIXI.Text(Utils.formatTime(this.timeBegin), { font: '12pt Gothic Standard', fill: '#646464' }); |
166 |
tBeg.x = this.circleX + 15; |
|
167 |
tBeg.y = this.circleY - this.radius - this.maxCellHeight - 10; |
|
168 |
this.container.addChild(tBeg); |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
169 |
|
131 | 170 |
var tEnd = new PIXI.Text(Utils.formatTime(this.timeEnd), { font: '12pt Gothic Standard', fill: '#646464' }); |
171 |
tEnd.x = this.circleX - 15 - tEnd.width; |
|
172 |
tEnd.y = this.circleY - this.radius - this.maxCellHeight - 10; |
|
173 |
this.container.addChild(tEnd); |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
174 |
|
131 | 175 |
var t15 = new PIXI.Text(Utils.formatTime(((this.timeEnd - this.timeBegin)/4) + this.timeBegin), { font: '12pt Gothic Standard', fill: '#646464' }); |
176 |
t15.x = this.circleX + this.radius + this.maxCellHeight + 10 ; |
|
177 |
t15.y = this.circleY - t15.height; |
|
178 |
t15.rotation = Math.PI /2; |
|
179 |
this.container.addChild(t15); |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
180 |
|
131 | 181 |
var t30 = new PIXI.Text(Utils.formatTime(((this.timeEnd - this.timeBegin)/2) + this.timeBegin), { font: '12pt Gothic Standard', fill: '#646464' }); |
182 |
t30.x = this.circleX - t30.width/2; |
|
183 |
t30.y = this.circleY + this.radius + this.maxCellHeight - 2; |
|
184 |
this.container.addChild(t30); |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
185 |
|
131 | 186 |
var t45 = new PIXI.Text(Utils.formatTime(((this.timeEnd - this.timeBegin)*3/4) + this.timeBegin), { font: '12pt Gothic Standard', fill: '#646464' }); |
187 |
t45.x = this.circleX - this.radius - this.maxCellHeight - 10 ; |
|
188 |
t45.y = this.circleY + t15.height; |
|
189 |
t45.rotation = -Math.PI/2; |
|
190 |
this.container.addChild(t45); |
|
191 |
||
192 |
var lineV = new PIXI.Graphics(); |
|
193 |
lineV.lineStyle(1, 0x646464) |
|
194 |
.moveTo(this.circleX, this.circleY - (this.radius/3)/2) |
|
195 |
.lineTo(this.circleX, this.circleY - this.radius - this.maxCellHeight - 10) |
|
196 |
.endFill(); |
|
197 |
this.container.addChild(lineV); |
|
198 |
}; |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
199 |
|
99
9d968fbcaa2a
Add General Time Line + minor change in annotsRoll
rougeronj
parents:
92
diff
changeset
|
200 |
//Draw the cellule |
123
d8ef840eaf68
get the correct json category depending on the timestamp
rougeronj
parents:
121
diff
changeset
|
201 |
this.redrawCell = function(cell, colorsDef){ |
131 | 202 |
var y = 0; |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
203 |
|
124 | 204 |
//Check if total height is higher than Max Cell Height |
205 |
var heightStep; |
|
206 |
if ((cell.totalAnnots*this.intervalHeight) > this.maxCellHeight){ |
|
207 |
heightStep = this.maxCellHeight/cell.totalAnnots; |
|
208 |
} else { |
|
209 |
heightStep = this.intervalHeight; |
|
210 |
} |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
211 |
|
131 | 212 |
//Draw the rect depending on the height step calculated |
213 |
for (var i=0; i< colorsDef.order.length; i++){ |
|
214 |
var currentCode = colorsDef.order[i]; |
|
215 |
cell.graphics.beginFill(cell.categories[currentCode].color.replace('#', '0x')) |
|
216 |
.drawRect(0, y, this.intervalWidth-1, -cell.categories[currentCode].count * heightStep) |
|
217 |
.endFill(); |
|
218 |
y -= cell.categories[currentCode].count*heightStep; |
|
219 |
} |
|
220 |
}; |
|
124 | 221 |
|
123
d8ef840eaf68
get the correct json category depending on the timestamp
rougeronj
parents:
121
diff
changeset
|
222 |
this.initCell = function(cell, colorsDef){ |
131 | 223 |
cell.graphics = new PIXI.Graphics(); |
224 |
cell.graphics.position.x = this.circleX + this.radius * Math.sin(cell.i*(360/totalIndex)*(Math.PI/180)); |
|
225 |
cell.graphics.position.y = this.circleY - this.radius * Math.cos(cell.i*(360/totalIndex)*(Math.PI/180)); |
|
226 |
cell.graphics.rotation = (cell.i)*(360/totalIndex)*(Math.PI/180) + (360/(totalIndex*2))*(Math.PI/180); |
|
227 |
this.container.addChild(cell.graphics); |
|
124 | 228 |
|
131 | 229 |
for (var category in colorsDef.colors){ |
230 |
cell.categories[category] = { |
|
231 |
'count': 0, |
|
232 |
'color': colorsDef.colors[category] |
|
233 |
}; |
|
234 |
} |
|
235 |
if (typeof(cell.categories['default']) === 'undefined'){ |
|
236 |
cell.categories['default'] = { |
|
237 |
'count': 0, |
|
238 |
'color': colorsDef.defaultColor |
|
239 |
}; |
|
240 |
} |
|
241 |
}; |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
242 |
|
100 | 243 |
this.init = function() { |
131 | 244 |
if (!this.archive){ |
245 |
ws.message(function(data) { |
|
246 |
_this.addAnnot(data); |
|
247 |
}); |
|
248 |
} |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
249 |
|
131 | 250 |
if (this.showClockGraphics){this.initClockGraphics();} |
100 | 251 |
}; |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
252 |
|
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
253 |
|
100 | 254 |
this.start = function() { |
131 | 255 |
this.startTs = Date.now(); |
124 | 256 |
loadArchives(); |
91 | 257 |
}; |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
258 |
|
100 | 259 |
this.refresh = function() { |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
260 |
|
100 | 261 |
}; |
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
262 |
|
100 | 263 |
this.stop = function(){ |
264 |
}; |
|
109
8546e2181a73
correct color codes sent by client
ymh <ymh.work@gmail.com>
parents:
105
diff
changeset
|
265 |
|
100 | 266 |
return this; |
91 | 267 |
} |
268 |
||
98 | 269 |
module.exports = { |
131 | 270 |
AnnotsTimeLine: AnnotsTimeLine |
98 | 271 |
}; |