Skip to content

Commit 1e14dfc

Browse files
correct totals and number formatting
1 parent 6e811fa commit 1e14dfc

3 files changed

Lines changed: 68 additions & 26 deletions

File tree

source/js/DataController.js

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,50 @@ DataController.prototype.setData = function (data) {
8787

8888
this._dataStack[this._dataStack.length - 1].data = data;
8989
//this.data = data;
90+
this.resetDimensionProps();
9091
this.resetRawData();
9192

9293
this._trigger();
9394
return data;
9495

9596
};
9697

98+
/**
99+
* Sets properties of rows/columns.
100+
*
101+
* @returns {null}
102+
*/
103+
DataController.prototype.resetDimensionProps = function () {
104+
105+
var data, columnProps = [];
106+
107+
if (!(data = this._dataStack[this._dataStack.length - 1].data)) {
108+
console.error("Unable to get dimension props for given data set.");
109+
return null;
110+
}
111+
112+
var parse = function (obj, props) {
113+
var tObj;
114+
if (obj["children"] && obj["children"].length > 0) {
115+
for (var i in obj.children) {
116+
tObj = obj.children[i];
117+
if (tObj["format"]) props["format"] = tObj["format"];
118+
if (tObj["style"]) props["style"] = (props["style"] || "") + tObj["style"];
119+
if (tObj["total"]) props["total"] = tObj["total"];
120+
if (tObj["type"]) props["type"] = tObj["type"];
121+
parse(tObj, props);
122+
}
123+
} else {
124+
columnProps.push(props);
125+
}
126+
};
127+
128+
parse({ children: data.dimensions[0] }, {});
129+
130+
data.columnProps = columnProps;
131+
132+
};
133+
97134
/**
98135
* Renders table data (pseudo-table object) from data retrieved from MDX2JSON source.
99136
*
@@ -262,7 +299,7 @@ DataController.prototype.resetRawData = function () {
262299
this.SUMMARY_SHOWN = false;
263300
this._dataStack[this._dataStack.length - 1].SUMMARY_SHOWN = false;
264301

265-
var countSummaryByColumn = function (array, iStart, iEnd, column) {
302+
var totalSUM = function (array, iStart, iEnd, column) {
266303
var sum = 0;
267304
for (var i = iStart; i < iEnd; i++) {
268305
if (!isFinite(array[i][column]["value"])) {
@@ -274,7 +311,7 @@ DataController.prototype.resetRawData = function () {
274311
return sum || "";
275312
};
276313

277-
var countAverageByColumn = function (array, iStart, iEnd, column) {
314+
var totalAVG = function (array, iStart, iEnd, column) {
278315
var sum = 0;
279316
for (var i = iStart; i < iEnd; i++) {
280317
if (!isFinite(array[i][column]["value"])) {
@@ -286,6 +323,18 @@ DataController.prototype.resetRawData = function () {
286323
return sum/(iEnd - iStart) || "";
287324
};
288325

326+
/**
327+
* @param {number} columnIndex
328+
* @returns {Function}
329+
*/
330+
var getTotalFunction = function (columnIndex) {
331+
if (!data["columnProps"][columnIndex]) return totalSUM;
332+
switch (data["columnProps"][columnIndex].total) {
333+
case "AVG": return totalAVG;
334+
default: return totalSUM;
335+
}
336+
};
337+
289338
if (this.controller.CONFIG["showSummary"] && rawData.length - xh > 1 // xh - see above
290339
&& (rawData[rawData.length - 1][0] || {})["isCaption"]) {
291340
this.SUMMARY_SHOWN = true;
@@ -303,11 +352,9 @@ DataController.prototype.resetRawData = function () {
303352
} else {
304353
summary[i] = {
305354
// very hard workaround (applying "avg" last column spec)
306-
value: ((rawData[x].length - 1 === parseInt(i)
307-
&& _.controller.CONFIG["_temp_lastColSpec"]
308-
&& _.controller.CONFIG["_temp_lastColSpec"]["levelSummary"] === "avg")
309-
? countAverageByColumn
310-
: countSummaryByColumn)(rawData, xh, rawData.length - 1, i),
355+
value:
356+
getTotalFunction(parseInt(i) - data.info.leftHeaderColumnsNumber)
357+
(rawData, xh, rawData.length - 1, i),
311358
// end
312359
//value: (countSummaryByColumn)(rawData, xh, rawData.length - 1, i),
313360
style: "font-weight: bold;"

source/js/DataSource.js

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,23 +155,6 @@ DataSource.prototype.getCurrentData = function (callback) {
155155
}
156156
}
157157

158-
// temporary hard workaround (getting last column specs)
159-
_.GLOBAL_CONFIG["_temp_lastColSpec"] = (function (lev) {
160-
var tc = lev,
161-
f = function (lev) {
162-
if (lev["childLevels"] && lev["childLevels"].length > 0) {
163-
for (var i in lev["childLevels"]) {
164-
f(lev["childLevels"][i]);
165-
}
166-
} else {
167-
tc = lev;
168-
}
169-
};
170-
if (lev) f(lev);
171-
return tc;
172-
})(data["columnLevels"][data["columnLevels"].length - 1]);
173-
// end
174-
175158
};
176159

177160
var handleDataReady = function () {

source/js/PivotView.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,7 @@ PivotView.prototype.renderRawData = function (data) {
511511
renderedGroups = {}, // keys of rendered groups; key = group, value = { x, y, element }
512512
rawData = data["rawData"],
513513
info = data["info"],
514+
columnProps = data["columnProps"],
514515
container = this.elements.tableContainer,
515516
pivotTopSection = document.createElement("div"),
516517
pivotBottomSection = document.createElement("div"),
@@ -632,8 +633,19 @@ PivotView.prototype.renderRawData = function (data) {
632633
for (x = info.leftHeaderColumnsNumber; x < rawData[0].length; x++) {
633634

634635
tr.appendChild(td = document.createElement("td"));
635-
if (!isFinite(rawData[y][x].value)) td.className += " formatLeft";
636-
td.textContent = rawData[y][x].value || "";
636+
if (!isFinite(rawData[y][x].value)) {
637+
td.className += " formatLeft";
638+
td.textContent = rawData[y][x].value || "";
639+
} else { // number
640+
if (columnProps[x - info.leftHeaderColumnsNumber].format) {
641+
td.textContent = this.formatNumber(
642+
columnProps[x - info.leftHeaderColumnsNumber].format,
643+
rawData[y][x].value || 0
644+
) || "";
645+
} else {
646+
td.textContent = rawData[y][x].value || "";
647+
}
648+
}
637649
if (rawData[y][x].style) td.setAttribute("style", rawData[y][x].style);
638650

639651
// add handlers

0 commit comments

Comments
 (0)