Skip to content

Commit

Permalink
reuse the loop to add breaks for cases without pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
archmoj committed Mar 28, 2020
1 parent b3a9a96 commit 4f1f9cf
Showing 1 changed file with 58 additions and 64 deletions.
122 changes: 58 additions & 64 deletions src/plots/cartesian/set_convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,72 +723,66 @@ module.exports = function setConvert(ax, fullLayout) {

if(brk.enabled) {
if(brk.bounds) {
bnds = Lib.simpleMap(brk.bounds, brk.pattern ? cleanNumber : ax.r2l);
b0 = bnds[0];
b1 = bnds[1];

// r0 value as date
var r0Date = new Date(r0);
// r0 value for break pattern
var bndDelta;
// step in ms between rangebreaks
var step;

var t0 = r0;
var t1 = r1;

switch(brk.pattern) {
case WEEKDAY_PATTERN:
step = 7 * ONEDAY;

bndDelta = (
(b1 < b0 ? 7 : 0) +
(b1 - b0)
) * ONEDAY;

t0 += b0 * ONEDAY - (
r0Date.getUTCDay() * ONEDAY +
r0Date.getUTCHours() * ONEHOUR +
r0Date.getUTCMinutes() * ONEMIN +
r0Date.getUTCSeconds() * ONESEC +
r0Date.getUTCMilliseconds()
);
break;
case HOUR_PATTERN:
step = ONEDAY;

bndDelta = (
(b1 < b0 ? 24 : 0) +
(b1 - b0)
) * ONEHOUR;

t0 += b0 * ONEHOUR - (
r0Date.getUTCHours() * ONEHOUR +
r0Date.getUTCMinutes() * ONEMIN +
r0Date.getUTCSeconds() * ONESEC +
r0Date.getUTCMilliseconds()
);
break;
default:
t0 = Math.min(bnds[0], bnds[1]);
t1 = Math.max(bnds[0], bnds[1]);
step = t1 - t0;
bndDelta = step;
}

if(brk.pattern) {
bnds = Lib.simpleMap(brk.bounds, cleanNumber);
b0 = bnds[0];
b1 = bnds[1];

// r0 value as date
var r0Date = new Date(r0);
// r0 value for break pattern
var bndDelta;
// step in ms between rangebreaks
var step;

var t0 = r0;
var t1 = r1;

switch(brk.pattern) {
case WEEKDAY_PATTERN:
step = 7 * ONEDAY;

bndDelta = (
(b1 < b0 ? 7 : 0) +
(b1 - b0)
) * ONEDAY;

t0 += b0 * ONEDAY - (
r0Date.getUTCDay() * ONEDAY +
r0Date.getUTCHours() * ONEHOUR +
r0Date.getUTCMinutes() * ONEMIN +
r0Date.getUTCSeconds() * ONESEC +
r0Date.getUTCMilliseconds()
);
break;
case HOUR_PATTERN:
step = ONEDAY;

bndDelta = (
(b1 < b0 ? 24 : 0) +
(b1 - b0)
) * ONEHOUR;

t0 += b0 * ONEHOUR - (
r0Date.getUTCHours() * ONEHOUR +
r0Date.getUTCMinutes() * ONEMIN +
r0Date.getUTCSeconds() * ONESEC +
r0Date.getUTCMilliseconds()
);
break;
}
// to remove decimal (most often found in auto ranges)
t0 = Math.floor(t0);
}

for(var t = t0; t <= t1; t += step) {
// TODO we need to remove decimal (most often found
// in auto ranges) for this to work correctly,
// should this be Math.floor, Math.ceil or
// Math.round ??
addBreak(Math.floor(t), Math.floor(t + bndDelta));
}
} else {
bnds = Lib.simpleMap(brk.bounds, ax.r2l);
if(bnds[0] <= bnds[1]) {
b0 = bnds[0];
b1 = bnds[1];
} else {
b0 = bnds[1];
b1 = bnds[0];
}
addBreak(b0, b1);
for(var t = t0; t < t1; t += step) {
addBreak(t, t + bndDelta);
}
} else {
var vals = Lib.simpleMap(brk.values, ax.d2c);
Expand Down

0 comments on commit 4f1f9cf

Please sign in to comment.