Skip to content

Commit 4a33271

Browse files
authored
Merge pull request #4973 from plotly/parcats-numeric-sort
Fix parcats category order when dimension only includes numbers
2 parents 7a6102a + 3b9184b commit 4a33271

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

src/traces/parcats/calc.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var colorscaleCalc = require('../../components/colorscale/calc');
1616
var filterUnique = require('../../lib/filter_unique.js');
1717
var Drawing = require('../../components/drawing');
1818
var Lib = require('../../lib');
19+
var isNumeric = require('fast-isnumeric');
1920

2021
/**
2122
* Create a wrapped ParcatsModel object from trace
@@ -40,9 +41,19 @@ module.exports = function calc(gd, trace) {
4041
// then add extra to the end in trace order
4142
categoryValues = dim.categoryarray;
4243
} else {
43-
// Get all categories up front so we can order them
44-
// Should we check for numbers as sort numerically?
45-
categoryValues = filterUnique(dim.values).sort();
44+
// Get all categories up front
45+
categoryValues = filterUnique(dim.values);
46+
47+
// order them
48+
var allNumeric = true;
49+
for(var i = 0; i < categoryValues.length; i++) {
50+
if(!isNumeric(categoryValues[i])) {
51+
allNumeric = false;
52+
break;
53+
}
54+
}
55+
categoryValues.sort(allNumeric ? Lib.sorterAsc : undefined);
56+
4657
if(dim.categoryorder === 'category descending') {
4758
categoryValues = categoryValues.reverse();
4859
}
6.16 KB
Loading
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"data": [
3+
{
4+
"type": "parcats",
5+
"dimensions": [
6+
{
7+
"label": "A",
8+
"values": [
9+
1,
10+
2,
11+
3
12+
],
13+
"categoryorder": "category ascending"
14+
},
15+
{
16+
"label": "B",
17+
"values": [
18+
9,
19+
10,
20+
11
21+
],
22+
"categoryorder": "category ascending"
23+
}
24+
]
25+
}
26+
],
27+
"layout": {
28+
"width": 320,
29+
"height": 200,
30+
"margin": {
31+
"t": 20,
32+
"b": 20,
33+
"l": 20,
34+
"r": 20
35+
},
36+
"font": {
37+
"size": 16
38+
}
39+
}
40+
}

test/jasmine/tests/mock_test.js

+2
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,7 @@ var list = [
743743
'parcats_hoveron_color',
744744
'parcats_hoveron_dimension',
745745
'parcats_invisible_dimension',
746+
'parcats_numeric_sort',
746747
'parcats_reordered',
747748
'parcats_unbundled',
748749
'percent_error_bar',
@@ -1785,6 +1786,7 @@ figs['parcats_grid_subplots'] = require('@mocks/parcats_grid_subplots');
17851786
figs['parcats_hoveron_color'] = require('@mocks/parcats_hoveron_color');
17861787
figs['parcats_hoveron_dimension'] = require('@mocks/parcats_hoveron_dimension');
17871788
figs['parcats_invisible_dimension'] = require('@mocks/parcats_invisible_dimension');
1789+
figs['parcats_numeric_sort'] = require('@mocks/parcats_numeric_sort');
17881790
figs['parcats_reordered'] = require('@mocks/parcats_reordered');
17891791
figs['parcats_unbundled'] = require('@mocks/parcats_unbundled');
17901792
figs['percent_error_bar'] = require('@mocks/percent_error_bar');

0 commit comments

Comments
 (0)