Skip to content

Commit 9f89dd6

Browse files
committed
fix for autorange with bad axis refs from a component
1 parent 7ed9eb9 commit 9f89dd6

File tree

2 files changed

+64
-28
lines changed

2 files changed

+64
-28
lines changed

Diff for: src/plot_api/plot_api.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -1822,10 +1822,22 @@ function _relayout(gd, aobj) {
18221822
}
18231823

18241824
// for editing annotations or shapes - is it on autoscaled axes?
1825-
function refAutorange(obj, axletter) {
1825+
function refAutorange(obj, axLetter) {
18261826
if(!Lib.isPlainObject(obj)) return false;
1827-
var axName = Plotly.Axes.id2name(obj[axletter + 'ref'] || axletter);
1828-
return (fullLayout[axName] || {}).autorange;
1827+
var axRef = obj[axLetter + 'ref'] || axLetter,
1828+
ax = Plotly.Axes.getFromId(gd, axRef);
1829+
1830+
if(!ax && axRef.charAt(0) === axLetter) {
1831+
// fall back on the primary axis in case we've referenced a
1832+
// nonexistent axis (as we do above if axRef is missing).
1833+
// This assumes the object defaults to data referenced, which
1834+
// is the case for shapes and annotations but not for images.
1835+
// The only thing this is used for is to determine whether to
1836+
// do a full `recalc`, so the only ill effect of this error is
1837+
// to waste some time.
1838+
ax = Plotly.Axes.getFromId(gd, axLetter);
1839+
}
1840+
return (ax || {}).autorange;
18291841
}
18301842

18311843
// alter gd.layout

Diff for: test/jasmine/tests/annotations_test.js

+49-25
Original file line numberDiff line numberDiff line change
@@ -474,43 +474,43 @@ describe('annotations log/linear axis changes', function() {
474474

475475
});
476476

477-
describe('annotations autosize', function() {
477+
describe('annotations autorange', function() {
478478
'use strict';
479479

480480
var mock = Lib.extendDeep({}, require('@mocks/annotations-autorange.json'));
481481
var gd;
482482

483483
beforeAll(function() {
484484
jasmine.addMatchers(customMatchers);
485+
486+
gd = createGraphDiv();
485487
});
486488

487489
afterEach(destroyGraphDiv);
488490

489-
it('should adapt to relayout calls', function(done) {
490-
gd = createGraphDiv();
491-
492-
function assertRanges(x, y, x2, y2, x3, y3) {
493-
var fullLayout = gd._fullLayout;
494-
var PREC = 1;
495-
496-
// xaxis2 need a bit more tolerance to pass on CI
497-
// this most likely due to the different text bounding box values
498-
// on headfull vs headless browsers.
499-
// but also because it's a date axis that we've converted to ms
500-
var PRECX2 = -10;
501-
// yaxis2 needs a bit more now too...
502-
var PRECY2 = 0.2;
503-
var dateAx = fullLayout.xaxis2;
504-
505-
expect(fullLayout.xaxis.range).toBeCloseToArray(x, PREC, '- xaxis');
506-
expect(fullLayout.yaxis.range).toBeCloseToArray(y, PREC, '- yaxis');
507-
expect(Lib.simpleMap(dateAx.range, dateAx.r2l))
508-
.toBeCloseToArray(Lib.simpleMap(x2, dateAx.r2l), PRECX2, 'xaxis2 ' + dateAx.range);
509-
expect(fullLayout.yaxis2.range).toBeCloseToArray(y2, PRECY2, 'yaxis2');
510-
expect(fullLayout.xaxis3.range).toBeCloseToArray(x3, PREC, 'xaxis3');
511-
expect(fullLayout.yaxis3.range).toBeCloseToArray(y3, PREC, 'yaxis3');
512-
}
491+
function assertRanges(x, y, x2, y2, x3, y3) {
492+
var fullLayout = gd._fullLayout;
493+
var PREC = 1;
494+
495+
// xaxis2 need a bit more tolerance to pass on CI
496+
// this most likely due to the different text bounding box values
497+
// on headfull vs headless browsers.
498+
// but also because it's a date axis that we've converted to ms
499+
var PRECX2 = -10;
500+
// yaxis2 needs a bit more now too...
501+
var PRECY2 = 0.2;
502+
var dateAx = fullLayout.xaxis2;
503+
504+
expect(fullLayout.xaxis.range).toBeCloseToArray(x, PREC, '- xaxis');
505+
expect(fullLayout.yaxis.range).toBeCloseToArray(y, PREC, '- yaxis');
506+
expect(Lib.simpleMap(dateAx.range, dateAx.r2l))
507+
.toBeCloseToArray(Lib.simpleMap(x2, dateAx.r2l), PRECX2, 'xaxis2 ' + dateAx.range);
508+
expect(fullLayout.yaxis2.range).toBeCloseToArray(y2, PRECY2, 'yaxis2');
509+
expect(fullLayout.xaxis3.range).toBeCloseToArray(x3, PREC, 'xaxis3');
510+
expect(fullLayout.yaxis3.range).toBeCloseToArray(y3, PREC, 'yaxis3');
511+
}
513512

513+
it('should adapt to relayout calls', function(done) {
514514
Plotly.plot(gd, mock).then(function() {
515515
assertRanges(
516516
[0.97, 2.03], [0.97, 2.03],
@@ -563,6 +563,30 @@ describe('annotations autosize', function() {
563563
.catch(failTest)
564564
.then(done);
565565
});
566+
567+
it('catches bad xref/yref', function(done) {
568+
Plotly.plot(gd, mock).then(function() {
569+
return Plotly.relayout(gd, {'annotations[1]': {
570+
text: 'LT',
571+
x: -1,
572+
y: 3,
573+
xref: 'x5', // will be converted to 'x' and xaxis should autorange
574+
yref: 'y5', // same 'y' -> yaxis
575+
ax: 50,
576+
ay: 50
577+
}});
578+
})
579+
.then(function() {
580+
assertRanges(
581+
[-1.09, 2.09], [0.94, 3.06],
582+
// the other axes shouldn't change
583+
['2000-10-01 08:23:18.0583', '2001-06-05 19:20:23.301'], [-0.245, 4.245],
584+
[0.9, 2.1], [0.86, 2.14]
585+
);
586+
})
587+
.catch(failTest)
588+
.then(done);
589+
});
566590
});
567591

568592
describe('annotation clicktoshow', function() {

0 commit comments

Comments
 (0)