-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
1593 lines (1389 loc) · 71.2 KB
/
index.js
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Custom Aggregators
(function () {
var callWithJQuery,
indexOf = [].indexOf || function (item) {
for (var i = 0, l = this.length; i < l; i++) {
if (i in this && this[i] === item) return i;
}
return -1;
},
slice = [].slice,
bind = function (fn, me) {
return function () {
return fn.apply(me, arguments);
};
},
hasProp = {}.hasOwnProperty;;
callWithJQuery = function (pivotModule) {
if (typeof exports === "object" && typeof module === "object") {
return pivotModule(require("jquery"));
} else if (typeof define === "function" && define.amd) {
return define(["jquery"], pivotModule);
} else {
return pivotModule(jQuery);
}
};
callWithJQuery(function ($) {
var multifactAggregator = function (aggMap, derivedAggregations) {
var allAggregators = $.map(aggMap, function (aggregation, key) {
var agg = $.pivotUtilities.aggregators[aggregation.aggType];
// console.log({a:agg})
var _numInputs = agg([])().numInputs || 0;
return {
aggregator: agg,
selfNumInputs: _numInputs,
name: aggregation.name,
key: key,
varName: aggregation.varName,
hidden: aggregation.hidden,
format: aggregation.format
}
});
return function (facts) {
//console.log('multifact sum agg called with ', facts);
var aggregations = $.map(allAggregators, function (_agg) {
//console.log(aggMap[_agg.name].arguments);
return {
aggregator: _agg.aggregator(aggMap[_agg.key].arguments),
key: _agg.key,
name: _agg.name,
varName: _agg.varName,
hidden: _agg.hidden,
format: _agg.format
};
})
return function (data, rowKey, colKey) {
var finalAggregators = $.map(aggregations, function (_agg) {
return {
aggregator: _agg.aggregator(data, rowKey, colKey),
key: _agg.key,
name: _agg.name,
varName: _agg.varName,
hidden: _agg.hidden,
format: _agg.format
};
});
var _finalAggregatorsNameMap = {};
for (var i = 0, x = finalAggregators.length; i < x; i++) {
var aggregation = finalAggregators[i];
_finalAggregatorsNameMap[aggregation.name] = aggregation;
}
var _finalDerivedAggregatorsNameMap = {};
for (var i = 0, x = derivedAggregations.length; i < x; i++) {
var derivedAggregation = derivedAggregations[i];
_finalDerivedAggregatorsNameMap[derivedAggregation.name] = derivedAggregation;
}
//console.log(finalAggregators);
if (!facts && !!data && !!data.valAttrs) {
facts = data.valAttrs;
}
//console.log('aggregator called for', data, rowKey, colKey)
var analytics = {};
return {
label: "Facts",
push: function (record) {
for (var i = 0, x = finalAggregators.length; i < x; i++) {
var aggregation = finalAggregators[i];
//console.log('Final Aggregation', aggregation)
aggregation.aggregator.push(record);
}
},
inner : {
value : function(){
try{
//extract which stat is this being called for
var stat = arguments.callee.caller.arguments[0];
//Get the aggregator for this stat
var aggregator = _finalAggregatorsNameMap[stat];
return aggregator.aggregator.inner.value();
} catch(e){
return -100;
}
}
}
,
multivalue: function () {
analytics = {};
var variables = {};
var finalAnalytics = {};
for (var i = 0, x = finalAggregators.length; i < x; i++) {
var aggregation = finalAggregators[i];
analytics[aggregation.name] = aggregation.aggregator.value(aggregation.name);
variables[aggregation.varName] = analytics[aggregation.name];
if (!aggregation.hidden) {
finalAnalytics[aggregation.name] = analytics[aggregation.name];
}
}
//console.log(analytics)
//console.log(variables);
var _derivedAnalytics = {};
for (var i = 0, x = derivedAggregations.length; i < x; i++) {
var derivedAggregation = derivedAggregations[i];
var derivedVal = 0;
var expression = 'derivedVal = ' + derivedAggregation.expression;
eval(expression);
_derivedAnalytics[derivedAggregation.name] = derivedVal;
}
//console.log(_derivedAnalytics);
finalAnalytics = $.extend(finalAnalytics, _derivedAnalytics)
return finalAnalytics;
},
// return the first element for unsupported renderers.
value: function () {
return 'Multi-Fact-Aggregator does not support single value';
},
format: function (x, aggKey) {
//console.log('i for formatter',i);
var formatter = null;
//console.log(_finalAggregatorsNameMap,aggKey)
if (!!_finalAggregatorsNameMap[aggKey]) {
formatter = _finalAggregatorsNameMap[aggKey].format;
} else if (!!_finalDerivedAggregatorsNameMap[aggKey]) {
var formatterOptions = $.extend({}, _finalDerivedAggregatorsNameMap[aggKey].formatterOptions);
formatter = $.pivotUtilities.numberFormat(formatterOptions);
}
if (!formatter) {
formatter = $.pivotUtilities.numberFormat();
}
//console.log(formatter)
return formatter(x);
}
};
};
};
}
$.pivotUtilities.multifactAggregatorGenerator = multifactAggregator;
$.fn.gtBarchart = function(opts) {
var barcharter, i, l, numCols, numRows, ref;
numRows = this.data("numrows");
numCols = this.data("numcols");
var _finalAggregatorsNameMap = {};
var finalAggregators = $.map(opts.aggregations.defaultAggregations, function (aggregation) {
return aggregation;
}) || [];
for (i = 0, x = finalAggregators.length; i < x; i++) {
var aggregation = finalAggregators[i];
_finalAggregatorsNameMap[aggregation.name] = aggregation;
}
var derivedAggregations = opts.aggregations.derivedAggregations || [];
var _finalDerivedAggregatorsNameMap = {};
for (i = 0, x = derivedAggregations.length; i < x; i++) {
var derivedAggregation = derivedAggregations[i];
_finalDerivedAggregatorsNameMap[derivedAggregation.name] = derivedAggregation;
}
var aggregationMap = $.extend(true, {}, _finalAggregatorsNameMap, _finalDerivedAggregatorsNameMap)
barcharter = (function(_this) {
return function(scope) {
var forEachCell, max, min, range, values, valueSets;
forEachCell = function(f) {
return _this.find(scope).each(function() {
var x, valueAttributeKey;
x = $(this).data("value");
valueAttributeKey = $(this).data("value-for");
if ((x != null) && isFinite(x)) {
return f(x, $(this), valueAttributeKey);
}
});
};
values = [];
valueSets = {};
forEachCell(function(x, elem, valueAttributeKey) {
valueSets[valueAttributeKey] = valueSets[valueAttributeKey] || [];
valueSets[valueAttributeKey].push(x);
return values.push(x);
});
var scalerSet = {};
Object.keys(valueSets).forEach(function(key){
if(valueSets.hasOwnProperty(key)){
max = Math.max.apply(Math, values);
if (max < 0) {
max = 0;
}
range = max;
min = Math.min.apply(Math, values);
if (min < 0) {
range = max - min;
}
scalerSet[key] = (function(){ return function(x) {
return 100 * x / (1.4 * range);
}})();
}
});
return forEachCell(function(x, elem, valueAttributeKey) {
var bBase, bgColor, text, wrapper, scaler;
scaler = scalerSet[valueAttributeKey];
var opts = aggregationMap[valueAttributeKey];
if(opts && opts.renderEnhancement ==='barchart'){
text = elem.text();
wrapper = $("<div>").css({
"position": "relative",
"width": "120px"
});
bgColor = opts.barchartColor || "steelblue";
bBase = 0;
if (min < 0) {
bBase = scaler(-min);
}
if (x < 0) {
bBase += scaler(x);
bgColor = opts.barchartNegativeColor || "darkred";
x = -x;
}
wrapper.append($("<div>").css({
"position": "absolute",
"top": bBase + "%",
"left": 0,
//"right": 0,
"height" : "12px",
"width": scaler(x) + "%",
"background-color": bgColor
}));
wrapper.append($("<div>").text(text).css({
"position": "relative",
"padding-left": "5px",
"padding-right": "5px"
}));
return elem.css({
"padding": 0,
"padding-top": "5px",
"text-align": "right"
}).html(wrapper);
} else {
return elem;
}
});
};
})(this);
for (i = l = 0, ref = numRows; 0 <= ref ? l < ref : l > ref; i = 0 <= ref ? ++l : --l) {
barcharter(".pvtVal.row" + i);
}
//barcharter(".pvtTotal.colTotal");
return this;
};
$.fn.gtHeatmap = function (scope, opts) {
var colorScaleGenerator, heatmapper, i, x, j, l, n, numCols, numRows, ref, ref1, ref2, _finalAggregatorsNameMap, _finalDerivedAggregatorsNameMap;
if (scope == null) {
scope = "heatmap";
}
numRows = this.data("numrows");
numCols = this.data("numcols");
_finalAggregatorsNameMap = {};
var finalAggregators = $.map(opts.aggregations.defaultAggregations, function (aggregation) {
return aggregation;
}) || [];
for (i = 0, x = finalAggregators.length; i < x; i++) {
var aggregation = finalAggregators[i];
_finalAggregatorsNameMap[aggregation.name] = aggregation;
}
var derivedAggregations = opts.aggregations.derivedAggregations || [];
_finalDerivedAggregatorsNameMap = {};
for (i = 0, x = derivedAggregations.length; i < x; i++) {
var derivedAggregation = derivedAggregations[i];
_finalDerivedAggregatorsNameMap[derivedAggregation.name] = derivedAggregation;
}
var aggregationMap = $.extend(true, {}, _finalAggregatorsNameMap, _finalDerivedAggregatorsNameMap)
colorScaleGenerator = opts != null ? (ref = opts.heatmap) != null ? ref.colorScaleGenerator : void 0 : void 0;
if (colorScaleGenerator == null) {
colorScaleGenerator = function (values) {
var max, min;
min = Math.min.apply(Math, values);
max = Math.max.apply(Math, values);
return function (x) {
var nonRed;
nonRed = 255 - Math.round(255 * (x - min) / (max - min));
return "rgb(255," + nonRed + "," + nonRed + ")";
};
};
}
heatmapper = (function (_this) {
return function (scope) {
var colorScale, forEachCell, values, valueSets, colorScaleSets;
forEachCell = function (f) {
return _this.find(scope).each(function () {
var x, valueAttributeKey;
x = $(this).data("value");
valueAttributeKey = $(this).data("value-for");
if ((x != null) && isFinite(x)) {
return f(x, $(this), valueAttributeKey);
}
});
};
values = [];
valueSets = {};
forEachCell(function (x, elem, valueAttributeKey) {
valueSets[valueAttributeKey] = valueSets[valueAttributeKey] || [];
valueSets[valueAttributeKey].push(x);
return values.push(x);
});
// console.log(valueSets);
colorScaleSets = {};
Object.keys(valueSets).forEach(function(key){
if(valueSets.hasOwnProperty(key)){
colorScaleSets[key] = colorScaleGenerator(valueSets[key], key)
}
});
return forEachCell(function (x, elem, valueAttributeKey) {
var opts = aggregationMap[valueAttributeKey];
if(opts && opts.renderEnhancement ==='heatmap'){
var cs = colorScaleSets[valueAttributeKey](x);
elem.css("background-color", cs);
if(cs !== "rgb(255,255,255)"){
elem.css("color","#FFFFFF");
}
return elem;
} else {
return elem;
}
});
};
})(this);
switch (scope) {
case "heatmap":
heatmapper(".pvtVal");
break;
case "rowheatmap":
for (i = l = 0, ref1 = numRows; 0 <= ref1 ? l < ref1 : l > ref1; i = 0 <= ref1 ? ++l : --l) {
heatmapper(".pvtVal.row" + i);
}
break;
case "colheatmap":
for (j = n = 0, ref2 = numCols; 0 <= ref2 ? n < ref2 : n > ref2; j = 0 <= ref2 ? ++n : --n) {
heatmapper(".pvtVal.col" + j);
}
}
heatmapper(".pvtTotal.rowTotal");
heatmapper(".pvtTotal.colTotal");
return this;
};
});
var pivotTableRenderer = function (pivotData, opts) {
var aggregator, c, colAttrs, colKey, colKeys, defaults, getClickHandler, getMouseEnterHandler, getMouseLeaveHandler, getMouseMoveHandler, i, j, r, result,
rowAttrs, rowKey, rowKeys, spanSize, tbody, td, th, thead, totalAggregator, tr, txt, val, x,
valueAttrs;
//this will be used to store the string representation of the table
const stringRows = [];
defaults = {
table: {
clickCallback: null,
mouseEnterCallback: null,
mouseLeaveCallback: null,
mouseMoveCallback: null,
rowTotals: true,
colTotals: true
},
localeStrings: {
totals: "Totals"
},
//group rows
group: true,
//show subtotals per row
rowSubtotal: false,
//show subtotals per column
columnSubtotal: false
};
opts = $.extend(true, {}, defaults, opts);
colAttrs = pivotData.colAttrs;
rowAttrs = pivotData.rowAttrs;
//now valueAttrs will come from analytics keys
valueAttrs = [];//pivotData.valAttrs;
rowKeys = pivotData.getRowKeys();
colKeys = pivotData.getColKeys();
//extra column/row span used to correct the size of cell in some particular scenarios
//such as a table with no row/column dimensions
var extraRowSpan = 1;
var extraColSpan = 1;
var aggregationMap = {};
if(opts.table.aggregationConfig){
Object.keys(opts.table.aggregationConfig).forEach(function(key){
if(opts.table.aggregationConfig.hasOwnProperty(key)){
var conf = opts.table.aggregationConfig[key];
aggregationMap[conf.name] = conf;
}
});
}
// get cell field type
var fieldType = (label) => {
if (label !== null && label !== "undefined" && label !== "") {
let type = opts.fieldTypes.filter(x => x.label === label);
if (type.length > 0) {
return type[0].fieldType;
}
}
return 'String';
}
//First get to know what does the value list look like?
var aggregator = pivotData.getAggregator([], []);
var multipleValues = aggregator.multivalue();
Object.keys(multipleValues).forEach(function(key){
if(multipleValues.hasOwnProperty(key)){
valueAttrs.push(key);
}
});
// console.log(valueAttrs);
var enableDebug = !!window.enablePivotDebug;
if (opts.table.clickCallback) {
getClickHandler = function (value, rowValues, colValues) {
var attr, filters, i;
filters = {};
for (i in colAttrs) {
if (!hasProp.call(colAttrs, i)) continue;
attr = colAttrs[i];
if (colValues[i] != null) {
filters[attr] = colValues[i];
}
}
for (i in rowAttrs) {
if (!hasProp.call(rowAttrs, i)) continue;
attr = rowAttrs[i];
if (rowValues[i] != null) {
filters[attr] = rowValues[i];
}
}
return function (e) {
return opts.table.clickCallback(e, value, filters, pivotData);
};
};
}
if (opts.table.mouseEnterCallback) {
getMouseEnterHandler = function (value, rowValues, colValues) {
var attr, filters, i;
filters = {};
for (i in colAttrs) {
if (!hasProp.call(colAttrs, i)) continue;
attr = colAttrs[i];
if (colValues[i] != null) {
filters[attr] = colValues[i];
}
}
for (i in rowAttrs) {
if (!hasProp.call(rowAttrs, i)) continue;
attr = rowAttrs[i];
if (rowValues[i] != null) {
filters[attr] = rowValues[i];
}
}
return function (e) {
return opts.table.mouseEnterCallback(e, value, filters, pivotData);
};
};
}
if (opts.table.mouseLeaveCallback) {
getMouseLeaveHandler= function (value, rowValues, colValues) {
var attr, filters, i;
filters = {};
for (i in colAttrs) {
if (!hasProp.call(colAttrs, i)) continue;
attr = colAttrs[i];
if (colValues[i] != null) {
filters[attr] = colValues[i];
}
}
for (i in rowAttrs) {
if (!hasProp.call(rowAttrs, i)) continue;
attr = rowAttrs[i];
if (rowValues[i] != null) {
filters[attr] = rowValues[i];
}
}
return function (e) {
return opts.table.mouseLeaveCallback(e, value, filters, pivotData);
};
};
}
if (opts.table.mouseMoveCallback) {
getMouseMoveHandler= function (value, rowValues, colValues) {
var attr, filters, i;
filters = {};
for (i in colAttrs) {
if (!hasProp.call(colAttrs, i)) continue;
attr = colAttrs[i];
if (colValues[i] != null) {
filters[attr] = colValues[i];
}
}
for (i in rowAttrs) {
if (!hasProp.call(rowAttrs, i)) continue;
attr = rowAttrs[i];
if (rowValues[i] != null) {
filters[attr] = rowValues[i];
}
}
return function (e) {
return opts.table.mouseMoveCallback(e, value, filters, pivotData);
};
};
}
result = document.createElement("table");
result.className = "pvtTable";
/**
* Function to get the span
* @param arr
* @param i
* @param j
* @returns {number}
*/
spanSize = function (arr, i, j) {
var l, len, n, noDraw, ref, ref1, stop, x;
if (i !== 0) {
noDraw = true;
for (x = l = 0, ref = j; 0 <= ref ? l <= ref : l >= ref; x = 0 <= ref ? ++l : --l) {
if (arr[i - 1][x] !== arr[i][x]) {
noDraw = false;
}
}
if (noDraw) {
return -1;
}
}
len = 0;
while (i + len < arr.length) {
stop = false;
for (x = n = 0, ref1 = j; 0 <= ref1 ? n <= ref1 : n >= ref1; x = 0 <= ref1 ? ++n : --n) {
if (arr[i][x] !== arr[i + len][x]) {
stop = true;
}
}
if (stop) {
break;
}
len++;
}
return len;
};
thead = document.createElement("thead");
function renderHeaderForTotal(label) {
if (parseInt(j) === 0 && opts.table.rowTotals) {
th = document.createElement("th");
th.className = "pvtTotalLabel pvtRowTotalLabel";
th.innerHTML = opts.localeStrings.totals;
var numHiddenMeasures = 0;
Object.keys(aggregationMap).forEach(function (key) {
if (aggregationMap[key].hideInTotals) {
numHiddenMeasures ++;
}
});
th.setAttribute("colspan", valueAttrs.length - numHiddenMeasures);
//Added 1 for value headers
th.setAttribute("rowspan", colAttrs.length);
tr.appendChild(th);
}
}
//creates a new columnkeyarray
// the pivottables uses an column key array which is passed as parameter in the options
// and defines the columns that are going to be shown in the pivottable
// this pluging 'multifact-pivottable' creates as many 'total columns' (ex. Cost to client, clicks, etc) as
// column keys we have. The idea here, if we have the column subtotal option on, is to add another column key ('Total')
// adding that column key will force the code to add the total columns for it
// the result would be something like this:
// +------------------------+------------------------+
// | 2019 | Totals |
// +----------------+-------+----------------+-------+
// | Cost to Client | Click | Cost to Client | Click |
// +----------------+-------+----------------+-------+
// | 0 | 0 | 0 | 0 |
// +----------------+-------+----------------+-------+
var auxiliarColumnKey = [];
if (opts.columnSubtotal && colKeys.length > 0 && colKeys[0].length > 1) {
var positions = {};
for (i in colKeys) {
var ck = _.cloneDeep(colKeys[i]);
auxiliarColumnKey.push(colKeys[i]);
for (j in ck.reverse()) {
//get the amount of rows that the group will use.
x = spanSize(colKeys, parseInt(i),
parseInt(colKeys[i].length - parseInt(j) - 1));
//if the span size is more than one row and we are not in the last row key save it
if (x > 0 && j > 0 && !positions.hasOwnProperty(ck[j])) {
//saving header col position and rowspan
positions[ck[j]] = {pos: 0, q: 0};
positions[ck[j]].pos = parseInt(j);
positions[ck[j]].q = x;
}
if (positions.hasOwnProperty(ck[j]) &&
positions[ck[j]].pos === parseInt(j) &&
(positions[ck[j]].q - 1) === 0) {
//creates a new rowKey like [APAC, ASIA, subtotal]
delete positions[ck[j]];
var newColKey = colKeys[parseInt(i)].slice(0,
colKeys[parseInt(i)].length - j);
auxiliarColumnKey.push(newColKey.concat(
MMP.Localization.Reporting.ReportStatusView.PivotTable.colTotalsHeader));
}
if (positions.hasOwnProperty(ck[j]) &&
!((ck[j].q - 1) === 0)) {
positions[ck[j]].q--;
}
}
}
} else {
auxiliarColumnKey = colKeys;
}
//creates each header in the pivottalbe for each column key
function renderHeadersForValues(label) {
for (i in auxiliarColumnKey) {
if (!hasProp.call(auxiliarColumnKey, i)) continue;
colKey = auxiliarColumnKey[i];
x = spanSize(auxiliarColumnKey, parseInt(i), parseInt(j));
if (x !== -1) {
th = document.createElement("th");
th.className = "pvtColLabel";
//adjust colspan according to multiple variables
x = x * valueAttrs.length;
th.textContent = colKey[j];
th.setAttribute("colspan", x);
th.setAttribute("data-cell-type", fieldType(label));
if (parseInt(j) === colAttrs.length - 1 && rowAttrs.length !== 0) {
th.setAttribute("rowspan", extraRowSpan);
}
//if the column is the total one then add the appropriate class to it
if(colKey[j] === MMP.Localization.Reporting.ReportStatusView.PivotTable.colTotalsHeader){
th.classList.add('totals');
}
tr.appendChild(th);
}
}
}
for (j in colAttrs) {
if (!hasProp.call(colAttrs, j)) continue;
c = colAttrs[j];
tr = document.createElement("tr");
if (parseInt(j) === 0 && rowAttrs.length > 1) {
th = document.createElement("th");
th.setAttribute("colspan", rowAttrs.length - 1);
th.setAttribute("rowspan", colAttrs.length);
th.className = "pvtBlank";
tr.appendChild(th);
}
th = document.createElement("th");
th.className = "pvtAxisLabel";
th.textContent = c;
tr.appendChild(th);
if(opts.table.prependRowTotals){
renderHeaderForTotal(c);
renderHeadersForValues(c);
} else {
renderHeadersForValues(c);
renderHeaderForTotal(c);
}
thead.appendChild(tr);
}
var rowsHeaders = [];
var totalsHeader = [];
if (rowAttrs.length !== 0) {
for (i in rowAttrs) {
if (!hasProp.call(rowAttrs, i)) continue;
r = rowAttrs[i];
th = document.createElement("th");
th.className = "pvtAxisLabel";
th.textContent = r;
//storing row headers
rowsHeaders.push($(th));
}
th = document.createElement("th");
if (colAttrs.length === 0) {
th.className = "pvtTotalLabel pvtRowTotalLabel";
th.setAttribute("colspan", valueAttrs.length);
th.innerHTML = opts.localeStrings.totals;
//storing column total headers
totalsHeader.push($(th));
}
rowsHeaders[rowsHeaders.length - 1].attr('colspan', colKeys.length > 0 ? extraColSpan : 0);
}
result.appendChild(thead);
tbody = document.createElement("tbody");
tbody.classList.add("clusterize-no-data");
tbody.id = "contentAreaBody"
/**
*
*
*
* Following Part Adds the headers for multiple measures
*
*
*
*
*/
//Setting up the value headers
tr = document.createElement("tr");
//blank header
th = document.createElement("th");
th.textContent = '';
//Add 1 to end if there are no cols
th.setAttribute("colspan", rowAttrs.length+(colKeys.length > 0 ? extraRowSpan : 0));
th.className = "pvtMeasureBlank";
//adding blank header if there is no columns
if (colAttrs.length === 0) {
var row = $('<tr>');
totalsHeader.push(th);
row.append(totalsHeader.reverse());
tbody.appendChild(row[0]);
}
//adding a blank header if there is no rows
if (rowAttrs.length === 0) {
rowsHeaders.push($(th));
}
$(tr).append(rowsHeaders);
/**
* Add the headers for multiple measures here
*/
function renderMeasureHeadersForValues() {
//Add the headers for multiple measures here
for (var k = 0, x = auxiliarColumnKey.length; k < x; k++) {
var colKey = auxiliarColumnKey[k];
if (!!colKey) {
var idx = 0;
for (var _key in valueAttrs) {
th = document.createElement("th");
th.className = "pvtMeasureLabel";
th.textContent = valueAttrs[idx++];
tr.appendChild(th);
}
}
}
}
/**
* For the Totals header to have the headers of multiple measures
*/
function renderMeasuredHeadersForTotals() {
if (opts.table.rowTotals) {
for (var l = 0, x = valueAttrs.length; l < x; l++) {
var valAttr = valueAttrs[l];
if (aggregationMap[valAttr] && aggregationMap[valAttr].hideInTotals) {
continue;
}
th = document.createElement("th");
th.className = "pvtMeasureLabel pvtMeasureTotalLabel";
th.textContent = valAttr;
tr.appendChild(th);
}
}
}
if(opts.table.prependRowTotals){
renderMeasuredHeadersForTotals();
renderMeasureHeadersForValues();
} else {
renderMeasureHeadersForValues();
renderMeasuredHeadersForTotals();
}
tbody.appendChild(tr);
var subtotals = {};
var colSubtotal = {};
// if the grouprow options is on then a new row is going to be added with the values we've already accumulated in renderValueCells function
function renderSubtotalCells() {
//get the next accumulator
subtotals = getNextAccumulator(accumulators);
//we have subtotals per column and here we iterate each of them in order tu put the correct value on each subtotal cell
for (var col in subtotals){
for (var stat in subtotals[col]) {
td = document.createElement('td');
// td.className = 'pvtVal row' + i + ' col' + j + ' stat' + idx;
var valueSpan = $('<span>');
if(subtotals[col][stat].aggregator && subtotals[col][stat].acc) {
valueSpan.append(
$('<span>').html(subtotals[col][stat].aggregator.format(subtotals[col][stat].acc, stat)));
}
//if the accumulator doesn't have anything then show a 0
if(subtotals[col][stat].acc === 0){
valueSpan.append(
$('<span>').html("0"));
}
td.append(valueSpan[0]);
td.setAttribute('data-value', val);
td.setAttribute('data-row', i);
td.setAttribute('data-stat-index', idx);
td.setAttribute('data-col', j);
td.setAttribute('data-value-for', stat);
td.setAttribute("data-cell-type", fieldType(stat));
td.className = 'subtotal';
tr.appendChild(td);
stringRows.push(tr.outerHTML);
}
}
//the subtotals is set to empty when the first element of the rowkeys changes
subtotals = {};
}
function renderValueCells() {
for (j in auxiliarColumnKey) {
if (!hasProp.call(auxiliarColumnKey, j)) continue;
colKey = auxiliarColumnKey[j];
aggregator = rowKey.includes(MMP.Localization.Reporting.ReportStatusView.PivotTable.rowTotalsHeader) ? pivotData.getAggregator([], colKey) : pivotData.getAggregator(rowKey, colKey);
val = aggregator.value();
// Keep track on when we start the rendering
opts.renderStart = opts.renderStart || new Date();
// Check how long the report has been rendering and stop if it has been processing for too long
var elapsedTime = new Date() - opts.renderStart;
if (elapsedTime > opts.longRunningReportTimeoutSeconds * 1000) {
// We have been executing for over a minute, use the callback and stop the process
if (opts.longRunningReportCallback) {
opts.longRunningReportCallback(elapsedTime / 1000);
}
throw "The report has been rendering for too long. Stopping the process.";
}
/**
* In this section we are adding values to these cells
*/
if (!!aggregator.multivalue) {
var stats = aggregator.multivalue(rowKey, colKey);
var idx = 0;
for (var stat in stats) {
val = stats[stat];
//if the row subtotal is on then start accumulating each value for each row and column
if (opts.rowSubtotal) {
createRowSubtotalProperties(j,stat, accumulators);
createColumnSubtotalProperties(j,stat, accumulators);
//accumulates the values
if(stats[stat] && stats[stat] !== "") {
setValuesToAccumulators(j,stat, stats[stat], aggregator, accumulators);
}
}
// if the iteration does not reach out to Totals, which is the new item we added to the columnkey array
// then continue accumulating the column values
if(opts.columnSubtotal && !colKey.includes(MMP.Localization.Reporting.ReportStatusView.PivotTable.colTotalsHeader)){
createRowSubtotalProperties(stat,"", columnAcc, true);
//if the stat already exists
if(stats[stat] && stat[stat] !== ""){
setValuesToAccumulators(stat, "", stats[stat], aggregator, columnAcc, true);
}
}
td = document.createElement("td");
td.className = "pvtVal row" + i + " col" + j + " stat" + idx;
td.setAttribute("data-cell-type", fieldType(stat));
//td.textContent = stat + ' : ' + aggregator.format(val);
//td.textContent = aggregator.format(val);
var valueSpan = $('<span>');
if (enableDebug) {
valueSpan.append($('<span>').html(stat).addClass('small text-grey'));
valueSpan.append($('<br>'));
}
//when the iteration reach to the total column key added then put the values in the new cells
if (opts.columnSubtotal && colKey.includes(MMP.Localization.Reporting.ReportStatusView.PivotTable.colTotalsHeader)) {
var accum = getNextAccumulator(columnAcc);
if (acum && acum[stat]) {
val = accum[stat].acc;
aggregator = accum[stat].aggregator ||
aggregator;
}
td.classList.add('totals');
}
valueSpan.append($('<span>').html(aggregator.format(val, stat)));
td.append(valueSpan[0]);
td.setAttribute("data-value", val);
td.setAttribute("data-row", i);
td.setAttribute("data-stat-index", idx);
td.setAttribute("data-col", j);
td.setAttribute("data-value-for", stat);
td.setAttribute("data-cell-type", fieldType(stat));
if (getClickHandler != null) {