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 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
7 changes: 7 additions & 0 deletions src/components/colorbar/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ module.exports = overrideAll({
tickvals: axesAttrs.tickvals,
ticktext: axesAttrs.ticktext,
ticks: extendFlat({}, axesAttrs.ticks, {dflt: ''}),
ticklabeloverflow: extendFlat({}, axesAttrs.ticklabeloverflow, {
description: [
'Determines how we handle tick labels that would overflow either the graph div or the domain of the axis.',
'The default value for inside tick labels is *hide past domain*.',
'In other cases the default is *hide past div*.'
].join(' ')
}),
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
110 changes: 58 additions & 52 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3020,8 +3020,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 @@ -3049,12 +3047,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 @@ -3064,63 +3060,73 @@ 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 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;
if(mathjaxGroup.empty()) {
var bb = Drawing.bBox(thisLabel.node());
var adjust = 0;
if(isX) {
if(bb.right > max) adjust = 1;
else if(bb.left < min) adjust = 1;
} else {
if(bb.bottom > max) adjust = 1;
else if(bb.top + (ax.tickangle ? 0 : d.fontSize / 4) < min) adjust = 1;
}

var t = thisLabel.select('text');
if(adjust) {
if(hideOverflow) 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 {
if(bb.bottom > max) hide = true;
else if(bb.top + (ax.tickangle ? 0 : d.fontSize / 4) < min) hide = true;
visibleLabelMin = -Infinity;
}

var t = thisLabel.select('text');
if(hide) {
t.style('opacity', 0); // hidden
if(side === 'top' || side === 'left') {
visibleLabelMax = Math.max(visibleLabelMax, isX ? bb.bottom : bb.right);
} 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;
}
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
15 changes: 15 additions & 0 deletions src/plots/cartesian/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,21 @@ module.exports = {
'so that the scales could match.'
].join(' ')
},
ticklabeloverflow: {
valType: 'enumerated',
values: [
'allow',
'hide past div',
'hide past domain'
],
editType: 'calc',
description: [
'Determines how we handle tick labels that would overflow either the graph div or the domain of the axis.',
'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
1 change: 1 addition & 0 deletions src/traces/indicator/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ function drawAngularGauge(gd, plotGroup, cd, opts) {
ax.type = 'linear';
ax.range = trace.gauge.axis.range;
ax._id = 'xangularaxis'; // or 'y', but I don't think this makes a difference here
ax.ticklabeloverflow = 'allow';
ax.setScale();

// 't'ick to 'g'eometric radians is used all over the place here
Expand Down
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