1 /** |
|
2 * js/generalView |
|
3 * |
|
4 * generalView basic component |
|
5 * |
|
6 */ |
|
7 |
|
8 'use strict'; |
|
9 |
|
10 var PIXI = require('pixi'); |
|
11 var randomColor = require('randomColor'); |
|
12 var rgb2hex = require('./utils'); |
|
13 |
|
14 function GeneralView(parentContainer, xInit, yInit, width, height, timeBegin, timeEnd, intervalWidth, intervalHeight, maxCellHeight, categories){ |
|
15 var _this = this; |
|
16 this.container = new PIXI.DisplayObjectContainer(); |
|
17 this.container.position.x = xInit; |
|
18 this.container.position.y = yInit; |
|
19 this.container.width = width; |
|
20 this.container.height = height; |
|
21 parentContainer.addChild(this.container); |
|
22 |
|
23 this.timeBegin = timeBegin; |
|
24 this.timeEnd = timeEnd; |
|
25 this.duration = (timeEnd - timeBegin)/1000 |
|
26 this.width = width; |
|
27 this.height = height; |
|
28 this.intervalHeight = intervalHeight; |
|
29 this.intervalWidth = intervalWidth; |
|
30 this.maxCellHeight = maxCellHeight; |
|
31 this.intervalDuration = (intervalWidth*this.duration/width); |
|
32 |
|
33 |
|
34 //Initialise the list of step |
|
35 //each cell will contain the list categories and the number of annotations per categories |
|
36 this.cells = [] |
|
37 for (var i=0; i<(width/intervalWidth) ; i++){ |
|
38 this.cells[i] = []; |
|
39 this.cells[i].x = i * this.intervalWidth; |
|
40 this.cells[i].totalAnnots = 0; |
|
41 this.cells[i].graphics = new PIXI.Graphics(); |
|
42 this.container.addChild(this.cells[i].graphics); |
|
43 this.cells[i].categories = {}; |
|
44 |
|
45 for (var category in categories){ |
|
46 this.cells[i].categories[category] = { |
|
47 "count": 0, |
|
48 "color": categories[category] |
|
49 }; |
|
50 } |
|
51 } |
|
52 // draw temp line to locate the middle of the container |
|
53 var graphics = new PIXI.Graphics(); |
|
54 graphics.beginFill(0x000000) |
|
55 .lineStyle(1, 0x000000) |
|
56 .moveTo(xInit, (-this.height/2) ) |
|
57 .lineTo(this.width, (-this.height/2)) |
|
58 .endFill(); |
|
59 this.container.addChild(graphics); |
|
60 |
|
61 //Add Annotation to the TimeLine |
|
62 this.addAnnot = function(category, time){ |
|
63 console.log(this.timeEnd); |
|
64 console.log(time); |
|
65 if (this.timeEnd < time){ |
|
66 return; |
|
67 } |
|
68 var i = Math.floor((time-this.timeBegin)/(1000*this.intervalDuration)); |
|
69 this.cells[i].categories[category].count += 1; |
|
70 this.cells[i].totalAnnots +=1; |
|
71 this.redrawCell(this.cells[i]); |
|
72 }; |
|
73 |
|
74 //Draw the cellule |
|
75 this.redrawCell = function(cell){ |
|
76 var x = cell.x; |
|
77 var y = -this.height/2; |
|
78 cell.graphics.clear(); |
|
79 |
|
80 //Check if total height is higher than Max Cell Height |
|
81 if ((cell.totalAnnots*this.intervalHeight) > this.maxCellHeight){ |
|
82 var heightStep = this.maxCellHeight/cell.totalAnnots; |
|
83 } else { |
|
84 var heightStep = this.intervalHeight; |
|
85 } |
|
86 //Draw the rect depending on the height step calculated |
|
87 for (var category in cell.categories){ |
|
88 cell.graphics.beginFill(cell.categories[category].color) |
|
89 .drawRect(x, y, this.intervalWidth, -cell.categories[category].count * heightStep) |
|
90 .endFill(); |
|
91 y -= cell.categories[category].count*heightStep; |
|
92 } |
|
93 } |
|
94 } |
|
95 |
|
96 module.exports = GeneralView; |
|