Skip to content

Add axis title standoff #4279

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

Merged
merged 5 commits into from
Oct 23, 2019
Merged
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
38 changes: 18 additions & 20 deletions src/components/titles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ var Color = require('../color');
var svgTextUtils = require('../../lib/svg_text_utils');
var interactConstants = require('../../constants/interactions');

module.exports = {
draw: draw
};

var OPPOSITE_SIDE = require('../../constants/alignment').OPPOSITE_SIDE;
var numStripRE = / [XY][0-9]* /;

/**
Expand Down Expand Up @@ -167,29 +164,26 @@ function draw(gd, titleClass, options) {

// move toward avoid.side (= left, right, top, bottom) if needed
// can include pad (pixels, default 2)
var shift = 0;
var backside = {
left: 'right',
right: 'left',
top: 'bottom',
bottom: 'top'
}[avoid.side];
var shiftSign = (['left', 'top'].indexOf(avoid.side) !== -1) ?
-1 : 1;
var backside = OPPOSITE_SIDE[avoid.side];
var shiftSign = (avoid.side === 'left' || avoid.side === 'top') ? -1 : 1;
var pad = isNumeric(avoid.pad) ? avoid.pad : 2;

var titlebb = Drawing.bBox(titleGroup.node());
var paperbb = {
left: 0,
top: 0,
right: fullLayout.width,
bottom: fullLayout.height
};
var maxshift = avoid.maxShift || (
(paperbb[avoid.side] - titlebb[avoid.side]) *
((avoid.side === 'left' || avoid.side === 'top') ? -1 : 1));

var maxshift = avoid.maxShift ||
shiftSign * (paperbb[avoid.side] - titlebb[avoid.side]);
var shift = 0;

// Prevent the title going off the paper
if(maxshift < 0) shift = maxshift;
else {
if(maxshift < 0) {
shift = maxshift;
} else {
// so we don't have to offset each avoided element,
// give the title the opposite offset
var offsetLeft = avoid.offsetLeft || 0;
Expand All @@ -211,15 +205,15 @@ function draw(gd, titleClass, options) {
});
shift = Math.min(maxshift, shift);
}

if(shift > 0 || maxshift < 0) {
var shiftTemplate = {
left: [-shift, 0],
right: [shift, 0],
top: [0, -shift],
bottom: [0, shift]
}[avoid.side];
titleGroup.attr('transform',
'translate(' + shiftTemplate + ')');
titleGroup.attr('transform', 'translate(' + shiftTemplate + ')');
}
}
}
Expand Down Expand Up @@ -265,3 +259,7 @@ function draw(gd, titleClass, options) {

return group;
}

module.exports = {
draw: draw
};
113 changes: 83 additions & 30 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ var ONESEC = constants.ONESEC;
var MINUS_SIGN = constants.MINUS_SIGN;
var BADNUM = constants.BADNUM;

var MID_SHIFT = require('../../constants/alignment').MID_SHIFT;
var LINE_SPACING = require('../../constants/alignment').LINE_SPACING;
var OPPOSITE_SIDE = require('../../constants/alignment').OPPOSITE_SIDE;
var alignmentConstants = require('../../constants/alignment');
var MID_SHIFT = alignmentConstants.MID_SHIFT;
var CAP_SHIFT = alignmentConstants.CAP_SHIFT;
var LINE_SPACING = alignmentConstants.LINE_SPACING;
var OPPOSITE_SIDE = alignmentConstants.OPPOSITE_SIDE;

var axes = module.exports = {};

Expand Down Expand Up @@ -1831,7 +1833,6 @@ axes.drawOne = function(gd, ax, opts) {

if(ax.type === 'multicategory') {
var pad = {x: 2, y: 10}[axLetter];
var sgn = {l: -1, t: -1, r: 1, b: 1}[ax.side.charAt(0)];

seq.push(function() {
var bboxKey = {x: 'height', y: 'width'}[axLetter];
Expand All @@ -1845,20 +1846,24 @@ axes.drawOne = function(gd, ax, opts) {
repositionOnUpdate: true,
secondary: true,
transFn: transFn,
labelFns: axes.makeLabelFns(ax, mainLinePosition + standoff * sgn)
labelFns: axes.makeLabelFns(ax, mainLinePosition + standoff * tickSigns[4])
});
});

seq.push(function() {
ax._depth = sgn * (getLabelLevelBbox('tick2')[ax.side] - mainLinePosition);
ax._depth = tickSigns[4] * (getLabelLevelBbox('tick2')[ax.side] - mainLinePosition);

return drawDividers(gd, ax, {
vals: dividerVals,
layer: mainAxLayer,
path: axes.makeTickPath(ax, mainLinePosition, sgn, ax._depth),
path: axes.makeTickPath(ax, mainLinePosition, tickSigns[4], ax._depth),
transFn: transFn
});
});
} else if(ax.title.hasOwnProperty('standoff')) {
seq.push(function() {
ax._depth = tickSigns[4] * (getLabelLevelBbox()[ax.side] - mainLinePosition);
});
}

var hasRangeSlider = Registry.getComponentMethod('rangeslider', 'isVisible')(ax);
Expand Down Expand Up @@ -1936,10 +1941,7 @@ axes.drawOne = function(gd, ax, opts) {
ax._anchorAxis.domain[domainIndices[0]];

if(ax.title.text !== fullLayout._dfltTitle[axLetter]) {
var extraLines = (ax.title.text.match(svgTextUtils.BR_TAG_ALL) || []).length;
push[s] += extraLines ?
ax.title.font.size * (extraLines + 1) * LINE_SPACING :
ax.title.font.size;
push[s] += approxTitleDepth(ax) + (ax.title.standoff || 0);
}

if(ax.mirror && ax.anchor !== 'free') {
Expand Down Expand Up @@ -2097,6 +2099,7 @@ function calcLabelLevelBbox(ax, cls) {
* - [1]: sign for bottom/left ticks (i.e. positive SVG direction)
* - [2]: sign for ticks corresponding to 'ax.side'
* - [3]: sign for ticks mirroring 'ax.side'
* - [4]: sign of arrow starting at axis pointing towards margin
*/
axes.getTickSigns = function(ax) {
var axLetter = ax._id.charAt(0);
Expand All @@ -2107,6 +2110,10 @@ axes.getTickSigns = function(ax) {
if((ax.ticks !== 'inside') === (axLetter === 'x')) {
out = out.map(function(v) { return -v; });
}
// independent of `ticks`; do not flip this one
if(ax.side) {
out.push({l: -1, t: -1, r: 1, b: 1}[ax.side.charAt(0)]);
}
return out;
};

Expand Down Expand Up @@ -2699,42 +2706,84 @@ axes.getPxPosition = function(gd, ax) {
}
};

