Skip to content

Commit a333acb

Browse files
committed
handle decimal bounds and simplify cross period logic
1 parent 2d2e627 commit a333acb

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

src/plots/cartesian/layout_attributes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ module.exports = {
287287
description: [
288288
'Determines a pattern on the time line that generates breaks.',
289289
'If *' + DAY_OF_WEEK + '* - Sunday-based weekday as a decimal number [0, 6].',
290-
'If *' + HOUR + '* - hour (24-hour clock) as integer numbers [0, 24].',
290+
'If *' + HOUR + '* - hour (24-hour clock) as decimal numbers between 0 and 24.',
291291
'for more info.',
292292
'Examples:',
293293
'- { pattern: \'' + DAY_OF_WEEK + '\', bounds: [6, 0] }',

src/plots/cartesian/set_convert.js

+28-19
Original file line numberDiff line numberDiff line change
@@ -630,22 +630,38 @@ module.exports = function setConvert(ax, fullLayout) {
630630
cleanNumber :
631631
ax.d2c // case of pattern: ''
632632
);
633-
b0 = Math.min(bnds[0], bnds[1]);
634-
b1 = Math.max(bnds[0], bnds[1]);
635-
var doesCrossPeriod = !!(pattern && bnds[0] > bnds[1]);
633+
b0 = bnds[0];
634+
b1 = bnds[1];
636635

637636
switch(pattern) {
638637
case WEEKDAY_PATTERN:
639638
vDate = new Date(v);
640639
vb = vDate.getUTCDay();
640+
641+
if(b0 > b1) {
642+
b1 += 7;
643+
if(vb < b0) vb += 7;
644+
}
645+
641646
break;
642647
case HOUR_PATTERN:
643648
vDate = new Date(v);
644-
vb = vDate.getUTCHours() + (
645-
vDate.getUTCMinutes() * ONEMIN +
646-
vDate.getUTCSeconds() * ONESEC +
647-
vDate.getUTCMilliseconds()
648-
) / ONEDAY;
649+
var hours = vDate.getUTCHours();
650+
var minutes = vDate.getUTCMinutes();
651+
var seconds = vDate.getUTCSeconds();
652+
var milliseconds = vDate.getUTCMilliseconds();
653+
654+
vb = hours + (
655+
minutes / 60 +
656+
seconds / 3600 +
657+
milliseconds / 3600000
658+
);
659+
660+
if(b0 > b1) {
661+
b1 += 24;
662+
if(vb < b0) vb += 24;
663+
}
664+
649665
break;
650666
case '':
651667
// N.B. should work on date axes as well!
@@ -655,17 +671,10 @@ module.exports = function setConvert(ax, fullLayout) {
655671
break;
656672
}
657673

658-
if(doesCrossPeriod) {
659-
if(
660-
(op0 === '(' ? vb > b1 : vb >= b1) ||
661-
(op1 === ')' ? vb < b0 : vb <= b0)
662-
) return BADNUM;
663-
} else {
664-
if(
665-
(op0 === '(' ? vb > b0 : vb >= b0) &&
666-
(op1 === ')' ? vb < b1 : vb <= b1)
667-
) return BADNUM;
668-
}
674+
if(
675+
(op0 === '(' ? vb > b0 : vb >= b0) &&
676+
(op1 === ')' ? vb < b1 : vb <= b1)
677+
) return BADNUM;
669678
} else {
670679
var vals = Lib.simpleMap(brk.values, ax.d2c).sort(Lib.sorterAsc);
671680
var onOpenBound = false;

test/jasmine/tests/axes_test.js

+39
Original file line numberDiff line numberDiff line change
@@ -4339,6 +4339,45 @@ describe('Test axes', function() {
43394339
]);
43404340
});
43414341

4342+
it('should discard coords within break bounds - date hour case of [23.75, 0.25]', function() {
4343+
_calc({
4344+
x: [
4345+
'2020-01-01 22',
4346+
'2020-01-01 23',
4347+
'2020-01-01 23:30',
4348+
'2020-01-01 23:59',
4349+
'2020-01-01 23:59:30',
4350+
'2020-01-01 23:59:59',
4351+
'2020-01-02 00:00:00',
4352+
'2020-01-02 00:00:01',
4353+
'2020-01-02 00:00:30',
4354+
'2020-01-02 00:30',
4355+
'2020-01-02 01',
4356+
'2020-01-02 02'
4357+
]
4358+
}, {
4359+
xaxis: {
4360+
rangebreaks: [
4361+
{pattern: 'hour', bounds: [23.75, 0.25]}
4362+
]
4363+
}
4364+
});
4365+
_assert('with dflt operation', [
4366+
Lib.dateTime2ms('2020-01-01 22'),
4367+
Lib.dateTime2ms('2020-01-01 23'),
4368+
Lib.dateTime2ms('2020-01-01 23:30'),
4369+
BADNUM,
4370+
BADNUM,
4371+
BADNUM,
4372+
BADNUM,
4373+
BADNUM,
4374+
BADNUM,
4375+
Lib.dateTime2ms('2020-01-02 00:30'),
4376+
Lib.dateTime2ms('2020-01-02 01'),
4377+
Lib.dateTime2ms('2020-01-02 02')
4378+
]);
4379+
});
4380+
43424381
it('should discard coords within [values[i], values[i] + dvalue] bounds', function() {
43434382
var x = [
43444383
// Thursday

0 commit comments

Comments
 (0)