Skip to content

Commit 8394af5

Browse files
all types of totals support
1 parent 65be431 commit 8394af5

File tree

1 file changed

+84
-32
lines changed

1 file changed

+84
-32
lines changed

source/js/DataController.js

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ DataController.prototype.resetDimensionProps = function () {
118118
if (tObj["format"]) clonedProps["format"] = tObj["format"];
119119
if (tObj["style"]) clonedProps["style"] =
120120
(clonedProps["style"] || "") + tObj["style"];
121-
if (tObj["total"]) clonedProps["total"] = tObj["total"];
121+
if (tObj["summary"]) clonedProps["summary"] = tObj["summary"];
122122
if (tObj["type"]) clonedProps["type"] = tObj["type"];
123123
parse(tObj, clonedProps);
124124
}
@@ -133,6 +133,75 @@ DataController.prototype.resetDimensionProps = function () {
133133

134134
};
135135

136+
/**
137+
* Total functions definition. When adding new total function, also check getTotalFunction.
138+
* "this" in context of functions equals to TOTAL_FUNCTIONS object.
139+
*
140+
* @see getTotalFunction
141+
*/
142+
DataController.prototype.TOTAL_FUNCTIONS = {
143+
144+
totalSUM: function (array, iStart, iEnd, column) {
145+
var sum = 0;
146+
for (var i = iStart; i < iEnd; i++) {
147+
if (isFinite(array[i][column]["value"])) {
148+
sum += parseFloat(array[i][column]["value"]) || 0;
149+
}
150+
}
151+
return sum || "";
152+
},
153+
154+
totalAVG: function (array, iStart, iEnd, column) {
155+
var sum = 0;
156+
for (var i = iStart; i < iEnd; i++) {
157+
if (!isFinite(array[i][column]["value"])) {
158+
sum = 0;
159+
break;
160+
}
161+
sum += parseFloat(array[i][column]["value"]) || 0;
162+
}
163+
return sum/(iEnd - iStart) || "";
164+
},
165+
166+
totalCOUNT: function (array, iStart, iEnd) {
167+
return iEnd - iStart;
168+
},
169+
170+
totalMIN: function (array, iStart, iEnd, column) {
171+
var min = Infinity;
172+
for (var i = iStart; i < iEnd; i++) {
173+
if (isFinite(array[i][column]["value"]) && array[i][column]["value"] < min) {
174+
min = array[i][column]["value"];
175+
}
176+
}
177+
return min;
178+
},
179+
180+
totalMAX: function (array, iStart, iEnd, column) {
181+
var max = -Infinity;
182+
for (var i = iStart; i < iEnd; i++) {
183+
if (isFinite(array[i][column]["value"]) && array[i][column]["value"] > max) {
184+
max = array[i][column]["value"];
185+
}
186+
}
187+
return max;
188+
},
189+
190+
totalPERCENTAGE: function (array, iStart, iEnd, column, xStart) {
191+
var averages = [], x, summ;
192+
for (x = xStart; x < array[0].length; x++) {
193+
averages.push(this.totalSUM(array, iStart, iEnd, x));
194+
}
195+
summ = averages.reduce(function(a, b) { return a + b; });
196+
return (averages[column - xStart] / summ * 100 || 0).toFixed(2) + "%";
197+
},
198+
199+
totalNONE: function () {
200+
return "";
201+
}
202+
203+
};
204+
136205
/**
137206
* Renders table data (pseudo-table object) from data retrieved from MDX2JSON source.
138207
*
@@ -302,39 +371,20 @@ DataController.prototype.resetRawData = function () {
302371
this.SUMMARY_SHOWN = false;
303372
this._dataStack[this._dataStack.length - 1].SUMMARY_SHOWN = false;
304373

305-
var totalSUM = function (array, iStart, iEnd, column) {
306-
var sum = 0;
307-
for (var i = iStart; i < iEnd; i++) {
308-
if (!isFinite(array[i][column]["value"])) {
309-
sum = 0;
310-
break;
311-
}
312-
sum += parseFloat(array[i][column]["value"]) || 0;
313-
}
314-
return sum || "";
315-
};
316-
317-
var totalAVG = function (array, iStart, iEnd, column) {
318-
var sum = 0;
319-
for (var i = iStart; i < iEnd; i++) {
320-
if (!isFinite(array[i][column]["value"])) {
321-
sum = 0;
322-
break;
323-
}
324-
sum += parseFloat(array[i][column]["value"]) || 0;
325-
}
326-
return sum/(iEnd - iStart) || "";
327-
};
328-
329374
/**
330375
* @param {number} columnIndex
331376
* @returns {Function}
332377
*/
333378
var getTotalFunction = function (columnIndex) {
334-
if (!data["columnProps"][columnIndex]) return totalSUM;
335-
switch (data["columnProps"][columnIndex].total) {
336-
case "AVG": return totalAVG;
337-
default: return totalSUM;
379+
if (!data["columnProps"][columnIndex]) return _.TOTAL_FUNCTIONS.totalSUM;
380+
switch (data["columnProps"][columnIndex].summary) {
381+
case "count": return _.TOTAL_FUNCTIONS.totalCOUNT;
382+
case "avg": return _.TOTAL_FUNCTIONS.totalAVG;
383+
case "min": return _.TOTAL_FUNCTIONS.totalMIN;
384+
case "max": return _.TOTAL_FUNCTIONS.totalMAX;
385+
case "pct": return _.TOTAL_FUNCTIONS.totalPERCENTAGE;
386+
case "none": return _.TOTAL_FUNCTIONS.totalNONE;
387+
default: return _.TOTAL_FUNCTIONS.totalSUM;
338388
}
339389
};
340390

@@ -355,9 +405,11 @@ DataController.prototype.resetRawData = function () {
355405
applyHeaderStyle(summary[i], false);
356406
} else {
357407
summary[i] = {
358-
value: getTotalFunction(parseInt(i) - data.info.leftHeaderColumnsNumber)
359-
(rawData, xh, rawData.length - 1, i),
360-
style: "font-weight: bold;"
408+
value: getTotalFunction(parseInt(i) - data.info.leftHeaderColumnsNumber).call(
409+
this.TOTAL_FUNCTIONS,
410+
rawData, xh, rawData.length - 1, i, data.info.leftHeaderColumnsNumber
411+
),
412+
style: "font-weight: bold;text-align: right;"
361413
}
362414
}
363415
}

0 commit comments

Comments
 (0)