-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarGraph.js
More file actions
724 lines (566 loc) · 21.7 KB
/
BarGraph.js
File metadata and controls
724 lines (566 loc) · 21.7 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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
if(!javaxt) var javaxt={};
if(!javaxt.dhtml) javaxt.dhtml={};
//******************************************************************************
//** BarGraph
//******************************************************************************
/**
* Used to create a simple bar graph.
*
******************************************************************************/
javaxt.dhtml.BarGraph = function (parent, config) {
this.className = "javaxt.dhtml.BarGraph";
var me = this;
var chartRow, tickRow, labelRow;
var innerTable, dataTable, yAxis, horizontalLines;
var cellWidth;
var values = [];
var minValue = 0;
var maxValue = 0;
var template = {};
var defaultConfig = {
animate: false, //null or false to disable
animationSteps: 750,
maxHorizonalLabels: 3,
style: {
cell: {
borderBottom: "1px solid #ccc",
padding: "0 5px"
},
bar: {
backgroundColor: "#7baaf7",
borderRadius: "3px 3px 0 0",
cursor: "pointer"
},
tick: {
borderLeft: "1px solid #ccc",
borderRight: "1px solid #ccc",
height: "5px"
},
labelX: {
textAlign: "center",
color: "#757575",
fontSize: "10px",
height: "14px"
},
labelY: {
textAlign: "right",
color: "#757575",
fontSize: "10px",
padding: "0 5px 0 0",
margin: "-5px 0 0 0"
},
hline:{
borderTop: "1px solid #ccc",
opacity: 0.5,
zIndex: 1
},
select:{
backgroundColor: "orange"
}
}
};
//**************************************************************************
//** Constructor
//**************************************************************************
var init = function(){
if (typeof parent === "string"){
parent = document.getElementById(parent);
}
if (!parent) return;
//Clone the config so we don't modify the original config object
var clone = {};
merge(clone, config);
//Merge clone with default config
merge(clone, defaultConfig);
config = clone;
//Create relative div used to bound the horizontal bars
var mainDiv = createElement('div', parent, {
height: "100%",
position: "relative"
});
mainDiv.setAttribute("desc", me.className);
me.el = mainDiv;
//Create table with one cell with a "bottom" vertical alignment
//used for animation purposes
var table = createTable(mainDiv);
var td = table.addRow().addColumn({
width: "100%",
height: "100%",
verticalAlign: "bottom"
});
//Create table with 2 columns. The first column is used for y-axis labels
//and the second column is used for the bar graphs and x-axis labels.
innerTable = createTable(td);
var tr = innerTable.addRow();
td = tr.addColumn({
width: "1px",
height: "100%",
verticalAlign: "top"
});
yAxis = createTable(td);
yAxis.setAttribute("desc", "yAxis");
td = tr.addColumn({
width: "100%",
height: "100%"
});
//Create relative div for the second column
var outerDiv = createElement('div', td, {
width: "100%",
height: "100%",
position: "relative"
});
//Add horizontal lines in the second column as an absolute div
var div = createElement('div', outerDiv, {
width: "100%",
height: "100%",
position: "absolute",
top: 0,
overflow: "hidden",
padding: "0 1px" //hack for chrome and safari
});
var innerDiv = createElement('div', div, {
width: "100%",
position: "relative"
});
horizontalLines = createTable(innerDiv);
//Add vertical bars in the second column as an absolute div
var div2 = createElement('div', outerDiv, {
width: "100%",
height: "100%",
position: "absolute",
overflow: "auto",
overflowY: "hidden",
padding: "0 1px" //hack for chrome and safari
});
dataTable = createTable(div2);
dataTable.setAttribute("desc", "dataTable");
chartRow = dataTable.addRow();
tickRow = dataTable.addRow();
labelRow = dataTable.addRow();
if (config.animate===true){
innerTable.style.height = 0;
}
onRender(outerDiv, function(){
me.update();
});
};
//**************************************************************************
//** show
//**************************************************************************
this.show = function(){
me.el.style.display = '';
};
//**************************************************************************
//** hide
//**************************************************************************
this.hide = function(){
me.el.style.display = 'none';
};
//**************************************************************************
//** add
//**************************************************************************
/** Used to add a value to the graph. Label is optional.
*/
this.add = function(value, label){
if (!isNumber(value)) value=0;
values.push(value);
if (value>maxValue) maxValue = value;
if (value<minValue) minValue = value;
var td, bar;
if (!template.cell){
//Add column
td = createElement('td');
setStyle(td, "cell");
td.style.height = "100%";
td.style.verticalAlign = "bottom";
var div = createElement('div', td, {
height: "100%",
position: "relative"
});
//Add bar
bar = createElement('div', div);
setStyle(bar, "bar");
bar.style.position = "absolute";
bar.style.bottom = 0;
bar.style.width = "100%";
template.cell = td;
}
else{
td = template.cell.cloneNode(true);
bar = td.childNodes[0].childNodes[0];
}
bar.onclick = function(){
var col = this.parentNode.parentNode;
for (var i=0; i<chartRow.childNodes.length; i++){
if (chartRow.childNodes[i]==col){
me.onClick(values[i], i, this);
break;
}
}
};
td.onclick = function(){
var col = this;
for (var i=0; i<chartRow.childNodes.length; i++){
if (chartRow.childNodes[i]==col){
me.onCellClick(i, col);
break;
}
}
};
chartRow.appendChild(td);
//Update column and table width as needed
if (config.style.cell.width){
td.style.width = config.style.bar.width;
td.childNodes[0].style.width = config.style.bar.width;
cellWidth = parseInt(config.style.cell.width);
}
else{
if (typeof config.style.cell === "string"){
if (!cellWidth){
var temp = createElement('div');
setStyle(temp, "cell");
temp.style.position = "absolute";
temp.style.visibility = 'hidden';
temp.style.display = 'block';
var body = document.getElementsByTagName("body")[0];
body.appendChild(temp);
var style = temp.currentStyle || window.getComputedStyle(temp);
var getStyle = function(prop){
if (style.getPropertyValue){
var val = style.getPropertyValue(prop);
if (val && val.length>0) return val;
return style.getPropertyValue(prop);
}
else{
return style[prop];
}
};
cellWidth = parseInt(getStyle("width"));
body.removeChild(temp);
temp = null;
}
}
}
//Add tick
var td = tickRow.addColumn();
setStyle(td, "tick");
//Add label
td = labelRow.addColumn();
setStyle(td, "labelX");
if (label!=null){
var outerDiv = createElement('div', td, {
width: "100%",
height: "100%",
position: "relative"
});
var innerDiv = createElement('div', outerDiv, {
width: "100%",
height: "100%",
position: "absolute"
});
if (typeof label === "string" || isNumber(label)){
innerDiv.innerHTML = label;
}
else if (label instanceof Function){
label.apply(me, [innerDiv]);
}
}
};
//**************************************************************************
//** setValue
//**************************************************************************
this.setValue = function(idx, value){
values[idx] = value;
if (value>maxValue) maxValue = value;
if (value<minValue) minValue = value;
};
//**************************************************************************
//** update
//**************************************************************************
this.update = function(callback){
//Update heights of all the bars
for (var i=0; i<values.length; i++){
var div = chartRow.childNodes[i].childNodes[0].childNodes[0];
if (div){
var val = (values[i]/maxValue)*100;
div.style.height = val + "%";
}
}
//Update width of the data table
if (!isNaN(cellWidth)){
if (cellWidth>0){
var width = (cellWidth*values.length) + "px";
dataTable.style.width = width;
horizontalLines.parentNode.style.width = width;
}
}
//Calculate number of horizontal bars
var numHorizonalLabels = yAxis.firstChild.childNodes.length;
var maxHorizonalLabels = config.maxHorizonalLabels;
var y = getUniqueValues(values).length;
if (y<maxHorizonalLabels){
maxHorizonalLabels = y;
}
var rowHeight = Math.round((1/maxHorizonalLabels)*100) + "%";
//Update maxHorizonalLabels if all the values are 0
if (y===1 && values[0]===0){
rowHeight = "100%";
maxHorizonalLabels = 0;
}
//Update numHorizonalLabels if the number is greater than maxHorizonalLabels
var t1 = yAxis.firstChild;
var t2 = horizontalLines.firstChild;
if (numHorizonalLabels>maxHorizonalLabels){
while (t1.childNodes.length>0){
t1.removeChild(t1.childNodes[0]);
}
while (t2.childNodes.length>0){
t2.removeChild(t2.childNodes[0]);
}
numHorizonalLabels = 0;
}
//Add new horizonal bars and labels as needed
for (var i=numHorizonalLabels; i<maxHorizonalLabels; i++){
//Add label and tick on the y-axis
var tr = yAxis.addRow();
var td = tr.addColumn({verticalAlign: "top"});
var div = createElement('div', td);
setStyle(div, "labelY");
td.label = div;
var td = tr.addColumn({verticalAlign: "top"});
var div = createElement('div', td);
setStyle(div, "hline");
div.style.width = "100%";
//Add horizontal line
tr = horizontalLines.addRow();
td = tr.addColumn({verticalAlign: "top"});
var div = createElement('div', td);
setStyle(div, "hline");
div.style.width = "100%";
div.style.position = "absolute";
}
//Update heights and labels
for (var i=0; i<t1.childNodes.length; i++){
var td = t1.childNodes[i].childNodes[0];
td.style.height = rowHeight;
var numRows = t1.childNodes.length;
var idx = numRows-i;
var stepSize = maxValue/maxHorizonalLabels;
var val = stepSize*idx;
me.createLabel(val, idx, numRows, td.label);
}
for (var i=0; i<t2.childNodes.length; i++){
var td = t2.childNodes[i].childNodes[0];
td.style.height = rowHeight;
}
if (config.animate===true){
yAxis.style.display = 'none';
animate(new Date().getTime(), config.animationSteps, function(){
yAxis.style.display = '';
yAxis.style.height = chartRow.offsetHeight + "px";
horizontalLines.style.height = chartRow.offsetHeight + "px";
if (callback!=null){
callback.apply(me, []);
}
});
}
else{
yAxis.style.height = chartRow.offsetHeight + "px";
horizontalLines.style.height = chartRow.offsetHeight + "px";
if (callback!=null){
callback.apply(me, []);
}
}
};
//**************************************************************************
//** clear
//**************************************************************************
this.clear = function(saveColumns){
minValue = 0;
maxValue = 0;
me.deselectAll();
if (saveColumns===true){
for (var i=0; i<values.length; i++){
values[i] = 0;
}
//me.update(); //<-- Do not use b/c it update labels. Instead do this:
for (var i=0; i<values.length; i++){
var div = chartRow.childNodes[i].childNodes[0].childNodes[0];
div.style.height = "0%";
}
}
else{
values = [];
while (chartRow.childNodes.length>1){
chartRow.removeChild(chartRow.childNodes[1]);
tickRow.removeChild(tickRow.childNodes[1]);
labelRow.removeChild(labelRow.childNodes[1]);
}
dataTable.style.width = "100%";
horizontalLines.parentNode.style.width = "100%";
me.update();
}
};
//**************************************************************************
//** onClick
//**************************************************************************
/** Called whenever a bar in the graph is clicked.
*/
this.onClick = function(val, idx, div){};
//**************************************************************************
//** onCellClick
//**************************************************************************
/** Called whenever a cell in the graph is clicked.
*/
this.onCellClick = function(idx, cell){};
//**************************************************************************
//** getCell
//**************************************************************************
/** Returns a cell in the bargraph at a given index
*/
this.getCell = function(idx){
return chartRow.childNodes[idx];
};
//**************************************************************************
//** select
//**************************************************************************
/** Used to "select" a bar in the graph by adding the select style to the
* bar.
*/
this.select = function(idx){
var td = chartRow.childNodes[idx];
if (td){
td.selected = true;
var bar = td.childNodes[0].childNodes[0];
addStyle(bar, "select");
}
};
//**************************************************************************
//** deselect
//**************************************************************************
/** Used to "deselect" a bar in the graph by removing the select style from
* the bar.
*/
this.deselect = function(idx){
var td = chartRow.childNodes[idx];
if (td){
td.selected = false;
var bar = td.childNodes[0].childNodes[0];
var h = bar.style.height;
setStyle(bar, "bar");
bar.style.position = "absolute";
bar.style.bottom = 0;
bar.style.width = "100%";
bar.style.height = h;
}
};
//**************************************************************************
//** selectAll
//**************************************************************************
/** Used to "select" all the bars in the graph by adding the select style to
* the bars.
*/
this.selectAll = function(){
for (var i=0; i<chartRow.childNodes.length; i++){
me.select(i);
}
};
//**************************************************************************
//** deselectAll
//**************************************************************************
/** Used to "deselect" all the bars in the graph by removing the select
* style from all the bars.
*/
this.deselectAll = function(){
for (var i=0; i<chartRow.childNodes.length; i++){
me.deselect(i);
}
};
//**************************************************************************
//** getSelectedItems
//**************************************************************************
/** Returns an array of selected items. Each item in the array includes the
* index, value, and cell
*/
this.getSelectedItems = function(){
var arr = [];
for (var i=0; i<chartRow.childNodes.length; i++){
var td = chartRow.childNodes[i];
if (td.selected) arr.push({
idx: i,
val: values[i],
cell: td
});
}
return arr;
};
//**************************************************************************
//** createLabel
//**************************************************************************
/** Default implementation. Users can override.
*/
this.createLabel = function(val, idx, max, div){
div.innerHTML = Math.round(val);
};
//**************************************************************************
//** getValues
//**************************************************************************
this.getValues = function(){
var arr = [];
for (var i=0; i<values.length; i++){
arr.push(values[i]);
}
return arr;
};
//**************************************************************************
//** getUniqueValues
//**************************************************************************
var getUniqueValues = function(arr) {
var a = [];
for (var i=0, l=arr.length; i<l; i++)
if (a.indexOf(arr[i]) === -1 && arr[i] !== '')
a.push(arr[i]);
return a;
};
//**************************************************************************
//** isNumber
//**************************************************************************
var isNumber = function(n) { return !isNaN(parseFloat(n)) && !isNaN(n - 0); };
//**************************************************************************
//** animate
//**************************************************************************
/** Used to adjust the hieght of the bar graph.
*/
var animate = function(lastTick, timeLeft, callback){
var curTick = new Date().getTime();
var elapsedTicks = curTick - lastTick;
//If the animation is complete, ensure that the panel is completely open
if (timeLeft <= elapsedTicks){
innerTable.style.height = "100%";
if (callback!=null){
callback.apply(me, []);
}
return;
}
timeLeft -= elapsedTicks;
var percentComplete = (timeLeft/config.animationSteps)*100;
innerTable.style.height = (100-percentComplete) + "%";
setTimeout(function(){
animate(curTick, timeLeft, callback);
}, 33);
};
//**************************************************************************
//** Utils
//**************************************************************************
var merge = javaxt.dhtml.utils.merge;
var onRender = javaxt.dhtml.utils.onRender;
var createTable = javaxt.dhtml.utils.createTable;
var createElement = javaxt.dhtml.utils.createElement;
var setStyle = function(el, style){
javaxt.dhtml.utils.setStyle(el, config.style[style]);
};
var addStyle = function(el, style){
javaxt.dhtml.utils.addStyle(el, config.style[style]);
};
init();
};