Skip to content

Commit 848c158

Browse files
authored
Merge pull request #7855 from SAY-5/fix-categoryorder-null-crash
Fix crash when ordering categories by value with null coordinates
2 parents ace19e2 + 2e9257c commit 848c158

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

draftlogs/7855_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix crash when ordering categories by value (e.g. `sum descending`) with `null` coordinates [[#7855](https://github.com/plotly/plotly.js/pull/7855)]

src/plots/plots.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3192,6 +3192,10 @@ function sortAxisCategoriesByValue(axList, gd) {
31923192
catIndex = cdi.p;
31933193
if(catIndex === undefined) catIndex = cdi[axLetter];
31943194

3195+
// Skip points whose position is not a valid category
3196+
// (e.g. a null/NaN coordinate maps to BADNUM)
3197+
if (catIndex == null || catIndex < 0) continue;
3198+
31953199
value = cdi.s;
31963200
if(value === undefined) value = cdi.v;
31973201
if(value === undefined) value = isX ? cdi.y : cdi.x;

test/jasmine/tests/calcdata_test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,21 @@ describe('calculated data and points', function() {
12151215
})
12161216
.then(done, done.fail);
12171217
});
1218+
1219+
it('does not throw when a category coordinate is null and ordering by value', function(done) {
1220+
Plotly.newPlot(gd, [{
1221+
type: 'bar',
1222+
x: ['a', null, 'b'],
1223+
y: [3, 5, 7]
1224+
}], {
1225+
xaxis: {type: 'category', categoryorder: 'sum descending'}
1226+
})
1227+
.then(function() {
1228+
// the null point is dropped, remaining categories are ordered by value
1229+
expect(gd._fullLayout.xaxis._categories).toEqual(['b', 'a']);
1230+
})
1231+
.then(done, done.fail);
1232+
});
12181233
});
12191234
});
12201235

0 commit comments

Comments
 (0)