forked from rwjblue/pivot.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpivot.min.js
71 lines (71 loc) · 11.6 KB
/
pivot.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @docauthor Jonathan Jackson
* @class Pivot
* # Welcome to Pivot.js
*
* Pivot.js is a simple way for you to get to your data. It allows for the
* creation of highly customizable unique table views from your browser.
*
* > In data processing, a pivot table is a data summarization tool found in
* > data visualization programs such as spreadsheets or business intelligence
* > software. Among other functions, pivot-table tools can automatically sort,
* > count, total or give the average of the data stored in one table or
* > spreadsheet. It displays the results in a second table (called a "pivot
* > table") showing the summarized data.
*
* In our case, results (or the pivot-table) will be displayed as an HTML table
* pivoting from the input data (CSV or JSON). Without further ado let's get to usage.
*
* View an [example](http://rjackson.github.com/pivot.js/).
*
* #Usage
*
* Step one is to initialize the pivot object. It expects the following attributes:
*
* - `csv` - which should contain a valid string of comma separated values. It is
* __important to note__ that you must include a header row in the CSV for pivot
* to work properly (you'll understand why in a minute).
*
* - `json` - which should contain a valid JSON string. At this time this string
* must be an array of arrays, and not an array of objects (storing the field
* names with each row consumes significantly more space).
*
* - `fields` - which should be an array of objects. This is used to instruct
* pivot on how to interact with the fields you pass in. It keys off of the
* header row names. And is formated like so:
*
* [ {name: 'header-name', type: 'string', optional_attributes: 'optional field' },
* {name: 'header-name', type: 'string', optional_attributes: 'optional field' }]
*
*
* - `filters` (default is empty) - which should contain any filters you would like to restrict your data to. A filter is defined as an object like so:
*
* {zip_code: '34471'}
*
*
* Those are the options that you should consider. There are other options that are well covered in the spec
* A valid pivot could then be set up from like so.
*
*
* var field_definitions = [{name: 'last_name', type: 'string', filterable: true},
* {name: 'first_name', type: 'string', filterable: true},
* {name: 'zip_code', type: 'integer', filterable: true},
* {name: 'pseudo_zip', type: 'integer', filterable: true },
* {name: 'billed_amount', type: 'float', labelable: false,},
* {name: 'last_billed_date', type: 'date', filterable: true}
*
* // from csv data:
* var csv_string = "last_name,first_name,zip_code,billed_amount,last_billed_date\n" +
* "Jackson,Robert,34471,100.00,\"Tue, 24 Jan 2012 00:00:00 +0000\"\n" +
* "Jackson,Jonathan,39401,124.63,\"Fri, 17 Feb 2012 00:00:00 +0000\""
* pivot.init({csv: csv_string, fields: field_definitions});
*
* // from json data:
* var json_string = '[["last_name","first_name","zip_code","billed_amount","last_billed_date"],' +
* ' ["Jackson", "Robert", 34471, 100.00, "Tue, 24 Jan 2012 00:00:00 +0000"],' +
* ' ["Smith", "Jon", 34471, 173.20, "Mon, 13 Feb 2012 00:00:00 +0000"]]'
*
* pivot.init({json: json_string, fields: field_definitions});
*
*/
var pivot=function(){function i(g){return c=[],d=[],e={},a={},b={},f={rowLabels:{},columnLabels:{},summaries:{}},g===undefined&&(g={}),g.fields!==undefined&&L(g.fields),g.filters!==undefined&&G(g.filters),g.rowLabels!==undefined&&db(g.rowLabels),g.columnLabels!==undefined&&eb(g.columnLabels),g.summaries!==undefined&&fb(g.summaries),g.csv!==undefined&&z(g.csv),g.json!==undefined&&y(g.json),pivot}function j(){return i()}function k(a){var c;return a===undefined?c=M():a===!1&&(c="Pass showFields as true in order to view fields here."),{fields:c,filters:b,rowLabels:u(f.rowLabels),columnLabels:u(f.columnLabels),summaries:u(f.summaries)}}function l(){return{pad:m,padRight:n,padLeft:o,formatDate:p,formatTime:q,isArray:r,isRegExp:s,shallowClone:t,objectKeys:u,objectType:v,sortNumerically:w}}function m(a,b,c,d){d===undefined&&(d=" "),b=b.toString(),d=d.toString();while(b.length<c)a==="left"?b=d+b:b+=d;return b}function n(a,b,c){return m("right",a,b,c)}function o(a,b,c){return m("left",a,b,c)}function p(a){return a.getUTCFullYear()+"-"+o(a.getUTCMonth()+1,2,"0")+"-"+o(a.getUTCDate(),2,"0")}function q(a){return p(a)+" "+o(a.getUTCHours(),2,"0")+":"+o(a.getUTCMinutes(),2,"0")}function r(a){return Array.isArray?Array.isArray(a):v(a)=="array"}function s(a){return v(a)=="regexp"}function t(a){var b={};for(var c in a)a.hasOwnProperty(c)&&(b[c]=a[c]);return b}function u(a){if(Object.keys)return Object.keys(a);var b=[];for(key in a)b.push(key);return b}function v(a){return{}.toString.call(a).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()}function w(a){return a.sort(function(a,b){return a-b})}function x(b){var c=[],d={},e=-1,f=b.length;while(++e<f){var g=a[b[e]];g===undefined&&(g=T(b[e])),c.push(g)}return c}function y(a){var b,d=O("pseudo");v(a)==="string"&&(a=JSON.parse(a)),c=[];var e={},f=-1,g=a.length;while(++f<g)f===0?b=x(a[f]):c.push(B(a[f],b,d))}function z(a){var b,d=O("pseudo");c=A(a,function(a,c){if(c!==0)return B(a,b,d);b=x(a)})}function A(a,b){function j(){if(f.lastIndex>=a.length)return d;if(i)return i=!1,c;var b=f.lastIndex;if(a.charCodeAt(b)===34){var e=b;while(e++<a.length)if(a.charCodeAt(e)===34){if(a.charCodeAt(e+1)!==34)break;e++}f.lastIndex=e+2;var g=a.charCodeAt(e+1);return g===13?(i=!0,a.charCodeAt(e+2)===10&&f.lastIndex++):g===10&&(i=!0),a.substring(b+1,e).replace(/""/g,'"')}var h=f.exec(a);return h?(i=h[0].charCodeAt(0)!==44,a.substring(b,h.index)):(f.lastIndex=a.length,a.substring(b))}var c={},d={},e=[],f=/\r\n|[,\r\n]/g,g=0,h,i;f.lastIndex=0;while((h=j())!==d){var k=[];while(h!==c&&h!==d)k.push(h),h=j();if(b&&!(k=b(k,g++)))continue;e.push(k)}return e}function B(a,b,c){var d={},e=-1,f=b.length;while(++e<f){var g=W(b[e].name,a[e]);d[b[e].name]=g,U(b[e].name,g)}e=-1,f=c.length;while(++e<f){var h=c[e],g=W(h.name,h.pseudoFunction(d,h));d[h.name]=g,U(h.name,g)}return d}function C(a){var b={all:F,set:G,apply:H,add:E};return a!==undefined?b[a]:b}function D(a){a===undefined&&(a=b);var c;for(c in a)if(a.hasOwnProperty(c)&&!s(a[c]))if(r(a[c])){var d=-1,e=a[c].length;while(++d<e)a[c][d]=W(c,a[c][d])}else a[c]=W(c,a[c])}function E(a){for(var c in a)a.hasOwnProperty(c)&&(b[c]=a[c]);D()}function F(){return b}function G(a){b=a,hb(),D()}function H(a){var f=d,g=[];a!==undefined&&G(a);var h=J();h?f=d:f=c;var i=f.length,j=u(b).length,k=-1;while(++k<i){var l=f[k],m=0;for(var n in b)b.hasOwnProperty(n)&&l.hasOwnProperty(n)&&I(b[n],l[n])&&(m+=1);m===j&&g.push(l)}return d=g,e=t(b),hb(),d}function I(a,b){if(!r(a))return s(a)?a.test(b):b===a;var c=-1,d=a.length;while(++c<d)if(a[c]===b)return!0;return!1}function J(){var a=0,c=u(e).length;for(var d in e)e.hasOwnProperty(d)&&e.hasOwnProperty(d)&&b[d]===e[d]&&(a+=1);return c>0&&a>=c}function K(a){var b={columnLabelable:O("columnLabelable"),rowLabelable:O("rowLabelable"),summarizable:O("summarizable"),filterable:O("filterable"),pseudo:O("pseudo"),clone:M,add:T,all:N,set:L,get:P};return a!==undefined?b[a]:b}function L(b){a={};var c=-1,d=b.length;while(++c<d)T(b[c])}function M(){var b=[];for(var c in a){var d={};for(var e in a[c])a[c].hasOwnProperty(e)&&e!=="values"&&(d[e]=a[c][e]);b.push(d)}return b}function N(){var b=[];for(var c in a)a.hasOwnProperty(c)&&(b[a[c].index]=a[c]);return b}function O(b){var c=[];for(var d in a)a.hasOwnProperty(d)&&a[d][b]===!0&&c.push(a[d]);return c}function P(b){return a[b]}function Q(a,b){var c=0,d=-1,e=a.length;while(++d<e)c+=a[d][b.dataSource];return c}function R(a,b){return Q(a,b)/a.length}function S(a,b){return a.length}function T(b){v(b)==="string"&&(b={name:b}),b.type===undefined&&(b.type="string"),b.pseudo===undefined&&(b.pseudo=!1),b.rowLabelable===undefined&&(b.rowLabelable=!0),b.columnLabelable===undefined&&(b.columnLabelable=!1),b.filterable===undefined&&(b.filterable=!1),b.dataSource===undefined&&(b.dataSource=b.name);if(b.summarizable&&(b.rowLabelable||b.columnLabelable||b.filterable)){var c=t(b);c.rowLabelable=!1,c.filterable=!1,c.dataSource=b.name,c.summarizable!==!0?c.name=c.name+"_"+c.summarizable:c.name=c.name+"_count",T(c),b.summarizable=!1,b.summarizeFunction=undefined}else if(b.summarizable){if(b.summarizeFunction===undefined){switch(b.summarizable){case"sum":b.summarizeFunction=Q;break;case"avg":b.summarizeFunction=R;break;default:b.summarizeFunction=S}b.summarizable=!0}}else b.summarizable=!1;return b.pseudo&&b.pseudoFunction===undefined&&(b.pseudoFunction=function(a){return""}),b.displayFunction===undefined&&(b.displayFunction=V),b.values={},b.displayValues={},b.index=u(a).length,a[b.name]=b,b}function U(b,c){if(a[b]===undefined||a[b].filterable===!1)return;a[b].values[c]===undefined?a[b].values[c]={count:1,displayValue:a[b].displayFunction(c,b)}:a[b].values[c].count+=1}function V(b,c){var d;v(c)==="string"&&(d=a[c]),d===undefined&&(d=T(c));switch(d.type){case"cents":return"$"+(b/100).toFixed(2);case"currency":return"$"+b.toFixed(2);case"date":return p(new Date(b));case"time":return q(new Date(b));default:return b}}function W(b,c){var d,e;v(b)==="string"&&(d=a[b]),d===undefined&&(d=T(b));switch(d.type){case"integer":case"cents":return v(c)==="number"?c:parseInt(c,10);case"float":case"currency":return v(c)==="number"?c:parseFloat(c,10);case"date":case"time":switch(v(c)){case"number":case"date":return c;default:var f=Date.parse(c);return isNaN(f)&&(f=parseInt(c)),f};default:return c.toString()}}function X(a){var b={raw:c,all:d};return a!==undefined?b[a]:b}function Y(){return{all:Z,rowLabels:$,columnLabels:_,summaries:ab}}function Z(){return f}function $(){return{set:db,get:f.rowLabels}}function _(){return{set:eb,get:f.columnLabels}}function ab(){return{set:fb,get:f.summaries}}function bb(b,c){v(c)==="string"&&(c=a[c]),hb(),f[b][c.name]=c}function cb(a,b){f[a]={},hb();var c=-1,d=b.length;while(++c<d)bb(a,b[c])}function db(a){cb("rowLabels",a)}function eb(a){cb("columnLabels",a)}function fb(a){cb("summaries",a)}function gb(){return{all:ib,columns:pb}}function hb(){g=undefined,h=undefined}function ib(){return g!==undefined&&h!==undefined?ob():(H(),g={},h=[],jb(),u(f.columnLabels).length>0?kb():(qb(),mb()),ob())}function jb(){var b=-1,c=d.length,e;while(++b<c){var i=d[b],j="";for(var k in f.rowLabels)f.rowLabels.hasOwnProperty(k)&&(b===0&&h.push({fieldName:k,width:1,type:"row"}),j+=k+":"+i[k]+"|");if(g[j]===undefined){g[j]={};for(var k in f.rowLabels)f.rowLabels.hasOwnProperty(k)&&(g[j][k]=a[k].displayFunction(i[k],k));g[j].rows=[]}g[j].rows.push(i)}}function kb(){for(var b in f.columnLabels)if(f.columnLabels.hasOwnProperty(b)){var c={};for(var d in g){var e=lb(g[d].rows,a[b]);for(var h in e)c[h]===undefined?c[h]=1:c[h]+=1,g[d][h]=nb(e[h])}rb(c)}return g}function lb(a,b){var c=-1,d=a.length,e={};while(++c<d){var f=a[c][b.name];e[f]===undefined&&(e[f]={rows:[]}),e[f].rows.push(a[c])}return e}function mb(){for(var a in g)nb(g[a]);return g}function nb(b){var c={};for(var d in f.summaries)f.summaries.hasOwnProperty(d)&&(b[d]=a[d].summarizeFunction(b.rows,a[d]),b[d]=a[d].displayFunction(b[d],d));return b}function ob(){var a=[],b=u(g).sort(),c=-1,d=b.length;while(++c<d)a.push(g[b[c]]);return a}function pb(){return(g===undefined||h===undefined)&&ib(),h}function qb(){for(var a in f.summaries)f.summaries.hasOwnProperty(a)&&h.push({fieldName:a,width:1,type:"summary"});return h}function rb(a){var b=u(a).sort(),c=-1,d=b.length,e=u(f.summaries).length;while(++c<d)h.push({fieldName:b[c],width:e,type:"column"});return h}"use strict";var a,b,c,d,e,f,g,h;return i(),{init:i,reset:j,config:k,utils:l,csv:z,json:y,data:X,results:gb,fields:K,filters:C,display:Y}}();