Skip to content

Commit a51b91f

Browse files
authored
Merge pull request #5268 from plotly/fix5267-cartesian-category-values
Fixup categoryorder for missing values in cartesian traces
2 parents 98dfdb8 + b95a0a5 commit a51b91f

File tree

4 files changed

+68
-23
lines changed

4 files changed

+68
-23
lines changed

src/plots/plots.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3098,6 +3098,9 @@ function sortAxisCategoriesByValue(axList, gd) {
30983098
var aggregator = match[1];
30993099
var order = match[2];
31003100

3101+
var axLetter = ax._id.charAt(0);
3102+
var isX = axLetter === 'x';
3103+
31013104
// Store values associated with each category
31023105
var categoriesValue = [];
31033106
for(j = 0; j < ax._categories.length; j++) {
@@ -3108,7 +3111,6 @@ function sortAxisCategoriesByValue(axList, gd) {
31083111
for(j = 0; j < ax._traceIndices.length; j++) {
31093112
var traceIndex = ax._traceIndices[j];
31103113
var fullTrace = gd._fullData[traceIndex];
3111-
var axLetter = ax._id.charAt(0);
31123114

31133115
// Skip over invisible traces
31143116
if(fullTrace.visible !== true) continue;
@@ -3118,27 +3120,28 @@ function sortAxisCategoriesByValue(axList, gd) {
31183120
delete fullTrace._xautoBinFinished;
31193121
delete fullTrace._yautoBinFinished;
31203122
}
3123+
var isSplom = type === 'splom';
3124+
var isScattergl = type === 'scattergl';
31213125

31223126
var cd = gd.calcdata[traceIndex];
31233127
for(k = 0; k < cd.length; k++) {
31243128
var cdi = cd[k];
3125-
var cat, catIndex, value;
3129+
var catIndex, value;
31263130

3127-
if(type === 'splom') {
3131+
if(isSplom) {
31283132
// If `splom`, collect values across dimensions
31293133
// Find which dimension the current axis is representing
31303134
var currentDimensionIndex = fullTrace._axesDim[ax._id];
31313135

31323136
// Apply logic to associated x axis if it's defined
3133-
if(axLetter === 'y') {
3137+
if(!isX) {
31343138
var associatedXAxisID = fullTrace._diag[currentDimensionIndex][0];
31353139
if(associatedXAxisID) ax = gd._fullLayout[axisIDs.id2name(associatedXAxisID)];
31363140
}
31373141

31383142
var categories = cdi.trace.dimensions[currentDimensionIndex].values;
31393143
for(l = 0; l < categories.length; l++) {
3140-
cat = categories[l];
3141-
catIndex = ax._categoriesMap[cat];
3144+
catIndex = ax._categoriesMap[categories[l]];
31423145

31433146
// Collect associated values at index `l` over all other dimensions
31443147
for(o = 0; o < cdi.trace.dimensions.length; o++) {
@@ -3147,18 +3150,14 @@ function sortAxisCategoriesByValue(axList, gd) {
31473150
categoriesValue[catIndex][1].push(dimension.values[l]);
31483151
}
31493152
}
3150-
} else if(type === 'scattergl') {
3153+
} else if(isScattergl) {
31513154
// If `scattergl`, collect all values stashed under cdi.t
31523155
for(l = 0; l < cdi.t.x.length; l++) {
3153-
if(axLetter === 'x') {
3154-
cat = cdi.t.x[l];
3155-
catIndex = cat;
3156+
if(isX) {
3157+
catIndex = cdi.t.x[l];
31563158
value = cdi.t.y[l];
3157-
}
3158-
3159-
if(axLetter === 'y') {
3160-
cat = cdi.t.y[l];
3161-
catIndex = cat;
3159+
} else {
3160+
catIndex = cdi.t.y[l];
31623161
value = cdi.t.x[l];
31633162
}
31643163
categoriesValue[catIndex][1].push(value);
@@ -3181,16 +3180,19 @@ function sortAxisCategoriesByValue(axList, gd) {
31813180
}
31823181
} else {
31833182
// For all other 2d cartesian traces
3184-
if(axLetter === 'x') {
3185-
cat = cdi.p + 1 ? cdi.p : cdi.x;
3186-
value = cdi.s || cdi.v || cdi.y;
3187-
} else if(axLetter === 'y') {
3188-
cat = cdi.p + 1 ? cdi.p : cdi.y;
3189-
value = cdi.s || cdi.v || cdi.x;
3183+
catIndex = cdi.p;
3184+
if(catIndex === undefined) catIndex = cdi[axLetter];
3185+
3186+
value = cdi.s;
3187+
if(value === undefined) value = cdi.v;
3188+
if(value === undefined) value = isX ? cdi.y : cdi.x;
3189+
3190+
if(!Array.isArray(value)) {
3191+
if(value === undefined) value = [];
3192+
else value = [value];
31903193
}
3191-
if(!Array.isArray(value)) value = [value];
31923194
for(l = 0; l < value.length; l++) {
3193-
categoriesValue[cat][1].push(value[l]);
3195+
categoriesValue[catIndex][1].push(value[l]);
31943196
}
31953197
}
31963198
}
5.26 KB
Loading
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"data": [
3+
{
4+
"type": "histogram",
5+
"x": [
6+
"A",
7+
"A",
8+
"B"
9+
]
10+
},
11+
{
12+
"type": "histogram",
13+
"x": [
14+
"A",
15+
"A",
16+
"C"
17+
]
18+
},
19+
{
20+
"type": "histogram",
21+
"x": [
22+
"B",
23+
"B"
24+
]
25+
}
26+
],
27+
"layout": {
28+
"barmode": "stack",
29+
"xaxis": {
30+
"categoryorder": "total descending"
31+
},
32+
"width": 300,
33+
"height": 200,
34+
"margin": {
35+
"t": 40,
36+
"b": 20,
37+
"l": 20,
38+
"r": 20
39+
}
40+
}
41+
}

test/jasmine/tests/mock_test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ var list = [
737737
'matching-missing-axes',
738738
'mathjax',
739739
'mirror-all-vs-allticks',
740+
'missing-category-order',
740741
'multicategory',
741742
'multicategory_histograms',
742743
'multicategory-inside-ticks',
@@ -1809,6 +1810,7 @@ figs['matching-categories'] = require('@mocks/matching-categories');
18091810
// figs['matching-missing-axes'] = require('@mocks/matching-missing-axes');
18101811
// figs['mathjax'] = require('@mocks/mathjax');
18111812
figs['mirror-all-vs-allticks'] = require('@mocks/mirror-all-vs-allticks');
1813+
figs['missing-category-order'] = require('@mocks/missing-category-order');
18121814
figs['multicategory'] = require('@mocks/multicategory');
18131815
figs['multicategory_histograms'] = require('@mocks/multicategory_histograms');
18141816
figs['multicategory-inside-ticks'] = require('@mocks/multicategory-inside-ticks');

0 commit comments

Comments
 (0)