Skip to content

Commit 7e22404

Browse files
authored
Merge pull request #7283 from plotly/clear-inputdomain
Fix: Prevent loss of domain information when template field is present in layout
2 parents ca6a886 + 5f128bb commit 7e22404

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

draftlogs/7283_fix.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fix handling of new domain values given in the Plotly.react function to prevent loss of new domain values. [[#7283](https://github.com/plotly/plotly.js/pull/7283)]

src/plot_api/plot_api.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -2811,6 +2811,32 @@ function diffLayout(gd, oldFullLayout, newFullLayout, immutable, transition) {
28112811
return PlotSchema.getLayoutValObject(newFullLayout, parts);
28122812
}
28132813

2814+
// Clear out any _inputDomain that's no longer valid
2815+
for (var key in newFullLayout) {
2816+
if (!key.startsWith('xaxis') && !key.startsWith('yaxis')) {
2817+
continue;
2818+
}
2819+
if (!oldFullLayout[key]) {
2820+
continue;
2821+
}
2822+
var newDomain = newFullLayout[key].domain;
2823+
var oldDomain = oldFullLayout[key].domain;
2824+
var oldInputDomain = oldFullLayout[key]._inputDomain;
2825+
if (oldFullLayout[key]._inputDomain) {
2826+
if (newDomain[0] === oldInputDomain[0] && newDomain[1] === oldInputDomain[1]) {
2827+
// what you're asking for hasn't changed, so let plotly.js start with what it
2828+
// concluded last time and iterate from there
2829+
newFullLayout[key].domain = oldFullLayout[key].domain;
2830+
} else if (newDomain[0] !== oldDomain[0] || newDomain[1] !== oldDomain[1]) {
2831+
// what you're asking for HAS changed, so clear _inputDomain and let us start from scratch
2832+
newFullLayout[key]._inputDomain = null;
2833+
}
2834+
// We skip the else case (newDomain !== oldInputDomain && newDomain === oldDomain)
2835+
// because it's likely that if the newDomain and oldDomain are the same, the user
2836+
// passed in the same layout object and we should keep the _inputDomain.
2837+
}
2838+
}
2839+
28142840
var diffOpts = {
28152841
getValObject: getLayoutValObject,
28162842
flags: flags,
@@ -2863,11 +2889,6 @@ function getDiffFlags(oldContainer, newContainer, outerparts, opts) {
28632889
flags.rangesAltered[outerparts[0]] = 1;
28642890
}
28652891

2866-
// clear _inputDomain on cartesian axes with altered domains
2867-
if(AX_DOMAIN_RE.test(astr)) {
2868-
nestedProperty(newContainer, '_inputDomain').set(null);
2869-
}
2870-
28712892
// track datarevision changes
28722893
if(key === 'datarevision') {
28732894
flags.newDataRevision = 1;

test/jasmine/tests/axes_test.js

+17
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,23 @@ describe('Test axes', function() {
17961796
'input range, ' + msg + ': ' + ax.range);
17971797
}
17981798

1799+
it('can change axis domain on call to react when template is present', function(done) {
1800+
Plotly.newPlot(gd,
1801+
[{z: [[0, 1], [2, 3]], type: 'heatmap'}],
1802+
{ template: {}, xaxis: { domain: [0,1], scaleanchor: 'y' } }
1803+
)
1804+
.then(function() {
1805+
return Plotly.react(gd,
1806+
[{z: [[0, 1], [2, 3]], type: 'heatmap'}],
1807+
{ template: {}, xaxis: { domain: [0.1, 0.9] } }
1808+
);
1809+
})
1810+
.then(function() {
1811+
assertRangeDomain('xaxis', [-0.5,1.5], [0.1, 0.9], [0.1, 0.9]);
1812+
})
1813+
.then(done, done.fail);
1814+
});
1815+
17991816
it('can change per-axis constrain:domain/range and constraintoward', function(done) {
18001817
Plotly.newPlot(gd,
18011818
// start with a heatmap as it has no padding so calculations are easy

0 commit comments

Comments
 (0)