/**
* Approximate axis title depth (w/o computing its bounding box)
*
* @param {object} ax (full) axis object
* - {string} title.text
* - {number} title.font.size
* - {number} title.standoff
* @return {number} (in px)
*/
function approxTitleDepth(ax) {
var fontSize = ax.title.font.size;
var extraLines = (ax.title.text.match(svgTextUtils.BR_TAG_ALL) || []).length;
if(ax.title.hasOwnProperty('standoff')) {
return extraLines ?
fontSize * (CAP_SHIFT + (extraLines * LINE_SPACING)) :
fontSize * CAP_SHIFT;
} else {
return extraLines ?
fontSize * (extraLines + 1) * LINE_SPACING :
fontSize;
}
}

/**
* Draw axis title, compute default standoff if necessary
*
* @param {DOM element} gd
* @param {object} ax (full) axis object
* - {string} _id
* - {string} _name
* - {string} side
* - {number} title.font.size
* - {object} _selections
*
* - {number} _depth
* - {number} title.standoff
* OR
* - {number} linewidth
* - {boolean} showticklabels
*/
function drawTitle(gd, ax) {
var fullLayout = gd._fullLayout;
var axId = ax._id;
var axLetter = axId.charAt(0);
var fontSize = ax.title.font.size;

var titleStandoff;
if(ax.type === 'multicategory') {
titleStandoff = ax._depth;

if(ax.title.hasOwnProperty('standoff')) {
titleStandoff = ax._depth + ax.title.standoff + approxTitleDepth(ax);
} else {
var offsetBase = 1.5;
titleStandoff = 10 + fontSize * offsetBase + (ax.linewidth ? ax.linewidth - 1 : 0);
if(ax.type === 'multicategory') {
titleStandoff = ax._depth;
} else {
var offsetBase = 1.5;
titleStandoff = 10 + fontSize * offsetBase + (ax.linewidth ? ax.linewidth - 1 : 0);
}

if(axLetter === 'x') {
titleStandoff += ax.side === 'top' ?
fontSize * (ax.showticklabels ? 1 : 0) :
fontSize * (ax.showticklabels ? 1.5 : 0.5);
} else {
titleStandoff += ax.side === 'right' ?
fontSize * (ax.showticklabels ? 1 : 0.5) :
fontSize * (ax.showticklabels ? 0.5 : 0);
}
}

var pos = axes.getPxPosition(gd, ax);
var transform, x, y;

if(axLetter === 'x') {
x = ax._offset + ax._length / 2;

if(ax.side === 'top') {
y = -titleStandoff - fontSize * (ax.showticklabels ? 1 : 0);
} else {
y = titleStandoff + fontSize * (ax.showticklabels ? 1.5 : 0.5);
}
y += pos;
y = (ax.side === 'top') ? pos - titleStandoff : pos + titleStandoff;
} else {
y = ax._offset + ax._length / 2;

if(ax.side === 'right') {
x = titleStandoff + fontSize * (ax.showticklabels ? 1 : 0.5);
} else {
x = -titleStandoff - fontSize * (ax.showticklabels ? 0.5 : 0);
}
x += pos;

x = (ax.side === 'right') ? pos + titleStandoff : pos - titleStandoff;
transform = {rotate: '-90', offset: 0};
}

Expand All @@ -2753,6 +2802,10 @@ function drawTitle(gd, ax) {
avoid.offsetLeft = translation.x;
avoid.offsetTop = translation.y;
}

if(ax.title.hasOwnProperty('standoff')) {
avoid.pad = 0;
}
}

return Titles.draw(gd, axId + 'title', {
Expand Down
15 changes: 15 additions & 0 deletions src/plots/cartesian/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ module.exports = {
'by the now deprecated `titlefont` attribute.'
].join(' ')
}),
standoff: {
valType: 'number',
role: 'info',
min: 0,
editType: 'ticks',
description: [
'Sets the standoff distance (in px) between the axis labels and the title text',
'The default value is a function of the axis tick labels, the title `font.size`',
'and the axis `linewidth`.',
'Note that the axis title position is always constrained within the margins,',
'so the actual standoff distance is always less than the set or default value.',
'By setting `standoff` and turning on `automargin`, plotly.js will push the',
'margins to fit the axis title at given standoff distance.'
].join(' ')
},
editType: 'ticks'
},
type: {
Expand Down
2 changes: 2 additions & 0 deletions src/plots/cartesian/layout_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, fullData) {
grid: layoutOut.grid
});

