Skip to content
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

Fix Plotly.react/Angular missing categories + axis tick labels #5345

Merged
merged 2 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
28 changes: 15 additions & 13 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2737,16 +2737,6 @@ function react(gd, data, layout, config) {

applyUIRevisions(gd.data, gd.layout, oldFullData, oldFullLayout);

var allNames = Object.getOwnPropertyNames(oldFullLayout);
for(var q = 0; q < allNames.length; q++) {
var name = allNames[q];
var start = name.substring(0, 5);
if(start === 'xaxis' || start === 'yaxis') {
var emptyCategories = oldFullLayout[name]._emptyCategories;
if(emptyCategories) emptyCategories();
}
}

// "true" skips updating calcdata and remapping arrays from calcTransforms,
// which supplyDefaults usually does at the end, but we may need to NOT do
// if the diff (which we haven't determined yet) says we'll recalc
Expand All @@ -2772,10 +2762,22 @@ function react(gd, data, layout, config) {

if(updateAutosize(gd)) relayoutFlags.layoutReplot = true;

// clear calcdata if required
if(restyleFlags.calc || relayoutFlags.calc) gd.calcdata = undefined;
// clear calcdata and empty categories if required
if(restyleFlags.calc || relayoutFlags.calc) {
gd.calcdata = undefined;
var allNames = Object.getOwnPropertyNames(newFullLayout);
for(var q = 0; q < allNames.length; q++) {
var name = allNames[q];
var start = name.substring(0, 5);
if(start === 'xaxis' || start === 'yaxis') {
var emptyCategories = newFullLayout[name]._emptyCategories;
if(emptyCategories) emptyCategories();
}
}
// otherwise do the calcdata updates and calcTransform array remaps that we skipped earlier
else Plots.supplyDefaultsUpdateCalc(gd.calcdata, newFullData);
} else {
Plots.supplyDefaultsUpdateCalc(gd.calcdata, newFullData);
}

// Note: what restyle/relayout use impliedEdits and clearAxisTypes for
// must be handled by the user when using Plotly.react.
Expand Down
58 changes: 58 additions & 0 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6896,6 +6896,64 @@ describe('more react tests', function() {
});
});

describe('category preservation tests on gd passed to Plotly.react()', function() {
var gd;

beforeEach(function() {
gd = createGraphDiv();
});

afterEach(destroyGraphDiv);

function _hover(gd, opts) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Borrowed from hover_label_test.js

function _hover(gd, opts) {
Fx.hover(gd, opts);
Lib.clearThrottle();
}

Fx.hover(gd, opts);
// needed for successive hover events
Lib.clearThrottle();
}

it('should preserve categories and axis ticklabels', function(done) {
var fig = {
data: [{
type: 'bar',
y: [3, 5, 3, 2],
x: ['a', 'b', 'c', 'd']
}],
layout: {
width: 500,
height: 500
}
};

Plotly.newPlot(gd, fig)
.then(function(gd) {
return Plotly.react(gd, fig);
})
.then(function() {
expect(gd._fullLayout.xaxis._categories).toEqual(['a', 'b', 'c', 'd']);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fails with empty array ([]) on master

expect(gd._fullLayout.xaxis._categoriesMap).toEqual({a: 0, b: 1, c: 2, d: 3});
})
.then(function() {
_hover(gd, { xval: fig.data[0].x.indexOf('a') });
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('a');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fails with empty string ('') on master

})
.then(function() {
_hover(gd, { xval: fig.data[0].x.indexOf('b') });
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('b');
})
.then(function() {
_hover(gd, { xval: fig.data[0].x.indexOf('c') });
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('c');
})
.then(function() {
_hover(gd, { xval: fig.data[0].x.indexOf('d') });
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('d');
})

.catch(failTest)
.then(done);
});
});

describe('more matching axes tests', function() {
var gd;

Expand Down