Skip to content
Snippets Groups Projects
Commit c7e010c1 authored by Allen Gilliland's avatar Allen Gilliland
Browse files

update pivot calculation.

parent 5faaf857
Branches
Tags
No related merge requests found
......@@ -7,60 +7,67 @@ var DataGrid = {
// find the lowest cardinality dimension and make it our "pivoted" column
// TODO: we assume dimensions are in the first 2 columns, which is less than ideal
var pivotCol = 0,
normalCol = 1,
pivotColValues = DataGrid.distinctValues(data, pivotCol);
if (DataGrid.cardinality(data, 1) <= pivotColValues.length) {
pivotCol = 1;
normalCol = 0;
pivotColValues = DataGrid.distinctValues(data, pivotCol);
normalCol = 1,
pivotColValues = DataGrid.distinctValues(data, pivotCol),
normalColValues = DataGrid.distinctValues(data, normalCol);
if (normalColValues.length <= pivotColValues.length) {
pivotCol = 1;
normalCol = 0;
var tmp = pivotColValues;
pivotColValues = normalColValues;
normalColValues = tmp;
}
// sort the pivot column values sensibly
// sort the column values sensibly
pivotColValues.sort();
pivotColValues.unshift(null);
normalColValues.sort();
// build the pivoted data grid
var values = data.rows.reduce(function(last, now) {
// grab the last "row" from the total dataset (if possible)
var row = (last.length > 0) ? last[last.length - 1] : null;
if (row === null || row[0] !== now[normalCol]) {
row = Array.apply(null, Array(pivotColValues.length)).map(function() { return null; });
row[0] = now[normalCol];
last.push(row);
}
// make sure that the first element in the pivoted column list is null which makes room for the label of the other column
pivotColValues.unshift(null);
// put current value into the result at the correct pivoted index
// TODO: we are hard coding to the 3rd value here, assuming that is always the metric :/
row[pivotColValues.lastIndexOf(now[pivotCol])] = now[2];
// start with an empty grid that we'll fill with the appropriate values
var pivotedRows = [];
var emptyRow = Array.apply(null, Array(pivotColValues.length)).map(function() { return null; });
for (var i=0; i < normalColValues.length; i++) {
pivotedRows.push(_.clone(emptyRow));
}
return last;
// fill it up with the data
for (var j=0; j < data.rows.length; j++) {
var normalColIdx = normalColValues.lastIndexOf(data.rows[j][normalCol]);
var pivotColIdx = pivotColValues.lastIndexOf(data.rows[j][pivotCol]);
}, []);
pivotedRows[normalColIdx][0] = data.rows[j][normalCol];
// NOTE: we are hard coding the expectation that the metric is in the 3rd column
pivotedRows[normalColIdx][pivotColIdx] = data.rows[j][2];
}
// provide some column metadata to maintain consistency
var cols = pivotColValues.map(function(val) {
var colDef = _.clone(data.cols[pivotCol]);
colDef['display_name'] = val || "";
colDef['name'] = val || "";
return colDef;
var colDef = _.clone(data.cols[pivotCol]);
colDef['display_name'] = val || "";
colDef['name'] = val || "";
return colDef;
});
return {
cols: cols,
columns: pivotColValues,
rows: values
cols: cols,
columns: pivotColValues,
rows: pivotedRows
};
},
distinctValues: function(data, colIdx) {
var vals = data.rows.map(function(r) {
return r[colIdx];
});
var vals = data.rows.map(function(r) {
return r[colIdx];
});
return vals.filter(function(v, i) { return i==vals.lastIndexOf(v); });
return vals.filter(function(v, i) { return i==vals.lastIndexOf(v); });
},
cardinality: function(data, colIdx) {
return DataGrid.distinctValues(data, colIdx).length;
return DataGrid.distinctValues(data, colIdx).length;
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment