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
78 changes: 55 additions & 23 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,9 @@ axes.calcTicks = function calcTicks(ax) {
var tickVals;
function generateTicks() {
var xPrevious = null;

var maxTicks = Math.max(1000, ax._length || 0);

tickVals = [];
for(var x = ax._tmin;
(axrev) ? (x >= endTick) : (x <= endTick);
Expand All @@ -611,31 +613,50 @@ axes.calcTicks = function calcTicks(ax) {
generateTicks();

if(ax.rangebreaks) {
var nTicksBefore = tickVals.length;
// replace ticks inside breaks that would get a tick
if(ax.tickmode === 'auto') {
for(var t = 0; t < tickVals.length; t++) {
var value = tickVals[t].value;
if(ax.maskBreaks(value) === BADNUM) {
// find which break we are in
for(var k = 0; k < ax._rangebreaks.length; k++) {
var brk = ax._rangebreaks[k];
if(value >= brk.min && value < brk.max) {
// replace with break end
tickVals[t] = {
minor: false,
value: brk.max
};

break;
}
}
}
}
}

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

// 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;
});
}
// reduce ticks
var len = tickVals.length;
if(len > 2) {
var tf2 = 2 * (ax.tickfont ? ax.tickfont.size : 12);

// 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);
});
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 All @@ -655,14 +676,25 @@ axes.calcTicks = function calcTicks(ax) {
ax._prevDateHead = '';
ax._inCalcTicks = true;

var ticksOut = new Array(tickVals.length);
var prevLabel;
var ticksOut = [];
for(var i = 0; i < tickVals.length; i++) {
ticksOut[i] = axes.tickText(
var label = axes.tickText(
ax,
tickVals[i].value,
false, // hover
tickVals[i].minor // noSuffixPrefix
);

if(ax.rangebreaks) { // this might be useful in general - but applying it only to rangebreaks for now
if(label.text !== prevLabel) {
ticksOut.push(label);
}
} else {
ticksOut.push(label);
}

prevLabel = label.text;
}

ax._inCalcTicks = false;
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