@@ -2861,6 +2861,31 @@ var sortAxisCategoriesByValueRegex = /(value|sum|min|max) (ascending|descending)
28612861function 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 {
0 commit comments