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

Implement ticklabeloverflow to improve control over display of tick labels #5584

Merged
merged 15 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 10 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
1 change: 1 addition & 0 deletions src/components/colorbar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ module.exports = overrideAll({
tickvals: axesAttrs.tickvals,
ticktext: axesAttrs.ticktext,
ticks: extendFlat({}, axesAttrs.ticks, {dflt: ''}),
ticklabeloverflow: extendFlat({}, axesAttrs.ticklabeloverflow, {dflt: 'allow'}),
Copy link
Collaborator

Choose a reason for hiding this comment

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

You don't want a dflt here, do you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good eye. Addressed in f833ce4.

ticklabelposition: {
valType: 'enumerated',
values: [
Expand Down
2 changes: 2 additions & 0 deletions src/components/colorbar/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ module.exports = function colorbarDefaults(containerIn, containerOut, layout) {
coerce('bordercolor');
coerce('borderwidth');
coerce('bgcolor');

var ticklabelposition = coerce('ticklabelposition');
coerce('ticklabeloverflow', ticklabelposition.indexOf('inside') !== -1 ? 'hide past domain' : 'hide past div');

handleTickValueDefaults(colorbarIn, colorbarOut, coerce, 'linear');

Expand Down
1 change: 1 addition & 0 deletions src/components/colorbar/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ function mockColorBarAxis(gd, opts, zrange) {
tickcolor: opts.tickcolor,
showticklabels: opts.showticklabels,
ticklabelposition: opts.ticklabelposition,
ticklabeloverflow: opts.ticklabeloverflow,
tickfont: opts.tickfont,
tickangle: opts.tickangle,
tickformat: opts.tickformat,
Expand Down
118 changes: 66 additions & 52 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3013,8 +3013,6 @@ axes.drawLabels = function(gd, ax, opts) {
}

function positionLabels(s, angle) {
var isInside = insideTicklabelposition(ax);

s.each(function(d) {
var thisLabel = d3.select(this);
var mathjaxGroup = thisLabel.select('.text-math-group');
Expand Down Expand Up @@ -3042,12 +3040,10 @@ axes.drawLabels = function(gd, ax, opts) {
'text-anchor': anchor
});

if(isInside) {
thisText.style('opacity', 1); // visible
thisText.style('opacity', 1); // visible

if(ax._hideOutOfRangeInsideTickLabels) {
ax._hideOutOfRangeInsideTickLabels();
}
if(ax._adjustTickLabelsOverflow) {
ax._adjustTickLabelsOverflow();
}
} else {
var mjWidth = Drawing.bBox(mathjaxGroup.node()).width;
Expand All @@ -3057,63 +3053,81 @@ axes.drawLabels = function(gd, ax, opts) {
});
}

ax._hideOutOfRangeInsideTickLabels = function() {
if(insideTicklabelposition(ax)) {
ax._adjustTickLabelsOverflow = function() {
var ticklabeloverflow = ax.ticklabeloverflow;
if(!ticklabeloverflow || ticklabeloverflow === 'allow') return;

var hideOverflow = ticklabeloverflow.indexOf('hide') !== -1;
// var pushOverflow = ticklabeloverflow.indexOf('push') !== -1;

var isX = ax._id.charAt(0) === 'x';
// div positions
var p0 = 0;
var p1 = isX ?
gd._fullLayout.width :
gd._fullLayout.height;

if(ticklabeloverflow.indexOf('domain') !== -1) {
// domain positions
var rl = Lib.simpleMap(ax.range, ax.r2l);
p0 = ax.l2p(rl[0]) + ax._offset;
p1 = ax.l2p(rl[1]) + ax._offset;
}

// hide inside tick labels that go outside axis end points
var p0 = ax.l2p(rl[0]);
var p1 = ax.l2p(rl[1]);
var min = Math.min(p0, p1);
var max = Math.max(p0, p1);

var min = Math.min(p0, p1) + ax._offset;
var max = Math.max(p0, p1) + ax._offset;
var side = ax.side;

var side = ax.side;
var isX = ax._id.charAt(0) === 'x';
var visibleLabelMin = Infinity;
var visibleLabelMax = -Infinity;

var visibleLabelMin = Infinity;
var visibleLabelMax = -Infinity;
tickLabels.each(function(d) {
var thisLabel = d3.select(this);
var mathjaxGroup = thisLabel.select('.text-math-group');

tickLabels.each(function(d) {
var thisLabel = d3.select(this);
var mathjaxGroup = thisLabel.select('.text-math-group');

if(mathjaxGroup.empty()) {
var bb = Drawing.bBox(thisLabel.node());
var hide = false;
if(isX) {
if(bb.right > max) hide = true;
else if(bb.left < min) hide = true;
} else {
if(bb.bottom > max) hide = true;
else if(bb.top + (ax.tickangle ? 0 : d.fontSize / 4) < min) hide = true;
if(mathjaxGroup.empty()) {
var bb = Drawing.bBox(thisLabel.node());
var adjust = '';
if(isX) {
if(bb.right > max) adjust += 'right';
else if(bb.left < min) adjust += 'left';
} else {
if(bb.bottom > max) adjust += 'top';
else if(bb.top + (ax.tickangle ? 0 : d.fontSize / 4) < min) adjust += 'bottom';
}

var t = thisLabel.select('text');
if(adjust) {
if(hideOverflow) t.style('opacity', 0); // hidden
/*
else if(pushOverflow) {
if(adjust.indexOf('left') !== -1) t.attr('text-anchor', 'start');
if(adjust.indexOf('right') !== -1) t.attr('text-anchor', 'end');
// more TODO: https://github.com/plotly/plotly.js/issues/3292
Copy link
Collaborator

Choose a reason for hiding this comment

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

You could leave another branch open with this WIP for "push to ..." but I'd rather not have it commented out in the main codebase. Anyway the eventual solution is going to need to be a bit more complex than this: we'll want to push these labels only as far as necessary, and then if doing so causes them to intersect the next labels we should still hide them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed by c9eaff2.

}
*/
} else {
t.style('opacity', 1); // visible

var t = thisLabel.select('text');
if(hide) {
t.style('opacity', 0); // hidden
if(side === 'bottom' || side === 'right') {
visibleLabelMin = Math.min(visibleLabelMin, isX ? bb.top : bb.left);
} else {
t.style('opacity', 1); // visible

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

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

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

Expand Down
14 changes: 13 additions & 1 deletion src/plots/cartesian/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
}
}

var ticklabelposition = '';
if(!options.noTicklabelposition || axType === 'multicategory') {
Lib.coerce(containerIn, containerOut, {
ticklabelposition = Lib.coerce(containerIn, containerOut, {
ticklabelposition: {
valType: 'enumerated',
dflt: 'outside',
Expand All @@ -75,6 +76,17 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
}, 'ticklabelposition');
}

if(!options.noTicklabeloverflow) {
coerce('ticklabeloverflow',
ticklabelposition.indexOf('inside') !== -1 ?
'hide past domain' :
axType === 'category' ||
axType === 'multicategory' ?
'allow' :
'hide past div'
);
}

setConvert(containerOut, layoutOut);

var autorangeDflt = !containerOut.isValidRange(containerIn.range);
Expand Down
17 changes: 17 additions & 0 deletions src/plots/cartesian/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,23 @@ module.exports = {
'so that the scales could match.'
].join(' ')
},
ticklabeloverflow: {
valType: 'enumerated',
values: [
'allow',
'hide past div',
'hide past domain',
// 'push to div',
// 'push to domain'
],
editType: 'calc',
description: [
'Determines whether or not the tick labels are drawn when overflown the div or domain.',
'The default value for inside tick labels is *hide past domain*.',
'Otherwise on *category* and *multicategory* axes the default is *allow*.',
'In other cases the default is *hide past div*.'
].join(' ')
},
mirror: {
valType: 'enumerated',
values: [true, 'ticks', false, 'all', 'allticks'],
Expand Down
1 change: 1 addition & 0 deletions src/plots/gl3d/layout/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut, options) {
noTickson: true,
noTicklabelmode: true,
noTicklabelposition: true,
noTicklabeloverflow: true,
bgColor: options.bgColor,
calendar: options.calendar
},
Expand Down
6 changes: 3 additions & 3 deletions src/plots/plots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2076,14 +2076,14 @@ plots.doAutoMargin = function(gd) {
}
}

hideInsideTickLabels(gd);
refineTicks(gd);
};

function hideInsideTickLabels(gd) {
function refineTicks(gd) {
var axList = axisIDs.list(gd, '', true);

[
'_hideOutOfRangeInsideTickLabels',
'_adjustTickLabelsOverflow',
'_hideCounterAxisInsideTickLabels'
].forEach(function(k) {
for(var i = 0; i < axList.length; i++) {
Expand Down
Binary file modified test/image/baselines/indicator_gauge.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/point-selection2.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 added test/image/baselines/ticklabeloverflow-0.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 added test/image/baselines/ticklabeloverflow-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 added test/image/baselines/ticklabeloverflow-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 added test/image/baselines/ticklabeloverflow-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 added test/image/baselines/ticklabeloverflow-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 added test/image/baselines/ticklabeloverflow-5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions test/image/mocks/ticklabeloverflow-0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"data": [{
"x": [-100, 0, 100],
"y": [-100, 0, 100]
}],
"layout": {
"xaxis": {
"range": [-115, 105],
"tickformat": ".3f",
"ticklabeloverflow": "allow"
},
"yaxis": {
"range": [-115, 105],
"tickangle": 90,
"tickformat": ".3f",
"ticklabeloverflow": "allow"
},
"plot_bgcolor": "lightblue",
"showlegend": false,
"width": 300,
"height": 300,
"margin": {
"t": 15,
"b": 15,
"l": 15,
"r": 15
}
}
}
29 changes: 29 additions & 0 deletions test/image/mocks/ticklabeloverflow-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"data": [{
"x": [-100, 0, 100],
"y": [-100, 0, 100]
}],
"layout": {
"xaxis": {
"range": [-115, 105],
"tickformat": ".3f",
"ticklabeloverflow": "hide past div"
},
"yaxis": {
"range": [-115, 105],
"tickangle": 90,
"tickformat": ".3f",
"ticklabeloverflow": "hide past div"
},
"plot_bgcolor": "lightblue",
"showlegend": false,
"width": 300,
"height": 300,
"margin": {
"t": 15,
"b": 15,
"l": 15,
"r": 15
}
}
}
29 changes: 29 additions & 0 deletions test/image/mocks/ticklabeloverflow-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"data": [{
"x": [-100, 0, 100],
"y": [-100, 0, 100]
}],
"layout": {
"xaxis": {
"range": [-115, 105],
"tickformat": ".3f",
"ticklabeloverflow": "hide past domain"
},
"yaxis": {
"range": [-115, 105],
"tickangle": 90,
"tickformat": ".3f",
"ticklabeloverflow": "hide past domain"
},
"plot_bgcolor": "lightblue",
"showlegend": false,
"width": 300,
"height": 300,
"margin": {
"t": 30,
"b": 30,
"l": 30,
"r": 30
}
}
}
31 changes: 31 additions & 0 deletions test/image/mocks/ticklabeloverflow-3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"data": [{
"colorbar": {
"dtick": 2,
"ticklen": 4,
"tickangle": 90,
"tickformat": ".4f",
"ticklabeloverflow": "allow"
},
"type": "heatmap",
"x": [-100, 0, 100],
"y": [-100, 0, 100],
"z": [
[0, 5, 10],
[5, 10, 0],
[10, 5, 0]
]
}],
"layout": {
"plot_bgcolor": "lightblue",
"showlegend": false,
"width": 200,
"height": 300,
"margin": {
"t": 5,
"b": 5,
"l": 45,
"r": 45
}
}
}
Loading