diff --git a/draftlogs/7855_fix.md b/draftlogs/7855_fix.md new file mode 100644 index 00000000000..4a2dc7af4b1 --- /dev/null +++ b/draftlogs/7855_fix.md @@ -0,0 +1 @@ +- Fix crash when ordering categories by value (e.g. `sum descending`) with `null` coordinates [[#7855](https://github.com/plotly/plotly.js/pull/7855)] diff --git a/src/plots/plots.js b/src/plots/plots.js index d86c5c7e06b..a28ac57a527 100644 --- a/src/plots/plots.js +++ b/src/plots/plots.js @@ -3188,6 +3188,10 @@ function sortAxisCategoriesByValue(axList, gd) { catIndex = cdi.p; if(catIndex === undefined) catIndex = cdi[axLetter]; + // Skip points whose position is not a valid category + // (e.g. a null/NaN coordinate maps to BADNUM) + if(!(catIndex + 1)) continue; + value = cdi.s; if(value === undefined) value = cdi.v; if(value === undefined) value = isX ? cdi.y : cdi.x; diff --git a/test/jasmine/tests/calcdata_test.js b/test/jasmine/tests/calcdata_test.js index 86ef265c7b4..10ff96e1165 100644 --- a/test/jasmine/tests/calcdata_test.js +++ b/test/jasmine/tests/calcdata_test.js @@ -1215,6 +1215,21 @@ describe('calculated data and points', function() { }) .then(done, done.fail); }); + + it('does not throw when a category coordinate is null and ordering by value', function(done) { + Plotly.newPlot(gd, [{ + type: 'bar', + x: ['a', null, 'b'], + y: [3, 5, 7] + }], { + xaxis: {type: 'category', categoryorder: 'sum descending'} + }) + .then(function() { + // the null point is dropped, remaining categories are ordered by value + expect(gd._fullLayout.xaxis._categories).toEqual(['b', 'a']); + }) + .then(done, done.fail); + }); }); });