Repository navigation
Expand file tree
/
Copy pathHorizontalBarChart.js
More file actions
633 lines (565 loc) · 17.4 KB
/
Copy pathHorizontalBarChart.js
File metadata and controls
633 lines (565 loc) · 17.4 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
function HorizontalBarChart(
uniqueID,
title,
coordX,
coordY,
visualWidth,
visualHeight,
parent,
colorPalette,
bgColor
) {
//Public variables
this.uniqueID = uniqueID;
this.title = title;
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 titleSize = 18;
let gapAfterTitle = 20;
let myData = [];
let minBarHeight = 30;
let maxBarHeight = 50;
let barGap = 5;
let labelGap = 5;
let visualPadding = 30;
let labelSize = 11;
let visualBgColor = bgColor;
let yOffset = 0;
let actualHeight = null;
let viewableHeight = null;
let actualWidth = 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 barBounds = [];
//let columnField = null; not available for now
let valueFields = [];
let lastCanvasHeight;
let lastCanvasWidth;
let sortAlias = "";
let sortOrder = "";
let yValues;
let xValues;
let chartAreaLeft;
let chartAreaTop;
let chartAreaRight;
let chartAreaBottom;
let chartAreaWidth;
let chartAreaHeight;
let breakYAxis;
let longestYAxisValue;
let maxYAxisValueLength;
let longestXAxisValue;
let maxXAxisValueLength;
let xMax;
let xMin;
let barAreaTop;
let barAreaLeft;
let barAreaWidth;
let xAxisAreaHeight;
let barHeight;
let yLabelsBetween;
let remainingGap;
//Public function to add row and value fields to the visual
this.addXAxis = function (alias = "Dimension", expression, format) {
valueFields.push({
alias,
expression,
format,
});
fetchRequired = 1;
return this; //return this instance
};
this.addYAxis = function (alias = "Field", tableID, fieldName) {
rowField = { alias, tableID, fieldName };
fetchRequired = 1;
return this;
};
/* Not Available for Now
this.addColumn = function (alias = "Field", 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 - viewableHeight);
vertical_barPosition =
(-yOffset * (this.visualHeight - vertical_barLength)) /
(actualHeight - viewableHeight); //(-yOffset * viewableHeight) / actualHeight;
};
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;
//Query the parent page to fetch data, pass this visual's fields
myData = this.parent.query(
uniqueID,
rowField,
null, //no column field allowed for bar chart, at least for midterms
valueFields,
[], //remove filters
sortAlias,
sortOrder
);
//Format values
for (let i = 0; i < valueFields.length; i++) {
for (let j = 1; j < myData[0].length; j++) {
myData[i + 1][j] = formatValue(myData[i + 1][j], valueFields[i].format);
}
}
fetchRequired = 0;
};
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);
}
//Draw table area
myCanvas.noStroke();
myCanvas.fill(visualBgColor);
myCanvas.rect(0, 0, this.visualWidth, this.visualHeight);
//If no data fetched yet
if (!rowField || !valueFields.length) {
myCanvas.textStyle("normal");
myCanvas.textSize(labelSize * 1.5);
myCanvas.fill(colorPalette[0]);
myCanvas.text(
"Fields Required",
(this.visualWidth - myCanvas.textWidth("Fields Required")) / 2,
yOffset + this.visualHeight / 2
);
return;
}
//Calculate important positions and sizes
yValues = myData[0].slice(1);
xValues = myData[1].slice(1);
chartAreaLeft = visualPadding;
chartAreaTop = visualPadding + titleSize + gapAfterTitle;
chartAreaRight = this.visualWidth - visualPadding - scrollbarWidth;
chartAreaBottom = this.visualHeight - visualPadding;
chartAreaWidth = chartAreaRight - chartAreaLeft;
chartAreaHeight = chartAreaBottom - chartAreaTop;
breakYAxis = 0;
myCanvas.textSize(labelSize);
longestYAxisValue = [...yValues].sort((a, b) => b.length - a.length)[0];
maxYAxisValueLength = myCanvas.textWidth(longestYAxisValue);
longestXAxisValue = [...xValues].sort((a, b) => b.length - a.length)[0];
maxXAxisValueLength = myCanvas.textWidth(Math.round(longestXAxisValue));
if (maxYAxisValueLength + labelSize * 2 > chartAreaWidth / 3) {
maxYAxisValueLength = chartAreaWidth / 3 - labelSize * 2;
breakYAxis = 1;
}
xMax = Math.max(...xValues);
xMin = Math.min(...xValues);
barAreaTop = chartAreaTop;
barAreaLeft = chartAreaLeft + maxYAxisValueLength + labelSize * 2;
barAreaWidth = chartAreaWidth - maxYAxisValueLength - labelSize * 2;
xAxisAreaHeight = maxXAxisValueLength + labelSize * 2;
barHeight = Math.min(
Math.max(minBarHeight, chartAreaHeight / yValues.length - barGap),
maxBarHeight
);
yLabelsBetween = Math.floor(barAreaWidth / (2 * labelSize));
remainingGap = barAreaWidth - yLabelsBetween * 2 * labelSize + labelSize;
actualHeight = (yValues.length - 1) * (barHeight + barGap);
viewableHeight = chartAreaHeight - visualPadding - xAxisAreaHeight;
vertical_barLength = (this.visualHeight * viewableHeight) / actualHeight;
myCanvas.fill(visualBgColor);
myCanvas.rect(chartAreaLeft, chartAreaTop, chartAreaWidth, chartAreaHeight);
//Draw x axis values
myCanvas.noStroke();
//Draw bars
barBounds = [];
for (let i = 0; i < yValues.length; i++) {
let currentValue = xValues[i];
let currentBarLength = map(currentValue, 0, xMax, 0, barAreaWidth);
let textLength = myCanvas.textWidth(currentValue);
let drawInside =
barAreaWidth - 100 < currentBarLength + labelSize + textLength / 2
? 1
: 0;
let currentBarColor =
this.colorPalette[
Math.round(
map(xValues[i], xMin - 1, xMax, 0, this.colorPalette.length - 1) //minus 1 prevents NaN when boundaries are equal
)
];
//Bar shadow
myCanvas.fill([170, 170, 170]);
myCanvas.rect(
barAreaLeft + 1.5,
yOffset + barAreaTop + i * (barHeight + barGap) + 1.5,
currentBarLength,
barHeight
);
//Bar rectangle
myCanvas.fill(currentBarColor);
myCanvas.rect(
barAreaLeft,
yOffset + barAreaTop + i * (barHeight + barGap),
currentBarLength,
barHeight
);
//Bar value
myCanvas.fill(
drawInside
? isLightcolor(currentBarColor)
? "black"
: "white"
: this.colorPalette[0]
);
let valueTxt = currentValue;
if (
valueFields[0].format == "integer" ||
valueFields[0].format == "double"
) {
valueTxt = Number(valueTxt).toLocaleString("en-US");
}
myCanvas.text(
valueTxt,
barAreaLeft +
currentBarLength +
labelSize / 2 -
(drawInside ? labelSize + textLength : 0),
yOffset + barAreaTop + i * (barHeight + barGap) + barHeight / 2
);
barBounds.push({
x: barAreaLeft,
y: yOffset + barAreaTop + i * (barHeight + barGap),
w: currentBarLength,
h: barHeight,
label: yValues[i],
field: myData[1][0],
value: xValues[i],
});
}
//Draw y axis line
myCanvas.stroke(this.colorPalette[0]);
myCanvas.strokeWeight(1);
myCanvas.line(
barAreaLeft - labelSize,
barAreaTop,
barAreaLeft - labelSize,
chartAreaBottom - xAxisAreaHeight
);
//Draw x axis line
myCanvas.strokeWeight(1);
myCanvas.line(
barAreaLeft - labelSize,
chartAreaBottom - xAxisAreaHeight,
barAreaLeft + barAreaWidth,
chartAreaBottom - xAxisAreaHeight
);
//Draw y axis values, break word if necessary
myCanvas.fill(this.colorPalette[0]);
myCanvas.noStroke();
for (let i = 0; i < yValues.length; i++) {
if (breakYAxis) {
let breakAt = Math.floor(maxYAxisValueLength / myCanvas.textWidth("b"));
let brokeDueToArea = yValues[i].slice(0, breakAt);
//Break at the first space you see scanning from the end to start
for (let ch = brokeDueToArea.length - 1; ch >= 0; ch--) {
if (yValues[i][ch] == " ") {
breakAt = ch;
break;
}
}
let secondBreakAt = Math.floor(
maxYAxisValueLength / myCanvas.textWidth("b")
);
//Much more readable this way
let row1 = yValues[i].slice(0, breakAt);
let row2 = yValues[i].slice(breakAt, breakAt + secondBreakAt);
myCanvas.text(
row1,
chartAreaLeft,
yOffset + chartAreaTop + i * (barHeight + barGap) + labelSize
);
myCanvas.text(
row2,
chartAreaLeft,
yOffset + chartAreaTop + i * (barHeight + barGap) + labelSize * 2
);
} else {
myCanvas.text(
yValues[i],
chartAreaLeft,
yOffset + chartAreaTop + i * (barHeight + barGap) + labelSize
);
}
}
//Draw x axis label area
myCanvas.noStroke();
myCanvas.fill(visualBgColor);
myCanvas.rect(
chartAreaLeft,
chartAreaBottom - xAxisAreaHeight + 1,
chartAreaWidth,
xAxisAreaHeight + visualPadding
);
//Min X Label
myCanvas.fill(this.colorPalette[0]);
myCanvas.noStroke();
myCanvas.push();
myCanvas.translate(
barAreaLeft - labelSize,
chartAreaBottom - xAxisAreaHeight + labelSize * 2
);
myCanvas.rotate(radians(45));
myCanvas.text(0, 0, 0);
myCanvas.pop();
//Max X Label
myCanvas.push();
myCanvas.translate(
barAreaLeft + barAreaWidth,
chartAreaBottom - xAxisAreaHeight + labelSize * 2
);
myCanvas.rotate(radians(45));
myCanvas.text(xMax, 0, 0);
myCanvas.pop();
//X Labels Between
for (let i = 0; i < yLabelsBetween; i++) {
let curX =
barAreaLeft -
labelSize +
labelSize / 2 +
remainingGap / 2 +
i * labelSize * 2 +
labelSize / 2;
let curValue = Math.round(
map(curX, barAreaLeft - labelSize, barAreaLeft + barAreaWidth, 0, xMax)
);
myCanvas.push();
myCanvas.translate(
curX,
chartAreaBottom - xAxisAreaHeight + labelSize * 2
);
myCanvas.rotate(radians(45));
myCanvas.text(curValue, 0, 0);
myCanvas.pop();
}
//Draw table border
myCanvas.noFill();
myCanvas.stroke(visualBorderColor);
myCanvas.strokeWeight(visualBorderWidth);
myCanvas.rect(
0,
0,
this.visualWidth - visualBorderWidth,
this.visualHeight - visualBorderWidth
);
//Draw title area
myCanvas.fill(visualBgColor);
myCanvas.noStroke();
myCanvas.rect(0, 0, this.visualWidth, chartAreaTop);
//Draw title
myCanvas.textStyle("bold");
myCanvas.textSize(titleSize);
myCanvas.fill(this.colorPalette[0]);
myCanvas.noStroke();
myCanvas.text(
this.title,
this.visualWidth / 2 - myCanvas.textWidth(this.title) / 2,
visualPadding + titleSize
);
//Draw vertical scroll bar
this.drawVerticalScrollBar();
};
this.drawVerticalScrollBar = function () {
if (fetchRequired) return;
if (viewableHeight >= 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 vertical scrollbar
if (
viewableHeight < actualHeight && //vertical bar exists
!(
visualY < vertical_barPosition ||
visualY > vertical_barPosition + vertical_barLength
) &&
!(
visualX < this.visualWidth - scrollbarWidth ||
visualX > this.visualWidth
) &&
!this.vScrollBarPressed
) {
this.vScrollBarPressed = 1;
}
} else {
this.vScrollBarPressed = 0;
}
//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);
} else if (diff < 0) {
this.setScrollY(-yOffset + (diff * actualHeight) / this.visualHeight);
}
}
lastMouseY = mouseY;
}
};
this.tooltip = 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 (!myData || !myData.length) return;
let visualX = mouseX - this.coordX;
let visualY = mouseY - this.coordY;
for (let i = 0; i < barBounds.length; i++) {
if (
!(
visualX < barBounds[i].x ||
visualX > barBounds[i].x + barBounds[i].w ||
visualY < barBounds[i].y ||
visualY > barBounds[i].y + barBounds[i].h ||
visualY > chartAreaBottom - xAxisAreaHeight
)
) {
//User is hovering over a bar
textSize(labelSize);
//Calculate rectangle size from text widths
textStyle("bold");
let valueLength = textWidth(`${barBounds[i].label} : `);
let fieldNameLength = textWidth(`${barBounds[i].field}`);
textStyle("normal");
valueLength += textWidth(`${barBounds[i].value}`);
//Shift the tooltip to leftside if it will be hidden due to framed canvas
let rectX = mouseX + 5;
let rectY = mouseY + 5;
let rectW = Math.max(fieldNameLength, valueLength) + 2 * labelSize;
let rectH = labelSize * 3.5;
if (rectX + rectW > actualWidth) {
rectX -= rectW + 10;
}
//Highlight the hovered bar
fill([255, 255, 255, 50]);
rect(
this.coordX + barBounds[i].x,
this.coordY + barBounds[i].y,
barBounds[i].w,
barBounds[i].h
);
//Draw the tooltip rectangle
stroke(1);
fill("white");
rect(rectX, rectY, rectW, rectH);
//Draw the field name
fill(colorPalette[0]);
textStyle("bold");
noStroke();
text(
`${barBounds[i].field}`,
rectX + labelSize, //padding is 1 labelsize
rectY + labelSize * 1.5 //padding is half labelSize plus text's own height
);
//Draw the first part of the text (label)
fill(colorPalette[0]);
textStyle("bold");
noStroke();
text(
`${barBounds[i].label} : `,
rectX + labelSize, //padding is 1 labelsize
rectY + labelSize * 2.5 //padding is half labelSize plus text's own height
);
//Draw the second part of the text (value)
tmpLen = textWidth(`${barBounds[i].label} : `);
textStyle("normal");
text(
`${barBounds[i].value}`,
rectX + labelSize + tmpLen,
rectY + labelSize * 2.5
);
}
}
};
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;
};
}