Skip to content

Commit ed7983c

Browse files
authored
Merge pull request #3534 from plotly/annotations-on-trace-less-subplots
Fix annotations SVG errors on trace-less subplots
2 parents 59405d8 + a35500a commit ed7983c

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

src/components/annotations/draw.js

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ function drawOne(gd, index) {
6767
var xa = Axes.getFromId(gd, options.xref);
6868
var ya = Axes.getFromId(gd, options.yref);
6969

70+
if(xa) xa.setScale();
71+
if(ya) ya.setScale();
72+
7073
drawRaw(gd, options, index, false, xa, ya);
7174
}
7275

src/plots/gl3d/scene.js

+27-2
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,29 @@ function computeTraceBounds(scene, trace, bounds) {
399399
}
400400
}
401401

402+
function computeAnnotationBounds(scene, bounds) {
403+
var sceneLayout = scene.fullSceneLayout;
404+
var annotations = sceneLayout.annotations || [];
405+
406+
for(var d = 0; d < 3; d++) {
407+
var axisName = axisProperties[d];
408+
var axLetter = axisName.charAt(0);
409+
var ax = sceneLayout[axisName];
410+
411+
for(var j = 0; j < annotations.length; j++) {
412+
var ann = annotations[j];
413+
414+
if(ann.visible) {
415+
var pos = ax.r2l(ann[axLetter]);
416+
if(!isNaN(pos) && isFinite(pos)) {
417+
bounds[0][d] = Math.min(bounds[0][d], pos);
418+
bounds[1][d] = Math.max(bounds[1][d], pos);
419+
}
420+
}
421+
}
422+
}
423+
}
424+
402425
proto.plot = function(sceneData, fullLayout, layout) {
403426
// Save parameters
404427
this.plotArgs = [sceneData, fullLayout, layout];
@@ -443,18 +466,20 @@ proto.plot = function(sceneData, fullLayout, layout) {
443466
[Infinity, Infinity, Infinity],
444467
[-Infinity, -Infinity, -Infinity]
445468
];
469+
446470
for(i = 0; i < sceneData.length; ++i) {
447471
data = sceneData[i];
448472
if(data.visible !== true) continue;
449473

450474
computeTraceBounds(this, data, dataBounds);
451475
}
476+
computeAnnotationBounds(this, dataBounds);
477+
452478
var dataScale = [1, 1, 1];
453479
for(j = 0; j < 3; ++j) {
454480
if(dataBounds[1][j] === dataBounds[0][j]) {
455481
dataScale[j] = 1.0;
456-
}
457-
else {
482+
} else {
458483
dataScale[j] = 1.0 / (dataBounds[1][j] - dataBounds[0][j]);
459484
}
460485
}

test/image/strict-d3.js

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ function checkAttrVal(sel, key, val) {
5858
if(/--/.test(val) && isNumeric(val.split('--')[1].charAt(0))) {
5959
throw new Error('d3 selection.attr called with value ' + val + ' which includes a double negative');
6060
}
61+
62+
if(/transform/.test(key) && /NaN/.test(val)) {
63+
throw new Error('d3 selection.attr called with ' + key + ' ' + val + ' containing a NaN');
64+
}
6165
}
6266

6367
function checkStyleVal(sel, key, val) {

test/jasmine/tests/annotations_test.js

+22
Original file line numberDiff line numberDiff line change
@@ -814,6 +814,28 @@ describe('annotations autorange', function() {
814814
.catch(failTest)
815815
.then(done);
816816
});
817+
818+
it('should not error out on subplots w/o visible traces', function(done) {
819+
Plotly.plot(gd, [{}], {
820+
annotations: [{
821+
x: 0.1,
822+
y: 0.1,
823+
text: 't',
824+
showarrow: false
825+
}, {
826+
x: 0.2,
827+
y: 0.3,
828+
text: 'a'
829+
}]
830+
})
831+
.then(function() {
832+
expect(gd._fullLayout.xaxis.range).toBeCloseToArray([0.099, 0.201], 1, 'x rng');
833+
expect(gd._fullLayout.yaxis.range).toBeCloseToArray([0.091, 0.335], 1, 'y rng');
834+
assertVisible([0, 1]);
835+
})
836+
.catch(failTest)
837+
.then(done);
838+
});
817839
});
818840

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

0 commit comments

Comments
 (0)