Repository navigation
Expand file tree
/
Copy pathHeatMap.pde
More file actions
243 lines (226 loc) · 8.34 KB
/
Copy pathHeatMap.pde
File metadata and controls
243 lines (226 loc) · 8.34 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
// HeatMap
// Jake Casserly's code for Heat map 15/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 heat map
* @param: y-coordinate the y position of the heat map
* @param: img the svg file of the states of america
* @param: database the database name to be loaded in
**/
class HeatMap {
int xpos;
int ypos;
float currentxpos;
float currentypos;
PShape img;
Table table;
int amountInState;
int amountInThisState;
boolean readInData;
boolean animated;
ArrayList<Integer> amountInStates;
int entries;
PShape stateShape;
float t;
int largest;
ExecutorService executorService = Executors.newCachedThreadPool();
readDataTask readStates;
String flightCarrier;
Data stateData;
String database;
String state;
int count;
boolean departures;
readDataTask animate;
boolean startingThread;
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"};
HeatMap(int xpos, int ypos, PShape img, String database) {
this.xpos = xpos;
this.ypos = ypos;
this.database = database;
currentxpos = 200;
currentypos = 900;
this.img = img;
departures = true;
readInData = false;
animated = false;
startingThread = true;
amountInStates = new ArrayList<>();
for (int i = 0; i < allStates.length; i++) {
amountInStates.add(0);
}
t = 0;
largest = 0;
flightCarrier = "";
readStates = new readDataTask("stateHeat");
executorService.execute(readStates);
//animate = new readDataTask("animateHeatMap");
}
void draw() {
entries = count;
if (!animated) { // if not fully animated, animate the rest
animate();
}
else { // else just draw the shape
shape(img, 200, 264);
}
img.enableStyle();
noStroke();
fill(255, 255, 0);
//rect(0,85,1512,900);
strokeWeight(4);
stroke(0);
fill(0);
text("HeatMap:\n(Concentration of flights per state)", 750, 190);
line(1260, 280, 1260, 800);
line(1260, 280, 1283, 280);
line(1260, 800, 1283, 800);
text("No.\nflights", 1330, 530);
fill(0);
text(largest, 1325, 277); // display the largest concentration of flights as the maximum value for the scale
text("0", 1310, 790);
textSize(25);
setGradient(1263, 283, 12, 514, color(255,20,50), color(0,20,50), 1);
if (theSearchBar.result != "null") {
//print(theChartSearchBar.result);
if (flightCarrier != theSearchBar.result) { // consistently check if the searchBar has a new result, if so
//prevFlightCarrier = flightCarrier; // start a new thread and execute it
flightCarrier = theSearchBar.result;
for (int i = 0; i < allStates.length; i++) {
amountInStates.set(i, 0);
}
readStates = new readDataTask("stateHeat");
executorService.execute(readStates); // execute the readInData() method from a new thread
count = 0;
}
}
}
void drawStates() {
//img.enableStyle();
String state;
strokeWeight(0.5);
stroke(255);
for (int i = 0; i < allStates.length; i++) {
state = allStates[i];
stateShape = img.getChild(state);
stateShape.disableStyle();
if ((((double)amountInStates.get(i)/(entries)) * 2550) >= 255) {
fill(255, 20, 50);
}
else {
fill((int)(((double)amountInStates.get(i) / (double)entries) * 2550), 10, 27); // filling each state in the heatMap based on the amount of flights in
} // that specific state compared to the flights in all states defined by "entries"
shape(stateShape, currentxpos, currentypos);
fill(255);
text("WA", currentxpos+245, currentypos+70); // drawing the text for each state in the heat map
text("OR", currentxpos+230, currentypos+145);
text("MT", currentxpos+390, currentypos+105);
text("WY", currentxpos+410, currentypos+185);
text("ID", currentxpos+310, currentypos+160);
text("CA", currentxpos+210, currentypos+290);
text("AK", currentxpos+220, currentypos+480);
text("NV", currentxpos+260, currentypos+232);
text("UT", currentxpos+340, currentypos+256);
text("AZ", currentxpos+332, currentypos+350);
text("CO", currentxpos+430, currentypos+270);
text("NM", currentxpos+420, currentypos+360);
text("TX", currentxpos+532, currentypos+430);
text("KS", currentxpos+550, currentypos+285);
text("OK", currentxpos+570, currentypos+345);
text("NE", currentxpos+520, currentypos+225);
text("SD", currentxpos+520, currentypos+165);
text("ND", currentxpos+520, currentypos+105);
text("MN", currentxpos+600, currentypos+120);
text("IA", currentxpos+615, currentypos+210);
text("MO", currentxpos+637, currentypos+275);
text("AR", currentxpos+645, currentypos+360);
text("LA", currentxpos+650, currentypos+420);
text("MS", currentxpos+698, currentypos+390);
text("AL", currentxpos+750, currentypos+385);
text("GL", currentxpos+810, currentypos+380);
text("FL", currentxpos+850, currentypos+460);
text("SC", currentxpos+852, currentypos+350);
text("NC", currentxpos+850, currentypos+305);
text("TN", currentxpos+740, currentypos+325);
text("KY", currentxpos+760, currentypos+285);
text("VA", currentxpos+856, currentypos+263);
textSize(13);
text("WV", currentxpos+819, currentypos+250);
textSize(25);
text("OH", currentxpos+780, currentypos+220);
text("IN", currentxpos+732, currentypos+230);
text("IL", currentxpos+690, currentypos+240);
text("MI", currentxpos+740, currentypos+160);
text("WI", currentxpos+660, currentypos+145);
text("PA", currentxpos+855, currentypos+193);
text("NY", currentxpos+883, currentypos+145);
}
}
void animate () {
if (currentypos < ypos) {
animated = true;
currentypos = ypos;
currentxpos = xpos;
}
else {
currentxpos = (200 + 130 * sin(t) );
t += 0.097;
currentypos -= 9.5;
}
}
void readInData() {
int number = 0;
String code = "";
//stateData.setData();
for (int i = 0; i < flightData.length; i++) { // loops through all the data and gets assigns totals to an arrayList of the name "amountInStates"
code = flightData.getCode(i);
if (code.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++;
}
}
largest = 0;
for (int i = 0; i < amountInStates.size(); i++) { // gets the largest amount in one specific state
if (amountInStates.get(i) > largest) {
largest = amountInStates.get(i);
}
}
}
void setGradient(int x, int y, float w, float h, color c1, color c2, int axis ) { // giving the gradient for the indicator to the right side of the heat map
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);
}
}
}
}