Skip to content
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

Hide gridlines and ticks overlapping inside ticklabels #5550

Merged
merged 23 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
54b559f
hide overlapping gridlines over inside ticklabels
archmoj Mar 15, 2021
2870634
update baselines
archmoj Mar 15, 2021
60fe3d2
fixup opacity
archmoj Mar 16, 2021
e63e65a
separate hide function
archmoj Mar 16, 2021
49c978f
pass in plotinfo instead of grid in opts
archmoj Mar 16, 2021
23a035f
interactive hide gridlines
archmoj Mar 16, 2021
8c88f24
use visibility instead of opacity to hide elements
archmoj Mar 16, 2021
b7855ce
use constants for setting visibility
archmoj Mar 16, 2021
1df0c42
centralize inside tick label position test
archmoj Mar 16, 2021
0fe8cec
function to call hideCounterAxisInsideTickLabels
archmoj Mar 16, 2021
4f6ab1e
hide ticks, dividers, gridlines and zerolines
archmoj Mar 16, 2021
c76214c
no need to hide tick2 and dividers for now
archmoj Mar 17, 2021
a7fc3c3
add partial hide options
archmoj Mar 17, 2021
d0fd7c4
fixup period labels and tick visiblity
archmoj Mar 17, 2021
769c7b8
fixup gridline selection - drop hiding zerolines
archmoj Mar 17, 2021
ec15832
hide zerolines overlapping inside ticklabels
archmoj Mar 22, 2021
8631bcd
use opacity instead of visibility so that page test pass
archmoj Mar 23, 2021
257abfc
use display:none instead of opacity:0 to hide elements
archmoj Mar 23, 2021
c3649fd
update baselines
archmoj Mar 23, 2021
f05cd39
refactor to avoid adding new instances of the single-arg form of sele…
archmoj Mar 30, 2021
1f146f5
update logic from inside label to past edge of label
archmoj Mar 30, 2021
caf3239
make ticks hide/show similar to gridlines
archmoj Mar 31, 2021
e683b07
interactively hide tick labels
archmoj Apr 6, 2021
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
122 changes: 106 additions & 16 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ var ONESEC = constants.ONESEC;
var MINUS_SIGN = constants.MINUS_SIGN;
var BADNUM = constants.BADNUM;

var ZERO_PATH = { K: 'zeroline' };
var GRID_PATH = { K: 'gridline', L: 'path' };
var TICK_PATH = { K: 'tick', L: 'path' };
var TICK_TEXT = { K: 'tick', L: 'text' };

