Skip to content

Commit e70e521

Browse files
committed
fix subtitle position after editing when title is MathJax
1 parent e3813f2 commit e70e521

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/components/titles/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var interactConstants = require('../../constants/interactions');
1414

1515
var OPPOSITE_SIDE = require('../../constants/alignment').OPPOSITE_SIDE;
1616
var numStripRE = / [XY][0-9]* /;
17+
var MATHJAX_PADDING_BOTTOM = 10;
1718

1819
/**
1920
* Titles - (re)draw titles on the axes and plot:
@@ -200,13 +201,16 @@ function draw(gd, titleClass, options) {
200201
// Mathjax is rendered asynchronously, which is why this step needs to be
201202
// passed as a callback
202203
function adjustSubtitlePosition(titleElMathGroup) {
203-
if(titleElMathGroup && subtitleEl) {
204+
if(!titleElMathGroup) return;
205+
206+
var subtitleElement = d3.select(titleElMathGroup.node().parentNode).select('.' + subtitleClass);
207+
if(subtitleElement) {
204208
var titleMathHeight = titleElMathGroup.node().getBBox().height;
205209
if(titleMathHeight) {
206210
// Increase the y position of the subtitle by the height of the title,
207-
// plus a (somewhat arbitrary) 10px of padding
208-
var newSubtitleY = Number(subtitleEl.attr('y')) + titleMathHeight + 10;
209-
subtitleEl.attr('y', newSubtitleY);
211+
// plus a bit of padding
212+
var newSubtitleY = Number(subtitleElement.attr('y')) + titleMathHeight + MATHJAX_PADDING_BOTTOM;
213+
subtitleElement.attr('y', newSubtitleY);
210214
}
211215
}
212216
}
@@ -227,8 +231,15 @@ function draw(gd, titleClass, options) {
227231
.call(svgTextUtils.convertToTspans, gd, adjustSubtitlePosition);
228232

229233
if(subtitleEl) {
234+
// Increase the subtitle y position so that it is drawn below the subtitle
235+
// We need to check the height of the MathJax group as well, in case the MathJax
236+
// has already rendered
237+
var titleElHeight = titleEl.node().getBBox().height;
238+
var titleElMathGroup = group.select('.' + titleClass + '-math-group');
239+
var titleElMathHeight = titleElMathGroup.node() ? titleElMathGroup.node().getBBox().height : 0;
240+
var subtitleShift = titleElMathHeight ? titleElMathHeight + MATHJAX_PADDING_BOTTOM : titleElHeight;
230241
var subtitleAttributes = Object.assign({}, attributes);
231-
subtitleAttributes.y += Drawing.bBox(titleEl.node()).height;
242+
subtitleAttributes.y += subtitleShift;
232243

233244
subtitleEl.attr('transform', transformVal);
234245
subtitleEl.style('opacity', subtitleOpacity * Color.opacity(subFontColor))

0 commit comments

Comments
 (0)