Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort categorical Cartesian axes by value #3864

Merged
merged 13 commits into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2861,6 +2861,31 @@ var sortAxisCategoriesByValueRegex = /(value|sum|min|max) (ascending|descending)
function sortAxisCategoriesByValue(axList, gd) {
var affectedTraces = [];
var i, j, k, l, o;

function zMapCategory(type, ax, value) {
var axLetter = ax._id.charAt(0);
if(type === 'histogram2dcontour') {
etpinard marked this conversation as resolved.
Show resolved Hide resolved
var counterAxLetter = ax._counterAxes[0];
var counterAx = axisIDs.getFromId(gd, counterAxLetter);

var xCategorical = axLetter === 'x' || (counterAxLetter === 'x' && counterAx.type === 'category');
var yCategorical = axLetter === 'y' || (counterAxLetter === 'y' && counterAx.type === 'category');

return function(o, l) {
if(o === 0 || l === 0) return -1; // Skip first row and column
if(xCategorical && o === value[l].length - 1) return -1;
if(yCategorical && l === value.length - 1) return -1;

var catIndex = axLetter === 'y' ? l : o;
return catIndex - 1;
};
} else {
return function(o, l) {
return axLetter === 'y' ? l : o;
};
}
}

for(i = 0; i < axList.length; i++) {
var ax = axList[i];
if(ax.type !== 'category') continue;
Expand Down Expand Up @@ -2950,11 +2975,12 @@ function sortAxisCategoriesByValue(axList, gd) {
// If 2dMap, collect values in `z`
if(cdi.hasOwnProperty('z')) {
value = cdi.z;
var mapping = zMapCategory(fullTrace.type, ax, value);

for(l = 0; l < value.length; l++) {
for(o = 0; o < value[l].length; o++) {
catIndex = ax._id.charAt(0) === 'y' ? l : o;
categoriesValue[catIndex][1].push(value[l][o]);
catIndex = mapping(o, l);
if(catIndex + 1) categoriesValue[catIndex][1].push(value[l][o]);
}
}
} else {
Expand Down
8 changes: 3 additions & 5 deletions test/jasmine/tests/calcdata_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,9 +885,7 @@ describe('calculated data and points', function() {
});

// excludedTraces are traces that do not support sorting by value
var excludedTraces = [ 'carpet', 'contourcarpet',
// TODO: add support for the following
'histogram2dcontour'];
var excludedTraces = [ 'carpet', 'contourcarpet'];

var supportedCartesianTraces = cartesianTraces.filter(function(t) {
if(excludedTraces.indexOf(t.type) === -1) return true;
Expand Down Expand Up @@ -975,12 +973,12 @@ describe('calculated data and points', function() {
Plotly.newPlot(gd, mock)
.then(function(gd) {
var agg = gd._fullLayout[trace.type === 'splom' ? 'xaxis' : axName]._categoriesAggregatedValue.sort(function(a, b) {
return a[0] > b[0];
return a[0] > b[0] ? 1 : -1;
});
expect(agg).toEqual(expectedAgg, 'wrong aggregation for ' + axName);

if(finalOrder) {
expect(gd._fullLayout[trace.type === 'splom' ? 'xaxis' : axName]._categories).toEqual(finalOrder, 'for trace ' + trace.type);
expect(gd._fullLayout[trace.type === 'splom' ? 'xaxis' : axName]._categories).toEqual(finalOrder, 'wrong order');
}
})
.catch(failTest)
Expand Down