-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.pde
427 lines (337 loc) · 12.4 KB
/
app.pde
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import guicomponents.*;
import treemap.*;
graphicClass graphic; //Initializing drawing class to draw graphic
mathClass compute; //Initializing math class to compute MEAN,SD and Correlations
POPUPClass popup;
//writerClass write; //Writer to output to a file an interesting correlation, NOT USED
int WORDCOLUMN=5; //MANTAINED FOR LEGACY PURPOSES (ALWAYS WORDCOLUMN=5)
PImage Image1;
int[] seleccio={1,2,3,4,5,6,7,8,9}; //TO BE DISCONTINUED
String[] popuptime = new String[] { "Q1 10", "Q2 10", "Q3 10", "Q4 10", "Q1 11", "Q2 11", "Q3 11", "Q4 11","Actual"}; //NEW TIME RANGE FOR POPUP
String[] indicatorNames = {"", "", "", ""}; // 4 INDICATORS OF POPUP CLASS
int[] COLOR={0,140,0};
int[] COLORDATA={80,80,200};
//----------------POPUP COLOR SCHEME------------------------
int sColor=30; //POPUP Fill color of scatterplot
int backColor=220; // Background Color
int graphbackColor=250; //POPUP Graph Background Color
//-----------------------------------------------------------
//---------------POPUP CONFIG DATA-COMENTARI GIT----------------------------
int select=1; //POPUP Screen selector changed from top buttons
int hola=1;
int drawselect=1; //POPUP Select wich variables to draw
int ST=10;
int w=950; //POPUP Total Width of sketch
int h=600; //POPUP Total Height of sketch
float m;
float M1,SD1,V1,r,t; //GLOBAL VARIABLES FOR MATH
float a=0; //acceleration parameter for animation movement
float R=1; //Aspect Ratio of top height of graph, used for animation
int move=2; //flag that indicates start(1)/stop(0)/static(2) of transition animation
//Aspect Atributes
float X0=180;
float X1=630;
float Y0=340;
float Y1=140;
//---------------------------------------------------------------
// Data map (year -> WordMap)
HashMap dataMap;
// Current state of system: treemap, popup
String state;
// Indicator list
HashMap indicators;
Indicator lastIndicator;
// Empty wording
String emptyWord = "(No se encuentran marcas)";
// TreeMap attributes
Treemap map;
WordMap currentWordMap;
WordItem chosenWordItem;
int currentMapTime;
int treeX = 100;
int treeY = 30;
int treeW = 590;
int treeH = 400;
Timeline timeline;
int deltaX = -10;
int deltaY = -20;
int xButtonDelta = 0;
int yButtonDelta = 0;
// Text attributes
PFont font;
PFont mapFont;
PFont popupFont;
PFont popup2Font;
PFont popupTitleFont;
PFont popupSmallFont;
// Inputfield
GTextField txfML1;
String textBoxContent;
int textX = 720;
int textY = 30;
int textW = 200;
int textH = 300;
// Updatebutton
GButton btnUpdate;
int buttonUpdateX = 650;
int buttonUpdateY = 180;
int buttonUpdateW = 100;
int buttonUpdateH = 50;
// Suggested word array
int arrayX = 722;
int arrayY = 340;
int arrayW = 95;
int arrayH = 25;
int spacing = 2;
// Suggested words buttons
GButton[][] suggestedArray; // 8 buttons
String[] suggestedWords = {"Vista general,danone,hacendado,abc,avia,aquilea",
"Dietetica,hacendado,almendrina,alven,monsoy,candy,montosa,mela,millac,montesano,osadas,laselva",
"Infantil,palmil,promavel,montesano,libbys,calvo,eroski,aprolis",
"Fitness,hacendado,danone,avia,mela",
"Celiacos,provamel,libbys,laselva,aviSerra,mela",
"Gourmet,mela,millac,montesano,alven",
"Marca Propia,alven,mela,hacendado,mrolli,milord,abc",
"Gluten Free,almendrina,aprolis,plamil,monsoy,avia"};
// Slider
GWSlider sdr1;
void setup() {
// Setting up the environment
size(950, 600); //Defining size of popup screen
background(color(0));
frameRate(30);
smooth();
// Load data into WordMaps
dataMap = new HashMap();
for (int i = 1; i < 9; i = i+1) {
float range = map (i,1,9,1970,2010);
int parse=floor(range);
WordMap u = new WordMap(parse);
//String[] lines = loadStrings("ExcelTSV"+i+".txt"); //LOAD DE FITXERS!!!
String[] lines = loadStrings("ExcelTSV" + i +".txt");
//print("ExcelTSV" + i +".txt"+"\n");
String lineone[] = lines[0].split("\\W");
//scanSampled DISCONTINUED (always 1) as we don't have a sample size N, every foodLinkerobservation is our entire universe)
int scanSampled = Integer.parseInt(lineone[0]);
u.setscanSampled(scanSampled);
//------------------------------
for (int k = 1; k < lines.length; k++) {
String[] line = lines[k].split("\\t");
u.inputWord(line[0], Integer.parseInt(line[1])); //NOM I DIMENSIO DE WORD PER TREEMAP!
//print(line[0]+TAB+line[1]+"\n");
}
u.finishAdd();
//print(parse+"\n");
dataMap.put(parse, u);
}
// Load data into indicator list (INDICADORS POPUP)
indicators = new HashMap();
String[] indicatorLines = loadStrings("indicators.csv");
String[] columns = split(indicatorLines[0],',');
for (int k = 1; k < indicatorLines.length; k++) {
//print(indicatorLines.length);
String values[] = split(indicatorLines[k], ',');
indicatorNames[k-1]=values[0]; //MODIFIES GLOBAL STRING TO ALLOW TO CHANGE INDICATORS ON EXCEL DIRECTLY
Indicator i = new Indicator(values[0]);
for (int u = 1; u < values.length; u++) {
int dataYear = Integer.parseInt(columns[u]);
float f = new Float(values[u]);
i.addDataPoint(dataYear, f);
}
indicators.put(values[0], i);
// Sets the default indicator
if (k == 1) lastIndicator = i;
}
graphic=new graphicClass();
compute=new mathClass();
popup=new POPUPClass();
background(backColor);
// Variables initiated
textBoxContent = "Introducir marcas";
currentMapTime = 1985;
// System wide fonts
font = createFont("Helvetica", 24);
mapFont = createFont("Serif", 13);
popupFont = createFont("Arial", 20); //Creating a FONT
popup2Font = createFont("Arial", 11); //Creating a FONT
popupTitleFont = createFont("Arial", 10); //Creating a FONT
popupSmallFont = createFont("Arial", 9); //Creating a FONT
// Set default indicator
// different choices for the layout method
//MapLayout algorithm = new SliceLayout();
//MapLayout algorithm = new StripTreemap();
//MapLayout algorithm = new PivotBySplitSize();
//MapLayout algorithm = new SquarifiedLayout();
setupSlider();
// Setting up Text Box component
GComponent.globalColor = GCScheme.getColor(this, GCScheme.GREEN_SCHEME);
GComponent.globalFont = GFont.getFont(this, "Arial", 13);
txfML1 = new GTextField(this, textBoxContent, textX, textY, textW, textH, true);
txfML1.tag = "Words input";
/*
// Setting up update button
btnUpdate = new GButton(this, "UPDATE BBDD >", buttonUpdateX,buttonUpdateY,buttonUpdateW,buttonUpdateH);
btnUpdate.setBorder(5);
btnUpdate.fireAllEvents(true);
*/
// Setting up suggested words buttons
suggestedArray = new GButton[2][4];
int counter = 0;
for (int dx = 0; dx < suggestedArray.length; dx++){
for (int dy = 0; dy < suggestedArray[0].length; dy++){
GButton u = new GButton(this, suggestedWords[counter].split(",")[0], arrayX+dx*(arrayW+spacing), arrayY+dy*(arrayH+spacing),arrayW,arrayH);
u.setBorder(0);
suggestedArray[dx][dy] = u;
u.fireAllEvents(false);
counter++;
}
}
// Setting the stage
state = "treemap";
updateTreemap();
mouseMoved();
}
void draw() {
background(color(155));
//drawLegend(25, 300);
Image1= loadImage("foodlinkerlogo.png");
image(Image1,10,530);
if (state == "treemap") {
drawMap();
}
if (state == "popup") {
drawMap();
fill(backColor);
rect(X0-72,35,570,385); //DRAWS HEADER, CAPSALERA OF POPUP
graphic.cleargraph();
stroke(COLORDATA[0],COLORDATA[1],COLORDATA[2]);
popup.getValue(WORDCOLUMN);
fill(255);
graphic.drawvalues(WORDCOLUMN); //Draws the value type into the graph
stroke(COLOR[0],COLOR[1],COLOR[2]);
noFill();
graphic.drawvalues(drawselect);
graphic.movegraphdown();
graphic.movegraphup();
stroke(COLORDATA[0],COLORDATA[1],COLORDATA[2]);
popup.getValue(WORDCOLUMN);
popup.drawscatterplot(); //Draws Scatterplot
}
}
void drawMap() {
strokeWeight(0.25f);
textFont(mapFont);
map.draw();
}
//------------------------------------------GUI components and event fields------------------//
// Add an event to the events textfield
void handleSliderEvents(GSlider slider) {
if (currentMapTime != getSliderTime()) { //COMPARA TIPUS DE MAPA AMB TEMPS SELECCIONAT
currentMapTime = getSliderTime();
updateTreemap();
}
}
void handleTextFieldEvents(GTextField tfield){
if (!textBoxContent.equals(txfML1.getText())) {
textBoxContent = txfML1.getText();
updateTreemap();
}
txfML1.draw();
}
void handleButtonEvents(GButton button) {
int counter = 0;
for (int dx = 0; dx < suggestedArray.length; dx++){
for (int dy = 0; dy < suggestedArray[0].length; dy++){
if(button == suggestedArray[dx][dy] && button.getEventType() == GButton.CLICKED) {
txfML1.setText(suggestedWords[counter].split(",",2)[1].replace(",", "\n"));
}
counter++;
}
}
}
void mouseMoved() {
// Check if it's over treemap
// Highlight a WordItem
if (state == "treemap") {
WordItem[] wordItems = (WordItem[]) currentWordMap.getItems();
for (int i = 0; i < wordItems.length; i++) {
wordItems[i].isMouseOver();
}
}
}
void mouseClicked() {
WordItem[] wordItems = (WordItem[]) currentWordMap.getItems();
for (int i = 0; i < wordItems.length; i++) {
WordItem m = wordItems[i];
if (state == "popup" && timeline.isMouseOverCloseButton()) {
chosenWordItem = null;
//timeline.removeButtons();
timeline = null;
state = "treemap";
updateTreemap();
}
else {
if (state == "treemap" && m.isMouseOver() && !m.getWord().equals(emptyWord)) {
chosenWordItem = m;
timeline = new Timeline(this,lastIndicator, m, dataMap, treeX-deltaX, treeY-deltaY, treeW+2*deltaX, treeH+2*deltaY);
compute.all(); //COMPUTES MEAN, SD, CORRELATION AND REGRESSION, STORED ON GLOBAL VARIABLES
state = "popup";
}
}
}
}
boolean isMouseInside(int x, int y, int w, int h) {
if (mouseX > x && mouseX < x+w) {
if (mouseY > y && mouseY < y+h) {
return true;
}
}
return false;
}
//------------------------------------------Calculating functions----------------------------------------
void updateTreemap() {
currentWordMap = createWordMap(textBoxContent, (WordMap)dataMap.get(currentMapTime));
map = new Treemap(currentWordMap, treeX, treeY, treeW , treeH );
}
WordMap createWordMap(String words, WordMap wordMap) {
WordMap returnMap = new WordMap(currentMapTime);
words = words.toLowerCase(); //FUNCIONS DE PARSEJAT!!!!!!!!!!!!!
words = words.replaceAll(" ", "");
String[] wordArray = words.split("\\n");
returnMap.setscanSampled(wordMap.getscanSampled());
for (int i = 0 ; i < wordArray.length; i++) {
String word = wordArray[i];
//AQUESTES SON LES PARAULES!!!!!!!!
if (wordMap.containsWord(word)) {
returnMap.putWord(word, wordMap.getWordItem(word));
}
}
if (returnMap.isEmpty()) {
returnMap.addWord(emptyWord);
returnMap.setscanSampled(0);
}
returnMap.finishAdd();
return returnMap;
}
int getSliderTime() {
return sdr1.getValue()*5 + 1970;
}
void mousePressed(){
if (state=="popup"){
if (mouseX>X0&&mouseX<X0+(X1-X0)/4&&mouseY>Y1-h/10&&mouseY<Y1){move=1;select=1;}
else if (mouseX>X0+(X1-X0)/4&&mouseX<X0+(X1-X0)/2&&mouseY>Y1-h/10&&mouseY<Y1){move=1;select=2;;}
else if (mouseX>X0+(X1-X0)/2&&mouseX<X1-(X1-X0)/4&&mouseY>Y1-h/10&&mouseY<Y1){move=1;select=3;}
else if (mouseX>X1-(X1-X0)/4&&mouseX<X1&&mouseY>Y1-h/10&&mouseY<Y1){move=1;select=4;}
}
}
void setupSlider(){
// Setting up slider
String[] sdr1TickLabels = new String[] { "Q1-Q2", "Q2-Q3", "Q3-Q4", "Q4-Q1", "Q1-Q2", "Q2-Q3", "Q3-Q4", "Q4-Q1"};
sdr1 = new GWSlider(this, treeX,treeY+treeH+30,treeW);
sdr1.setTickLabels(sdr1TickLabels);
sdr1.setLimits(0, 0, 7);
sdr1.setRenderValueLabel(false);
sdr1.setStickToTicks(true);
sdr1.setValue(2.345f);
sdr1.setFontColour(55, 15, 255); //COLOR TEXT SLIDER
}