Skip to content

Commit 7a2a2e1

Browse files
committed
re-initialize categories internals after calc transforms
1 parent f986703 commit 7a2a2e1

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

src/plots/plots.js

+23-17
Original file line numberDiff line numberDiff line change
@@ -1973,8 +1973,6 @@ plots.doCalcdata = function(gd, traces) {
19731973

19741974
var trace, _module, i, j;
19751975

1976-
var hasCategoryAxis = false;
1977-
19781976
// XXX: Is this correct? Needs a closer look so that *some* traces can be recomputed without
19791977
// *all* needing doCalcdata:
19801978
var calcdata = new Array(fullData.length);
@@ -1996,20 +1994,6 @@ plots.doCalcdata = function(gd, traces) {
19961994
fullLayout._piecolormap = {};
19971995
fullLayout._piedefaultcolorcount = 0;
19981996

1999-
// initialize the category list, if there is one, so we start over
2000-
// to be filled in later by ax.d2c
2001-
for(i = 0; i < axList.length; i++) {
2002-
axList[i]._categories = axList[i]._initialCategories.slice();
2003-
2004-
// Build the lookup map for initialized categories
2005-
axList[i]._categoriesMap = {};
2006-
for(j = 0; j < axList[i]._categories.length; j++) {
2007-
axList[i]._categoriesMap[axList[i]._categories[j]] = j;
2008-
}
2009-
2010-
if(axList[i].type === 'category') hasCategoryAxis = true;
2011-
}
2012-
20131997
// If traces were specified and this trace was not included,
20141998
// then transfer it over from the old calcdata:
20151999
for(i = 0; i < fullData.length; i++) {
@@ -2019,6 +2003,8 @@ plots.doCalcdata = function(gd, traces) {
20192003
}
20202004
}
20212005

2006+
var hasCategoryAxis = plots.initCategories(axList);
2007+
20222008
var hasCalcTransform = false;
20232009

20242010
// transform loop
@@ -2051,9 +2037,9 @@ plots.doCalcdata = function(gd, traces) {
20512037
axList[i]._min = [];
20522038
axList[i]._max = [];
20532039
axList[i]._categories = [];
2054-
// Reset the look up map
20552040
axList[i]._categoriesMap = {};
20562041
}
2042+
plots.initCategories(axList);
20572043
}
20582044

20592045
// 'regular' loop
@@ -2099,6 +2085,26 @@ plots.doCalcdata = function(gd, traces) {
20992085
}
21002086
};
21012087

2088+
plots.initCategories = function(axList) {
2089+
var hasCategoryAxis = false;
2090+
2091+
// initialize the category list, if there is one, so we start over
2092+
// to be filled in later by ax.d2c
2093+
for(var i = 0; i < axList.length; i++) {
2094+
axList[i]._categories = axList[i]._initialCategories.slice();
2095+
2096+
// Build the lookup map for initialized categories
2097+
axList[i]._categoriesMap = {};
2098+
for(var j = 0; j < axList[i]._categories.length; j++) {
2099+
axList[i]._categoriesMap[axList[i]._categories[j]] = j;
2100+
}
2101+
2102+
if(axList[i].type === 'category') hasCategoryAxis = true;
2103+
}
2104+
2105+
return hasCategoryAxis;
2106+
};
2107+
21022108
plots.rehover = function(gd) {
21032109
if(gd._fullLayout._rehover) {
21042110
gd._fullLayout._rehover();

test/jasmine/tests/transform_sort_test.js

+37
Original file line numberDiff line numberDiff line change
@@ -355,4 +355,41 @@ describe('Test sort transform interactions:', function() {
355355
.catch(fail)
356356
.then(done);
357357
});
358+
359+
it('should honor *categoryarray* when set', function(done) {
360+
var gd = createGraphDiv();
361+
362+
Plotly.plot(gd, [{
363+
x: ['C', 'B', 'A'],
364+
y: [3, 1, 2],
365+
marker: {
366+
size: [10, 20, 5]
367+
},
368+
transforms: [{
369+
enabled: false,
370+
type: 'sort',
371+
target: [0, 2, 1],
372+
}]
373+
}], {
374+
xaxis: {
375+
categoryorder: 'trace',
376+
categoryarray: ['A', 'B', 'C']
377+
}
378+
})
379+
.then(function() {
380+
expect(gd._fullLayout.xaxis._categories).toEqual(['C', 'B', 'A']);
381+
382+
return Plotly.restyle(gd, 'transforms[0].enabled', true);
383+
})
384+
.then(function() {
385+
expect(gd._fullLayout.xaxis._categories).toEqual(['C', 'A', 'B']);
386+
387+
return Plotly.relayout(gd, 'xaxis.categoryorder', 'array');
388+
})
389+
.then(function() {
390+
expect(gd._fullLayout.xaxis._categories).toEqual(['A', 'B', 'C']);
391+
})
392+
.catch(fail)
393+
.then(done);
394+
});
358395
});

0 commit comments

Comments
 (0)