Skip to content

Commit

Permalink
add editType to clear automargins from pushmargins
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaskruchten committed Mar 2, 2018
1 parent 20bdefa commit 1df34bd
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 63 deletions.
3 changes: 2 additions & 1 deletion src/plot_api/edit_types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var layoutOpts = {
valType: 'flaglist',
extras: ['none'],
flags: [
'calc', 'calcIfAutorange', 'plot', 'legend', 'ticks',
'calc', 'calcIfAutorange', 'plot', 'legend', 'ticks', 'margins',
'layoutstyle', 'modebar', 'camera', 'arraydraw'
],
description: [
Expand All @@ -47,6 +47,7 @@ var layoutOpts = {
'*plot* calls `Plotly.plot` but without first clearing `gd.calcdata`.',
'*legend* only redraws the legend.',
'*ticks* only redraws axis ticks, labels, and gridlines.',
'*margins* recomputes ticklabel automargins.',
'*layoutstyle* reapplies global and SVG cartesian axis styles.',
'*modebar* just updates the modebar.',
'*camera* just updates the camera settings for gl3d scenes.',
Expand Down
9 changes: 9 additions & 0 deletions src/plot_api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,12 @@ exports.clearAxisTypes = function(gd, traces, layoutUpdate) {
}
}
};

exports.clearAxisAutomargins = function(gd) {
var keys = Object.keys(gd._fullLayout._pushmargin);
for(var i = 0; i < keys.length; i++) {
if(keys[i].indexOf('automargin') !== -1) {
delete gd._fullLayout._pushmargin[keys[i]];
}
}
};
3 changes: 3 additions & 0 deletions src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,7 @@ Plotly.relayout = function relayout(gd, astr, val) {

// clear calcdata if required
if(flags.calc) gd.calcdata = undefined;
if(flags.margins) helpers.clearAxisAutomargins(gd);

// fill in redraw sequence

Expand Down Expand Up @@ -2182,6 +2183,7 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, _traces) {
// clear calcdata and/or axis types if required
if(restyleFlags.clearCalc || relayoutFlags.calc) gd.calcdata = undefined;
if(restyleFlags.clearAxisTypes) helpers.clearAxisTypes(gd, traces, layoutUpdate);
if(relayoutFlags.margins) helpers.clearAxisAutomargins(gd);

// fill in redraw sequence
var seq = [];
Expand Down Expand Up @@ -2309,6 +2311,7 @@ Plotly.react = function(gd, data, layout, config) {

// clear calcdata if required
if(restyleFlags.calc || relayoutFlags.calc) gd.calcdata = undefined;
if(relayoutFlags.margins) helpers.clearAxisAutomargins(gd);

// Note: what restyle/relayout use impliedEdits and clearAxisTypes for
// must be handled by the user when using Plotly.react.
Expand Down
45 changes: 19 additions & 26 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2219,38 +2219,31 @@ axes.doTicks = function(gd, axid, skipTitle) {
}

function doAutoMargins() {
if(!ax.automargin) { return; }
if(axLetter !== 'x' && axLetter !== 'y') { return; }
var pushKey = ax._name + '.automargin';
var sideLetter = ax.side[0];
var existingPush = fullLayout._pushmargin[pushKey];
var pushParams = {x: 0, y: 0, r: 0, l: 0, t: 0, b: 0};

if(!ax.automargin || ax.anchor === 'free' || !ax._anchorAxis) {
if(existingPush && !(
existingPush.r.size === 0 && existingPush.l.size === 0 &&
existingPush.b.size === 0 && existingPush.t.size === 0)) {
Plots.autoMargin(gd, pushKey, pushParams);
}
return;
}

var axisDim;
var s = ax.side[0];
var push = {x: 0, y: 0, r: 0, l: 0, t: 0, b: 0};

if(axLetter === 'x') {
pushParams.y = ax._anchorAxis.domain[sideLetter === 't' ? 1 : 0];
axisDim = ax._boundingBox.height;
push.y = (ax.anchor === 'free' ? ax.position :
ax._anchorAxis.domain[s === 't' ? 1 : 0]);
push[s] += ax._boundingBox.height;
}
else {
pushParams.x = ax._anchorAxis.domain[sideLetter === 'r' ? 1 : 0];
axisDim = ax._boundingBox.width;
push.x = (ax.anchor === 'free' ? ax.position :
ax._anchorAxis.domain[s === 'r' ? 1 : 0]);
push[s] += ax._boundingBox.width;
}
var axisTitleDim = (ax.title !== fullLayout._dfltTitle[axLetter] ?
ax.titlefont.size : 0);
var marginPush = axisTitleDim + axisDim;

if(!fullLayout._replotting ||
!existingPush || existingPush[sideLetter].size < marginPush) {
pushParams[sideLetter] = marginPush;
Plots.autoMargin(gd, pushKey, pushParams);

if(ax.title !== fullLayout._dfltTitle[axLetter]) {
push[s] += ax.titlefont.size;
}

var pushKey = ax._name + '.automargin';
var prevPush = fullLayout._pushmargin[pushKey];
if(!prevPush || prevPush[s].size < push[s]) {
Plots.autoMargin(gd, pushKey, push);
}
}

Expand Down
72 changes: 36 additions & 36 deletions src/plots/cartesian/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ module.exports = {
title: {
valType: 'string',
role: 'info',
editType: 'ticks',
editType: 'ticks+margins',
description: 'Sets the title of this axis.'
},
titlefont: fontAttrs({
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Sets this axis\' title font.'
].join(' ')
Expand Down Expand Up @@ -100,10 +100,10 @@ module.exports = {
valType: 'info_array',
role: 'info',
items: [
{valType: 'any', editType: 'plot', impliedEdits: {'^autorange': false}},
{valType: 'any', editType: 'plot', impliedEdits: {'^autorange': false}}
{valType: 'any', editType: 'plot+margins', impliedEdits: {'^autorange': false}},
{valType: 'any', editType: 'plot+margins', impliedEdits: {'^autorange': false}}
],
editType: 'plot',
editType: 'plot+margins',
impliedEdits: {'autorange': false},
description: [
'Sets the range of this axis.',
Expand Down Expand Up @@ -198,7 +198,7 @@ module.exports = {
valType: 'enumerated',
values: ['auto', 'linear', 'array'],
role: 'info',
editType: 'ticks',
editType: 'ticks+margins',
impliedEdits: {tick0: undefined, dtick: undefined},
description: [
'Sets the tick mode for this axis.',
Expand All @@ -216,7 +216,7 @@ module.exports = {
min: 0,
dflt: 0,
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Specifies the maximum number of ticks for the particular axis.',
'The actual number of ticks will be chosen automatically to be',
Expand All @@ -227,7 +227,7 @@ module.exports = {
tick0: {
valType: 'any',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
impliedEdits: {tickmode: 'linear'},
description: [
'Sets the placement of the first tick on this axis.',
Expand All @@ -243,7 +243,7 @@ module.exports = {
dtick: {
valType: 'any',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
impliedEdits: {tickmode: 'linear'},
description: [
'Sets the step in-between ticks on this axis. Use with `tick0`.',
Expand All @@ -269,7 +269,7 @@ module.exports = {
},
tickvals: {
valType: 'data_array',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Sets the values at which ticks on this axis appear.',
'Only has an effect if `tickmode` is set to *array*.',
Expand All @@ -278,7 +278,7 @@ module.exports = {
},
ticktext: {
valType: 'data_array',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Sets the text displayed at the ticks position via `tickvals`.',
'Only has an effect if `tickmode` is set to *array*.',
Expand All @@ -289,7 +289,7 @@ module.exports = {
valType: 'enumerated',
values: ['outside', 'inside', ''],
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Determines whether ticks are drawn or not.',
'If **, this axis\' ticks are not drawn.',
Expand Down Expand Up @@ -341,14 +341,14 @@ module.exports = {
valType: 'boolean',
dflt: true,
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: 'Determines whether or not the tick labels are drawn.'
},
automargin: {
valType: 'boolean',
dflt: false,
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Determines whether long tick labels automatically grow the figure',
'margins.'
Expand Down Expand Up @@ -406,14 +406,14 @@ module.exports = {
description: 'Determines whether spikelines are stuck to the cursor or to the closest datapoints.'
},
tickfont: fontAttrs({
editType: 'ticks',
editType: 'ticks+margins',
description: 'Sets the tick font.'
}),
tickangle: {
valType: 'angle',
dflt: 'auto',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Sets the angle of the tick labels with respect to the horizontal.',
'For example, a `tickangle` of -90 draws the tick labels',
Expand All @@ -424,15 +424,15 @@ module.exports = {
valType: 'string',
dflt: '',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: 'Sets a tick label prefix.'
},
showtickprefix: {
valType: 'enumerated',
values: ['all', 'first', 'last', 'none'],
dflt: 'all',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'If *all*, all tick labels are displayed with a prefix.',
'If *first*, only the first tick is displayed with a prefix.',
Expand All @@ -444,23 +444,23 @@ module.exports = {
valType: 'string',
dflt: '',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: 'Sets a tick label suffix.'
},
showticksuffix: {
valType: 'enumerated',
values: ['all', 'first', 'last', 'none'],
dflt: 'all',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: 'Same as `showtickprefix` but for tick suffixes.'
},
showexponent: {
valType: 'enumerated',
values: ['all', 'first', 'last', 'none'],
dflt: 'all',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'If *all*, all exponents are shown besides their significands.',
'If *first*, only the exponent of the first tick is shown.',
Expand All @@ -473,7 +473,7 @@ module.exports = {
values: ['none', 'e', 'E', 'power', 'SI', 'B'],
dflt: 'B',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Determines a formatting rule for the tick exponents.',
'For example, consider the number 1,000,000,000.',
Expand All @@ -489,7 +489,7 @@ module.exports = {
valType: 'boolean',
dflt: false,
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'If "true", even 4-digit integers are separated'
].join(' ')
Expand All @@ -498,7 +498,7 @@ module.exports = {
valType: 'string',
dflt: '',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Sets the tick label formatting rule using d3 formatting mini-languages',
'which are very similar to those in Python. For numbers, see:',
Expand All @@ -517,10 +517,10 @@ module.exports = {
valType: 'info_array',
role: 'info',
items: [
{valType: 'any', editType: 'ticks'},
{valType: 'any', editType: 'ticks'}
{valType: 'any', editType: 'ticks+margins'},
{valType: 'any', editType: 'ticks+margins'}
],
editType: 'ticks',
editType: 'ticks+margins',
description: [
'range [*min*, *max*], where *min*, *max* - dtick values',
'which describe some zoom level, it is possible to omit *min*',
Expand All @@ -531,12 +531,12 @@ module.exports = {
valType: 'string',
dflt: '',
role: 'style',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'string - dtickformat for described zoom level, the same as *tickformat*'
].join(' ')
},
editType: 'ticks'
editType: 'ticks+margins'
},
hoverformat: {
valType: 'string',
Expand Down Expand Up @@ -638,7 +638,7 @@ module.exports = {
constants.idRegex.y.toString()
],
role: 'info',
editType: 'plot',
editType: 'plot+margins',
description: [
'If set to an opposite-letter axis id (e.g. `x2`, `y`), this axis is bound to',
'the corresponding opposite-letter axis.',
Expand All @@ -651,7 +651,7 @@ module.exports = {
valType: 'enumerated',
values: ['top', 'bottom', 'left', 'right'],
role: 'info',
editType: 'plot',
editType: 'plot+margins',
description: [
'Determines whether a x (y) axis is positioned',
'at the *bottom* (*left*) or *top* (*right*)',
Expand Down Expand Up @@ -695,11 +695,11 @@ module.exports = {
valType: 'info_array',
role: 'info',
items: [
{valType: 'number', min: 0, max: 1, editType: 'plot'},
{valType: 'number', min: 0, max: 1, editType: 'plot'}
{valType: 'number', min: 0, max: 1, editType: 'plot+margins'},
{valType: 'number', min: 0, max: 1, editType: 'plot+margins'}
],
dflt: [0, 1],
editType: 'plot',
editType: 'plot+margins',
description: [
'Sets the domain of this axis (in plot fraction).'
].join(' ')
Expand All @@ -710,7 +710,7 @@ module.exports = {
max: 1,
dflt: 0,
role: 'style',
editType: 'plot',
editType: 'plot+margins',
description: [
'Sets the position of this axis in the plotting space',
'(in normalized coordinates).',
Expand Down Expand Up @@ -754,7 +754,7 @@ module.exports = {
autotick: {
valType: 'boolean',
role: 'info',
editType: 'ticks',
editType: 'ticks+margins',
description: [
'Obsolete.',
'Set `tickmode` to *auto* for old `autotick` *true* behavior.',
Expand Down
Loading

0 comments on commit 1df34bd

Please sign in to comment.