Skip to content

Commit 5bb74bc

Browse files
authoredDec 15, 2020
Merge pull request #5345 from plotly/fix-5318-missing-axes-tick-labels
Fix Plotly.react/Angular missing categories + axis tick labels
2 parents 82da615 + eed5c8a commit 5bb74bc

File tree

2 files changed

+73
-13
lines changed

2 files changed

+73
-13
lines changed
 

‎src/plot_api/plot_api.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -2737,16 +2737,6 @@ function react(gd, data, layout, config) {
27372737

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

2740-
var allNames = Object.getOwnPropertyNames(oldFullLayout);
2741-
for(var q = 0; q < allNames.length; q++) {
2742-
var name = allNames[q];
2743-
var start = name.substring(0, 5);
2744-
if(start === 'xaxis' || start === 'yaxis') {
2745-
var emptyCategories = oldFullLayout[name]._emptyCategories;
2746-
if(emptyCategories) emptyCategories();
2747-
}
2748-
}
2749-
27502740
// "true" skips updating calcdata and remapping arrays from calcTransforms,
27512741
// which supplyDefaults usually does at the end, but we may need to NOT do
27522742
// if the diff (which we haven't determined yet) says we'll recalc
@@ -2772,10 +2762,22 @@ function react(gd, data, layout, config) {
27722762

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

2775-
// clear calcdata if required
2776-
if(restyleFlags.calc || relayoutFlags.calc) gd.calcdata = undefined;
2765+
// clear calcdata and empty categories if required
2766+
if(restyleFlags.calc || relayoutFlags.calc) {
2767+
gd.calcdata = undefined;
2768+
var allNames = Object.getOwnPropertyNames(newFullLayout);
2769+
for(var q = 0; q < allNames.length; q++) {
2770+
var name = allNames[q];
2771+
var start = name.substring(0, 5);
2772+
if(start === 'xaxis' || start === 'yaxis') {
2773+
var emptyCategories = newFullLayout[name]._emptyCategories;
2774+
if(emptyCategories) emptyCategories();
2775+
}
2776+
}
27772777
// otherwise do the calcdata updates and calcTransform array remaps that we skipped earlier
2778-
else Plots.supplyDefaultsUpdateCalc(gd.calcdata, newFullData);
2778+
} else {
2779+
Plots.supplyDefaultsUpdateCalc(gd.calcdata, newFullData);
2780+
}
27792781

27802782
// Note: what restyle/relayout use impliedEdits and clearAxisTypes for
27812783
// must be handled by the user when using Plotly.react.

‎test/jasmine/tests/axes_test.js

+58
Original file line numberDiff line numberDiff line change
@@ -6896,6 +6896,64 @@ describe('more react tests', function() {
68966896
});
68976897
});
68986898

6899+
describe('category preservation tests on gd passed to Plotly.react()', function() {
6900+
var gd;
6901+
6902+
beforeEach(function() {
6903+
gd = createGraphDiv();
6904+
});
6905+
6906+
afterEach(destroyGraphDiv);
6907+
6908+
function _hover(gd, opts) {
6909+
Fx.hover(gd, opts);
6910+
// needed for successive hover events
6911+
Lib.clearThrottle();
6912+
}
6913+
6914+
it('should preserve categories and axis ticklabels', function(done) {
6915+
var fig = {
6916+
data: [{
6917+
type: 'bar',
6918+
y: [3, 5, 3, 2],
6919+
x: ['a', 'b', 'c', 'd']
6920+
}],
6921+
layout: {
6922+
width: 500,
6923+
height: 500
6924+
}
6925+
};
6926+
6927+
Plotly.newPlot(gd, fig)
6928+
.then(function(gd) {
6929+
return Plotly.react(gd, fig);
6930+
})
6931+
.then(function() {
6932+
expect(gd._fullLayout.xaxis._categories).toEqual(['a', 'b', 'c', 'd']);
6933+
expect(gd._fullLayout.xaxis._categoriesMap).toEqual({a: 0, b: 1, c: 2, d: 3});
6934+
})
6935+
.then(function() {
6936+
_hover(gd, { xval: fig.data[0].x.indexOf('a') });
6937+
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('a');
6938+
})
6939+
.then(function() {
6940+
_hover(gd, { xval: fig.data[0].x.indexOf('b') });
6941+
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('b');
6942+
})
6943+
.then(function() {
6944+
_hover(gd, { xval: fig.data[0].x.indexOf('c') });
6945+
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('c');
6946+
})
6947+
.then(function() {
6948+
_hover(gd, { xval: fig.data[0].x.indexOf('d') });
6949+
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('d');
6950+
})
6951+
6952+
.catch(failTest)
6953+
.then(done);
6954+
});
6955+
});
6956+
68996957
describe('more matching axes tests', function() {
69006958
var gd;
69016959

0 commit comments

Comments
 (0)
Please sign in to comment.