-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubble.js
204 lines (149 loc) · 4.26 KB
/
bubble.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
function BubbleGraph(){
var data;
var bubbles = [];
let years
let yearString
let marginSize = 35
// Name for the visualisation to appear in the menu bar.
this.name = 'Household consumption of different food items by year';
// Each visualisation must have a unique ID with no special
// characters.
this.id = 'Household consumption of different food items by year';
// Title to display above the plot.
this.title = 'Household consumption of different food items by year';
this.layout = {
marginSize: marginSize,
// Locations of margin positions. Left and bottom have double margin
// size due to axis and tick labels.
leftMargin: marginSize * 2,
rightMargin: width - marginSize,
topMargin: marginSize,
bottomMargin: height - marginSize * 2,
pad: 5,
plotWidth: function() {
return this.rightMargin - this.leftMargin;
},
plotHeight: function() {
return this.bottomMargin - this.topMargin;
},
// Boolean to enable/disable background grid.
grid: true,
// Number of axis tick labels to draw so that they are not drawn on
// top of one another.
numXTickLabels: 10,
numYTickLabels: 8,
};
this.preload = function(){
data = loadTable("./data/food/foodData.csv", "csv", "header");
}
this.setup = function (){
years = [];
for(let i = 5; i < data.getColumnCount(); i++){
let s = data.columns[i]
years.push(s)
let button = createButton(s)
button.parent('year-div')
button.mousePressed(function(){
yearString = this.elt.innerHTML
let yearIndex = years.indexOf(yearString);
this.currentYear = yearString;
for(var i = 0; i<bubbles.length;i++){
bubbles[i].setYear(yearIndex)
}
})
}
for(var i = 0; i < data.getRowCount();i++){
let r = data.getRow(i);
let name = r.getString("L1")
if(name != ""){
let d = [];
let val = []
for(let j = 0; j < years.length; j++){
val = Number(r.get(years[j]));
d.push(val);
}
var b = new Bubble(name, d);
b.setYear(0);
bubbles.push(b);
}
}
}
this.destroy = function() {
const yearDiv = document.getElementById('year-div');
let yearDivCild = yearDiv.lastElementChild;
while (yearDivCild) {
yearDiv.removeChild(yearDivCild);
yearDivCild = yearDiv.lastElementChild;
}
bubbles = [];
};
this.draw = function(){
background(200)
translate(width/2, height/2);
textAlign(CENTER);
textSize(30)
text(1974, 0, -250)
for(var i = 0; i < bubbles.length; i++){
bubbles[i].update(bubbles);
bubbles[i].show();
}
}
}
function Bubble(_name, _data)
{
this.name = _name;
this.pos = createVector(0,0);
this.dir = createVector(0,0);
this.id = randomID();
this.data = _data;
this.clr = color(random(0,255),random(0,255),random(0,255))
this.size = 20;
this.targetSize = this.size;
this.show = function(){
stroke(1)
fill(this.clr);
ellipse(this.pos.x, this.pos.y, this.size)
noStroke()
fill(15);
textSize(15)
text(this.name, this.pos.x, this.pos.y)
this.pos.add(this.dir);
if(this.size<this.targetSize){
this.size += 1
}
else if (this.size>this.targetSize){
this.size -= 1;
}
}
this.setYear = function(year_index){
let v = this.data[year_index]
this.targetSize = map(v, 0, 3600, 5, 200)
}
this.update = function(_bubbles){
this.dir = createVector(0,0)
for(var i = 0; i < _bubbles.length; i++){
if(_bubbles[i].id != this.id){
let vect = p5.Vector.sub(this.pos, _bubbles[i].pos);
let d = vect.mag();
if(d < this.size/2 + _bubbles[i].size/2){
if(d == 0){
this.dir.add(p5.Vector.random2D())
}
else{
//console.log("collision")
this.dir.add(vect);
}
}
}
}
this.dir.normalize()
}
}
function randomID(){
let alpha = "abcdefghijklmnopqrstuvwxyz0123456789"
let s = ""
for(var i = 0; i < 10; i++){
s += alpha[floor(random(0,alpha.length))];
}
return s
}