coerce('title.standoff');

axLayoutOut._input = axLayoutIn;
}

Expand Down
5 changes: 4 additions & 1 deletion src/plots/gl3d/layout/axis_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ module.exports = overrideAll({
color: axesAttrs.color,
categoryorder: axesAttrs.categoryorder,
categoryarray: axesAttrs.categoryarray,
title: axesAttrs.title,
title: {
text: axesAttrs.title.text,
font: axesAttrs.title.font
},
type: extendFlat({}, axesAttrs.type, {
values: ['-', 'linear', 'log', 'date', 'category']
}),
Expand Down
17 changes: 12 additions & 5 deletions src/plots/polar/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,18 @@ var radialAxisAttrs = {
},


title: overrideAll(axesAttrs.title, 'plot', 'from-root'),
// might need a 'titleside' and even 'titledirection' down the road
title: {
// radial title is not gui-editable at the moment,
// so it needs dflt: '', similar to carpet axes.
text: extendFlat({}, axesAttrs.title.text, {editType: 'plot', dflt: ''}),
font: extendFlat({}, axesAttrs.title.font, {editType: 'plot'}),

// TODO
// - might need a 'titleside' and even 'titledirection' down the road
// - what about standoff ??

editType: 'plot'
},

hoverformat: axesAttrs.hoverformat,

Expand All @@ -138,9 +148,6 @@ var radialAxisAttrs = {
}
};

// radial title is not gui-editable, so it needs dflt: '', similar to carpet axes.
radialAxisAttrs.title.text.dflt = '';

extendFlat(
radialAxisAttrs,

Expand Down
6 changes: 5 additions & 1 deletion src/plots/ternary/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ var overrideAll = require('../../plot_api/edit_types').overrideAll;
var extendFlat = require('../../lib/extend').extendFlat;

var ternaryAxesAttrs = {
title: axesAttrs.title,
title: {
text: axesAttrs.title.text,
font: axesAttrs.title.font
// TODO does standoff here make sense?
},
color: axesAttrs.color,
// ticks
tickmode: axesAttrs.tickmode,
Expand Down
1 change: 1 addition & 0 deletions src/traces/carpet/axis_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module.exports = {
'by the now deprecated `titlefont` attribute.'
].join(' ')
}),
// TODO how is this different than `title.standoff`
offset: {
valType: 'number',
role: 'info',
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/image/baselines/axis-title-standoff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading