Skip to content

Refactor title and subtitle selection in drawMainTitle function to use scoped selector #7522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions draftlogs/7522_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +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)]
6 changes: 3 additions & 3 deletions src/plot_api/subroutines.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,8 @@ exports.drawMainTitle = function(gd) {
});

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

// If there is a subtitle
var subtitleObj = d3.selectAll('.gtitle-subtitle');
var subtitleObj = d3.select(gd).selectAll('.gtitle-subtitle');
if(subtitleObj.node()) {
// Get bottom edge of title bounding box
var titleBB = titleObj.node().getBBox();
Expand Down
4 changes: 2 additions & 2 deletions test/jasmine/assets/create_graph_div.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

module.exports = function createGraphDiv() {
module.exports = function createGraphDiv(divId = 'graph') {
var gd = document.createElement('div');
gd.id = 'graph';
gd.id = divId;
document.body.appendChild(gd);

// force the graph to be at position 0,0 no matter what
Expand Down
45 changes: 45 additions & 0 deletions test/jasmine/tests/titles_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,51 @@ describe('Title automargining', function() {
expect(gd._fullLayout._size.h).toBeCloseTo(243, -1);
}).then(done, done.fail);
});

it('computes title automargins independently when multiple plots exist', function(done) {
var gd1 = gd;
var gd2 = createGraphDiv('title-automargining-2');

var dataLocal = [{x: [1, 2], y: [1, 2]}];

var layout1 = {
margin: {t: 0, b: 0, l: 0, r: 0},
height: 300,
width: 400,
title: {
text: 'Large title for plot 1',
font: {size: 36},
yref: 'paper',
automargin: true
}
};

var layout2 = {
margin: {t: 0, b: 0, l: 0, r: 0},
height: 300,
width: 400,
title: {
text: 'Small',
font: {size: 12},
yref: 'paper',
automargin: true
}
};

Plotly.newPlot(gd1, dataLocal, layout1)
.then(function() { return Plotly.newPlot(gd2, dataLocal, layout2); })
.then(function() {
// Each graph should compute its own top automargin from its own title bbox
var t1 = gd1._fullLayout._size.t;
var t2 = gd2._fullLayout._size.t;

expect(t1).toBeGreaterThan(t2);
}).then(function() {
var el = document.getElementById('title-automargining-2');
if(el) document.body.removeChild(el);
})
.then(done, done.fail);
});
});

function expectTitle(expTitle) {
Expand Down