Skip to content

Commit 76683ba

Browse files
committed
sort categories by values: add support for histogram2dcontour
1 parent e4e5eaa commit 76683ba

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

Diff for: src/plots/plots.js

+28-2
Original file line numberDiff line numberDiff line change
@@ -2861,6 +2861,31 @@ var sortAxisCategoriesByValueRegex = /(value|sum|min|max) (ascending|descending)
28612861
function sortAxisCategoriesByValue(axList, gd) {
28622862
var affectedTraces = [];
28632863
var i, j, k, l, o;
2864+
2865+
function zMapCategory(type, ax, value) {
2866+
var axLetter = ax._id.charAt(0);
2867+
if(type === 'histogram2dcontour') {
2868+
var counterAxLetter = ax._counterAxes[0];
2869+
var counterAx = axisIDs.getFromId(gd, counterAxLetter);
2870+
2871+
var xCategorical = axLetter === 'x' || (counterAxLetter === 'x' && counterAx.type === 'category');
2872+
var yCategorical = axLetter === 'y' || (counterAxLetter === 'y' && counterAx.type === 'category');
2873+
2874+
return function(o, l) {
2875+
if(o === 0 || l === 0) return -1; // Skip first row and column
2876+
if(xCategorical && o === value[l].length - 1) return -1;
2877+
if(yCategorical && l === value.length - 1) return -1;
2878+
2879+
var catIndex = axLetter === 'y' ? l : o;
2880+
return catIndex - 1;
2881+
};
2882+
} else {
2883+
return function(o, l) {
2884+
return axLetter === 'y' ? l : o;
2885+
};
2886+
}
2887+
}
2888+
28642889
for(i = 0; i < axList.length; i++) {
28652890
var ax = axList[i];
28662891
if(ax.type !== 'category') continue;
@@ -2950,11 +2975,12 @@ function sortAxisCategoriesByValue(axList, gd) {
29502975
// If 2dMap, collect values in `z`
29512976
if(cdi.hasOwnProperty('z')) {
29522977
value = cdi.z;
2978+
var mapping = zMapCategory(fullTrace.type, ax, value);
29532979

29542980
for(l = 0; l < value.length; l++) {
29552981
for(o = 0; o < value[l].length; o++) {
2956-
catIndex = ax._id.charAt(0) === 'y' ? l : o;
2957-
categoriesValue[catIndex][1].push(value[l][o]);
2982+
catIndex = mapping(o, l);
2983+
if(catIndex + 1) categoriesValue[catIndex][1].push(value[l][o]);
29582984
}
29592985
}
29602986
} else {

Diff for: test/jasmine/tests/calcdata_test.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -885,9 +885,7 @@ describe('calculated data and points', function() {
885885
});
886886

887887
// excludedTraces are traces that do not support sorting by value
888-
var excludedTraces = [ 'carpet', 'contourcarpet',
889-
// TODO: add support for the following
890-
'histogram2dcontour'];
888+
var excludedTraces = [ 'carpet', 'contourcarpet'];
891889

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

982980
if(finalOrder) {
983-
expect(gd._fullLayout[trace.type === 'splom' ? 'xaxis' : axName]._categories).toEqual(finalOrder, 'for trace ' + trace.type);
981+
expect(gd._fullLayout[trace.type === 'splom' ? 'xaxis' : axName]._categories).toEqual(finalOrder, 'wrong order');
984982
}
985983
})
986984
.catch(failTest)

0 commit comments

Comments
 (0)