Skip to content

Commit ec166a3

Browse files
committed
better method of determining subtitle position
1 parent b7cda0b commit ec166a3

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/components/titles/index.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ 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_MULTIPLIER = 0.85;
18-
var EXTRA_SPACING_BETWEEN_TITLE_AND_SUBTITLE = 0;
17+
var SUBTITLE_PADDING_MATHJAX_EM = 1.6;
18+
var SUBTITLE_PADDING_EM = 1.6;
1919

2020
/**
2121
* Titles - (re)draw titles on the axes and plot:
@@ -207,12 +207,11 @@ function draw(gd, titleClass, options) {
207207

208208
var subtitleElement = d3.select(titleElMathGroup.node().parentNode).select('.' + subtitleClass);
209209
if(!subtitleElement.empty()) {
210-
var titleMathHeight = titleElMathGroup.node().getBBox().height;
211-
if(titleMathHeight) {
212-
// Increase the y position of the subtitle by the height of the title,
213-
// plus a bit of padding
214-
var newSubtitleY = Number(subtitleElement.attr('y')) + titleMathHeight + MATHJAX_PADDING_MULTIPLIER * subFontSize + EXTRA_SPACING_BETWEEN_TITLE_AND_SUBTITLE;
215-
subtitleElement.attr('y', newSubtitleY);
210+
var titleElMathBbox = titleElMathGroup.node().getBBox();
211+
if(titleElMathBbox.height) {
212+
// Position subtitle based on bottom of Mathjax title
213+
var subtitleY = titleElMathBbox.y + titleElMathBbox.height + (SUBTITLE_PADDING_MATHJAX_EM * subFontSize);
214+
subtitleElement.attr('y', subtitleY);
216215
}
217216
}
218217
}
@@ -233,15 +232,16 @@ function draw(gd, titleClass, options) {
233232
.call(svgTextUtils.convertToTspans, gd, adjustSubtitlePosition);
234233

235234
if(subtitleEl) {
236-
// Increase the subtitle y position so that it is drawn below the subtitle
237-
// We need to check the height of the MathJax group as well, in case the MathJax
235+
// Set subtitle y position based on bottom of title
236+
// We need to check the Mathjax group as well, in case the Mathjax
238237
// has already rendered
239-
var titleElHeight = titleEl.node().getBBox().height;
240238
var titleElMathGroup = group.select('.' + titleClass + '-math-group');
241-
var titleElMathHeight = titleElMathGroup.node() ? titleElMathGroup.node().getBBox().height : 0;
242-
var subtitleShift = titleElMathHeight ? titleElMathHeight + (MATHJAX_PADDING_MULTIPLIER * subFontSize) : titleElHeight;
239+
var titleElBbox = titleEl.node().getBBox();
240+
var titleElMathBbox = titleElMathGroup.node() ? titleElMathGroup.node().getBBox() : undefined;
241+
var subtitleY = titleElMathBbox ? titleElMathBbox.y + titleElMathBbox.height + (SUBTITLE_PADDING_MATHJAX_EM * subFontSize) : titleElBbox.y + titleElBbox.height + (SUBTITLE_PADDING_EM * subFontSize);
242+
243243
var subtitleAttributes = Lib.extendFlat({}, attributes, {
244-
y: subtitleShift + EXTRA_SPACING_BETWEEN_TITLE_AND_SUBTITLE + attributes.y
244+
y: subtitleY
245245
});
246246

247247
subtitleEl.attr('transform', transformVal);
@@ -380,9 +380,9 @@ function draw(gd, titleClass, options) {
380380
// Adjust subtitle position now that title placeholder has been added
381381
// Only adjust if subtitle is enabled and title text was originally empty
382382
if(subtitleEnabled && !txt) {
383-
var ht = Drawing.bBox(el.node()).height;
384-
var newSubtitleY = Number(subtitleEl.attr('y')) + ht + EXTRA_SPACING_BETWEEN_TITLE_AND_SUBTITLE;
385-
subtitleEl.attr('y', newSubtitleY);
383+
var titleElBbox = Drawing.bBox(el.node());
384+
var subtitleY = titleElBbox.y + titleElBbox.height + (SUBTITLE_PADDING_EM * subFontSize);
385+
subtitleEl.attr('y', subtitleY);
386386
}
387387

388388
if(!subtitleTxt) {

0 commit comments

Comments
 (0)