Skip to content

Commit a86a5ac

Browse files
authored
Merge pull request #1689 from plotly/sort-with-categoryarray
Sort + set 'categoryarray'
2 parents a729acc + b8e5f52 commit a86a5ac

File tree

2 files changed

+75
-17
lines changed

2 files changed

+75
-17
lines changed

src/plots/plots.js

Lines changed: 23 additions & 17 deletions
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 = 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+
initCategories(axList);
20572043
}
20582044

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

2088+
function initCategories(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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,21 @@ describe('Test sort transform calc:', function() {
134134
expect(out[0].y).toEqual([0, 2, 4, 3, 1]);
135135
});
136136

137+
it('should sort via categorical targets', function() {
138+
var trace = extend({
139+
transforms: [{ target: 'marker.size' }]
140+
});
141+
trace.x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];
142+
143+
var out = _transform([trace]);
144+
145+
expect(out[0].x).toEqual(['F', 'D', 'C', 'E', 'A', 'G', 'B']);
146+
expect(out[0].y).toEqual([3, 1, 3, 2, 1, 1, 2]);
147+
expect(out[0].ids).toEqual(['p2', 'z', 'n2', 'p1', 'n0', 'p3', 'n1']);
148+
expect(out[0].marker.size).toEqual([0, 1, 5, 6, 10, 10, 20]);
149+
expect(out[0].marker.color).toEqual([0.3, 0.1, 0.3, 0.2, 0.1, 0.4, 0.2]);
150+
});
151+
137152
it('should sort via custom targets', function() {
138153
var out = _transform([extend({
139154
transforms: [{
@@ -340,4 +355,41 @@ describe('Test sort transform interactions:', function() {
340355
.catch(fail)
341356
.then(done);
342357
});
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+
});
343395
});

0 commit comments

Comments
 (0)