Skip to content

Commit 8c8850e

Browse files
column props fix & clever number formatting (library used)
1 parent 09bfdd0 commit 8c8850e

File tree

2 files changed

+22
-74
lines changed

2 files changed

+22
-74
lines changed

source/js/DataController.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,20 @@ DataController.prototype.resetDimensionProps = function () {
110110
}
111111

112112
var parse = function (obj, props) {
113-
var tObj;
113+
var tObj, clonedProps = {}, i;
114+
for (i in props) clonedProps[i] = props[i];
114115
if (obj["children"] && obj["children"].length > 0) {
115-
for (var i in obj.children) {
116+
for (i in obj.children) {
116117
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);
118+
if (tObj["format"]) clonedProps["format"] = tObj["format"];
119+
if (tObj["style"]) clonedProps["style"] =
120+
(clonedProps["style"] || "") + tObj["style"];
121+
if (tObj["total"]) clonedProps["total"] = tObj["total"];
122+
if (tObj["type"]) clonedProps["type"] = tObj["type"];
123+
parse(tObj, clonedProps);
122124
}
123125
} else {
124-
columnProps.push(props);
126+
columnProps.push(clonedProps);
125127
}
126128
};
127129

source/js/PivotView.js

Lines changed: 12 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@
55
*/
66
var PivotView = function (controller, container) {
77

8+
/*! INCLUDES numeral.js LIBRARY FOR NUMBER FORMATTING
9+
* numeral.js
10+
* version : 1.5.3
11+
* author : Adam Draper
12+
* license : MIT
13+
* http://adamwdraper.github.com/Numeral-js/
14+
*/
15+
(function(){function a(a){this._value=a}function b(a,b,c,d){var e,f,g=Math.pow(10,b);return f=(c(a*g)/g).toFixed(b),d&&(e=new RegExp("0{1,"+d+"}$"),f=f.replace(e,"")),f}function c(a,b,c){var d;return d=b.indexOf("$")>-1?e(a,b,c):b.indexOf("%")>-1?f(a,b,c):b.indexOf(":")>-1?g(a,b):i(a._value,b,c)}function d(a,b){var c,d,e,f,g,i=b,j=["KB","MB","GB","TB","PB","EB","ZB","YB"],k=!1;if(b.indexOf(":")>-1)a._value=h(b);else if(b===q)a._value=0;else{for("."!==o[p].delimiters.decimal&&(b=b.replace(/\./g,"").replace(o[p].delimiters.decimal,".")),c=new RegExp("[^a-zA-Z]"+o[p].abbreviations.thousand+"(?:\\)|(\\"+o[p].currency.symbol+")?(?:\\))?)?$"),d=new RegExp("[^a-zA-Z]"+o[p].abbreviations.million+"(?:\\)|(\\"+o[p].currency.symbol+")?(?:\\))?)?$"),e=new RegExp("[^a-zA-Z]"+o[p].abbreviations.billion+"(?:\\)|(\\"+o[p].currency.symbol+")?(?:\\))?)?$"),f=new RegExp("[^a-zA-Z]"+o[p].abbreviations.trillion+"(?:\\)|(\\"+o[p].currency.symbol+")?(?:\\))?)?$"),g=0;g<=j.length&&!(k=b.indexOf(j[g])>-1?Math.pow(1024,g+1):!1);g++);a._value=(k?k:1)*(i.match(c)?Math.pow(10,3):1)*(i.match(d)?Math.pow(10,6):1)*(i.match(e)?Math.pow(10,9):1)*(i.match(f)?Math.pow(10,12):1)*(b.indexOf("%")>-1?.01:1)*((b.split("-").length+Math.min(b.split("(").length-1,b.split(")").length-1))%2?1:-1)*Number(b.replace(/[^0-9\.]+/g,"")),a._value=k?Math.ceil(a._value):a._value}return a._value}function e(a,b,c){var d,e,f=b.indexOf("$"),g=b.indexOf("("),h=b.indexOf("-"),j="";return b.indexOf(" $")>-1?(j=" ",b=b.replace(" $","")):b.indexOf("$ ")>-1?(j=" ",b=b.replace("$ ","")):b=b.replace("$",""),e=i(a._value,b,c),1>=f?e.indexOf("(")>-1||e.indexOf("-")>-1?(e=e.split(""),d=1,(g>f||h>f)&&(d=0),e.splice(d,0,o[p].currency.symbol+j),e=e.join("")):e=o[p].currency.symbol+j+e:e.indexOf(")")>-1?(e=e.split(""),e.splice(-1,0,j+o[p].currency.symbol),e=e.join("")):e=e+j+o[p].currency.symbol,e}function f(a,b,c){var d,e="",f=100*a._value;return b.indexOf(" %")>-1?(e=" ",b=b.replace(" %","")):b=b.replace("%",""),d=i(f,b,c),d.indexOf(")")>-1?(d=d.split(""),d.splice(-1,0,e+"%"),d=d.join("")):d=d+e+"%",d}function g(a){var b=Math.floor(a._value/60/60),c=Math.floor((a._value-60*b*60)/60),d=Math.round(a._value-60*b*60-60*c);return b+":"+(10>c?"0"+c:c)+":"+(10>d?"0"+d:d)}function h(a){var b=a.split(":"),c=0;return 3===b.length?(c+=60*Number(b[0])*60,c+=60*Number(b[1]),c+=Number(b[2])):2===b.length&&(c+=60*Number(b[0]),c+=Number(b[1])),Number(c)}function i(a,c,d){var e,f,g,h,i,j,k=!1,l=!1,m=!1,n="",r=!1,s=!1,t=!1,u=!1,v=!1,w="",x="",y=Math.abs(a),z=["B","KB","MB","GB","TB","PB","EB","ZB","YB"],A="",B=!1;if(0===a&&null!==q)return q;if(c.indexOf("(")>-1?(k=!0,c=c.slice(1,-1)):c.indexOf("+")>-1&&(l=!0,c=c.replace(/\+/g,"")),c.indexOf("a")>-1&&(r=c.indexOf("aK")>=0,s=c.indexOf("aM")>=0,t=c.indexOf("aB")>=0,u=c.indexOf("aT")>=0,v=r||s||t||u,c.indexOf(" a")>-1?(n=" ",c=c.replace(" a","")):c=c.replace("a",""),y>=Math.pow(10,12)&&!v||u?(n+=o[p].abbreviations.trillion,a/=Math.pow(10,12)):y<Math.pow(10,12)&&y>=Math.pow(10,9)&&!v||t?(n+=o[p].abbreviations.billion,a/=Math.pow(10,9)):y<Math.pow(10,9)&&y>=Math.pow(10,6)&&!v||s?(n+=o[p].abbreviations.million,a/=Math.pow(10,6)):(y<Math.pow(10,6)&&y>=Math.pow(10,3)&&!v||r)&&(n+=o[p].abbreviations.thousand,a/=Math.pow(10,3))),c.indexOf("b")>-1)for(c.indexOf(" b")>-1?(w=" ",c=c.replace(" b","")):c=c.replace("b",""),g=0;g<=z.length;g++)if(e=Math.pow(1024,g),f=Math.pow(1024,g+1),a>=e&&f>a){w+=z[g],e>0&&(a/=e);break}return c.indexOf("o")>-1&&(c.indexOf(" o")>-1?(x=" ",c=c.replace(" o","")):c=c.replace("o",""),x+=o[p].ordinal(a)),c.indexOf("[.]")>-1&&(m=!0,c=c.replace("[.]",".")),h=a.toString().split(".")[0],i=c.split(".")[1],j=c.indexOf(","),i?(i.indexOf("[")>-1?(i=i.replace("]",""),i=i.split("["),A=b(a,i[0].length+i[1].length,d,i[1].length)):A=b(a,i.length,d),h=A.split(".")[0],A=A.split(".")[1].length?o[p].delimiters.decimal+A.split(".")[1]:"",m&&0===Number(A.slice(1))&&(A="")):h=b(a,null,d),h.indexOf("-")>-1&&(h=h.slice(1),B=!0),j>-1&&(h=h.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g,"$1"+o[p].delimiters.thousands)),0===c.indexOf(".")&&(h=""),(k&&B?"(":"")+(!k&&B?"-":"")+(!B&&l?"+":"")+h+A+(x?x:"")+(n?n:"")+(w?w:"")+(k&&B?")":"")}function j(a,b){o[a]=b}function k(a){var b=a.toString().split(".");return b.length<2?1:Math.pow(10,b[1].length)}function l(){var a=Array.prototype.slice.call(arguments);return a.reduce(function(a,b){var c=k(a),d=k(b);return c>d?c:d},-1/0)}var m,n="1.5.3",o={},p="en",q=null,r="0,0",s="undefined"!=typeof module&&module.exports;m=function(b){return m.isNumeral(b)?b=b.value():0===b||"undefined"==typeof b?b=0:Number(b)||(b=m.fn.unformat(b)),new a(Number(b))},m.version=n,m.isNumeral=function(b){return b instanceof a},m.language=function(a,b){if(!a)return p;if(a&&!b){if(!o[a])throw new Error("Unknown language : "+a);p=a}return(b||!o[a])&&j(a,b),m},m.languageData=function(a){if(!a)return o[p];if(!o[a])throw new Error("Unknown language : "+a);return o[a]},m.language("en",{delimiters:{thousands:",",decimal:"."},abbreviations:{thousand:"k",million:"m",billion:"b",trillion:"t"},ordinal:function(a){var b=a%10;return 1===~~(a%100/10)?"th":1===b?"st":2===b?"nd":3===b?"rd":"th"},currency:{symbol:"$"}}),m.zeroFormat=function(a){q="string"==typeof a?a:null},m.defaultFormat=function(a){r="string"==typeof a?a:"0.0"},"function"!=typeof Array.prototype.reduce&&(Array.prototype.reduce=function(a,b){"use strict";if(null===this||"undefined"==typeof this)throw new TypeError("Array.prototype.reduce called on null or undefined");if("function"!=typeof a)throw new TypeError(a+" is not a function");var c,d,e=this.length>>>0,f=!1;for(1<arguments.length&&(d=b,f=!0),c=0;e>c;++c)this.hasOwnProperty(c)&&(f?d=a(d,this[c],c,this):(d=this[c],f=!0));if(!f)throw new TypeError("Reduce of empty array with no initial value");return d}),m.fn=a.prototype={clone:function(){return m(this)},format:function(a,b){return c(this,a?a:r,void 0!==b?b:Math.round)},unformat:function(a){return"[object Number]"===Object.prototype.toString.call(a)?a:d(this,a?a:r)},value:function(){return this._value},valueOf:function(){return this._value},set:function(a){return this._value=Number(a),this},add:function(a){function b(a,b){return a+c*b}var c=l.call(null,this._value,a);return this._value=[this._value,a].reduce(b,0)/c,this},subtract:function(a){function b(a,b){return a-c*b}var c=l.call(null,this._value,a);return this._value=[a].reduce(b,this._value*c)/c,this},multiply:function(a){function b(a,b){var c=l(a,b);return a*c*b*c/(c*c)}return this._value=[this._value,a].reduce(b,1),this},divide:function(a){function b(a,b){var c=l(a,b);return a*c/(b*c)}return this._value=[this._value,a].reduce(b),this},difference:function(a){return Math.abs(m(this._value).subtract(a).value())}},s&&(module.exports=m),"undefined"==typeof ender&&(this.numeral=m),"function"==typeof define&&define.amd&&define([],function(){return m})}).call(this);
16+
817
if (!(container instanceof HTMLElement)) throw new Error("Please, provide HTMLElement " +
918
"instance \"container\" into pivot table configuration.");
1019

