Skip to content

Improve auto ticks over axes with rangebreaks #4677

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 10 commits into from
Mar 25, 2020
46 changes: 29 additions & 17 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,31 +611,43 @@ axes.calcTicks = function calcTicks(ax) {
generateTicks();

if(ax.rangebreaks) {
var nTicksBefore = tickVals.length;

// remove ticks falling inside rangebreaks
tickVals = tickVals.filter(function(d) {
return ax.maskBreaks(d.value) !== BADNUM;
});

// if 'numerous' ticks get placed into rangebreaks,
// increase dtick to generate more ticks,
// so that some hopefully fall between rangebreaks
if(ax.tickmode === 'auto' && tickVals.length < nTicksBefore / 6) {
axes.autoTicks(ax, ax._roughDTick / 3);
autoTickRound(ax);
ax._tmin = axes.tickFirst(ax);
generateTicks();
tickVals = tickVals.filter(function(d) {
return ax.maskBreaks(d.value) !== BADNUM;
});
// add tick at the start of every rangebreak
if(ax.tickmode === 'auto') {
for(var k = 0; k < ax._rangebreaks.length; k++) {
Lib.pushUnique(tickVals, {
minor: false,
value: ax._rangebreaks[k].min
});
}
}

// remove "overlapping" ticks (e.g. on either side of a break)
var tf2 = ax.tickfont ? 1.5 * ax.tickfont.size : 0;
tickVals = tickVals.filter(function(d, i, self) {
return !(i && Math.abs(ax.c2p(d.value) - ax.c2p(self[i - 1].value)) < tf2);
// sort
tickVals = tickVals.sort(function(a, b) {
return a.value - b.value;
});

// remove "overlapping" ticks (e.g. on either side of a break)
var len = tickVals.length;
if(len > 2) {
var tf2 = 2 * (ax.tickfont ? ax.tickfont.size : 12);

var newTickVals = [];
var prevPos;
for(var q = 0; q < len; q++) {
var pos = ax.c2p(tickVals[q].value);

if(prevPos === undefined || Math.abs(pos - prevPos) > tf2) {
prevPos = pos;
newTickVals.push(tickVals[q]);
}
}
tickVals = newTickVals;
}
}

// If same angle over a full circle, the last tick vals is a duplicate.
Expand Down
Binary file modified test/image/baselines/axes_breaks-finance.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/axes_breaks-gridlines.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/axes_breaks-night_autorange-reversed.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/axes_breaks-rangeslider.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/axes_breaks-values.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/axes_breaks-weekends-weeknights.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/axes_breaks-weekends_autorange-reversed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading