-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.pde
More file actions
437 lines (385 loc) · 13 KB
/
code.pde
File metadata and controls
437 lines (385 loc) · 13 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
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
428
429
430
431
432
433
434
435
436
437
HashMap<String,TableRow[]> database = new HashMap<String,TableRow[]>();
var dateFireCount = {};
int hT = 400;
int wT = 800;
int numberOfDays = 0;
int maxFirePtDayCount = 0;
HashMap<String, ArrayList<String>> dataByState = new HashMap<String, ArrayList<String>>();
interface Javascript{
//function delcarations
int findStates(String state);
int getSize(Object obj);
float getMax(Object obj, String attribute);
float getMin(Object obj, String attribute);
float average(Object obj, String attribute, String date);
float sum(Object obj, String attribute, String date);
float count(Object obj, String attribute, String date);
float evaluateAggr(String aggr, Object obj, String attribute, String date);
Object filterDateRange(String startDate, String endDate);
Object filterByState(String state);
}
void bindJavascript(Javascript js){
javascript = js;
}
JavaScript javascript;
// Generic Line Graph
// Assuming the object is already setup -
void drawLineGraph(int x, int y, int w, int h, Object xObject, string xAttribute, Object yObject, string yAttribute, String aggr, data) {
fill(255,255,255);
int textSpacing = 20;
float xCount = 12;//(float) getSize(xObject) - 2;
float xMax = (float) getMax(xObject, xAttribute);
float xMin = (float) getMin(xObject, xAttribute);
// float yMax = (float) getMax(yObject, yAttribute);
// float yMin = (float) getMin(yObject, yAttribute);
float yMax = -99999, yMin = 99999;
float thing = x-w+textSpacing;
int i = 0;
float previous = null;
float value;
for(var date in xObject){
value = evaluateAggr(aggr.toLowerCase(), yObject, yAttribute, xObject[date]);
if(value < yMin){
yMin = value;
}
if(yMax < value){
yMax = value;
}
}
float widthScaler = (float)((w-textSpacing)*2)/xCount;
float heightScaler = (float)(h*2-textSpacing)/(float)(yMax); // do we want the graph to scale between the min/max value? or not?
//console.log('yMax: ' + yMax + ' yMin: ' + yMin);
//console.log('widthScaler: ' + widthScaler + ' heightScaler: ' + heightScaler);
for(var date in xObject){
value = evaluateAggr(aggr.toLowerCase(), yObject, yAttribute, xObject[date]);
int current = (y+h-textSpacing)-(value*heightScaler);
if(value == NaN){
value = 0;
}
//console.log('current: ' + current + " value: " + value);
if(previous != null){
text(yMax,x-w-10,y-h);
text(yMin,x-w-10,y+h);
text(xMax,x+w,y+h+10);
text(xMin,x-w,y+h+10);
stroke(255-data.r,255-data.g,255-data.b);
strokeWeight(1);
line((i-1)*(widthScaler)+(int)thing, previous, i*(widthScaler)+(int)thing,current);
}
previous = current;
i++;
}
int textSpacing = 20;
textSize(12);
pushMatrix();
translate(x-w+textSpacing/2, y-h/4);
rotate(radians(90));
text(yAttribute,0, 0);
popMatrix();
int xAttWid = textWidth(xAttribute);
text(xAttribute,x-xAttWid/2, y+h-textSpacing/2);
text(data.state,x-w+textSpacing/2, y-h+textSpacing)
}
void drawIntensityBar(int x, int y, int w, int h, Object xObject, string xAttribute, Object yObject, string yAttribute, String aggr, data) {
fill(255,255,255);
float xCount = 12;//(float) getSize(xObject) - 2;
float xMax = (float) getMax(xObject, xAttribute);
float xMin = (float) getMin(xObject, xAttribute);
float yMax = -99999, yMin = 99999;
float thing = x-w;
int i = 0;
for(var date in xObject){
value = evaluateAggr(aggr.toLowerCase(), yObject, yAttribute, xObject[date]);
if(value < yMin){
yMin = value;
}
if(yMax < value){
yMax = value;
}
}
float widthScaler = (float)(w*2)/xCount;
float heightScaler = (float)(h*2)/(float)(yMax);
for(var date in xObject){
// histogram
float value = evaluateAggr(aggr.toLowerCase(), yObject, yAttribute, xObject[date]);
int color = 255*(value/yMax);
fill(color,0,0);
stroke(color,0,0);
rect((i*(widthScaler)+thing), y-h, (widthScaler+thing), h);
int positionInGraph = Math.round(i*(widthScaler)+100);
if(mouseX == positionInGraph){
fill(255);
text(date +" had a fire presence count of: "+value, 50, 105);
}
i++;
}
}
void drawSingleSpiral(int x, int y, int w, int h, Object xObject, string xAttribute, Object yObject, string yAttribute, String aggr, int r, data) {
float xCount = 12;//(float) getSize(xObject) - 2;
float degree = 360/xCount;
float xMax = (float) getMax(xObject, xAttribute);
float xMin = (float) getMin(xObject, xAttribute);
float thing = x-w;
float yMax = -99999, yMin = 99999;
float startRad = 0;
strokeWeight(3);
int i = 1;
for(var date in xObject){
value = evaluateAggr(aggr.toLowerCase(), yObject, yAttribute, xObject[date]);
if(value < yMin){
yMin = value;
}
if(yMax < value){
yMax = value;
}
}
float widthScaler = (float)(w*2)/xCount;
float heightScaler = (float)(h*2)/(float)(yMax);
for(var date in xObject){
// histogram
float value = evaluateAggr(aggr.toLowerCase(), yObject, yAttribute, xObject[date]);
int color = 255*(value/yMax);
fill(color,0,0);
stroke(color,0,0);
float endRad = radians(i*degree);
noFill();
arc(x, y, r, r, startRad, endRad, open);
startRad = endRad;
i++
}
}
void deleteGraphs() {
graphBoxes.each(function(item) {
if(item.data.selected) {console.log("Deleting"); graphBoxes.delete(item.data); console.log("Deleted");}
});
}
void snapGraphs() {
graphBoxes.each(function(item) {
if(item.data.selected) {item.data.x = mouseX; item.data.y = mouseY;}
});
}
void snapGraphsDim() {
var maxHeight = 0;
var maxWidth = 0;
graphBoxes.each(function(item) {
if(item.data.selected) {
if(item.data.h > maxHeight)
{
maxHeight = item.data.h;
}
if(item.data.w > maxWidth)
{
maxWidth = item.data.w;
}
}
});
graphBoxes.each(function(item) {
if(item.data.selected) {item.data.h = maxHeight; item.data.w = maxWidth;}
});
}
void selectAll() {
graphBoxes.each(function(item) {item.data.selected = true;});
}
void deselectAll() {
graphBoxes.each(function(item) {item.data.selected = false;});
}
void expandGraphs() {
var expandCount = 0;
graphBoxes.each(function(item) {if(item.data.selected) {item.data.x += (expandCount++)*20;}});
}
// void testBoxCreate() {
// var box1 = new graphBox(400,200,100,100);
// box1.r = 255;
// var box2 = new graphBox(350,200,50,50);
// box2.g = 255;
// var box3 = new graphBox(500,200,50,50);
// box3.b = 255;
// graphBoxes.add(box1);
// graphBoxes.add(box2);
// //graphBoxes.add(box3);
// console.log("test boxes created");
// }
int num;
void setup() {
size(wT, hT);
rectMode(RADIUS);
int state = -1;
int acq_date = -1;
}
void draw(){
background(80);
fill(color(255,123,13));
text("Current state: ", 50, 50);
text("\t\t\t"+ currState, 50, 70);
fill(color(0,151,178));
text("Selected state: ", 50, 90);
var textSpace = 0;
var currentState = selStates.start;
while(currentState !== null){
text("\t\t\t"+ currentState.data, 50, 110+(textSpace*20));
currentState = currentState.next;
textSpace++;
}
//fill(color(255,123,13), 128);
//text("Data: " + num + " elements of " + selectedState, 50, 90);
//drawLineGraph(100,350, 600, 300);
// drawIntensityBar(100,380, 600, 10);
//drawSingleSpiral(400, 200, 300, 2);
graphBoxes.each(function(item) {
//if(item.data.selected) {stroke(0, 102, 204);}
//rect(item.data.x, item.data.y, item.data.w, item.data.h);
//stroke(0,0,0);
if(item.data.selector){
noFill();
rect(item.data.x, item.data.y, item.data.w, item.data.h);
}
else{
fill(item.data.r,item.data.g,item.data.b, 128);
//console.log("no selector");
if(item.data.selected){stroke(0, 0, 200);}
rect(item.data.x, item.data.y, item.data.w, item.data.h);
stroke(0, 0, 200);
//fill(color(item.data.r,item.data.g,item.data.b,item.data.a));
//item.data.drawSingleSpiral(item.data.x, item.data.y, 2*item.data.w, 2/*item.data.h*/);
if(item.data.graphType == "Line"){item.data.drawLineGraph(item.data.x,item.data.y,item.data.w,item.data.h,
item.data.xObject,item.data.axisX,item.data.yObject,item.data.axisY,item.data.aggr,item.data);}
if(item.data.graphType == "Spiral"){item.data.drawSingleSpiral(item.data.x,item.data.y,item.data.w,item.data.h,
item.data.xObject,item.data.axisX,item.data.yObject,item.data.axisY,item.data.aggr,1,item.data);}
if(item.data.graphType == "Intensity"){item.data.drawIntensityBar(item.data.x,item.data.y,item.data.w,item.data.h,
item.data.xObject,item.data.axisX,item.data.yObject,item.data.axisY,item.data.aggr,item.data);}
//drawSingleSpiral(400, 200, 300, 2);
}
});
// drawLineGraph(100, 350, 600, 300, currentMonths, "acq_date", stateEntires, "confidence", "Sum");
}
void mousePressed(){
// if(javascript != null){
// num = javascript.findStates(selectedState);
// }
//console.log("clicked at X:" + mouseX + " Y:" + mouseY);
var current = graphBoxes.end;
while(current !== null) {
//current.data.selected = false;
//console.log(current.data.intersect(mouseX,mouseY));
if(current.data.intersect(mouseX,mouseY) != null) { //hit something
//console.log("intersected");
if(current.data.selected==false) {graphBoxes.each(function(item) {item.data.selected = false;});}
//current.data.locked = true;
current.data.selected = true;
graphBoxes.delete(current.data);
graphBoxes.add(current.data);
return;
}
current = current.prev;
}
//Not clicking a graph, create selection rectangle
graphBoxes.each(function(item) {item.data.selected = false;});
graphBoxes.selectCount = 0;
graphBoxes.add(new graphBox(mouseX,mouseY,0,0));
graphBoxes.end.data.locked = true;
graphBoxes.end.data.selector = true;
graphBoxes.end.data.sInitX = mouseX;
graphBoxes.end.data.sInitY = mouseY;
}
void mouseReleased(){
/*for(int i = 0; i<graphBoxes.length() ; i++){
if(graphBoxes[i] != null) {
graphBoxes[i].locked = false;
}
}*/
graphBoxes.end.data.transformLock = false;
graphBoxes.end.data.translateLock = false;
graphBoxes.end.data.locked = false;
graphBoxes.end.data.xTransform = 0;
graphBoxes.end.data.yTransform = 0;
if(graphBoxes.end.data.selector && graphBoxes.end.prev != null)
{
var current = graphBoxes.end.prev;
while(current !== null) {
if(current.data.inside(graphBoxes.end.data.x, graphBoxes.end.data.y,
graphBoxes.end.data.w, graphBoxes.end.data.h)) {
//current.data.locked = true;
current.data.selected = true;
graphBoxes.selectCount++;
}
current = current.prev;
}
}
if(graphBoxes.end.data.selector){graphBoxes.delete(graphBoxes.end.data);}
}
void mouseDragged() {
/*for(int i = 0; i<graphBoxes.length() ; i++){
if(graphBoxes[i] != null && graphBoxes[i].locked) {
graphBoxes[i].x = mouseX - graphBoxes[i].xOffset;
graphBoxes[i].y = mouseY - graphBoxes[i].yOffset;
}
}*/
if(graphBoxes.end.data.transformLock) //stretch or shrink
{
if(graphBoxes.selectCount>1) {
graphBoxes.revEach(function(item) {
if(item.data.selected) {
item.data.w += (mouseX-pmouseX)*graphBoxes.end.data.xTransform;
item.data.h += (mouseY-pmouseY)*graphBoxes.end.data.yTransform;
}
});
return;
}
graphBoxes.end.data.w += (mouseX-pmouseX)*graphBoxes.end.data.xTransform;
graphBoxes.end.data.h += (mouseY-pmouseY)*graphBoxes.end.data.yTransform;
}
else if(graphBoxes.end.data.translateLock) //move around canvas
{
if(graphBoxes.selectCount>1) {
graphBoxes.revEach(function(item) {
item.data.x += (mouseX-pmouseX);
item.data.y += (mouseY-pmouseY);
});
return;
}
graphBoxes.end.data.x = mouseX - graphBoxes.end.data.xOffset;
graphBoxes.end.data.y = mouseY - graphBoxes.end.data.yOffset;
}
else if(graphBoxes.end.data.selector)
{
graphBoxes.end.data.w += (mouseX-pmouseX)/2;
graphBoxes.end.data.h += (mouseY-pmouseY)/2;
graphBoxes.end.data.x = graphBoxes.end.data.sInitX + (graphBoxes.end.data.w);
graphBoxes.end.data.y = graphBoxes.end.data.sInitY + (graphBoxes.end.data.h);
}
}
void keyPressed() {
if(key == DELETE) //delete graph
{
deleteGraphs();
}
if(key == ' ') //snap to 'center' coordinates
{
snapGraphs();
}
if(key == CODED)
{
if(keyCode == ALT) //snap width and height
{
snapGraphsDim();
}
}
if(key == 'e')
{
expandGraphs();
}
if(key == 'a')
{
selectAll();
}
if(key == 'd')
{
deselectAll();
}
}
void javaClicked(){
console.log("here");
if(javascript != null){
num = javascript.findStates(selectedState);
}
}