@@ -20,11 +29,6 @@ var PivotView = function (controller, container) {
2029

2130
this.controller = controller;
2231

23-
/**
24-
* @private
25-
*/
26-
this._scrollListener = null;
27-
2832
/**
2933
* Fixed headers links.
3034
*
@@ -231,52 +235,6 @@ PivotView.prototype.removeMessage = function () {
231235

232236
};
233237

234-
/**
235-
* Poor function that provides number formatting.
236-
*
237-
* @param {string} mask - String like "#'###.##"
238-
* @param {number} value
239-
* @returns {string}
240-
*/
241-
PivotView.prototype.formatNumber = function (mask, value) {
242-
243-
var begin = true,
244-
fp = mask.match(/#+|[^#]+/g),
245-
integerPart, fractionalPart,
246-
last = -1, ip1, fp1;
247-
248-
ip1 = integerPart = parseInt(value).toString();
249-
fp1 = fractionalPart = (parseFloat(value) - parseInt(integerPart))
250-
.toString()
251-
.concat((new Array(mask.length))
252-
.join("0"));
253-
for (var i = fp.length - 1; i > -1; i--) {
254-
if (fp[i][0] !== "#") continue;
255-
if (begin) {
256-
fp[i] = fractionalPart.substr(2, fp[i].length); // flooring
257-
begin = false;
258-
} else {
259-
fp[i] = integerPart
260-
.substr(Math.max(integerPart.length - fp[i].length, 0), integerPart.length);
261-
integerPart = integerPart.substr(0, integerPart.length - fp[i].length);
262-
last = i;
263-
}
264-
if (integerPart.length === 0) {
265-
fp = fp.slice(i, fp.length);
266-
break;
267-
}
268-
}
269-
if (fp.join("") === "4.0.") {
270-
console.log(ip1, fp1);
271-
}
272-
if (last !== -1 && integerPart.length !== 0) {
273-
fp[0] = integerPart + fp[0];
274-
}
275-
276-
return fp.join("");
277-
278-
};
279-
280238
/**
281239
* @param {HTMLElement} container
282240
*/
@@ -411,13 +369,6 @@ PivotView.prototype.renderRawData = function (data) {
411369

412370
// setup th
413371
if (rendered = renderedGroups.hasOwnProperty(rawData[y][x].group)) {
414-
//console.log("Already rendered group " + rawData[y][x].group,
415-
// renderedGroups[rawData[y][x].group].element,
416-
// "modifying col/row 'span", "rendered on ("
417-
// + renderedGroups[rawData[y][x].group].x + ";"
418-
// + renderedGroups[rawData[y][x].group].y + "), now on (" +
419-
// + x + "; " + y + ")");
420-
// recalculate c/r 'span
421372
if (x > 0 && rawData[y][x - 1].group === rawData[y][x].group) {
422373
separatelyGrouped = false;
423374
renderedGroups[rawData[y][x].group].element.colSpan =
@@ -504,14 +455,9 @@ PivotView.prototype.renderRawData = function (data) {
504455
td.className += " formatLeft";
505456
td.textContent = rawData[y][x].value || "";
506457
} else { // number
507-
if (columnProps[x - info.leftHeaderColumnsNumber].format) {
508-
td.textContent = this.formatNumber(
509-
columnProps[x - info.leftHeaderColumnsNumber].format,
510-
rawData[y][x].value || 0
511-
) || "";
512-
} else {
513-
td.textContent = rawData[y][x].value || "";
514-
}
458+
td.textContent = rawData[y][x].value ? this.numeral(rawData[y][x].value).format(
459+
columnProps[x - info.leftHeaderColumnsNumber].format || ""
460+
) : "";
515461
}
516462
if (rawData[y][x].style) td.setAttribute("style", rawData[y][x].style);
517463

0 commit comments

Comments
 (0)