Skip to content

Commit 2d5370c

Browse files
committed
link matches axes x2, x3 to each other if both linked to x while x was not listed in allAxisIds
1 parent cf2c188 commit 2d5370c

File tree

3 files changed

+105
-1
lines changed

3 files changed

+105
-1
lines changed

src/plots/cartesian/constraints.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var FROM_BL = require('../../constants/alignment').FROM_BL;
1919

2020
exports.handleConstraintDefaults = function(containerIn, containerOut, coerce, opts) {
2121
var allAxisIds = opts.allAxisIds;
22+
var layoutIn = opts.layoutIn;
2223
var layoutOut = opts.layoutOut;
2324
var scaleanchorDflt = opts.scaleanchorDflt;
2425
var constrainDflt = opts.constrainDflt;
@@ -44,8 +45,30 @@ exports.handleConstraintDefaults = function(containerIn, containerOut, coerce, o
4445
var matches, matchOpts;
4546

4647
if((containerIn.matches || splomStash.matches) && !containerOut.fixedrange) {
48+
var copyContainerIn = containerIn; // deep copy may not be needed
49+
if(!splomStash.matches) {
50+
var m = copyContainerIn.matches;
51+
if(allAxisIds.indexOf(m) === -1) {
52+
// in case e.g. x was not listed in allAxisIds
53+
// attempt linking x2, x3 to each other if both were linked to x
54+
// see https://github.com/plotly/plotly.js/issues/4501
55+
56+
for(var q = 0; q < allAxisIds.length - 1; q++) {
57+
var idQ = allAxisIds[q];
58+
var nameQ = id2name(idQ);
59+
if(m === layoutIn[nameQ].matches) {
60+
// now make a deep copy to avoid mutate input data
61+
copyContainerIn = Lib.extendDeep({}, containerIn);
62+
63+
copyContainerIn.matches = idQ;
64+
break;
65+
}
66+
}
67+
}
68+
}
69+
4770
matchOpts = getConstraintOpts(matchGroups, thisID, allAxisIds, layoutOut);
48-
matches = Lib.coerce(containerIn, containerOut, {
71+
matches = Lib.coerce(copyContainerIn, containerOut, {
4972
matches: {
5073
valType: 'enumerated',
5174
values: matchOpts.linkableAxes || [],

src/plots/cartesian/layout_defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
314314

315315
handleConstraintDefaults(axLayoutIn, axLayoutOut, coerce, {
316316
allAxisIds: allAxisIds,
317+
layoutIn: layoutIn,
317318
layoutOut: layoutOut,
318319
scaleanchorDflt: scaleanchorDflt,
319320
constrainDflt: constrainDflt

test/jasmine/tests/axes_test.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,86 @@ describe('Test axes', function() {
12531253
});
12541254
});
12551255

1256+
describe('find matching axes', function() {
1257+
var gd;
1258+
1259+
beforeEach(function() {
1260+
gd = createGraphDiv();
1261+
});
1262+
1263+
afterEach(destroyGraphDiv);
1264+
1265+
it('should match groups even with temp reference', function(done) {
1266+
// see https://github.com/plotly/plotly.js/issues/4501
1267+
1268+
var x = [1, 2, 3];
1269+
var y = [1, 3, 2];
1270+
1271+
Plotly.plot(gd, {
1272+
data: [
1273+
{
1274+
xaxis: 'x2',
1275+
yaxis: 'y2',
1276+
1277+
type: 'bar',
1278+
name: 'x2.y2',
1279+
x: x,
1280+
y: y
1281+
},
1282+
{
1283+
xaxis: 'x3',
1284+
yaxis: 'y3',
1285+
1286+
type: 'bar',
1287+
name: 'x3.y3',
1288+
x: x,
1289+
y: y
1290+
}
1291+
],
1292+
layout: {
1293+
title: {
1294+
text: 'Should pan together since x and y are referenced in the layout.'
1295+
},
1296+
xaxis: {
1297+
domain: [0, 0.48]
1298+
},
1299+
xaxis2: {
1300+
matches: 'x',
1301+
anchor: 'y2',
1302+
domain: [0.52, 1]
1303+
},
1304+
xaxis3: {
1305+
matches: 'x',
1306+
anchor: 'y3',
1307+
domain: [0, 0.48]
1308+
},
1309+
yaxis: {
1310+
domain: [0, 0.48]
1311+
},
1312+
yaxis2: {
1313+
matches: 'y',
1314+
anchor: 'x2',
1315+
domain: [0.52, 1]
1316+
},
1317+
yaxis3: {
1318+
matches: 'y',
1319+
anchor: 'x3',
1320+
domain: [0.52, 1]
1321+
}
1322+
}
1323+
})
1324+
.then(function() {
1325+
expect(gd._fullLayout.xaxis3.matches).toBe('x2'); // not x
1326+
expect(gd._fullLayout.yaxis3.matches).toBe('y2'); // not x
1327+
expect(gd._fullLayout._axisMatchGroups.length).toBe(2);
1328+
expect(gd._fullLayout._axisMatchGroups).toContain({x2: 1, x3: 1});
1329+
expect(gd._fullLayout._axisMatchGroups).toContain({y2: 1, y3: 1});
1330+
})
1331+
.catch(failTest)
1332+
.then(done);
1333+
});
1334+
});
1335+
12561336
describe('matching axes relayout calls', function() {
12571337
var gd;
12581338

0 commit comments

Comments
 (0)