-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bar.js
90 lines (80 loc) · 2.03 KB
/
Bar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class Bar{
constructor(bsWidth,hMax,position,easing,vals,legends,color){
colorMode(HSB);
this.position = position;
this.bsWidth = bsWidth;
this.col = color;
console.log(this.col);
this.nbSet = vals.length;
this.labels = []; //Values
this.legends = legends;
this.posLabels = []; // Table of the position of each labels
this.vals = vals;
this.hMax = hMax;
this.shiftx = 5;
this.barheights=[];
let h,s,b;
this.bWidth = 0;
this.parseColor();
this.calcWidths(this.bsWidth);
this.initBar();
this.label();
this.mapVals();
}
initBar(){
for(var i=0;i<this.nbSet;i++){
this.barheights[i] = 0;
}
}
parseColor(){
this.h = hue(this.col);
this.s = saturation(this.col);
this.b = brightness(this.col);
console.log("h="+this.h+" -s="+this.s+" -b="+this.b);
}
calcWidths(bsWidth){
this.bWidth = this.bsWidth/this.nbSet;
return this.bWidth;
}
mapVals(){
let maxVal = max(this.vals);
for(var i=0;i<this.nbSet;i++){
this.vals[i] = map(this.vals[i],0,maxVal,0,this.hMax);
}
}
label(){
for(var i=0;i<this.nbSet;i++){
this.labels[i]=round(this.vals[i]);
}
}
update(){
// EASING
for(var i=0;i<this.nbSet;i++){
this.barheights[i] += (this.vals[i]-this.barheights[i])*easing;
}
}
wlegends(i,posx){ //Write legends at the right place
text(this.legends[i],posx,20);
}
display(){
textFont("staatliches");
textAlign(CENTER,CENTER);
push();
translate(this.position.x-(this.bsWidth/2),this.position.y);
this.update();
for(var i=0;i<this.nbSet;i++){
let bright = this.b-((this.b/this.nbSet)*i);
fill(this.h,this.s,bright);
// fill(this.h,this.s,this.b);
rect((i*this.bWidth)+(this.shiftx*i)/2,0,this.bWidth,-this.barheights[i]);
textSize(this.bWidth/3.5);
//text(this.labels[i],((i*this.bWidth)+(this.shiftx*i))+(this.bWidth/2)-(this.shiftx*i/2),20);
let posx = ((i*this.bWidth)+(this.shiftx*i))+(this.bWidth/2)-(this.shiftx*i/2);
//Labels
text(this.labels[i],posx,-this.barheights[i]-20);
//Legends
this.wlegends(i,posx);
}
pop();
}
}