Repository navigation
Expand file tree
/
Copy pathBarChart.pde
More file actions
383 lines (345 loc) · 12.9 KB
/
Copy pathBarChart.pde
File metadata and controls
383 lines (345 loc) · 12.9 KB
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
// barChart
// Jake Casserly's code for Bar Chart 20/03/2024
/**
* This class generates a BarChart of data. The draw method periodically draws to the screen.
* The type of BarChart that is drawn varies between different x-axis.
* The BarChart is able to detect a change in paramaters e.g. xAxis, departures etc.
* And as a result reload the data accordingly, using a new thread.
*
* @param: x-coordinate the x position of the barChart
* @param: y-coordinate the y position of the barChart
* @param: height the height of the barChart
* @param: width the width of the barChart
* @param: database the database name to be loaded in
* @param: cp5 the instance of cp5 for the dropdownLists
*/
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import controlP5.*;
class barChart {
DropdownList d1, d2, d3, d4;
int x;
int y;
double height;
double width;
Data stateData;
Data timeData;
boolean paramatersSame;
int[] yOutputStates;
ArrayList<Integer> amountInStates;
String state;
String flightCarrier;
String prevFlightCarrier;
float count;
float countTime;
String[] date;
boolean departures;
boolean prevdepartures;
int[] dates;
ArrayList<Integer> amountOnDate;
String xAxis;
String prevxAxis;
boolean dataRead;
ExecutorService executorService = Executors.newCachedThreadPool();
readDataTask readStates;
readDataTask readTimes;
boolean readState;
boolean readTime;
int number;
int numberTime;
ControlP5 cp5;
String state1;
String state2;
int state1num;
int state2num;
int time1num;
int time2num;
String database;
float value;
String[] allStates = {"AL","AK","AZ","AR","CA","CO","CT","DE","FL","GA","HI","ID","IL",
"IN","IA","KS","KY","LA","ME","MD","MA","MI","MN","MS","MO","MT","NE","NV","NH","NJ",
"NM","NY","NC","ND","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VT","VA","WA",
"WV","WI","WY"};
String[] StringDates = {"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"};
barChart (int x, int y, double height, double width, String database, ControlP5 cp5) {
this.x = x;
this.y = y;
this.database = database;
this.width = width;
this.height = height;
xAxis = "time";
prevxAxis = "time";
paramatersSame = false;
readTime = false;
readState = false;
count = 0;
countTime = 0;
number = 0;
numberTime = 0;
amountInStates = new ArrayList<>();
dates = new int[30];
for (int i = 0; i < dates.length; i++) {
dates[i] = i+1;
}
amountOnDate = new ArrayList<>();
for (int i = 0; i < allStates.length; i++) {
amountInStates.add(0);
}
departures = true;
prevdepartures = departures;
flightCarrier = "B6";
prevFlightCarrier = "B6";
value = 1;
PFont font = createFont("arial",16);
this.cp5 = cp5;
d1 = cp5.addDropdownList("S1").setPosition(x+1060, 850).setSize(40, 100).setFont(font);
d2 = cp5.addDropdownList("S2").setPosition(x+1350, 850).setSize(40, 100).setFont(font);
d3 = cp5.addDropdownList("T1").setPosition(x+1060, 850).setSize(40, 100).setFont(font);
d4 = cp5.addDropdownList("T2").setPosition(x+1350, 850).setSize(40, 100).setFont(font);
customize(d1);
customize(d2);
customize(d3);
customize(d4);
state1 = "TX";
state2 = "NY";
state1num = 0;
state2num = 1;
time1num = 0;
time2num = 1;
readStates = new readDataTask("state");
readTimes = new readDataTask("time");
}
void draw() {
//println(departures);
if (prevdepartures != departures) {
prevdepartures = departures;
paramatersSame = false;
}
if (!paramatersSame) { // read of the data when changing from arrivals to departures and vica-versa
System.out.println("Starting Executor");
for (int i = 0; i < allStates.length; i++) {
amountInStates.set(i, 0);
}
for (int i = 0; i < dates.length; i++) {
amountOnDate.set(i, 0);
}
count = 0;
countTime = 0;
readStates = new readDataTask("state");
executorService.execute(readStates);
readTimes = new readDataTask("time");
executorService.execute(readTimes);
// Start each individual task (i.e. you've created the task objects,
// now you just want to run them on individual threads
// which the executor will handle for you by using a "ThreadPool")
//executorService.shutdown(); // this is used to shut down the threads
paramatersSame = true;
}
fill(255);
strokeWeight(7);
line(x, y, x, y+750); // draw the x-Axis and y-Axis:
line(x, y+750, x+1000, y+750);
strokeWeight(3);
if(xAxis == "state") { // if the x-Axis is meant to display states, then display states, else display time
d1.setVisible(true);
d2.setVisible(true);
d3.setVisible(false);
d4.setVisible(false);
for (int i = 0; i < allStates.length; i++) {
strokeWeight(3);
stroke(3);
line(x+(i*20), y+750, (x-5)+(i*20), y+770);
fill(0);
textSize(30);
text("BarChart:\n(Concentration of flights per state)\nFlight Carrier: " + ((flightCarrier != "") ? flightCarrier : "all carriers"), 750, 165);
textSize(19);
text("No.\nflights", 50, 200);
text("States/(state abreviation)", 560, 925);
text("Search Flight_Carrier", 1330, 670);
textSize(11);
fill(0);
text(allStates[i], x+6+(i*20), y+764);
stroke(1);
fill(17, 17, 200);
//rect(x+(i*20), y+(750-(amountInStates.get(i)/count)*2000), 20, (amountInStates.get(i)/count)*2000);
setGradient((int)x+(i*20), (int)(y+(750-(amountInStates.get(i)/count)*2500)), (float)20, (float)((amountInStates.get(i)/count)*2500), (int)color(255,20,50), (int)color(0,20,50), 1);
}
// Y-Axis
for (int i = 0; i < 25; i++) {
strokeWeight(4);
line(x, y+750-(i*25), x-8, y+750-(i*25));
}
textSize(16);
text(amountInStates.get(8), x-40, y+(750-(amountInStates.get(8)/count)*2500));
line(x, y+(750-(amountInStates.get(8)/count)*2500), x-6, y+(750-(amountInStates.get(8)/count)*2500));
textSize(32);
fill(200, 20, 25);
strokeWeight(2);
value = (((float)amountInStates.get(state1num)/((float)amountInStates.get(state1num)+amountInStates.get(state2num)))*220);
rect(x+1115, 850, value, 25);
fill(100, 220, 20);
rect(x+1115+value, 850, ((1-value/220)*220), 25);
//fill(120);
//rect(x+1120, 850, 0.2*100, 25);
//fill(255);
//rect(x+1120 + 0.2*100, 850, (1-0.2)*100, 25);
}
else if (xAxis == "time") {
d1.setVisible(false);
d2.setVisible(false);
d3.setVisible(true);
d4.setVisible(true);
for (int i = 0; i < dates.length; i++) {
strokeWeight(3);
stroke(3);
line(x+(i*25), y+750, (x-5)+(i*25), y+770);
fill(0);
textSize(30);
text("BarChart:\n(Flights sorted by dates)\nFlight Carrier: " + ((flightCarrier != "") ? flightCarrier : "all carriers"), 750, 180); // text to be displayed for user to see description of data being displayed
textSize(19);
text("No.\nflights", 50, 200);
text("Dates/(day in the month)", 480, 925);
text("Search Flight_Carrier", 1330, 670);
textSize(11);
fill(0);
text(Integer.toString(i+1), x+6+(i*25), y+764);
stroke(1);
fill(17, 17, 200);
rect(x+(i*25), y+(750-(amountOnDate.get(i)/countTime)*12000), 20, (amountOnDate.get(i)/countTime)*12000); // for each rectangle, draw it from the top of the rectangle, which should be 750 - height.
} // inherintly the user will see the rectangle drawn from the bottom, when actually it is drawn
// as usual from the top left.
// Y-Axis
for (int i = 0; i < 25; i++) {
strokeWeight(4);
line(x, y+750-(i*25), x-8, y+750-(i*25));
}
textSize(20);
text(amountOnDate.get(10), x-40, y+(750-(amountOnDate.get(10)/countTime)*12000)); // give some idea of the scale of the data
line(x, y+(750-(amountOnDate.get(10)/countTime)*12000), x-6, y+(750-(amountOnDate.get(10)/countTime)*12000)); // to be displayed on the y-axis
text(amountOnDate.get(10)/2, x-40, y+(750-((amountOnDate.get(10)/countTime)*12000)/2));
line(x, y+(750-((amountOnDate.get(10)/countTime)*12000)/2), x-6, y+(750-((amountOnDate.get(10)/countTime)*12000)/2));
strokeWeight(2);
value = (((float)amountOnDate.get(time1num)/((float)amountOnDate.get(time1num)+amountOnDate.get(time2num)))*220);
rect(x+1115, 850, value, 25);
fill(100, 220, 20);
rect(x+1115+value, 850, ((1-value/220)*220), 25);
}
if (theChartSearchBar.result != "null") {
if (theChartSearchBar.result != "state" && theChartSearchBar.result != "time"){
//print(theChartSearchBar.result);
//print(flightCarrier);
if (flightCarrier != theChartSearchBar.result) {
//prevFlightCarrier = flightCarrier;
flightCarrier = theChartSearchBar.result;
for (int i = 0; i < allStates.length; i++) {
amountInStates.set(i, 0);
}
for (int i = 0; i < dates.length; i++) {
amountOnDate.set(i, 0);
}
//readStates.setRunning();
count = 0;
countTime = 0;
readStates = new readDataTask("state");
executorService.execute(readStates);
readTimes = new readDataTask("time");
executorService.execute(readTimes);
//print("something");
}
}
else if (theSearchBar.result == "state" || theSearchBar.result == "time"){
xAxis = theSearchBar.result;
}
else {
// do nothing forever
}
}
}
synchronized void readTime() {
if (readTime == false) {
//timeData = new Data(database); // made code more inefficient, now using one object initialized in main
//timeData.setData();
readTime = true;
}
for (int i = 0; i < dates.length; i++) {
amountOnDate.add(0);
}
countTime = 0;
for (int i = 0; i < flightData.length; i++) {
if (flightData.getCode(i).contains(flightCarrier)) {
date = flightData.getDate(i).split("/");
for (int z = 0; z < dates.length; z++) {
if (Integer.parseInt(date[1]) == dates[z]) {
numberTime = amountOnDate.get(z);
amountOnDate.set(z, numberTime+1);
}
}
countTime++;
}
}
}
void readState() {
if (readState == false) {
//stateData = new Data(database);
//stateData.setData();
readState = true;
}
for (int i = 0; i < flightData.length; i++) {
if (flightData.getCode(i).contains(flightCarrier)) {
for (int z = 0; z < allStates.length; z++) {
if (departures) {
state = flightData.getDep(i).getState();
}
else {
state = flightData.getArr(i).getState();
}
if (state.equals(allStates[z])) {
number = amountInStates.get(z);
amountInStates.set(z, number+1);
}
}
count++;
}
}
}
void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) {
noFill();
if (axis == 1) { // Top to bottom gradient
for (int i = y; i <= y+h; i++) {
float inter = map(i, y, y+h, 0, 1);
color c = lerpColor(c1, c2, inter);
stroke(c);
line(x, i, x+w, i);
}
}
else if (axis == 2) { // Left to right gradient
for (int i = x; i <= x+w; i++) {
float inter = map(i, x, x+w, 0, 1);
color c = lerpColor(c1, c2, inter);
stroke(c);
line(i, y, i, y+h);
}
}
}
void mousePressed() {
}
void customize(DropdownList ddl) {
// a convenience function to customize a DropdownList
ddl.setBackgroundColor(color(190));
ddl.setItemHeight(30);
ddl.setBarHeight(30);
if (ddl.getLabel() == "S1" || ddl.getLabel() == "S2") {
for (int i=0;i<allStates.length;i++) { // adding states to the 1st set of dropdownlist
ddl.addItem(allStates[i], i);
}
}
else {
for (int z=0;z<dates.length;z++) { // adding dates to the 2nd set of dropdownlist
ddl.addItem(StringDates[z], z);
}
}
ddl.setColorBackground(color(20));
ddl.setColorActive(color(255, 128));
}
}