Skip to content

Automargin fixes #2681

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 17 commits into from
Jun 6, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
23 changes: 2 additions & 21 deletions src/components/rangeslider/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,9 @@ module.exports = function(gd) {

// remove exiting sliders and their corresponding clip paths
rangeSliders.exit().each(function(axisOpts) {
var rangeSlider = d3.select(this),
opts = axisOpts[constants.name];

rangeSlider.remove();
var opts = axisOpts[constants.name];
fullLayout._topdefs.select('#' + opts._clipId).remove();
});

// remove push margin object(s)
if(rangeSliders.exit().size()) clearPushMargins(gd);
}).remove();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha exit().each() returns the exit selection, good to know.


// return early if no range slider is visible
if(rangeSliderData.length === 0) return;
Expand Down Expand Up @@ -602,16 +596,3 @@ function drawGrabbers(rangeSlider, gd, axisOpts, opts) {
});
grabAreaMax.attr('height', opts._height);
}

function clearPushMargins(gd) {
var pushMargins = gd._fullLayout._pushmargin || {},
keys = Object.keys(pushMargins);

for(var i = 0; i < keys.length; i++) {
var k = keys[i];

if(k.indexOf(constants.name) !== -1) {
Plots.autoMargin(gd, k);
}
}
}
70 changes: 28 additions & 42 deletions src/components/updatemenus/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ module.exports = function draw(gd) {
* ...
*/

function clearAutoMargin(menuOpts) {
Plots.autoMargin(gd, autoMarginId(menuOpts));
}

// draw update menu container
var menus = fullLayout._menulayer
.selectAll('g.' + constants.containerClassName)
Expand All @@ -63,10 +67,15 @@ module.exports = function draw(gd) {
.classed(constants.containerClassName, true)
.style('cursor', 'pointer');

menus.exit().remove();

// remove push margin object(s)
if(menus.exit().size()) clearPushMargins(gd);
menus.exit().each(function() {
// Most components don't need to explicitly remove autoMargin, because
// marginPushers does this - but updatemenu updates don't go through
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this the case? Is it because updatemenus attributes are under the "arraydraw" edit type? But then, in this case layout sliders should also have the same problem.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man, I missed sliders in this PR but sure enough, they still have the older pattern - thanks, I'll take a look at those.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated sliders in 3deab4c

// a full replot so we need to explicitly remove it.
// This is for removing *all* updatemenus, removing individuals is
// handled below, in hederGroups.exit
d3.select(this).selectAll('g.' + constants.headerGroupClassName)
.each(clearAutoMargin);
}).remove();

// return early if no update menus are visible
if(menuData.length === 0) return;
Expand Down Expand Up @@ -97,21 +106,13 @@ module.exports = function draw(gd) {
if(headerGroups.enter().size()) {
// make sure gButton is on top of all headers
gButton.node().parentNode.appendChild(gButton.node());

gButton
.call(removeAllButtons)
.attr(constants.menuIndexAttrName, '-1');
gButton.call(removeAllButtons);
}

headerGroups.exit().each(function(menuOpts) {
d3.select(this).remove();

gButton
.call(removeAllButtons)
.attr(constants.menuIndexAttrName, '-1');

Plots.autoMargin(gd, constants.autoMarginIdRoot + menuOpts._index);
});
gButton.call(removeAllButtons);
clearAutoMargin(menuOpts);
}).remove();

// draw headers!
headerGroups.each(function(menuOpts) {
Expand Down Expand Up @@ -219,16 +220,8 @@ function drawHeader(gd, gHeader, gButton, scrollBox, menuOpts) {
});

header.on('click', function() {
gButton.call(removeAllButtons);


// if this menu is active, fold the dropdown container
// otherwise, make this menu active
gButton.attr(
constants.menuIndexAttrName,
isActive(gButton, menuOpts) ?
-1 :
String(menuOpts._index)
gButton.call(removeAllButtons,
String(isActive(gButton, menuOpts) ? -1 : menuOpts._index)
);

drawButtons(gd, gHeader, gButton, scrollBox, menuOpts);
Expand Down Expand Up @@ -608,7 +601,7 @@ function findDimensions(gd, menuOpts) {
dims.lx = Math.round(dims.lx);
dims.ly = Math.round(dims.ly);

Plots.autoMargin(gd, constants.autoMarginIdRoot + menuOpts._index, {
Plots.autoMargin(gd, autoMarginId(menuOpts), {
x: menuOpts.x,
y: menuOpts.y,
l: paddedWidth * ({right: 1, center: 0.5}[xanchor] || 0),
Expand All @@ -618,6 +611,10 @@ function findDimensions(gd, menuOpts) {
});
}

function autoMarginId(menuOpts) {
return constants.autoMarginIdRoot + menuOpts._index;
}

// set item positions (mutates posOpts)
function setItemPosition(item, menuOpts, posOpts, overrideOpts) {
overrideOpts = overrideOpts || {};
Expand Down Expand Up @@ -655,19 +652,8 @@ function setItemPosition(item, menuOpts, posOpts, overrideOpts) {
posOpts.index++;
}

function removeAllButtons(gButton) {
gButton.selectAll('g.' + constants.dropdownButtonClassName).remove();
}

function clearPushMargins(gd) {
var pushMargins = gd._fullLayout._pushmargin || {};
var keys = Object.keys(pushMargins);

for(var i = 0; i < keys.length; i++) {
var k = keys[i];

if(k.indexOf(constants.autoMarginIdRoot) !== -1) {
Plots.autoMargin(gd, k);
}
}
function removeAllButtons(gButton, newMenuIndexAttr) {
gButton
.attr(constants.menuIndexAttrName, newMenuIndexAttr || '-1')
.selectAll('g.' + constants.dropdownButtonClassName).remove();
}
33 changes: 20 additions & 13 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,21 +217,24 @@ lib.simpleMap = function(array, func, x1, x2) {
return out;
};

// random string generator
lib.randstr = function randstr(existing, bits, base) {
/*
* Include number of bits, the base of the string you want
* and an optional array of existing strings to avoid.
*/
/**
* Random string generator
*
* @param {object} existing
* pass in strings to avoid as keys with truthy values
* @param {int} bits
* bits of information in the output string, default 24
* @param {int} base
* base of string representation, default 16. Should be a power of 2.
*/
lib.randstr = function randstr(existing, bits, base, _recursion) {
if(!base) base = 16;
if(bits === undefined) bits = 24;
if(bits <= 0) return '0';

var digits = Math.log(Math.pow(2, bits)) / Math.log(base),
res = '',
i,
b,
x;
var digits = Math.log(Math.pow(2, bits)) / Math.log(base);
var res = '';
var i, b, x;

for(i = 2; digits === Infinity; i *= 2) {
digits = Math.log(Math.pow(2, bits / i)) / Math.log(base) * i;
Expand All @@ -251,9 +254,13 @@ lib.randstr = function randstr(existing, bits, base) {
}

var parsed = parseInt(res, base);
if((existing && (existing.indexOf(res) > -1)) ||
if((existing && existing[res]) ||
(parsed !== Infinity && parsed >= Math.pow(2, bits))) {
return randstr(existing, bits, base);
if(_recursion > 10) {
lib.warn('randstr failed uniqueness');
return res;
}
return randstr(existing, bits, base, (_recursion || 0) + 1);
}
else return res;
};
Expand Down
2 changes: 1 addition & 1 deletion src/lib/svg_text_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function cleanEscapesForTex(s) {
}

function texToSVG(_texString, _config, _callback) {
var randomID = 'math-output-' + Lib.randstr([], 64);
var randomID = 'math-output-' + Lib.randstr({}, 64);
var tmpDiv = d3.select('body').append('div')
.attr({id: randomID})
.style({visibility: 'hidden', position: 'absolute'})
Expand Down
3 changes: 1 addition & 2 deletions 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', 'axrange', 'margins',
'calc', 'calcIfAutorange', 'plot', 'legend', 'ticks', 'axrange',
'layoutstyle', 'modebar', 'camera', 'arraydraw'
],
description: [
Expand All @@ -47,7 +47,6 @@ 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.',
'*axrange* minimal sequence when updating axis ranges.',
'*layoutstyle* reapplies global and SVG cartesian axis styles.',
'*modebar* just updates the modebar.',
Expand Down
40 changes: 3 additions & 37 deletions src/plot_api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,42 +197,17 @@ function cleanAxRef(container, attr) {
}

/*
* cleanData: Make a few changes to the data right away
* before it gets used for anything
* Mostly for backward compatibility, modifies the data traces users provide.
* cleanData: Make a few changes to the data for backward compatibility
* before it gets used for anything. Modifies the data traces users provide.
*
* Important: if you're going to add something here that modifies a data array,
* update it in place so the new array === the old one.
*/
exports.cleanData = function(data, existingData) {
// Enforce unique IDs
var suids = [], // seen uids --- so we can weed out incoming repeats
uids = data.concat(Array.isArray(existingData) ? existingData : [])
.filter(function(trace) { return 'uid' in trace; })
.map(function(trace) { return trace.uid; });

exports.cleanData = function(data) {
for(var tracei = 0; tracei < data.length; tracei++) {
var trace = data[tracei];
var i;

// assign uids to each trace and detect collisions.
if(!('uid' in trace) || suids.indexOf(trace.uid) !== -1) {
var newUid;

for(i = 0; i < 100; i++) {
newUid = Lib.randstr(uids);
if(suids.indexOf(newUid) === -1) break;
}
trace.uid = Lib.randstr(uids);
uids.push(trace.uid);
}
// keep track of already seen uids, so that if there are
// doubles we force the trace with a repeat uid to
// acquire a new one
suids.push(trace.uid);

// BACKWARD COMPATIBILITY FIXES

// use xbins to bin data in x, and ybins to bin data in y
if(trace.type === 'histogramy' && 'xbins' in trace && !('ybins' in trace)) {
trace.ybins = trace.xbins;
Expand Down Expand Up @@ -619,12 +594,3 @@ 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]];
}
}
};
Loading