var alignmentConstants = require('../../constants/alignment');
var MID_SHIFT = alignmentConstants.MID_SHIFT;
var CAP_SHIFT = alignmentConstants.CAP_SHIFT;
Expand Down Expand Up @@ -1449,7 +1454,7 @@ function formatDate(ax, out, hover, extraPrecision) {
ax._prevDateHead = headStr;
dateStr += '<br>' + headStr;
} else {
var isInside = (ax.ticklabelposition || '').indexOf('inside') !== -1;
var isInside = insideTicklabelposition(ax);
var side = ax._realSide || ax.side; // polar mocks the side of the radial axis
if(
(!isInside && side === 'top') ||
Expand Down Expand Up @@ -2180,6 +2185,7 @@ axes.drawOne = function(gd, ax, opts) {
return axes.drawLabels(gd, ax, {
vals: vals,
layer: mainAxLayer,
plotinfo: plotinfo,
transFn: transTickLabelFn,
labelFns: axes.makeLabelFns(ax, mainLinePosition)
});
Expand Down Expand Up @@ -2798,7 +2804,10 @@ axes.drawTicks = function(gd, ax, opts) {
.classed('crisp', opts.crisp !== false)
.call(Color.stroke, ax.tickcolor)
.style('stroke-width', Drawing.crispRound(gd, ax.tickwidth, 1) + 'px')
.attr('d', opts.path);
.attr('d', opts.path)
.style('display', null); // visible

hideCounterAxisInsideTickLabels(ax, [TICK_PATH]);

ticks.attr('transform', opts.transFn);
};
Expand Down Expand Up @@ -2861,7 +2870,10 @@ axes.drawGrid = function(gd, ax, opts) {
grid.attr('transform', opts.transFn)
.attr('d', opts.path)
.call(Color.stroke, ax.gridcolor || '#ddd')
.style('stroke-width', ax._gw + 'px');
.style('stroke-width', ax._gw + 'px')
.style('display', null); // visible

hideCounterAxisInsideTickLabels(ax, [GRID_PATH]);

if(typeof opts.path === 'function') grid.attr('d', opts.path);
};
Expand Down Expand Up @@ -2910,7 +2922,10 @@ axes.drawZeroLine = function(gd, ax, opts) {
zl.attr('transform', opts.transFn)
.attr('d', opts.path)
.call(Color.stroke, ax.zerolinecolor || Color.defaultLine)
.style('stroke-width', Drawing.crispRound(gd, ax.zerolinewidth, ax._gw || 1) + 'px');
.style('stroke-width', Drawing.crispRound(gd, ax.zerolinewidth, ax._gw || 1) + 'px')
.style('display', null); // visible

hideCounterAxisInsideTickLabels(ax, [ZERO_PATH]);
};

/**
Expand Down Expand Up @@ -2983,7 +2998,10 @@ axes.drawLabels = function(gd, ax, opts) {
// sync label: just position it now.
positionLabels(thisLabel, tickAngle);
}
});
})
.style('display', null); // visible

hideCounterAxisInsideTickLabels(ax, [TICK_TEXT]);

tickLabels.exit().remove();

Expand All @@ -2995,7 +3013,7 @@ axes.drawLabels = function(gd, ax, opts) {
}

function positionLabels(s, angle) {
var isInside = (ax.ticklabelposition || '').indexOf('inside') !== -1;
var isInside = insideTicklabelposition(ax);

s.each(function(d) {
var thisLabel = d3.select(this);
Expand Down Expand Up @@ -3025,8 +3043,7 @@ axes.drawLabels = function(gd, ax, opts) {
});

if(isInside) {
// ensure visible
thisText.style({ opacity: 100 });
thisText.style('opacity', 0); // visible

if(ax._hideOutOfRangeInsideTickLabels) {
ax._hideOutOfRangeInsideTickLabels();
Expand All @@ -3040,9 +3057,8 @@ axes.drawLabels = function(gd, ax, opts) {
});
}

ax._hideOutOfRangeInsideTickLabels = undefined;
if((ax.ticklabelposition || '').indexOf('inside') !== -1) {
ax._hideOutOfRangeInsideTickLabels = function() {
ax._hideOutOfRangeInsideTickLabels = function() {
if(insideTicklabelposition(ax)) {
var rl = Lib.simpleMap(ax.range, ax.r2l);

// hide inside tick labels that go outside axis end points
Expand All @@ -3052,8 +3068,12 @@ axes.drawLabels = function(gd, ax, opts) {
var min = Math.min(p0, p1) + ax._offset;
var max = Math.max(p0, p1) + ax._offset;

var side = ax.side;
var isX = ax._id.charAt(0) === 'x';

var visibleLabelMin = Infinity;
var visibleLabelMax = -Infinity;

tickLabels.each(function(d) {
var thisLabel = d3.select(this);
var mathjaxGroup = thisLabel.select('.text-math-group');
Expand All @@ -3068,11 +3088,69 @@ axes.drawLabels = function(gd, ax, opts) {
if(bb.bottom > max) hide = true;
else if(bb.top + (ax.tickangle ? 0 : d.fontSize / 4) < min) hide = true;
}
if(hide) thisLabel.select('text').style({ opacity: 0 });

var t = thisLabel.select('text');
if(hide) {
t.style('opacity', 0); // hidden
} else {
t.style('opacity', 1); // visible

if(side === 'bottom' || side === 'right') {
visibleLabelMin = Math.min(visibleLabelMin, isX ? bb.top : bb.left);
} else {
visibleLabelMin = -Infinity;
}

if(side === 'top' || side === 'left') {
visibleLabelMax = Math.max(visibleLabelMax, isX ? bb.bottom : bb.right);
} else {
visibleLabelMax = Infinity;
}
}
} // TODO: hide mathjax?
});
};
}

if(ax._anchorAxis) {
ax._anchorAxis._visibleLabelMin = visibleLabelMin;
ax._anchorAxis._visibleLabelMax = visibleLabelMax;
}
}
};

ax._hideCounterAxisInsideTickLabels = function(partialOpts) {
if(insideTicklabelposition(ax._anchorAxis || {})) {
(partialOpts || [
ZERO_PATH,
GRID_PATH,
TICK_PATH,
TICK_TEXT
]).forEach(function(e) {
var isTickText = e.K === 'tick' && e.L === 'text';
if(isTickText && ax.ticklabelmode === 'period') return;

var sel;
if(e.K === ZERO_PATH.K) sel = opts.plotinfo.zerolinelayer.selectAll('.' + ax._id + 'zl');
else if(e.K === GRID_PATH.K) sel = opts.plotinfo.gridlayer.selectAll('.' + ax._id);
else sel = opts.plotinfo[ax._id.charAt(0) + 'axislayer'];

sel.each(function() {
var w = d3.select(this);
if(e.L) w = w.selectAll(e.L);

w.each(function(d) {
var q = ax.l2p(d.x) + ax._offset;

var t = d3.select(this);
if(q < ax._visibleLabelMax && q > ax._visibleLabelMin) {
t.style('display', 'none'); // hidden
} else if(e.K === 'tick') {
t.style('display', null); // visible
}
});
});
});
}
};

// make sure all labels are correctly positioned at their base angle
// the positionLabels call above is only for newly drawn labels.
Expand Down Expand Up @@ -3201,7 +3279,7 @@ axes.drawLabels = function(gd, ax, opts) {
var anchorAx = ax._anchorAxis;
if(
anchorAx && anchorAx.autorange &&
(ax.ticklabelposition || '').indexOf('inside') !== -1 &&
insideTicklabelposition(ax) &&
!isLinked(fullLayout, ax._id)
) {
if(!fullLayout._insideTickLabelsAutorange) {
Expand Down Expand Up @@ -3350,7 +3428,7 @@ function drawTitle(gd, ax) {
if(ax.title.hasOwnProperty('standoff')) {
titleStandoff = ax._depth + ax.title.standoff + approxTitleDepth(ax);
} else {
var isInside = (ax.ticklabelposition || '').indexOf('inside') !== -1;
var isInside = insideTicklabelposition(ax);

if(ax.type === 'multicategory') {
titleStandoff = ax._depth;
Expand Down Expand Up @@ -3708,3 +3786,15 @@ function moveOutsideBreak(v, ax) {
}
return v;
}

function insideTicklabelposition(ax) {
return ((ax.ticklabelposition || '').indexOf('inside') !== -1);
}

function hideCounterAxisInsideTickLabels(ax, opts) {
if(insideTicklabelposition(ax._anchorAxis || {})) {
if(ax._hideCounterAxisInsideTickLabels) {
ax._hideCounterAxisInsideTickLabels(opts);
}
}
}
18 changes: 11 additions & 7 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2076,17 +2076,21 @@ plots.doAutoMargin = function(gd) {
}
}

hideOutOfRangeInsideTickLabels(gd);
hideInsideTickLabels(gd);
};

function hideOutOfRangeInsideTickLabels(gd) {
function hideInsideTickLabels(gd) {
var axList = axisIDs.list(gd, '', true);
for(var i = 0; i < axList.length; i++) {
var ax = axList[i];

var hideFn = ax._hideOutOfRangeInsideTickLabels;
if(hideFn) hideFn();
}
[
'_hideOutOfRangeInsideTickLabels',
'_hideCounterAxisInsideTickLabels'
].forEach(function(k) {
for(var i = 0; i < axList.length; i++) {
var hideFn = axList[i][k];
if(hideFn) hideFn();
}
});
}

var marginKeys = ['l', 'r', 't', 'b', 'p', 'w', 'h'];
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 modified test/image/baselines/ticklabelposition-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-c.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/ticklabelposition-d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.