Fixed Order Rules for Multi-dimensional Series

master
johnkiernander 2014-04-07 21:57:21 +01:00
parent fa8fbd4d5d
commit d1b937c5c4
3 changed files with 58 additions and 17 deletions

View File

@ -2,17 +2,18 @@
// License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt" // License: "https://github.com/PMSI-AlignAlytics/dimple/blob/master/MIT-LICENSE.txt"
// Source: /src/objects/chart/methods/_getOrderedList.js // Source: /src/objects/chart/methods/_getOrderedList.js
dimple._getOrderedList = function (data, mainField, levelDefinitions) { dimple._getOrderedList = function (data, mainField, levelDefinitions) {
var rollupData = [], var rollupData,
sortStack = [], sortStack = [],
finalArray = [], finalArray = [],
fields = [mainField], mainArray = [].concat(mainField),
fields = [].concat(mainField),
defs = []; defs = [];
// Force the level definitions into an array // Force the level definitions into an array
if (levelDefinitions !== null && levelDefinitions !== undefined) { if (levelDefinitions !== null && levelDefinitions !== undefined) {
defs = defs.concat(levelDefinitions); defs = defs.concat(levelDefinitions);
} }
// Add the base case // Add the base case
defs = defs.concat({ ordering: mainField, desc: false }); defs = defs.concat({ ordering: mainArray, desc: false });
// Exclude fields if this does not contain a function // Exclude fields if this does not contain a function
defs.forEach(function (def) { defs.forEach(function (def) {
var field; var field;
@ -26,7 +27,7 @@
fields.push(def.ordering); fields.push(def.ordering);
} }
}, this); }, this);
rollupData = dimple._rollUp(data, mainField, fields); rollupData = dimple._rollUp(data, mainArray, fields);
// If we go below the leaf stop recursing // If we go below the leaf stop recursing
if (defs.length >= 1) { if (defs.length >= 1) {
// Build a stack of compare methods // Build a stack of compare methods
@ -81,10 +82,19 @@
}, this); }, this);
// Sort according to the axis position // Sort according to the axis position
sortStack.push(function (a, b) { sortStack.push(function (a, b) {
var aStr = "".concat(a[mainField]), var aStr = "",
bStr = "".concat(b[mainField]), bStr = "",
aIx, aIx,
bIx; bIx,
i;
for (i = 0; i < mainArray.length; i += 1) {
if (i > 0) {
aStr += "|";
bStr += "|";
}
aStr += a[mainArray[i]];
bStr += b[mainArray[i]];
}
// If the value is not found it should go to the end (if descending it // If the value is not found it should go to the end (if descending it
// should go to the start so that it ends up at the back when reversed) // should go to the start so that it ends up at the back when reversed)
aIx = orderArray.indexOf(aStr); aIx = orderArray.indexOf(aStr);
@ -119,7 +129,16 @@
// Return a simple array if only one field is being returned. // Return a simple array if only one field is being returned.
// for multiple fields remove extra fields but leave objects // for multiple fields remove extra fields but leave objects
rollupData.forEach(function (d) { rollupData.forEach(function (d) {
finalArray.push(d[mainField]); var i,
newRow = [];
if (mainArray.length === 1) {
finalArray.push(d[mainArray[0]]);
} else {
for (i = 0; i < mainArray.length; i += 1) {
newRow.push(d[mainArray[i]]);
}
finalArray.push(newRow);
}
}, this); }, this);
} }
// Return the ordered list // Return the ordered list

View File

@ -69,7 +69,7 @@
// Concat is used here to break the reference to the parent array, if we don't do this, in a storyboarded chart, // Concat is used here to break the reference to the parent array, if we don't do this, in a storyboarded chart,
// the series rules to grow and grow until the system grinds to a halt trying to deal with them all. // the series rules to grow and grow until the system grinds to a halt trying to deal with them all.
rules = [].concat(series._orderRules); rules = [].concat(series._orderRules);
seriesCat = series.categoryFields[0]; seriesCat = series.categoryFields;
if (series.c !== null && series.c !== undefined && series.c._hasMeasure()) { if (series.c !== null && series.c !== undefined && series.c._hasMeasure()) {
rules.push({ ordering : series.c.measure, desc : true }); rules.push({ ordering : series.c.measure, desc : true });
} else if (series.z !== null && series.z !== undefined && series.z._hasMeasure()) { } else if (series.z !== null && series.z !== undefined && series.z._hasMeasure()) {
@ -83,7 +83,12 @@
} }
sortedData.sort(function (a, b) { sortedData.sort(function (a, b) {
var returnValue = 0; var returnValue = 0,
cats,
p,
q,
aMatch,
bMatch;
if (storyCat !== "") { if (storyCat !== "") {
returnValue = orderedStoryboardArray.indexOf(a[storyCat]) - orderedStoryboardArray.indexOf(b[storyCat]); returnValue = orderedStoryboardArray.indexOf(a[storyCat]) - orderedStoryboardArray.indexOf(b[storyCat]);
} }
@ -93,8 +98,27 @@
if (yCat !== "" && returnValue === 0) { if (yCat !== "" && returnValue === 0) {
returnValue = ySortArray.indexOf(a[yCat]) - ySortArray.indexOf(b[yCat]); returnValue = ySortArray.indexOf(a[yCat]) - ySortArray.indexOf(b[yCat]);
} }
if (seriesCat !== "" && returnValue === 0) { if (seriesCat !== null && seriesCat !== undefined && seriesCat.length > 0) {
returnValue = orderedSeriesArray.indexOf(a[seriesCat]) - orderedSeriesArray.indexOf(b[seriesCat]); cats = [].concat(seriesCat);
returnValue = 0;
for (p = 0; p < orderedSeriesArray.length; p += 1) {
aMatch = true;
bMatch = true;
for (q = 0; q < cats.length; q += 1) {
aMatch = aMatch && (a[cats[q]] === orderedSeriesArray[p][q]);
bMatch = bMatch && (b[cats[q]] === orderedSeriesArray[p][q]);
}
if (aMatch && bMatch) {
returnValue = 0;
break;
} else if (aMatch) {
returnValue = -1;
break;
} else if (bMatch) {
returnValue = 1;
break;
}
}
} }
return returnValue; return returnValue;
}); });
@ -197,11 +221,7 @@
if (axis !== null && axis !== undefined) { if (axis !== null && axis !== undefined) {
if (passStoryCheck) { if (passStoryCheck) {
retRow = returnData[foundIndex]; retRow = returnData[foundIndex];
if (axis._hasMeasure()) { if (axis._hasMeasure() && d[axis.measure] !== null && d[axis.measure] !== undefined) {
// Treat undefined values as zero
if (d[axis.measure] === undefined) {
d[axis.measure] = 0;
}
// Keep a distinct list of values to calculate a distinct count in the case of a non-numeric value being found // Keep a distinct list of values to calculate a distinct count in the case of a non-numeric value being found
if (retRow[axis.position + "ValueList"].indexOf(d[axis.measure]) === -1) { if (retRow[axis.position + "ValueList"].indexOf(d[axis.measure]) === -1) {
retRow[axis.position + "ValueList"].push(d[axis.measure]); retRow[axis.position + "ValueList"].push(d[axis.measure]);

View File

@ -30,6 +30,8 @@
this.lineWeight = 2; this.lineWeight = 2;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.series#wiki-lineMarkers // Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.series#wiki-lineMarkers
this.lineMarkers = false; this.lineMarkers = false;
// Help: http://github.com/PMSI-AlignAlytics/dimple/wiki/dimple.series#wiki-afterDraw
this.afterDraw = null;
// Any event handlers joined to this series // Any event handlers joined to this series
this._eventHandlers = []; this._eventHandlers = [];