diff --git a/draftlogs/6718_fix.md b/draftlogs/6718_fix.md new file mode 100644 index 00000000000..45eac127840 --- /dev/null +++ b/draftlogs/6718_fix.md @@ -0,0 +1 @@ +- Column order changes on hover [[#6718](https://github.com/plotly/plotly.js/pull/6718)] \ No newline at end of file diff --git a/src/plots/plots.js b/src/plots/plots.js index 2feb453c40d..5a6b5e0466c 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -3207,6 +3207,14 @@ function sortAxisCategoriesByValue(axList, gd) { median: function(values) {return Lib.median(values);} }; + function sortAscending(a, b) { + return a[1] - b[1]; + } + + function sortDescending(a, b) { + return b[1] - a[1]; + } + for(i = 0; i < axList.length; i++) { var ax = axList[i]; if(ax.type !== 'category') continue; @@ -3328,9 +3336,7 @@ function sortAxisCategoriesByValue(axList, gd) { } // Sort by aggregated value - categoriesAggregatedValue.sort(function(a, b) { - return a[1] - b[1]; - }); + categoriesAggregatedValue.sort(order === 'descending' ? sortDescending : sortAscending); ax._categoriesAggregatedValue = categoriesAggregatedValue; @@ -3339,11 +3345,6 @@ function sortAxisCategoriesByValue(axList, gd) { return c[0]; }); - // Reverse if descending - if(order === 'descending') { - ax._initialCategories.reverse(); - } - // Sort all matching axes affectedTraces = affectedTraces.concat(ax.sortByInitialCategories()); } diff --git a/test/jasmine/tests/calcdata_test.js b/test/jasmine/tests/calcdata_test.js index 57134876b50..2111018242a 100644 --- a/test/jasmine/tests/calcdata_test.js +++ b/test/jasmine/tests/calcdata_test.js @@ -1074,8 +1074,14 @@ describe('calculated data and points', function() { if(categoryorder === 'total descending') finalOrder.reverse(); var expectedAgg = [['a', 7], ['b', 2], ['c', 3]]; - if(trace.type === 'ohlc' || trace.type === 'candlestick') expectedAgg = [['a', 14], ['b', 4], ['c', 6]]; - if(trace.type.match(/histogram/)) expectedAgg = [['a', 2], ['b', 1], ['c', 1]]; + if(trace.type === 'ohlc' || trace.type === 'candlestick') { + expectedAgg = [['a', 14], ['b', 4], ['c', 6]]; + if(categoryorder === 'total descending') finalOrder = ['a', 'c', 'b']; + } + if(trace.type.match(/histogram/)) { + expectedAgg = [['a', 2], ['b', 1], ['c', 1]]; + if(categoryorder === 'total descending') finalOrder = ['a', 'b', 'c']; + } checkAggregatedValue(baseMock, expectedAgg, finalOrder, done); });