forked from rwjblue/pivot.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpivot.min.js
More file actions
71 lines (71 loc) · 11.6 KB
/
pivot.min.js
File metadata and controls
71 lines (71 loc) · 11.6 KB
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', rowLabelable: 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(){"use strict";function a(o){return n=[],r=[],i={},e={},t={},s={rowLabels:{},columnLabels:{},summaries:{}},o===undefined&&(o={}),o.fields!==undefined&&j(o.fields),o.filters!==undefined&&_(o.filters),o.rowLabels!==undefined&&rt(o.rowLabels),o.columnLabels!==undefined&&it(o.columnLabels),o.summaries!==undefined&&st(o.summaries),o.csv!==undefined&&N(o.csv),o.json!==undefined&&T(o.json),pivot}function f(){return a()}function l(e){var n;return e===undefined?n=F():e===!1&&(n="Pass showFields as true in order to view fields here."),{fields:n,filters:t,rowLabels:w(s.rowLabels),columnLabels:w(s.columnLabels),summaries:w(s.summaries)}}function c(){return{pad:h,padRight:p,padLeft:d,formatDate:v,formatTime:m,isArray:g,isRegExp:y,shallowClone:b,objectKeys:w,objectType:E,sortNumerically:S}}function h(e,t,n,r){r===undefined&&(r=" "),t=t.toString(),r=r.toString();while(t.length<n)e==="left"?t=r+t:t+=r;return t}function p(e,t,n){return h("right",e,t,n)}function d(e,t,n){return h("left",e,t,n)}function v(e){return e.getUTCFullYear()+"-"+d(e.getUTCMonth()+1,2,"0")+"-"+d(e.getUTCDate(),2,"0")}function m(e){return v(e)+" "+d(e.getUTCHours(),2,"0")+":"+d(e.getUTCMinutes(),2,"0")}function g(e){return Array.isArray?Array.isArray(e):E(e)=="array"}function y(e){return E(e)=="regexp"}function b(e){var t={};for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);return t}function w(e){if(Object.keys)return Object.keys(e);var t=[];for(key in e)t.push(key);return t}function E(e){return{}.toString.call(e).match(/\s([a-z|A-Z]+)/)[1].toLowerCase()}function S(e){return e.sort(function(e,t){return e-t})}function x(t){var n=[],r={},i=-1,s=t.length;while(++i<s){var o=e[t[i]];o===undefined&&(o=X(t[i])),n.push(o)}return n}function T(e){var t,r=q("pseudo");E(e)==="string"&&(e=JSON.parse(e)),n=[];var i={},s=-1,o=e.length;while(++s<o)s===0?t=x(e[s]):n.push(k(e[s],t,r))}function N(e){var t,r=q("pseudo");n=C(e,function(e,n){if(n!==0)return k(e,t,r);t=x(e)})}function C(e,t){function f(){if(s.lastIndex>=e.length)return r;if(a)return a=!1,n;var t=s.lastIndex;if(e.charCodeAt(t)===34){var i=t;while(i++<e.length)if(e.charCodeAt(i)===34){if(e.charCodeAt(i+1)!==34)break;i++}s.lastIndex=i+2;var o=e.charCodeAt(i+1);return o===13?(a=!0,e.charCodeAt(i+2)===10&&s.lastIndex++):o===10&&(a=!0),e.substring(t+1,i).replace(/""/g,'"')}var u=s.exec(e);return u?(a=u[0].charCodeAt(0)!==44,e.substring(t,u.index)):(s.lastIndex=e.length,e.substring(t))}var n={},r={},i=[],s=/\r\n|[,\r\n]/g,o=0,u,a;s.lastIndex=0;while((u=f())!==r){var l=[];while(u!==n&&u!==r)l.push(u),u=f();if(t&&!(l=t(l,o++)))continue;i.push(l)}return i}function k(e,t,n){var r={},i=-1,s=t.length;while(++i<s){var o=J(t[i].name,e[i]);r[t[i].name]=o,V(t[i].name,o)}i=-1,s=n.length;while(++i<s){var u=n[i],o=J(u.name,u.pseudoFunction(r,u));r[u.name]=o,V(u.name,o)}return r}function L(e){var t={all:M,set:_,apply:D,add:O};return e!==undefined?t[e]:t}function A(e){e===undefined&&(e=t);var n;for(n in e)if(e.hasOwnProperty(n)&&!y(e[n]))if(g(e[n])){var r=-1,i=e[n].length;while(++r<i)e[n][r]=J(n,e[n][r])}else e[n]=J(n,e[n])}function O(e){for(var n in e)e.hasOwnProperty(n)&&(t[n]=e[n]);A()}function M(){return t}function _(e){t=e,ut(),A()}function D(e){var s=r,o=[];e!==undefined&&_(e);var u=H();u?s=r:s=n;var a=s.length,f=w(t).length,l=-1;while(++l<a){var c=s[l],h=0;for(var p in t)t.hasOwnProperty(p)&&c.hasOwnProperty(p)&&P(t[p],c[p])&&(h+=1);h===f&&o.push(c)}return r=o,i=b(t),ut(),r}function P(e,t){if(!g(e))return y(e)?e.test(t):t===e;var n=-1,r=e.length;while(++n<r)if(e[n]===t)return!0;return!1}function H(){var e=0,n=w(i).length;for(var r in i)i.hasOwnProperty(r)&&i.hasOwnProperty(r)&&t[r]===i[r]&&(e+=1);return n>0&&e>=n}function B(e){var t={columnLabelable:q("columnLabelable"),rowLabelable:q("rowLabelable"),summarizable:q("summarizable"),filterable:q("filterable"),pseudo:q("pseudo"),clone:F,add:X,all:I,set:j,get:R};return e!==undefined?t[e]:t}function j(t){e={};var n=-1,r=t.length;while(++n<r)X(t[n])}function F(){var t=[];for(var n in e){var r={};for(var i in e[n])e[n].hasOwnProperty(i)&&i!=="values"&&(r[i]=e[n][i]);t.push(r)}return t}function I(){var t=[];for(var n in e)e.hasOwnProperty(n)&&(t[e[n].index]=e[n]);return t}function q(t){var n=[];for(var r in e)e.hasOwnProperty(r)&&e[r][t]===!0&&n.push(e[r]);return n}function R(t){return e[t]}function U(e,t){var n=0,r=-1,i=e.length;while(++r<i)n+=e[r][t.dataSource];return n}function z(e,t){return U(e,t)/e.length}function W(e,t){return e.length}function X(t){E(t)==="string"&&(t={name:t}),t.type===undefined&&(t.type="string"),t.pseudo===undefined&&(t.pseudo=!1),t.rowLabelable===undefined&&(t.rowLabelable=!0),t.columnLabelable===undefined&&(t.columnLabelable=!1),t.filterable===undefined&&(t.filterable=!1),t.dataSource===undefined&&(t.dataSource=t.name);if(t.summarizable&&(t.rowLabelable||t.columnLabelable||t.filterable)){var n=b(t);n.rowLabelable=!1,n.filterable=!1,n.dataSource=t.name,n.summarizable!==!0?n.name=n.name+"_"+n.summarizable:n.name=n.name+"_count",X(n),t.summarizable=!1,t.summarizeFunction=undefined}else if(t.summarizable){if(t.summarizeFunction===undefined){switch(t.summarizable){case"sum":t.summarizeFunction=U;break;case"avg":t.summarizeFunction=z;break;default:t.summarizeFunction=W}t.summarizable=!0}}else t.summarizable=!1;return t.pseudo&&t.pseudoFunction===undefined&&(t.pseudoFunction=function(e){return""}),t.displayFunction===undefined&&(t.displayFunction=$),t.values={},t.displayValues={},t.index=w(e).length,e[t.name]=t,t}function V(t,n){if(e[t]===undefined||e[t].filterable===!1)return;e[t].values[n]===undefined?e[t].values[n]={count:1,displayValue:e[t].displayFunction(n,t)}:e[t].values[n].count+=1}function $(t,n){var r;E(n)==="string"&&(r=e[n]),r===undefined&&(r=X(n));switch(r.type){case"cents":return"$"+(t/100).toFixed(2);case"currency":return"$"+t.toFixed(2);case"date":return v(new Date(t));case"time":return m(new Date(t));default:return t}}function J(t,n){var r,i;E(t)==="string"&&(r=e[t]),r===undefined&&(r=X(t));switch(r.type){case"integer":case"cents":return E(n)==="number"?n:parseInt(n,10);case"float":case"currency":return E(n)==="number"?n:parseFloat(n,10);case"date":case"time":switch(E(n)){case"number":case"date":return n;default:var s=Date.parse(n);return isNaN(s)&&(s=parseInt(n)),s};default:return n.toString()}}function K(e){var t={raw:n,all:r};return e!==undefined?t[e]:t}function Q(){return{all:G,rowLabels:Y,columnLabels:Z,summaries:et}}function G(){return s}function Y(){return{set:rt,get:s.rowLabels}}function Z(){return{set:it,get:s.columnLabels}}function et(){return{set:st,get:s.summaries}}function tt(t,n){E(n)==="string"&&(n=e[n]),ut(),s[t][n.name]=n}function nt(e,t){s[e]={},ut();var n=-1,r=t.length;while(++n<r)tt(e,t[n])}function rt(e){nt("rowLabels",e)}function it(e){nt("columnLabels",e)}function st(e){nt("summaries",e)}function ot(){return{all:at,columns:vt}}function ut(){o=undefined,u=undefined}function at(){return o!==undefined&&u!==undefined?dt():(D(),o={},u=[],ft(),w(s.columnLabels).length>0?lt():(mt(),ht()),dt())}function ft(){var t=-1,n=r.length,i;while(++t<n){var a=r[t],f="";for(var l in s.rowLabels)s.rowLabels.hasOwnProperty(l)&&(t===0&&u.push({fieldName:l,width:1,type:"row"}),f+=l+":"+a[l]+"|");if(o[f]===undefined){o[f]={};for(var l in s.rowLabels)s.rowLabels.hasOwnProperty(l)&&(o[f][l]=e[l].displayFunction(a[l],l));o[f].rows=[]}o[f].rows.push(a)}}function lt(){for(var t in s.columnLabels)if(s.columnLabels.hasOwnProperty(t)){var n={};for(var r in o){var i=ct(o[r].rows,e[t]);for(var u in i)n[u]===undefined?n[u]=1:n[u]+=1,o[r][u]=pt(i[u])}gt(n)}return o}function ct(e,t){var n=-1,r=e.length,i={};while(++n<r){var s=e[n][t.name];i[s]===undefined&&(i[s]={rows:[]}),i[s].rows.push(e[n])}return i}function ht(){for(var e in o)pt(o[e]);return o}function pt(t){var n={};for(var r in s.summaries)s.summaries.hasOwnProperty(r)&&(t[r]=e[r].summarizeFunction(t.rows,e[r]),t[r]=e[r].displayFunction(t[r],r));return t}function dt(){var e=[],t=w(o).sort(),n=-1,r=t.length;while(++n<r)e.push(o[t[n]]);return e}function vt(){return(o===undefined||u===undefined)&&at(),u}function mt(){for(var e in s.summaries)s.summaries.hasOwnProperty(e)&&u.push({fieldName:e,width:1,type:"summary"});return u}function gt(e){var t=w(e).sort(),n=-1,r=t.length,i=w(s.summaries).length;while(++n<r)u.push({fieldName:t[n],width:i,type:"column"});return u}var e,t,n,r,i,s,o,u;return a(),{init:a,reset:f,config:l,utils:c,csv:N,json:T,data:K,results:ot,fields:B,filters:L,display:Q}}();