Skip to content

Commit dd9439d

Browse files
fixed hard formatting issues (latest MDX2JSON compatibility?), Caché date format support
1 parent e014903 commit dd9439d

File tree

4 files changed

+103
-15
lines changed

4 files changed

+103
-15
lines changed

gulpfile.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ gulp.task("gatherScripts", ["clean"], function () {
3636
.pipe(uglify({
3737
output: {
3838
ascii_only: true,
39-
width: 30000,
40-
max_line_len: 30000
39+
width: 25000,
40+
max_line_len: 25000
4141
}
4242
}))
4343
.pipe(header(banner, { pkg: pkg }))

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.3.3",
4+
"version": "1.4.2",
55
"description": "A lightweight pivot table for MDX2JSON source for InterSystems Cache",
66
"main": "test/testServer.js",
77
"repository": {

source/js/DataController.js

+35-9
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ DataController.prototype.setData = function (data) {
9797

9898
this._dataStack[this._dataStack.length - 1].data = data;
9999
//this.data = data;
100+
this.setLeftHeaderColumnsNumber(data); // required in resetDimensionProps()
100101
this.resetDimensionProps();
101102
this.resetConditionalFormatting();
102103
this.resetRawData();
@@ -130,13 +131,16 @@ DataController.prototype.setDrillThroughHandler = function (handler) {
130131
*/
131132
DataController.prototype.resetDimensionProps = function () {
132133

133-
var data, columnProps = [];
134+
var data, columnProps;
134135

135136
if (!(data = this._dataStack[this._dataStack.length - 1].data)) {
136137
console.error("Unable to get dimension props for given data set.");
137138
return;
138139
}
139140

141+
columnProps = new Array(data.info.leftHeaderColumnsNumber || 0); // fill left headers as empty
142+
for (var i = 0; i < columnProps.length; i++) { columnProps[i] = {}; }
143+
140144
var cloneObj = function (obj) {
141145
var i, newObj = {};
142146
for (i in obj) newObj[i] = obj[i];
@@ -153,12 +157,14 @@ DataController.prototype.resetDimensionProps = function () {
153157
if (tObj["style"]) clonedProps["style"] =
154158
(clonedProps["style"] || "") + tObj["style"];
155159
if (tObj["summary"]) clonedProps["summary"] = tObj["summary"];
160+
// somehow "summary" were changed to "total" - reapplying
161+
if (tObj["total"]) clonedProps["summary"]
162+
= (tObj["total"] || "").toLowerCase().replace(/:.*/, ""); // what is "max:Days"?
156163
if (tObj["type"]) clonedProps["type"] = tObj["type"];
157164
parse(tObj, clonedProps);
158165
}
159166
} else {
160-
clonedProps = cloneObj(props);
161-
columnProps.push(clonedProps);
167+
columnProps.push(cloneObj(props));
162168
}
163169
};
164170

@@ -270,11 +276,16 @@ DataController.prototype.resetConditionalFormatting = function () {
270276
* @see getTotalFunction
271277
*/
272278
DataController.prototype.TOTAL_FUNCTIONS = {
279+
280+
isNumber: function (a) {
281+
if (a == "") return false;
282+
return isFinite(a);
283+
},
273284

274285
totalSUM: function (array, iStart, iEnd, column) {
275286
var sum = 0;
276287
for (var i = iStart; i < iEnd; i++) {
277-
if (isFinite(array[i][column]["value"])) {
288+
if (this.isNumber(array[i][column]["value"])) {
278289
sum += parseFloat(array[i][column]["value"]) || 0;
279290
}
280291
}
@@ -284,7 +295,7 @@ DataController.prototype.TOTAL_FUNCTIONS = {
284295
totalAVG: function (array, iStart, iEnd, column) {
285296
var sum = 0;
286297
for (var i = iStart; i < iEnd; i++) {
287-
if (!isFinite(array[i][column]["value"])) {
298+
if (!this.isNumber(array[i][column]["value"])) {
288299
sum = 0;
289300
break;
290301
}
@@ -300,7 +311,7 @@ DataController.prototype.TOTAL_FUNCTIONS = {
300311
totalMIN: function (array, iStart, iEnd, column) {
301312
var min = Infinity;
302313
for (var i = iStart; i < iEnd; i++) {
303-
if (isFinite(array[i][column]["value"]) && array[i][column]["value"] < min) {
314+
if (this.isNumber(array[i][column]["value"]) && array[i][column]["value"] < min) {
304315
min = array[i][column]["value"];
305316
}
306317
}
@@ -310,7 +321,7 @@ DataController.prototype.TOTAL_FUNCTIONS = {
310321
totalMAX: function (array, iStart, iEnd, column) {
311322
var max = -Infinity;
312323
for (var i = iStart; i < iEnd; i++) {
313-
if (isFinite(array[i][column]["value"]) && array[i][column]["value"] > max) {
324+
if (this.isNumber(array[i][column]["value"]) && array[i][column]["value"] > max) {
314325
max = array[i][column]["value"];
315326
}
316327
}
@@ -332,6 +343,21 @@ DataController.prototype.TOTAL_FUNCTIONS = {
332343

333344
};
334345

346+
DataController.prototype.setLeftHeaderColumnsNumber = function (data) {
347+
348+
function getLev (o, lev) {
349+
if (!(o.children instanceof Array)) return lev;
350+
var nextLev = lev + 1;
351+
for (var i = 0; i < o.children.length; i++) {
352+
lev = Math.max(getLev(o.children[i], nextLev), lev);
353+
}
354+
return lev;
355+
}
356+
357+
data.info.leftHeaderColumnsNumber = getLev({ children: data.dimensions[1] || [] }, 0);
358+
359+
};
360+
335361
/**
336362
* Renders table data (pseudo-table object) from data retrieved from MDX2JSON source.
337363
*
@@ -540,7 +566,7 @@ DataController.prototype.resetRawData = function () {
540566
var pivotDefault = _.controller.getPivotProperty(["rowTotalAgg"]);
541567
if (!data["columnProps"][columnIndex] && !pivotDefault)
542568
return _.TOTAL_FUNCTIONS.totalSUM;
543-
switch (data["columnProps"][columnIndex].summary || pivotDefault) {
569+
switch ((data["columnProps"][columnIndex] || {}).summary || pivotDefault) {
544570
case "count": return _.TOTAL_FUNCTIONS.totalCOUNT;
545571
case "avg": return _.TOTAL_FUNCTIONS.totalAVG;
546572
case "min": return _.TOTAL_FUNCTIONS.totalMIN;
@@ -570,7 +596,7 @@ DataController.prototype.resetRawData = function () {
570596
applyHeaderStyle(summary[i], false);
571597
} else {
572598
summary[i] = {
573-
value: getTotalFunction(parseInt(i) - data.info.leftHeaderColumnsNumber).call(
599+
value: getTotalFunction(parseInt(i)).call(
574600
this.TOTAL_FUNCTIONS,
575601
rawData, xh, rawData.length, i, data.info.leftHeaderColumnsNumber
576602
),

source/js/PivotView.js

+65-3
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,63 @@ PivotView.prototype.recalculateSizes = function (container) {
834834

835835
};
836836

837+
/**
838+
* Converts retrieved from mdx2json date to JS date format.
839+
* @param {string} s Date as string
840+
* @returns {number} Date as number
841+
* @author Anton Gnibeda (https://github.com/gnibeda)
842+
*/
843+
PivotView.prototype.getUnixDateFromCacheFormat = function (s) {
844+
function addDays(date, days) {
845+
var result = new Date(date);
846+
result.setDate(date.getDate() + days);
847+
return result;
848+
}
849+
function getDate(str) {
850+
var months = [
851+
"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"
852+
];
853+
854+
var d = Date.parse(str);
855+
if (!isNaN(d)) return d;
856+
if (str.split("-").length == 2) {
857+
var parts = str.split("-");
858+
var idx = months.indexOf(parts[0].toLowerCase());
859+
if (idx != -1) {
860+
return Date.parse((idx+1).toString() + "/01/" + parts[1]);
861+
}
862+
} else
863+
if (str.split(" ").length == 2) {
864+
//like 2015-01-07 05
865+
var timeParts = str.split(" ")[1].split(":").length;
866+
if (timeParts === 0) str += ":00";
867+
d = Date.parse(str.replace(/-/g, "/"));
868+
if (!isNaN(d)) return d;
869+
}
870+
return 0;
871+
}
872+
if (s === "" && s === undefined || s === null) return null;
873+
var str = s.toString();
874+
if (str.length == 4) return getDate(s);
875+
if (str.indexOf("-") != -1) return getDate(s);
876+
if (str.indexOf(" ") != -1) return getDate(s);
877+
if (str.length == 6) {
878+
var y = str.substr(0, 4);
879+
var m = str.substr(4, 2);
880+
return Date.parse(new Date(parseInt(y), parseInt(m)-1, 1));
881+
}
882+
if (str.length == 5 && !isNaN(parseInt(str))) {
883+
var base = new Date(1840, 11, 31);
884+
var p = str.toString().split(",");
885+
var d = parseInt(p[0]);
886+
var t = null;
887+
if (p.length > 1) t = parseInt(p[1]);
888+
base = addDays(base, parseInt(d));
889+
if (t) base.setSeconds(t);
890+
return Date.parse(base);
891+
} else return getDate(s);
892+
};
893+
837894
/**
838895
* Raw data - plain 2-dimensional array of data to render.
839896
*
@@ -924,7 +981,12 @@ PivotView.prototype.renderRawData = function (data) {
924981
+ p + "</a>";
925982
});
926983
} else if (!LISTING) { // number
927-
if (format) { // set format
984+
if (format === "%date%") { // Cach? internal date
985+
var d = new Date(_.getUnixDateFromCacheFormat(value));
986+
if (isNaN(d.getTime())) { element.textContent = value; return; }
987+
element.textContent = d.getHours() + d.getMinutes() + d.getSeconds() === 0
988+
? d.toLocaleDateString() : d.toLocaleString();
989+
} else if (format) { // set format
928990
element.textContent = value ? _.numeral(value).format(format) : "";
929991
} else if (value) {
930992
element.textContent = _.numeral(value).format(
@@ -1074,7 +1136,7 @@ PivotView.prototype.renderRawData = function (data) {
10741136
if (!rawData[y][x].isCaption) formatContent(
10751137
rawData[y][x].value,
10761138
th,
1077-
columnProps[x - info.leftHeaderColumnsNumber].format
1139+
columnProps[x].format
10781140
);
10791141
}
10801142

@@ -1169,7 +1231,7 @@ PivotView.prototype.renderRawData = function (data) {
11691231
formatContent(
11701232
rawData[y][x].value,
11711233
div,
1172-
columnProps[x - info.leftHeaderColumnsNumber].format
1234+
columnProps[x].format
11731235
);
11741236
if (
11751237
colorScale

0 commit comments

Comments
 (0)