8000 Fix rangebreaks overlapping and tick positions by archmoj · Pull Request #4831 · plotly/plotly.js · GitHub
[go: up one dir, main page]

Skip to content

Fix rangebreaks overlapping and tick positions 8000 #4831

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 22 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c28d0e5
centralize & refactor range expansions
archmoj Apr 6, 2020
1e63bfc
add rangebreaks tests with hourly and auto dticks
archmoj May 13, 2020
3baed24
revise tick positioning - fix dtick hourly - move tick0 outside the b…
archmoj May 13, 2020
4eab137
drop endpoint ticks that fall inside breaks
archmoj May 14, 2020
a7633c3
move last tick on rangebreaks to the start of break instead of removi…
archmoj May 15, 2020
d2d8e6e
revisit ticks on axes with rangebreaks
archmoj May 19, 2020
ca193eb
reject last tick that may include decimals
archmoj May 19, 2020
6b77b31
refactor - no need for else ifs in axes tickIncrement
archmoj May 19, 2020
ef14d60
add jasmine tests for axes with rangebreaks and set dtick
archmoj May 19, 2020
a0d4d0d
improve auto ticks on axes with rangebreaks
archmoj May 20, 2020
0fb92d4
attempt improving ticks on rangebreaks
archmoj Jun 1, 2020
5c8055b
fix issue 4879
archmoj Jun 1, 2020
0a782d8
invert if statement
archmoj Jun 2, 2020
6cd55b5
drop extra function for hour rangebreaks
archmoj Jun 2, 2020
db7d47d
drop extra logic for dayRatio
archmoj Jun 2, 2020
8c8c928
drop extra logic for dynamic oneDay
archmoj Jun 2, 2020
21f4284
drop extra logic for startHour
archmoj Jun 2, 2020
9e7b80e
drop extra logic for dayHours
archmoj Jun 2, 2020
22e5658
avoid too tick overlaps on x axes with rangebreaks and now only when …
archmoj Jun 2, 2020
b38688d
reverse loop order
archmoj Jun 2, 2020
c5cf45a
fixups to avoid single tick
archmoj Jun 2, 2020
9241578
reduce roughDTick by total length of breaks
archmoj Jun 3, 2020
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
Prev Previous commit
Next Next commit
improve auto ticks on axes with rangebreaks
  • Loading branch information
