Skip to content

Commit 82a3593

Browse files
committedFeb 26, 2017
Row totals feature add
1 parent a868e95 commit 82a3593

File tree

3 files changed

+82
-28
lines changed

3 files changed

+82
-28
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "LightPivotTable",
33
"author": "ZitRo",
4-
"version": "1.7.5",
4+
"version": "1.8.0",
55
"description": "A lightweight pivot table for MDX2JSON source for InterSystems Cache",
66
"main": "test/testServer.js",
77
"repository": {

‎source/js/DataController.js

+73-24
Original file line numberDiff line numberDiff line change
@@ -295,59 +295,78 @@ DataController.prototype.TOTAL_FUNCTIONS = {
295295
return isFinite(a);
296296
},
297297

298-
totalSUM: function (array, iStart, iEnd, column) {
298+
totalSUM: function (array, iStart, iEnd, column, xStart, row) {
299299
var sum = 0;
300300
for (var i = iStart; i < iEnd; i++) {
301-
if (this.isNumber(array[i][column]["value"])) {
302-
sum += parseFloat(array[i][column]["value"]) || 0;
301+
var r = typeof row === "undefined" ? i : row,
302+
c = typeof column === "undefined" ? i : column;
303+
if (this.isNumber(array[r][c]["value"])) {
304+
sum += parseFloat(array[r][c]["value"]) || 0;
303305
}
304306
}
305307
return sum;
306308
},
307309

308-
totalAVG: function (array, iStart, iEnd, column) {
310+
totalAVG: function (array, iStart, iEnd, column, xStart, row) {
309311
var sum = 0;
310312
for (var i = iStart; i < iEnd; i++) {
311-
if (!this.isNumber(array[i][column]["value"])) {
313+
var r = typeof row === "undefined" ? i : row,
314+
c = typeof column === "undefined" ? i : column;
315+
if (!this.isNumber(array[r][c]["value"])) {
312316
sum = 0;
313317
break;
314318
}
315-
sum += parseFloat(array[i][column]["value"]) || 0;
319+
sum += parseFloat(array[r][c]["value"]) || 0;
316320
}
317321
return sum/(iEnd - iStart) || "";
318322
},
319323

320-
totalCOUNT: function (array, iStart, iEnd) {
321-
return iEnd - iStart;
324+
totalCOUNT: function (array, iStart, iEnd, column, xStart, row) {
325+
var count = 0;
326+
for (var i = iStart; i < iEnd; i++) {
327+
var r = typeof row === "undefined" ? i : row,
328+
c = typeof column === "undefined" ? i : column;
329+
if (array[r][c]["value"]) {
330+
count++;
331+
}
332+
}
333+
return count;
322334
},
323335

324-
totalMIN: function (array, iStart, iEnd, column) {
336+
totalMIN: function (array, iStart, iEnd, column, xStart, row) {
325337
var min = Infinity;
326338
for (var i = iStart; i < iEnd; i++) {
327-
if (this.isNumber(array[i][column]["value"]) && array[i][column]["value"] < min) {
328-
min = array[i][column]["value"];
339+
var r = typeof row === "undefined" ? i : row,
340+
c = typeof column === "undefined" ? i : column;
341+
if (this.isNumber(array[r][c]["value"]) && array[r][c]["value"] < min) {
342+
min = array[r][c]["value"];
329343
}
330344
}
331345
return min;
332346
},
333347

334-
totalMAX: function (array, iStart, iEnd, column) {
348+
totalMAX: function (array, iStart, iEnd, column, xStart, row) {
335349
var max = -Infinity;
336350
for (var i = iStart; i < iEnd; i++) {
337-
if (this.isNumber(array[i][column]["value"]) && array[i][column]["value"] > max) {
338-
max = array[i][column]["value"];
351+
var r = typeof row === "undefined" ? i : row,
352+
c = typeof column === "undefined" ? i : column;
353+
if (this.isNumber(array[r][c]["value"]) && array[r][c]["value"] > max) {
354+
max = array[r][c]["value"];
339355
}
340356
}
341357
return max;
342358
},
343359

344-
totalPERCENTAGE: function (array, iStart, iEnd, column, xStart) {
360+
totalPERCENTAGE: function (array, iStart, iEnd, column, xStart, row) {
345361
var averages = [], x, summ;
346-
for (x = xStart; x < array[0].length; x++) {
347-
averages.push(this.totalSUM(array, iStart, iEnd, x));
362+
for (x = xStart; x < typeof column === "undefined" ? array.length : array[0].length; x++) {
363+
averages.push(this.totalSUM(array, iStart, iEnd,
364+
typeof column === "undefined" ? column : x, xStart,
365+
typeof row === "undefined" ? row : x));
348366
}
349367
summ = averages.reduce(function(a, b) { return a + b; });
350-
return (averages[column - xStart] / summ * 100 || 0).toFixed(2) + "%";
368+
return (averages[(typeof row === "undefined" ? column : row) - xStart]
369+
/ summ * 100 || 0).toFixed(2) + "%";
351370
},
352371

353372
totalNONE: function () {
@@ -378,6 +397,8 @@ DataController.prototype.setLeftHeaderColumnsNumber = function (data) {
378397
*/
379398
DataController.prototype.resetRawData = function () {
380399

400+
var TOTALS_STYLE = "font-weight: bold;text-align: right;";
401+
381402
var data, summary, y, x,
382403
dimCaption,
383404
_ = this;
@@ -579,13 +600,16 @@ DataController.prototype.resetRawData = function () {
579600

580601
/**
581602
* @param {number} columnIndex
603+
* @param {boolean=false} byColumns
582604
* @returns {Function}
583605
*/
584-
var getTotalFunction = function (columnIndex) {
585-
var pivotDefault = _.controller.getPivotProperty(["rowTotalAgg"]);
586-
if (!data["columnProps"][columnIndex] && !pivotDefault)
606+
var getTotalFunction = function (columnIndex, byColumns) {
607+
var pivotDefault = _.controller.getPivotProperty(["rowTotalAgg"]) || "sum",
608+
pivotDefaultCol = _.controller.getPivotProperty(["columnTotalAgg"]) || "sum",
609+
props = byColumns ? "rowProps" : "columnProps";
610+
if (!data[props][columnIndex] && !(byColumns ? pivotDefaultCol : pivotDefault))
587611
return _.TOTAL_FUNCTIONS.totalSUM;
588-
switch ((data["columnProps"][columnIndex] || {}).summary || pivotDefault) {
612+
switch ((data[props][columnIndex] || {}).summary || pivotDefault) {
589613
case "count": return _.TOTAL_FUNCTIONS.totalCOUNT;
590614
case "avg": return _.TOTAL_FUNCTIONS.totalAVG;
591615
case "min": return _.TOTAL_FUNCTIONS.totalMIN;
@@ -596,7 +620,7 @@ DataController.prototype.resetRawData = function () {
596620
}
597621
};
598622

599-
if (this.controller.CONFIG["showSummary"] && rawData.length - xh > 1 // xh - see above
623+
if (this.controller.CONFIG["showSummary"] && rawData.length - data.info.topHeaderRowsNumber > 1
600624
&& (rawData[rawData.length - 1][0] || {})["isCaption"]) {
601625
data.info.SUMMARY_SHOWN = true;
602626
this.SUMMARY_SHOWN = true;
@@ -619,7 +643,7 @@ DataController.prototype.resetRawData = function () {
619643
this.TOTAL_FUNCTIONS,
620644
rawData, xh, rawData.length, i, data.info.leftHeaderColumnsNumber
621645
),
622-
style: "font-weight: bold;text-align: right;"
646+
style: TOTALS_STYLE
623647
}
624648
}
625649
}
@@ -632,6 +656,31 @@ DataController.prototype.resetRawData = function () {
632656
}
633657
}
634658

659+
if (this.controller.getPivotProperty(["columnTotals"])
660+
&& rawData.length - data.info.topHeaderRowsNumber > 1
661+
&& data.info.leftHeaderColumnsNumber > 0) {
662+
var group = ++groupNum,
663+
row;
664+
for (row = 0; row < data.info.topHeaderRowsNumber; row++) {
665+
rawData[row].push({
666+
group: group,
667+
isCaption: true,
668+
value: pivotLocale.get(0)
669+
});
670+
}
671+
for (row = data.info.topHeaderRowsNumber; row < rawData.length; row++) {
672+
rawData[row].push({
673+
isCaption: true,
674+
value: getTotalFunction(row, true).call(
675+
this.TOTAL_FUNCTIONS,
676+
rawData, data.info.leftHeaderColumnsNumber, rawData[row].length, undefined,
677+
data.info.leftHeaderColumnsNumber, row
678+
),
679+
style: TOTALS_STYLE
680+
});
681+
}
682+
}
683+
635684
rawData = parseColumnFormatting(rawData);
636685

637686
data.rawData = data._rawDataOrigin = rawData;

‎source/js/PivotLocale.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,25 @@ var PivotLocale = function (locale) {
2323
* @type {{ru: string, en: string, de: string}[]}
2424
*/
2525
PivotLocale.prototype.LOCALES = [
26-
{ "ru": "Всего", "en": "Total", "de": "Summe","cs":"Celkem" },
2726
{
27+
"ru": "Всего",
28+
"en": "Total",
29+
"de": "Summe",
30+
"cs": "Celkem"
31+
},
32+
{ // 1
2833
"ru": "Невозможно отобразить данные",
2934
"en": "Unable to render data",
3035
"de": "Daten können nicht rendern",
3136
"cs": "Nebylo možné zobrazit data"
3237
},
33-
{
38+
{ // 2
3439
"ru": "Неправильные данные для отображения.",
3540
"en": "Invalid data to display.",
3641
"de": "Nicht korrekt Informationen angezeigt werden soll.",
3742
"cs": "Nevhodná data k zobrazení"
3843
},
39-
{
44+
{ // 3
4045
"ru": "Возникла ошибка при получении данных с сервера.",
4146
"en": "Error while trying to retrieve data from server.",
4247
"de": "Beim Abrufen der Daten vom Server ist ein Fehler aufgetreten.",

0 commit comments

Comments
 (0)
Please sign in to comment.