Permalink
Browse files

Handle columnLabel columns.

  • Loading branch information...
1 parent 5488e7d commit 0715c596daca881a6d8b8587f7c656f84140b622 Robert Jackson committed Apr 13, 2012
Showing with 136 additions and 121 deletions.
  1. +38 −39 docs/source/pivot.html
  2. +38 −39 pivot.js
  3. +1 −1 pivot.min.js
  4. +21 −3 spec/pivotjs_spec.js
  5. +1 −1 src/display.js
  6. +37 −38 src/results.js
View
@@ -844,7 +844,7 @@
if (objectType(field) === 'string')
field = fields[field];
- results = undefined;
+ results = undefined; resultsColumns = undefined;
displayFields[type][field.name] = field;
};
@@ -902,23 +902,23 @@
function pivotResults(){
return {
all: getFormattedResults,
- columns: populateColumnResults
+ columns: getColumnResults
}
};
function getFormattedResults(){
- if (results !== undefined) return getResultArray();
+ if (results !== undefined && resultsColumns !== undefined) return getResultArray();
- results = {}; resultsColumns = {order: []};
+ results = {}; resultsColumns = [];
processRowLabelResults();
if (objectKeys(displayFields.columnLabels).length > 0)
processColumnLabelResults();
- else
+ else {
+ populateSummaryColumnsResults();
processSummaryResults();
-
- //populateColumnResults();
+ }
return getResultArray();
};
@@ -933,7 +933,11 @@
resultKey = '';
for (var key in displayFields.rowLabels) {
- if (displayFields.rowLabels.hasOwnProperty(key)) resultKey += key + ':' + row[key] + '|';
+ if (displayFields.rowLabels.hasOwnProperty(key)) {
+ if (i === 0) resultsColumns.push({fieldName: key, width: 1});
+
+ resultKey += key + ':' + row[key] + '|';
+ }
}
if (results[resultKey] === undefined) {
results[resultKey] = {};
@@ -953,13 +957,22 @@
function processColumnLabelResults(){
for (var resultKey in results) {
for (var key in displayFields.columnLabels) {
+ var columnLabelColumns = {};
+
if (displayFields.columnLabels.hasOwnProperty(key)) {
var values = pluckValues(results[resultKey].rows, fields[key]);
for (var value in values){
+ if (columnLabelColumns[value] === undefined)
+ columnLabelColumns[value] = 1;
+ else
+ columnLabelColumns[value] += 1;
+
results[resultKey][value] = getSummaryResults(values[value]);
};
}
+
+ populateColumnLabelColumnsResults(columnLabelColumns);
};
};
@@ -1009,49 +1022,35 @@
return output;
};
- function getColumnArray(){
- var output = [],
- i = -1, m = resultsColumns.order.length;
-
- while (++i < m){
- output.push(resultsColumns[resultsColumns.order[i]])
- };
-
- return output;
- };
-
- function populateColumnResults(){
- populateRowLabelColumnsResults();
-
- if (objectKeys(displayFields.columnLabels).length > 0)
- populateColumnLabelColumnsResults();
- else
- populateSummaryColumnsResults();
-
- return getColumnArray();
- };
-
- function populateRowLabelColumnsResults(){
- for (var key in displayFields.rowLabels){
- if (displayFields.rowLabels.hasOwnProperty(key))
- resultsColumns[key] = 1; resultsColumns.order.push(key)
- }
+ function getColumnResults(){
+ if (results === undefined || resultsColumns === undefined)
+ getFormattedResults();
return resultsColumns;
- };
+ }
function populateSummaryColumnsResults(){
for (var key in displayFields.summaries){
if (displayFields.summaries.hasOwnProperty(key))
- resultsColumns[key] = 1; resultsColumns.order.push(key)
+ resultsColumns.push({fieldName: key, width: 1})
}
return resultsColumns;
};
- function populateColumnLabelColumnsResults(){
+ function populateColumnLabelColumnsResults(columnLabels){
+ var keys = objectKeys(columnLabels).sort(),
+ i = -1,
+ m = keys.length,
+ w = objectKeys(displayFields.summaries).length;
- };
+ while (++i < m){
+ resultsColumns.push({fieldName: keys[i], width: w})
+ };
+
+
+ return resultsColumns;
+ }
// Entry Point
return {
init: init,
View
@@ -827,7 +827,7 @@ function pivotData(type) {
if (objectType(field) === 'string')
field = fields[field];
- results = undefined;
+ results = undefined; resultsColumns = undefined;
displayFields[type][field.name] = field;
};
@@ -885,23 +885,23 @@ function pivotData(type) {
function pivotResults(){
return {
all: getFormattedResults,
- columns: populateColumnResults
+ columns: getColumnResults
}
};
function getFormattedResults(){
- if (results !== undefined) return getResultArray();
+ if (results !== undefined && resultsColumns !== undefined) return getResultArray();
- results = {}; resultsColumns = {order: []};
+ results = {}; resultsColumns = [];
processRowLabelResults();
if (objectKeys(displayFields.columnLabels).length > 0)
processColumnLabelResults();
- else
+ else {
+ populateSummaryColumnsResults();
processSummaryResults();
-
- //populateColumnResults();
+ }
return getResultArray();
};
@@ -916,7 +916,11 @@ function pivotData(type) {
resultKey = '';
for (var key in displayFields.rowLabels) {
- if (displayFields.rowLabels.hasOwnProperty(key)) resultKey += key + ':' + row[key] + '|';
+ if (displayFields.rowLabels.hasOwnProperty(key)) {
+ if (i === 0) resultsColumns.push({fieldName: key, width: 1});
+
+ resultKey += key + ':' + row[key] + '|';
+ }
}
if (results[resultKey] === undefined) {
results[resultKey] = {};
@@ -936,13 +940,22 @@ function pivotData(type) {
function processColumnLabelResults(){
for (var resultKey in results) {
for (var key in displayFields.columnLabels) {
+ var columnLabelColumns = {};
+
if (displayFields.columnLabels.hasOwnProperty(key)) {
var values = pluckValues(results[resultKey].rows, fields[key]);
for (var value in values){
+ if (columnLabelColumns[value] === undefined)
+ columnLabelColumns[value] = 1;
+ else
+ columnLabelColumns[value] += 1;
+
results[resultKey][value] = getSummaryResults(values[value]);
};
}
+
+ populateColumnLabelColumnsResults(columnLabelColumns);
};
};
@@ -992,49 +1005,35 @@ function pivotData(type) {
return output;
};
- function getColumnArray(){
- var output = [],
- i = -1, m = resultsColumns.order.length;
-
- while (++i < m){
- output.push(resultsColumns[resultsColumns.order[i]])
- };
-
- return output;
- };
-
- function populateColumnResults(){
- populateRowLabelColumnsResults();
-
- if (objectKeys(displayFields.columnLabels).length > 0)
- populateColumnLabelColumnsResults();
- else
- populateSummaryColumnsResults();
-
- return getColumnArray();
- };
-
- function populateRowLabelColumnsResults(){
- for (var key in displayFields.rowLabels){
- if (displayFields.rowLabels.hasOwnProperty(key))
- resultsColumns[key] = 1; resultsColumns.order.push(key)
- }
+ function getColumnResults(){
+ if (results === undefined || resultsColumns === undefined)
+ getFormattedResults();
return resultsColumns;
- };
+ }
function populateSummaryColumnsResults(){
for (var key in displayFields.summaries){
if (displayFields.summaries.hasOwnProperty(key))
- resultsColumns[key] = 1; resultsColumns.order.push(key)
+ resultsColumns.push({fieldName: key, width: 1})
}
return resultsColumns;
};
- function populateColumnLabelColumnsResults(){
+ function populateColumnLabelColumnsResults(columnLabels){
+ var keys = objectKeys(columnLabels).sort(),
+ i = -1,
+ m = keys.length,
+ w = objectKeys(displayFields.summaries).length;
- };
+ while (++i < m){
+ resultsColumns.push({fieldName: keys[i], width: w})
+ };
+
+
+ return resultsColumns;
+ }
// Entry Point
return {
init: init,
Oops, something went wrong.

0 comments on commit 0715c59

Please sign in to comment.