-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathparallelCoordinates.pde
377 lines (353 loc) · 10.2 KB
/
parallelCoordinates.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
// displays a data set using parallel coordinates
// dataset info
String dataSet = "cars";
String fileName = dataSet + ".csv";
boolean cluster = true;
FloatTable table;
float[][] data;
// row, column info
String[] colNames;
int col = 0;
int colTot;
String[] rowNames;
int row = 0;
int rowTot;
// data values info
float[] colMin, colMax, colRan, colLow, colHigh;
// color info
color light = color(248, 248, 248);
color lightTransp = color(248, 248, 248, 32);
color dark = color(7, 7, 7);
color color0 = color(228, 26, 28, 32);
color color1 = color(55, 126, 184, 32);
color color2 = color(77, 175, 74, 32);
color color3 = color(152, 78, 163, 32);
color color4 = color(255, 127, 0, 32);
color color5 = color(255, 255, 51, 32);
color color6 = color(166, 86, 40, 32);
// gui variables
int topBorder = 63;
int border = 17;
int axisH;
int axesW;
float axesSep;
float[] axes;
// interactivity variables
boolean[] invert;
boolean brushing;
float brushY;
int brushCol;
boolean moving;
int moveCol;
// initialization
void setup(){
// set up canvas
size(720, 405);
smooth();
// grab our data
table = new FloatTable(fileName);
data = table.getData();
rowNames = table.getRowNames();
colNames = table.getColNames();
colTot = table.getColCount();
rowTot = table.getRowCount();
// store all min/max values and ranges
colMin = new float[colTot];
colMax = new float[colTot];
colRan = new float[colTot];
colLow = new float[colTot];
colHigh = new float[colTot];
invert = new boolean[colTot];
int j;
for(j = 0; j < colTot; j++){
colMin[j] = table.getColMin(j);
colMax[j] = table.getColMax(j);
colLow[j] = colMin[j];
colHigh[j] = colMax[j];
colRan[j] = colMax[j] - colMin[j];
invert[j] = false;
}
// set gui variables
axisH = height - topBorder - border - 24;
axesW = width - 2 * border;
axes = new float[colTot];
// determine axes distance
axesSep = (float) axesW / colTot;
int i;
for(i = 0; i < colTot; i++){
if(i == 0)
axes[i] = axesSep / 2 + border;
else
axes[i] = axes[i-1] + axesSep;
}
// set up interactive variables
brushing = false;
}
// draw cycle
void draw(){
// redraw dark canvas
background(dark);
// draw and label all axes
strokeWeight(3);
stroke(light);
textAlign(CENTER);
int k;
for(k = 0; k < colTot; k++){
strokeWeight(3);
line(axes[k], topBorder, axes[k], topBorder + axisH);
textSize(8);
if(!invert[k]){
text(int(colMax[k]), axes[k], topBorder - 5);
text(int(colMin[k]), axes[k], topBorder + axisH + 11);
}else{
text(int(colMin[k]), axes[k], topBorder - 5);
text(int(colMax[k]), axes[k], topBorder + axisH + 11);
}
textSize(9);
text(colNames[k], axes[k], topBorder + axisH + 24);
noFill();
strokeWeight(1);
arc(axes[k], topBorder - border - 5, 12, 12, PI, TWO_PI);
if(!invert[k]){
line(axes[k] - 6, topBorder - border - 5, axes[k] - 6 - 2, topBorder - border - 5 - 2);
line(axes[k] - 6, topBorder - border - 5, axes[k] - 6 + 2, topBorder - border - 5 - 2);
}else{
line(axes[k] + 6, topBorder - border - 5, axes[k] + 6 - 2, topBorder - border - 5 - 2);
line(axes[k] + 6, topBorder - border - 5, axes[k] + 6 + 2, topBorder - border - 5 - 2);
}
}
// draw data
strokeWeight(1);
int i;
float y0 = 0;
float y1 = 0;
for(i = 0; i < rowTot; i++){
boolean filter = false;
// set color (varies if clustering data)
if(!cluster)
stroke(lightTransp);
else
if(data[i][0] == 0)
stroke(color0);
else if(data[i][0] == 1)
stroke(color1);
else if(data[i][0] == 2)
stroke(color2);
else if(data[i][0] == 3)
stroke(color3);
else if(data[i][0] == 4)
stroke(color4);
else if(data[i][0] == 5)
stroke(color5);
else if(data[i][0] == 6)
stroke(color6);
int l;
for(l = 0; l < colTot; l++){
if(data[i][l] > colHigh[l] || data[i][l] < colLow[l])
filter = true;
}
int j;
if(!filter)
for(j = 0; j < colTot; j++){
y0 = y1;
if(!invert[j])
y1 = topBorder + axisH - ((float) axisH * (data[i][j] - colMin[j]) / colRan[j]);
else
y1 = topBorder + axisH - ((float) axisH * (colMax[j] - data[i][j]) / colRan[j]);
if(j != 0)
line(axes[j-1], y0, axes[j], y1);
}
}
// write title
fill(light);
textSize(20);
textAlign(CENTER);
String title = dataSet;
text(title, width / 2, border + 5);
}
// mouse interaction: invert axes or brushing data or moving axes
void mousePressed(){
// detect which column / axis was pressed
int j;
int col = -1;
boolean valid = false;
for(j = 0; j < colTot; j++){
if(mouseX > (axes[j] - axesSep / 2) && mouseX < (axes[j] + axesSep / 2)){
col = j;
valid = true;
}
}
// inverting axes
if(mouseY > (topBorder - border - 15) && mouseY < topBorder && valid){
if(!invert[col])
invert[col] = true;
else
invert[col] = false;
}
// brushing data
if(mouseY > topBorder && mouseY < (topBorder + axisH) && valid){
brushing = true;
brushY = mouseY;
brushCol = col;
}
// reset brush data (click on title)
if(mouseY < 25){
int k;
for(k = 0; k < colTot; k++){
colLow[k] = colMin[k];
colHigh[k] = colMax[k];
}
}
// moving axes
if(mouseY > (topBorder + axisH)){
moving = true;
moveCol = col;
}
}
// when releasing mouse: brushing data or moving axes
void mouseReleased(){
// brushing data
if(brushing && ((mouseY - brushY) > 3 || (mouseY - brushY) < -3)){
float val1;
float val2;
if(!invert[brushCol]){
val1 = (float) (topBorder + axisH - brushY) / axisH * colRan[brushCol] + colMin[brushCol];
val2 = (float) (topBorder + axisH - mouseY) / axisH * colRan[brushCol] + colMin[brushCol];
}else{
val1 = colMax[brushCol] - (float) (topBorder + axisH - brushY) / axisH * colRan[brushCol];
val2 = colMax[brushCol] - (float) (topBorder + axisH - mouseY) / axisH * colRan[brushCol];
}
if(val1 > val2){
colLow[brushCol] = val2;
colHigh[brushCol] = val1;
}else{
colLow[brushCol] = val1;
colHigh[brushCol] = val2;
}
brushing = false;
// moving axes
}else if(moving){
// detect which column was released
int k;
int mcol = -1;
boolean valid = false;
for(k = 0; k < colTot; k++){
if(mouseX > (axes[k] - axesSep / 2) && mouseX < (axes[k] + axesSep / 2)){
mcol = k;
valid = true;
}
}
if(mcol == moveCol){
valid = false;
}
// if valid, move the data around
if(valid){
float[] movingCol = new float[rowTot];
float movingMinCol;
float movingMaxCol;
float movingRanCol;
float movingLowCol;
float movingHighCol;
String movingNameCol;
float[] movingCol2 = new float[rowTot];
float movingMinCol2;
float movingMaxCol2;
float movingRanCol2;
float movingLowCol2;
float movingHighCol2;
String movingNameCol2;
int l;
for(l = 0; l < rowTot; l++){
movingCol[l] = data[l][moveCol];
}
movingMinCol = colMin[moveCol];
movingMaxCol = colMax[moveCol];
movingRanCol = colRan[moveCol];
movingLowCol = colLow[moveCol];
movingHighCol = colHigh[moveCol];
movingNameCol = colNames[moveCol];
int j;
for(j = 0; j < colTot; j++){
int i;
if(j < moveCol && j < mcol){
// no change
}else if(j < moveCol && j == mcol){
for(i = 0; i < rowTot; i++){
movingCol[i] = data[i][j];
data[i][j] = data[i][moveCol];
}
movingMinCol = colMin[j];
movingMaxCol = colMax[j];
movingRanCol = colRan[j];
movingLowCol = colLow[j];
movingHighCol = colHigh[j];
movingNameCol = colNames[j];
colMin[j] = colMin[moveCol];
colMax[j] = colMax[moveCol];
colRan[j] = colRan[moveCol];
colLow[j] = colLow[moveCol];
colHigh[j] = colHigh[moveCol];
colNames[j] = colNames[moveCol];
}else if(j >= moveCol && j < mcol){
for(i = 0; i < rowTot; i++){
data[i][j] = data[i][j+1];
}
colMin[j] = colMin[j+1];
colMax[j] = colMax[j+1];
colRan[j] = colRan[j+1];
colLow[j] = colLow[j+1];
colHigh[j] = colHigh[j+1];
colNames[j] = colNames[j+1];
}else if(j < moveCol && j > mcol){
for(i = 0; i < rowTot; i++)
movingCol2[i] = movingCol[i];
movingMinCol2 = movingMinCol;
movingMaxCol2 = movingMaxCol;
movingRanCol2 = movingRanCol;
movingLowCol2 = movingLowCol;
movingHighCol2 = movingHighCol;
movingNameCol2 = movingNameCol;
for(i = 0; i < rowTot; i++){
movingCol[i] = data[i][j];
data[i][j] = movingCol2[i];
}
movingMinCol = colMin[j];
movingMaxCol = colMax[j];
movingRanCol = colRan[j];
movingLowCol = colLow[j];
movingHighCol = colHigh[j];
movingNameCol = colNames[j];
colMin[j] = movingMinCol2;
colMax[j] = movingMaxCol2;
colRan[j] = movingRanCol2;
colLow[j] = movingLowCol2;
colHigh[j] = movingHighCol2;
colNames[j] = movingNameCol2;
}else if(j > moveCol && j == mcol){
for(i = 0; i < rowTot; i++){
data[i][j] = movingCol[i];
}
colMin[j] = movingMinCol;
colMax[j] = movingMaxCol;
colRan[j] = movingRanCol;
colLow[j] = movingLowCol;
colHigh[j] = movingHighCol;
colNames[j] = movingNameCol;
}else if(j == moveCol && j > mcol){
for(i = 0; i < rowTot; i++){
data[i][j] = movingCol[i];
}
colMin[j] = movingMinCol;
colMax[j] = movingMaxCol;
colRan[j] = movingRanCol;
colLow[j] = movingLowCol;
colHigh[j] = movingHighCol;
colNames[j] = movingNameCol;
}else if(j > moveCol && j > mcol){
// no change
}else{println("we shoudl never reach this!");}
}
}
moving = false;
}
}