Skip to content

Sort + set 'categoryarray' #1689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -1973,8 +1973,6 @@ plots.doCalcdata = function(gd, traces) {

var trace, _module, i, j;

var hasCategoryAxis = false;

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

// initialize the category list, if there is one, so we start over
// to be filled in later by ax.d2c
for(i = 0; i < axList.length; i++) {
axList[i]._categories = axList[i]._initialCategories.slice();

// Build the lookup map for initialized categories
axList[i]._categoriesMap = {};
for(j = 0; j < axList[i]._categories.length; j++) {
axList[i]._categoriesMap[axList[i]._categories[j]] = j;
}

if(axList[i].type === 'category') hasCategoryAxis = true;
}

// If traces were specified and this trace was not included,
// then transfer it over from the old calcdata:
for(i = 0; i < fullData.length; i++) {
Expand All @@ -2019,6 +2003,8 @@ plots.doCalcdata = function(gd, traces) {
}
}

var hasCategoryAxis = plots.initCategories(axList);

var hasCalcTransform = false;

// transform loop
Expand Down Expand Up @@ -2051,9 +2037,9 @@ plots.doCalcdata = function(gd, traces) {
axList[i]._min = [];
axList[i]._max = [];
axList[i]._categories = [];
// Reset the look up map
axList[i]._categoriesMap = {};
}
plots.initCategories(axList);
}

// 'regular' loop
Expand Down Expand Up @@ -2099,6 +2085,26 @@ plots.doCalcdata = function(gd, traces) {
}
};

plots.initCategories = function(axList) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason to export this fn?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but I'd prefer not to since it goes into Plotly.Plots

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good call 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in b8e5f52

var hasCategoryAxis = false;

// initialize the category list, if there is one, so we start over
// to be filled in later by ax.d2c
for(var i = 0; i < axList.length; i++) {
axList[i]._categories = axList[i]._initialCategories.slice();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly would have been caught by now if this could ever be undefined, right? (I'm too cautious about checking for undefined unnecessarily)

Copy link
Contributor Author

@etpinard etpinard May 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. That _initialCategories thing is set in the defaults. Calling Plots.doCalcdata before supplyDefaults is like 🔫 yourself in the 👣 .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha the squirt gun. And then your socks are wet for the rest of the day.


// Build the lookup map for initialized categories
axList[i]._categoriesMap = {};
for(var j = 0; j < axList[i]._categories.length; j++) {
axList[i]._categoriesMap[axList[i]._categories[j]] = j;
}

if(axList[i].type === 'category') hasCategoryAxis = true;
}

return hasCategoryAxis;
};

plots.rehover = function(gd) {
if(gd._fullLayout._rehover) {
gd._fullLayout._rehover();
Expand Down
52 changes: 52 additions & 0 deletions test/jasmine/tests/transform_sort_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,21 @@ describe('Test sort transform calc:', function() {
expect(out[0].y).toEqual([0, 2, 4, 3, 1]);
});

it('should sort via categorical targets', function() {
var trace = extend({
transforms: [{ target: 'marker.size' }]
});
trace.x = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I'];

var out = _transform([trace]);

expect(out[0].x).toEqual(['F', 'D', 'C', 'E', 'A', 'G', 'B']);
expect(out[0].y).toEqual([3, 1, 3, 2, 1, 1, 2]);
expect(out[0].ids).toEqual(['p2', 'z', 'n2', 'p1', 'n0', 'p3', 'n1']);
expect(out[0].marker.size).toEqual([0, 1, 5, 6, 10, 10, 20]);
expect(out[0].marker.color).toEqual([0.3, 0.1, 0.3, 0.2, 0.1, 0.4, 0.2]);
});

it('should sort via custom targets', function() {
var out = _transform([extend({
transforms: [{
Expand Down Expand Up @@ -340,4 +355,41 @@ describe('Test sort transform interactions:', function() {
.catch(fail)
.then(done);
});

it('should honor *categoryarray* when set', function(done) {
var gd = createGraphDiv();

Plotly.plot(gd, [{
x: ['C', 'B', 'A'],
y: [3, 1, 2],
marker: {
size: [10, 20, 5]
},
transforms: [{
enabled: false,
type: 'sort',
target: [0, 2, 1],
}]
}], {
xaxis: {
categoryorder: 'trace',
categoryarray: ['A', 'B', 'C']
}
})
.then(function() {
expect(gd._fullLayout.xaxis._categories).toEqual(['C', 'B', 'A']);

return Plotly.restyle(gd, 'transforms[0].enabled', true);
})
.then(function() {
expect(gd._fullLayout.xaxis._categories).toEqual(['C', 'A', 'B']);

return Plotly.relayout(gd, 'xaxis.categoryorder', 'array');
})
.then(function() {
expect(gd._fullLayout.xaxis._categories).toEqual(['A', 'B', 'C']);
})
.catch(fail)
.then(done);
});
});