Skip to content

Commit 7f89c73

Browse files
archmojhannahker
authored andcommitted
move smart default for title.y and title.yanchor to supply defaults
1 parent 0c0e47e commit 7f89c73

File tree

2 files changed

+34
-45
lines changed

2 files changed

+34
-45
lines changed

src/plot_api/subroutines.js

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var d3 = require('@plotly/d3');
44
var Registry = require('../registry');
55
var Plots = require('../plots/plots');
6-
var Loggers = require('../lib/loggers');
76

87
var Lib = require('../lib');
98
var svgTextUtils = require('../lib/svg_text_utils');
@@ -406,7 +405,6 @@ exports.drawMainTitle = function(gd) {
406405
var y = getMainTitleY(fullLayout, dy);
407406
var x = getMainTitleX(fullLayout, textAnchor);
408407

409-
// Draw title without positioning to get size
410408
Titles.draw(gd, 'gtitle', {
411409
propContainer: fullLayout,
412410
propName: 'title.text',
@@ -424,13 +422,9 @@ exports.drawMainTitle = function(gd) {
424422
var titleHeight = Drawing.bBox(titleObj.node()).height;
425423
var pushMargin = needsMarginPush(gd, title, titleHeight);
426424
if(pushMargin > 0) {
427-
setDflts(title, getDflts(title)[0], getDflts(title)[1]);
428-
// Recalculate these since the defaults have changed
429-
dy = getMainTitleDy(fullLayout);
430-
y = getMainTitleY(fullLayout, dy);
431425
applyTitleAutoMargin(gd, y, pushMargin, titleHeight);
432426

433-
// Position the title once we know where it needs to be
427+
// Re-position the title once we know where it needs to be
434428
titleObj.attr({
435429
x: x,
436430
y: y,
@@ -453,36 +447,6 @@ function isOutsideContainer(gd, title, position, y, titleHeight) {
453447
}
454448
}
455449

456-
457-
// title.y is 1 or 0 if automargin and paper ref
458-
// 'auto' is not supported for either title.y or title.yanchor when automargin=true
459-
function getDflts(title) {
460-
var titleY = title.y;
461-
var titleYanchor = title.yanchor;
462-
if(title.automargin && title.yref === 'paper') {
463-
titleY = title.y === 0 ? 0 : 1;
464-
if(title.yanchor === 'auto') {
465-
titleYanchor = title.y === 0 ? 'top' : 'bottom';
466-
}
467-
}
468-
if(title.automargin && title.yref === 'container') {
469-
if(title.y === 'auto') titleY = 1;
470-
if(title.yanchor === 'auto') {
471-
titleYanchor = title.y < 0.5 ? 'bottom' : 'top';
472-
}
473-
}
474-
return [titleY, titleYanchor];
475-
}
476-
477-
function setDflts(title, titleY, titleYanchor) {
478-
if(title.yref === 'paper' && title.y !== 0 && title.y !== 1 && title.y !== 'auto') {
479-
Loggers.warn('title.automargin=true so resetting the supplied title.y value to 1.');
480-
}
481-
title.y = titleY;
482-
title.yanchor = titleYanchor;
483-
}
484-
485-
486450
function containerPushVal(position, titleY, titleYanchor, height, titleDepth) {
487451
var push = 0;
488452
if(titleYanchor === 'middle') {
@@ -503,8 +467,8 @@ function containerPushVal(position, titleY, titleYanchor, height, titleDepth) {
503467
}
504468

505469
function needsMarginPush(gd, title, titleHeight) {
506-
var titleY = getDflts(title)[0];
507-
var titleYanchor = getDflts(title)[1];
470+
var titleY = title.y;
471+
var titleYanchor = title.yanchor;
508472
var position = titleY > 0.5 ? 't' : 'b';
509473
var curMargin = gd._fullLayout.margin[position];
510474
var pushMargin = 0;

src/plots/plots.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,16 +1480,41 @@ plots.supplyLayoutGlobalDefaults = function(layoutIn, layoutOut, formatObj) {
14801480

14811481
coerce('title.text', layoutOut._dfltTitle.plot);
14821482
coerce('title.xref');
1483-
coerce('title.yref');
1484-
coerce('title.x');
1485-
coerce('title.y');
1486-
coerce('title.xanchor');
1487-
coerce('title.yanchor');
1483+
var titleYref = coerce('title.yref');
14881484
coerce('title.pad.t');
14891485
coerce('title.pad.r');
14901486
coerce('title.pad.b');
14911487
coerce('title.pad.l');
1492-
coerce('title.automargin');
1488+
var titleAutomargin = coerce('title.automargin');
1489+
1490+
coerce('title.x');
1491+
coerce('title.xanchor');
1492+
coerce('title.y');
1493+
coerce('title.yanchor');
1494+
1495+
if(titleAutomargin) {
1496+
// when automargin=true
1497+
// title.y is 1 or 0 if paper ref
1498+
// 'auto' is not supported for either title.y or title.yanchor
1499+
1500+
// TODO: mention this smart default in the title.y and title.yanchor descriptions
1501+
1502+
if(titleYref === 'paper') {
1503+
if(layoutOut.title.y !== 0) layoutOut.title.y = 1;
1504+
1505+
if(layoutOut.title.yanchor === 'auto') {
1506+
layoutOut.title.yanchor = layoutOut.title.y === 0 ? 'top' : 'bottom';
1507+
}
1508+
}
1509+
1510+
if(titleYref === 'container') {
1511+
if(layoutOut.title.y === 'auto') layoutOut.title.y = 1;
1512+
1513+
if(layoutOut.title.yanchor === 'auto') {
1514+
layoutOut.title.yanchor = layoutOut.title.y < 0.5 ? 'bottom' : 'top';
1515+
}
1516+
}
1517+
}
14931518

14941519
var uniformtextMode = coerce('uniformtext.mode');
14951520
if(uniformtextMode) {

0 commit comments

Comments
 (0)