Skip to content

Commit 3b11f4f

Browse files
authored
Merge pull request #7522 from davibarbosa2/fix-global-title-calcs
Refactor title and subtitle selection in drawMainTitle function to use scoped selector
2 parents 607ac29 + e8434c8 commit 3b11f4f

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

draftlogs/7522_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Refactor `drawMainTitle` to use context-specific selections for title and subtitle, avoiding conflicts when multiple plots are present on the same page [[#7522](https://github.com/plotly/plotly.js/pull/7522)]

src/plot_api/subroutines.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ exports.drawMainTitle = function(gd) {
428428
});
429429

430430
if(title.text && title.automargin) {
431-
var titleObj = d3.selectAll('.gtitle');
432-
var titleHeight = Drawing.bBox(d3.selectAll('.g-gtitle').node()).height;
431+
var titleObj = d3.select(gd).selectAll('.gtitle');
432+
var titleHeight = Drawing.bBox(d3.select(gd).selectAll('.g-gtitle').node()).height;
433433
var pushMargin = needsMarginPush(gd, title, titleHeight);
434434
if(pushMargin > 0) {
435435
applyTitleAutoMargin(gd, y, pushMargin, titleHeight);
@@ -455,7 +455,7 @@ exports.drawMainTitle = function(gd) {
455455
}
456456

457457
// If there is a subtitle
458-
var subtitleObj = d3.selectAll('.gtitle-subtitle');
458+
var subtitleObj = d3.select(gd).selectAll('.gtitle-subtitle');
459459
if(subtitleObj.node()) {
460460
// Get bottom edge of title bounding box
461461
var titleBB = titleObj.node().getBBox();

test/jasmine/assets/create_graph_div.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
module.exports = function createGraphDiv() {
3+
module.exports = function createGraphDiv(divId = 'graph') {
44
var gd = document.createElement('div');
5-
gd.id = 'graph';
5+
gd.id = divId;
66
document.body.appendChild(gd);
77

88
// force the graph to be at position 0,0 no matter what

test/jasmine/tests/titles_test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,51 @@ describe('Title automargining', function() {
980980
expect(gd._fullLayout._size.h).toBeCloseTo(243, -1);
981981
}).then(done, done.fail);
982982
});
983+
984+
it('computes title automargins independently when multiple plots exist', function(done) {
985+
var gd1 = gd;
986+
var gd2 = createGraphDiv('title-automargining-2');
987+
988+
var dataLocal = [{x: [1, 2], y: [1, 2]}];
989+
990+
var layout1 = {
991+
margin: {t: 0, b: 0, l: 0, r: 0},
992+
height: 300,
993+
width: 400,
994+
title: {
995+
text: 'Large title for plot 1',
996+
font: {size: 36},
997+
yref: 'paper',
998+
automargin: true
999+
}
1000+
};
1001+
1002+
var layout2 = {
1003+
margin: {t: 0, b: 0, l: 0, r: 0},
1004+
height: 300,
1005+
width: 400,
1006+
title: {
1007+
text: 'Small',
1008+
font: {size: 12},
1009+
yref: 'paper',
1010+
automargin: true
1011+
}
1012+
};
1013+
1014+
Plotly.newPlot(gd1, dataLocal, layout1)
1015+
.then(function() { return Plotly.newPlot(gd2, dataLocal, layout2); })
1016+
.then(function() {
1017+
// Each graph should compute its own top automargin from its own title bbox
1018+
var t1 = gd1._fullLayout._size.t;
1019+
var t2 = gd2._fullLayout._size.t;
1020+
1021+
expect(t1).toBeGreaterThan(t2);
1022+
}).then(function() {
1023+
var el = document.getElementById('title-automargining-2');
1024+
if(el) document.body.removeChild(el);
1025+
})
1026+
.then(done, done.fail);
1027+
});
9831028
});
9841029

9851030
function expectTitle(expTitle) {

0 commit comments

Comments
 (0)