Repository navigation
Expand file tree
/
Copy pathMatrix.js
More file actions
488 lines (440 loc) · 13.3 KB
/
Copy pathMatrix.js
File metadata and controls
488 lines (440 loc) · 13.3 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
function Matrix(
uniqueID,
coordX,
coordY,
visualWidth,
visualHeight,
parent,
colorPalette,
bgColor
) {
//Public variables
this.uniqueID = uniqueID;
this.coordX = coordX;
this.coordY = coordY;
this.visualWidth = visualWidth;
this.visualHeight = visualHeight;
this.parent = parent;
this.framed = true;
this.priority = false;
this.colorPalette = colorPalette;
//Private variables
let myCanvas = null;
let myData = [];
let highlightRows = 1;
let visualPadding = 1;
let valueTextSize = 11;
let cellPadding = 5;
let visualBgColor = bgColor;
let headerFill = colorPalette[1];
let headerColor = "white";
let valueFill = "white";
let valueColor = "black";
let columnFill = colorPalette[2];
let columnColor = "white";
let cellGap = 2;
let columnWidths = [];
let rowHeights = null;
let yOffset = 0;
let xOffset = 0;
let actualWidth = null;
let actualHeight = null;
let visualBorderColor = "lightgray";
let visualBorderWidth = 1;
let horizontal_barLength = null;
let vertical_barPosition = 0;
let horizontal_barPosition = 0;
let vertical_barLength = null;
let scrollbarWidth = 10;
let scrollBarBackground = (233, 233, 233);
let scrollBarFill = (200, 200, 200);
let scrollBarActive = (150, 150, 150);
let lastMouseX = null;
let lastMouseY = null;
let fetchRequired = 1;
let rowField = null;
let columnField = null;
let valueFields = [];
let lastCanvasHeight;
let lastCanvasWidth;
let sortAlias = "";
let sortOrder = "";
//Public function to add row and value fields to the visual
this.addValue = function (alias = "Dimension", expression, format) {
valueFields.push({
alias,
expression,
format,
});
fetchRequired = 1;
return this; //return this instance
};
this.addRow = function (alias = "Row Values", tableID, fieldName) {
rowField = { alias, tableID, fieldName };
fetchRequired = 1;
return this;
};
this.addColumn = function (alias = "Colum Values", tableID, fieldName) {
columnField = { alias, tableID, fieldName };
fetchRequired = 1;
return this;
};
this.sortBy = function (rowOrFieldAlias, ascendingOrDescending) {
sortAlias = rowOrFieldAlias;
sortOrder = ascendingOrDescending;
fetchRequired = 1;
};
//Private function to set vertical scroll position
this.setScrollY = function (newValue) {
yOffset = -constrain(
newValue,
0,
actualHeight - this.visualHeight + scrollbarWidth
);
vertical_barPosition = (-yOffset * this.visualHeight) / actualHeight;
};
//Private function to set horizontal scroll position
this.setScrollX = function (newValue) {
xOffset = -constrain(
newValue,
0,
actualWidth - this.visualWidth + scrollbarWidth
);
horizontal_barPosition = (-xOffset * this.visualWidth) / actualWidth;
};
//Private function to calculate column widths and store in local array variable
this.getColumnWidth = function (colIndex) {
let longestString = "";
for (let i = 0; i < myData[colIndex].length; i++) {
if (String(myData[colIndex][i]).length > longestString.length) {
longestString = myData[colIndex][i];
}
}
textSize(valueTextSize);
return 2 * cellPadding + textWidth(longestString);
};
this.fetch = function () {
if (!fetchRequired) return;
//Create buffered graphics
if (
!myCanvas ||
this.visualWidth != lastCanvasWidth ||
this.visualHeight != lastCanvasHeight
) {
lastCanvasWidth = this.visualWidth;
lastCanvasHeight = this.visualHeight;
myCanvas = createGraphics(this.visualWidth, this.visualHeight);
}
if (!rowField || !valueFields.length) return;
//Display loading text..
myCanvas.background(visualBgColor);
myCanvas.text(
"Loading..",
xOffset + visualPadding,
yOffset + visualPadding + valueTextSize
);
//Query the parent page to fetch data, pass this visual's fields
myData = this.parent.query(
uniqueID,
rowField,
columnField,
valueFields,
[], //remove filters
sortAlias,
sortOrder
);
//Format values
if (columnField) {
for (let i = 1; i < myData.length; i++) {
for (let j = 1; j < myData[i].length; j++) {
myData[i][j] = formatValue(myData[i][j], valueFields[0].format);
}
}
} else {
for (let i = 1; i < myData.length; i++) {
for (let j = 1; j < myData[i].length; j++) {
myData[i][j] = formatValue(myData[i][j], valueFields[i - 1].format);
}
}
}
//Calculate column widths
columnWidths = [];
for (let i = 0; i < myData.length; i++) {
columnWidths.push(this.getColumnWidth(i));
}
//Calculate row heights
rowHeights = 2 * cellPadding + valueTextSize;
//Calculate actual table width (might be larger than visible area)
let buffer = 0;
for (let i = 0; i < columnWidths.length; i++) {
buffer += columnWidths[i] + cellGap;
}
actualWidth = 2 * visualPadding + buffer;
//Calculate actual table height (might be larger than visible area)
actualHeight =
2 * visualPadding + myData[0].length * (rowHeights + cellGap);
//Calculate scrollbar lengths
horizontal_barLength =
((this.visualWidth - scrollbarWidth) / actualWidth) *
(this.visualWidth - scrollbarWidth);
vertical_barLength =
((this.visualHeight - scrollbarWidth) / actualHeight) *
(this.visualHeight - scrollbarWidth);
//Reset scrolls
this.setScrollX(0);
this.setScrollY(0);
fetchRequired = 0;
};
this.drawHorizontalScrollBar = function () {
if (fetchRequired) return;
if (this.visualWidth - scrollbarWidth >= actualWidth) return;
//Draw horizontal scrollbar
myCanvas.fill(scrollBarBackground);
myCanvas.rect(
0,
this.visualHeight - scrollbarWidth,
this.visualWidth,
scrollbarWidth
);
myCanvas.stroke(this.hScrollBarPressed ? scrollBarActive : scrollBarFill);
myCanvas.fill(this.hScrollBarPressed ? scrollBarActive : scrollBarFill);
myCanvas.rect(
horizontal_barPosition,
this.visualHeight - scrollbarWidth * 0.8,
horizontal_barLength,
scrollbarWidth * 0.6
);
};
this.drawVerticalScrollBar = function () {
if (fetchRequired) return;
if (this.visualHeight - scrollbarWidth >= actualHeight) return;
//Draw vertical scrollbar
myCanvas.noStroke();
myCanvas.fill(scrollBarBackground);
myCanvas.rect(
this.visualWidth - scrollbarWidth,
0,
scrollbarWidth,
this.visualHeight
);
myCanvas.stroke(this.vScrollBarPressed ? scrollBarActive : scrollBarFill);
myCanvas.fill(this.vScrollBarPressed ? scrollBarActive : scrollBarFill);
myCanvas.rect(
this.visualWidth - scrollbarWidth * 0.8,
vertical_barPosition,
scrollbarWidth * 0.6,
vertical_barLength
);
};
this.mouseEvents = function () {
if (fetchRequired) return;
if (
parent.visuals.filter((x) => x.priority && x.uniqueID != this.uniqueID)
.length
)
return; //Some other visual has priority over mouse actions, return
if (mouseIsPressed) {
//Check mouse for scroll events
let visualX = mouseX - this.coordX;
let visualY = mouseY - this.coordY;
//Check if mouse is pressed on horizontal scrollbar
if (
this.visualWidth - scrollbarWidth < actualWidth &&
!(
visualX < horizontal_barPosition ||
visualX > horizontal_barPosition + horizontal_barLength
) &&
!(
visualY < this.visualHeight - scrollbarWidth ||
visualY > this.visualHeight
) &&
!this.hScrollBarPressed &&
!this.vScrollBarPressed
) {
this.hScrollBarPressed = 1;
}
//Check if mouse is pressed on vertical scrollbar
if (
this.visualHeight - scrollbarWidth < actualHeight &&
!(
visualY < vertical_barPosition ||
visualY > vertical_barPosition + vertical_barLength
) &&
!(
visualX < this.visualWidth - scrollbarWidth ||
visualX > this.visualWidth
) &&
!this.hScrollBarPressed &&
!this.vScrollBarPressed
) {
this.vScrollBarPressed = 1;
}
} else {
this.vScrollBarPressed = 0;
this.hScrollBarPressed = 0;
}
//Check if mouse moved
if (lastMouseX != mouseX) {
//Handle dragging horizontal scroll bar
if (this.hScrollBarPressed) {
let diff = mouseX - lastMouseX;
if (diff > 0) {
this.setScrollX(
-xOffset +
(diff * (actualWidth - this.visualWidth + scrollbarWidth)) / //(diff * outside area / visual area)
(this.visualWidth - horizontal_barLength)
);
} else if (diff < 0) {
this.setScrollX(
-xOffset +
(diff * (actualWidth - this.visualWidth + scrollbarWidth)) /
(this.visualWidth - horizontal_barLength)
);
}
}
lastMouseX = mouseX;
}
//Check if mouse moved
if (lastMouseY != mouseY) {
//Handle dragging vertical scroll bar
if (this.vScrollBarPressed) {
let diff = mouseY - lastMouseY;
if (diff > 0) {
this.setScrollY(
-yOffset +
(diff * (actualHeight - this.visualHeight + scrollbarWidth)) /
(this.visualHeight - vertical_barLength)
);
} else if (diff < 0) {
this.setScrollY(
-yOffset +
(diff * (actualHeight - this.visualHeight + scrollbarWidth)) /
(this.visualHeight - vertical_barLength)
);
}
}
lastMouseY = mouseY;
}
};
this.draw = function () {
//Create buffered graphics
if (
!myCanvas ||
this.visualWidth != lastCanvasWidth ||
this.visualHeight != lastCanvasHeight
) {
lastCanvasWidth = this.visualWidth;
lastCanvasHeight = this.visualHeight;
myCanvas = createGraphics(this.visualWidth, this.visualHeight);
}
//Styles
myCanvas.textSize(valueTextSize);
myCanvas.noStroke();
//Draw Table Area
myCanvas.fill(visualBgColor);
myCanvas.rect(0, 0, this.visualWidth, this.visualHeight);
//If no data fetched yet
if (!rowField || !valueFields.length) {
myCanvas.textSize(valueTextSize * 1.5);
myCanvas.fill("black");
myCanvas.text(
"Fields Required",
xOffset +
(this.visualWidth - myCanvas.textWidth("Fields Required")) / 2,
yOffset + this.visualHeight / 2
);
}
//Draw columns and rows
for (
//For each column
let currentColumn = 0;
currentColumn < myData.length;
currentColumn++
) {
for (
//For each row in that column
let currentRow = 0;
currentRow < myData[currentColumn].length;
currentRow++
) {
//Calculate position of the current cell
let cellX = visualPadding + xOffset;
let cellY = visualPadding + yOffset;
//Shift the current cell according to previous columns
for (let i = 0; i < currentColumn; i++) {
cellX += columnWidths[i] + cellGap;
}
//Shift the current cell according to previous rows
for (let i = 0; i < currentRow; i++) {
cellY += rowHeights + cellGap;
}
//Use different colors for headers vs. values
myCanvas.fill(
currentRow == 0
? headerFill
: currentColumn == 0 && highlightRows
? columnFill
: valueFill
);
myCanvas.rect(cellX, cellY, columnWidths[currentColumn], rowHeights);
myCanvas.fill(
currentRow == 0
? headerColor
: currentColumn == 0 && highlightRows
? columnColor
: valueColor
);
let valueTxt = myData[currentColumn][currentRow];
if (currentColumn > 0 && currentRow > 0) {
if (columnField) {
if (
valueFields[0].format == "integer" ||
valueFields[0].format == "double"
) {
valueTxt = Number(valueTxt).toLocaleString("en-US");
}
} else {
if (
valueFields[currentColumn - 1].format == "integer" ||
valueFields[currentColumn - 1].format == "double"
) {
valueTxt = Number(valueTxt).toLocaleString("en-US");
}
}
}
myCanvas.text(
valueTxt,
cellX + cellPadding,
cellY + cellPadding + valueTextSize
);
}
}
//Draw table border
myCanvas.noFill();
myCanvas.stroke(visualBorderColor);
myCanvas.strokeWeight(visualBorderWidth);
myCanvas.rect(
0,
0,
this.visualWidth - visualBorderWidth,
this.visualHeight - visualBorderWidth
);
this.drawHorizontalScrollBar();
this.drawVerticalScrollBar();
};
this.mouseWheel = function (event) {
//A visual's mouseWheel method is only triggered by the page if mouse is over that visual
//console.log(event.delta);
this.setScrollY(-yOffset + event.delta);
};
this.refresh = function () {
fetchRequired = 1;
};
//Render function called in loop
this.render = function () {
this.fetch();
this.draw();
this.mouseEvents();
return myCanvas;
};
}