dimple-js
Version:
Dimple is an object-oriented API allowing you to create flexible axis-based charts using [d3.js](http://d3js.org "d3.js").
60 lines (58 loc) • 2.34 kB
JavaScript
// Copyright: 2015 AlignAlytics
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/methods/_rollUp.js
dimple._rollUp = function (data, fields, includeFields) {
var returnList = [];
// Put single values into single value arrays
if (fields !== null && fields !== undefined) {
fields = [].concat(fields);
} else {
fields = [];
}
// Iterate every row in the data
data.forEach(function (d) {
// The index of the corresponding row in the return list
var index = -1,
newRow = {},
match = true;
// Find the corresponding value in the return list
returnList.forEach(function (r, j) {
if (index === -1) {
// Indicates a match
match = true;
// Iterate the passed fields and compare
fields.forEach(function (f) {
match = match && d[f] === r[f];
}, this);
// If this is a match get the index
if (match) {
index = j;
}
}
}, this);
// Pick up the matched row, or add a new one
if (index !== -1) {
newRow = returnList[index];
} else {
// Iterate the passed fields and add to the new row
fields.forEach(function (f) {
newRow[f] = d[f];
}, this);
returnList.push(newRow);
index = returnList.length - 1;
}
// Iterate all the fields in the data
includeFields.forEach(function (field) {
if (fields.indexOf(field) === -1) {
if (newRow[field] === undefined) {
newRow[field] = [];
}
newRow[field] = newRow[field].concat(d[field]);
}
}, this);
// Update the return list
returnList[index] = newRow;
}, this);
// Return the flattened list
return returnList;
};