archmoj committed May 21, 2020
commit a0d4d0d4d4bb336b3dcfd4c1dd58c24115eadf8d
41 changes: 36 additions & 5 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ axes.prepTicks = function(ax) {
if(ax.tickmode === 'array') nt *= 100;


ax._roughDTick = (Math.abs(rng[1] - rng[0]) - (ax._lBreaks || 0)) / nt;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Looking much nicer now!

I'm just noticing that the auto dtick we get depends on whether there's a break within the visible range or not, which seems weird, and seems like exactly what this - ax._lBreaks was designed to avoid. For example (back on everyone's favorite, axes_breaks-candlestick2 😅) if I zoom in while keeping a break in range I can't get dtick less than 2 hours:
Screen Shot 2020-06-02 at 10 55 55 PM

But shift the break off the edge with exactly the same scale and it snaps to 15 min:
Screen Shot 2020-06-02 at 10 57 52 PM

Is there something bad that happens if this line is left as it was?

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 catch. Addressed in 9241578.

ax._roughDTick = Math.abs(rng[1] - rng[0]) / nt;
axes.autoTicks(ax, ax._roughDTick);

// check for a forced minimum dtick
Expand Down Expand Up @@ -731,6 +731,25 @@ function arrayTicks(ax) {
return ticksOut;
}

function roundBaseDay(dayHours) {
switch(dayHours) {
case 4: return [1, 2];
case 6: return [1, 2, 3];
case 8: return [1, 2, 4];
case 9: return [1, 3];
case 10: return [1, 2, 5];
case 12: return [1, 2, 3, 4, 6];
case 14: return [1, 2, 7];
case 15: return [1, 3, 5];
case 16: return [1, 2, 4, 8];
case 18: return [1, 2, 3, 6, 9];
case 20: return [1, 2, 4, 5, 10];
case 21: return [1, 3, 7];
case 22: return [1, 2, 11];
Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh interesting... so just a precise list of factors of the rounded number of visible hours. I'm not sure we really want all of these, and we might want to add something intermediate for the larger primes so we don't jump straight from 1 day to 1 hour, even though we can't do it precisely uniformly. But this is a great starting point anyway, we can tweak later.

However, looking at the mocks changed in this PR it seems like there's a problem when dtick is not an even fraction of a day. For example in axes_breaks-candlestick2, dtick is 18 hours (how did that happen? I only see 9 here, I would have thought it would go to 24 after that? That doesn't matter though, there are lots of periods we want that don't divide 24 evenly, this is just an example), and as a result some days have 2 ticks (at 9:30 and 12:00) and other days have only one (at 9:30).

Trying to figure out a reasonable way to handle this, in conjunction with tick0 possibly being set manually. I think what we may need to say is: when you have hourly rangebreaks and dtick < 24h, the only piece of tick0 that we consider is the time portion and we start at that time on each new day, then increment by dtick until we get to the next day, at which point we reset the time to tick0 again, so you get ticks at the same hours in every day.

}
return [1];
}

var roundBase10 = [2, 5, 10];
var roundBase24 = [1, 2, 3, 6, 12];
var roundBase60 = [1, 2, 5, 10, 15, 30];
Expand Down Expand Up @@ -776,22 +795,34 @@ axes.autoTicks = function(ax, roughDTick) {
// being > half of the final unit - so precalculate twice the rough val
var roughX2 = 2 * roughDTick;

var oneDay = ONEDAY;
var dayRatio = 1;
if(ax._hasHourBreaks) {
oneDay = ax._dayHours * ONEHOUR;
dayRatio = Math.round(ax._dayHours / 24 * 7) / 7; // we use this in week context
}

if(roughX2 > ONEAVGYEAR) {
roughDTick /= ONEAVGYEAR;
base = getBase(10);
ax.dtick = 'M' + (12 * roundDTick(roughDTick, base, roundBase10));
} else if(roughX2 > ONEAVGMONTH) {
roughDTick /= ONEAVGMONTH;
ax.dtick = 'M' + roundDTick(roughDTick, 1, roundBase24);
} else if(roughX2 > ONEDAY) {
ax.dtick = roundDTick(roughDTick, ONEDAY, ax._hasDayOfWeekBreaks ? [1, 7, 14] : roundDays);

} else if(roughX2 > oneDay) {
ax.dtick = roundDTick(roughDTick, oneDay, ax._hasDayOfWeekBreaks ?
[1, 2 * dayRatio, 7 * dayRatio, 14 * dayRatio] :
roundDays
);
// get week ticks on sunday
// this will also move the base tick off 2000-01-01 if dtick is
// 2 or 3 days... but that's a weird enough case that we'll ignore it.
ax.tick0 = Lib.dateTick0(ax.calendar, true);
} else if(roughX2 > ONEHOUR) {
ax.dtick = roundDTick(roughDTick, ONEHOUR, roundBase24);
ax.dtick = roundDTick(roughDTick, ONEHOUR, ax._hasHourBreaks ?
roundBaseDay(ax._dayHours) :
roundBase24
);
} else if(roughX2 > ONEMIN) {
ax.dtick = roundDTick(roughDTick, ONEMIN, roundBase60);
} else if(roughX2 > ONESEC) {
Expand Down
15 changes: 13 additions & 2 deletions src/plots/cartesian/axis_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,22 @@ module.exports = function handleAxisDefaults(containerIn, containerOut, coerce,
if(!containerOut.rangebreaks.length) {
delete containerOut.rangebreaks;
} else {
var n = 0;
for(var k = 0; k < containerOut.rangebreaks.length; k++) {
if(containerOut.rangebreaks[k].pattern === DAY_OF_WEEK) {
var brk = containerOut.rangebreaks[k];
if(brk.pattern === DAY_OF_WEEK) {
containerOut._hasDayOfWeekBreaks = true;
break;
n++;
}

if(brk.pattern === HOUR) {
containerOut._hasHourBreaks = true;
containerOut._dayHours = Math.round((brk.bounds[1] - brk.bounds[0] + 24) % 24);
n++;
}

// break when found all types
if(n === 2) break;
Copy link
Collaborator

Choose a reason for hiding this comment

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

what if the user includes two breaks of one type, THEN a break of the other type? I suppose I can theoretically imagine wanting two disjoint hour periods - a morning shift and an evening shift, with a midday break? I wouldn't worry about doing something "pretty" in this case, perhaps just set _dayHours to something like 1 or 2 so we go straight from day to hour ticks or perhaps just use the first break. But it definitely shouldn't stop us from noticing _hasDayOfWeekBreaks

And vice versa I guess... maybe someone wants to skip Sundays and Wednesdays? Again, weird, but shouldn't stop us from catching _hasHourBreaks

}

setConvert(containerOut, layoutOut);
Expand Down
Binary file modified test/image/baselines/axes_breaks-candlestick2.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-contour1d.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-contour2d.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-dtick_auto.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-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 modified test/image/baselines/axes_breaks-heatmap1d.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-heatmap2d.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-histogram2d.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-reversed-without-pattern.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-round-weekdays.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.
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.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4977,7 +4977,7 @@ describe('Test axes', function() {
.withContext(msg).toEqual(exp.tickVals);
}

it('should include requested ticks that fall within rangebreaks', function(done) {
it('should not include requested ticks that fall within rangebreaks', function(done) {
Plotly.plot(gd, [{
x: [
'1970-01-01 00:00:00.000',
Expand Down Expand Up @@ -5016,7 +5016,7 @@ describe('Test axes', function() {
})
.then(function() {
_assert('with two rangebreaks', {
tickVals: [0, 5, 10, 90, 95, 100, 190, 195, 200]
tickVals: [0, 89, 100, 200]
});
})
.catch(failTest)
Expand Down
0