Skip to content

Commit baf753a

Browse files
committedMar 3, 2020
add ax.maskBreaks method
- used during makeCalcdata to mask (i.e. set to BADNUM) coordinates inside the axis breaks.
1 parent e9cfe04 commit baf753a

File tree

2 files changed

+283
-0
lines changed

2 files changed

+283
-0
lines changed
 

‎src/plots/cartesian/set_convert.js

+80
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,79 @@ module.exports = function setConvert(ax, fullLayout) {
492492
}
493493
};
494494

495+
ax.maskBreaks = function(v) {
496+
var breaksIn = ax.breaks || [];
497+
var bnds, b0, b1, vb;
498+
499+
for(var i = 0; i < breaksIn.length; i++) {
500+
var brk = breaksIn[i];
501+
502+
if(brk.enabled) {
503+
var op = brk.operation;
504+
var op0 = op.charAt(0);
505+
var op1 = op.charAt(1);
506+
507+
if(brk.bounds) {
508+
var doesCrossPeriod = false;
509+
510+
switch(brk.pattern) {
511+
case '%w':
512+
bnds = Lib.simpleMap(brk.bounds, cleanNumber);
513+
b0 = bnds[0];
514+
b1 = bnds[1];
515+
vb = (new Date(v)).getUTCDay();
516+
if(bnds[0] > bnds[1]) doesCrossPeriod = true;
517+
break;
518+
case '%H':
519+
bnds = Lib.simpleMap(brk.bounds, cleanNumber);
520+
b0 = bnds[0];
521+
b1 = bnds[1];
522+
vb = (new Date(v)).getUTCHours();
523+
if(bnds[0] > bnds[1]) doesCrossPeriod = true;
524+
break;
525+
default:
526+
bnds = Lib.simpleMap(brk.bounds, ax.d2c);
527+
if(bnds[0] <= bnds[1]) {
528+
b0 = bnds[0];
529+
b1 = bnds[1];
530+
} else {
531+
b0 = bnds[1];
532+
b1 = bnds[0];
533+
}
534+
vb = v;
535+
}
536+
537+
if(doesCrossPeriod) {
538+
if(
539+
(op0 === '(' ? vb > b0 : vb >= b0) ||
540+
(op1 === ')' ? vb < b1 : vb <= b1)
541+
) return BADNUM;
542+
} else {
543+
if(
544+
(op0 === '(' ? vb > b0 : vb >= b0) &&
545+
(op1 === ')' ? vb < b1 : vb <= b1)
546+
) return BADNUM;
547+
}
548+
} else {
549+
var vals = Lib.simpleMap(brk.values, ax.d2c).sort(Lib.sorterAsc);
550+
var onOpenBound = false;
551+
552+
for(var j = 0; j < vals.length; j++) {
553+
b0 = vals[j];
554+
b1 = b0 + brk.dvalue;
555+
if(
556+
(op0 === '(' ? v > b0 : v >= b0) &&
557+
(op1 === ')' ? v < b1 : v <= b1)
558+
) return BADNUM;
559+
560+
if(onOpenBound && op0 === '(' && v === b0) return BADNUM;
561+
onOpenBound = op1 === ')' && v === b1;
562+
}
563+
}
564+
}
565+
}
566+
return v;
567+
};
495568
// makeCalcdata: takes an x or y array and converts it
496569
// to a position on the axis object "ax"
497570
// inputs:
@@ -541,6 +614,13 @@ module.exports = function setConvert(ax, fullLayout) {
541614
}
542615
}
543616

617+
// mask (i.e. set to BADNUM) coords that fall inside breaks
618+
if(ax.breaks) {
619+
for(i = 0; i < len; i++) {
620+
arrayOut[i] = ax.maskBreaks(arrayOut[i]);
621+
}
622+
}
623+
544624
return arrayOut;
545625
};
546626

‎test/jasmine/tests/axes_test.js

+203
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var Axes = require('@src/plots/cartesian/axes');
1313
var Fx = require('@src/components/fx');
1414
var supplyLayoutDefaults = require('@src/plots/cartesian/layout_defaults');
1515
var BADNUM = require('@src/constants/numerical').BADNUM;
16+
var ONEDAY = require('@src/constants/numerical').ONEDAY;
1617

1718
var createGraphDiv = require('../assets/create_graph_div');
1819
var destroyGraphDiv = require('../assets/destroy_graph_div');
@@ -4011,6 +4012,208 @@ describe('Test axes', function() {
40114012
.then(done);
40124013
});
40134014
});
4015+
4016+
describe('*breaks*', function() {
4017+
describe('during doCalcdata', function() {
4018+
var gd;
4019+
4020+
function _calc(trace, layout) {
4021+
gd = {data: [trace], layout: layout};
4022+
supplyDefaults(gd);
4023+
Plots.doCalcdata(gd);
4024+
}
4025+
4026+
function _assert(msg, exp) {
4027+
var cd = gd.calcdata[0];
4028+
var xc = cd.map(function(cdi) { return cdi.x; });
4029+
expect(xc).withContext(msg).toEqual(exp);
4030+
}
4031+
4032+
it('should discard coords within break bounds', function() {
4033+
_calc({
4034+
x: [0, 10, 50, 90, 100, 150, 190, 200]
4035+
}, {
4036+
xaxis: {
4037+
breaks: [
4038+
{'operation': '()', bounds: [10, 90]},
4039+
{'operation': '()', bounds: [100, 190]}
4040+
]
4041+
}
4042+
});
4043+
_assert('with operation:()', [0, 10, BADNUM, 90, 100, BADNUM, 190, 200]);
4044+
4045+
_calc({
4046+
x: [0, 10, 50, 90, 100, 150, 190, 200]
4047+
}, {
4048+
xaxis: {
4049+
breaks: [
4050+
{operation: '[]', bounds: [10, 90]},
4051+
{operation: '[]', bounds: [100, 190]}
4052+
]
4053+
}
4054+
});
4055+
_assert('with operation:[]', [0, BADNUM, BADNUM, BADNUM, BADNUM, BADNUM, BADNUM, 200]);
4056+
4057+
_calc({
4058+
x: [0, 10, 50, 90, 100, 150, 190, 200]
4059+
}, {
4060+
xaxis: {
4061+
breaks: [
4062+
{operation: '[)', bounds: [10, 90]},
4063+
{operation: '(]', bounds: [100, 190]}
4064+
]
4065+
}
4066+
});
4067+
_assert('with mixed operation values', [0, BADNUM, BADNUM, 90, 100, BADNUM, BADNUM, 200]);
4068+
});
4069+
4070+
it('should discard coords within break bounds - date %w case', function() {
4071+
var x = [
4072+
// Thursday
4073+
'2020-01-02 08:00', '2020-01-02 16:00',
4074+
// Friday
4075+
'2020-01-03 08:00', '2020-01-03 16:00',
4076+
// Saturday
4077+
'2020-01-04 08:00', '2020-01-04 16:00',
4078+
// Sunday
4079+
'2020-01-05 08:00', '2020-01-05 16:00',
4080+
// Monday
4081+
'2020-01-06 08:00', '2020-01-06 16:00',
4082+
// Tuesday
4083+
'2020-01-07 08:00', '2020-01-07 16:00'
4084+
];
4085+
4086+
var noWeekend = [
4087+
1577952000000, 1577980800000,
4088+
1578038400000, 1578067200000,
4089+
BADNUM, BADNUM,
4090+
BADNUM, BADNUM,
4091+
1578297600000, 1578326400000,
4092+
1578384000000, 1578412800000
4093+
];
4094+
4095+
_calc({x: x}, {
4096+
xaxis: {
4097+
breaks: [
4098+
{pattern: '%w', bounds: [6, 0], operation: '[]'}
4099+
]
4100+
}
4101+
});
4102+
_assert('[6,0]', noWeekend);
4103+
4104+
_calc({x: x}, {
4105+
xaxis: {
4106+
breaks: [
4107+
{pattern: '%w', bounds: [5, 1], operation: '()'}
4108+
]
4109+
}
4110+
});
4111+
_assert('(5,1)', noWeekend);
4112+
4113+
_calc({x: x}, {
4114+
xaxis: {
4115+
breaks: [
4116+
{pattern: '%w', bounds: [6, 1], operation: '[)'}
4117+
]
4118+
}
4119+
});
4120+
_assert('[6,1)', noWeekend);
4121+
4122+
_calc({x: x}, {
4123+
xaxis: {
4124+
breaks: [
4125+
{pattern: '%w', bounds: [5, 0], operation: '(]'}
4126+
]
4127+
}
4128+
});
4129+
_assert('(5,0]', noWeekend);
4130+
});
4131+
4132+
it('should discard coords within break bounds - date %H case', function() {
4133+
_calc({
4134+
x: [
4135+
'2020-01-02 08:00', '2020-01-02 20:00',
4136+
'2020-01-03 08:00', '2020-01-03 20:00',
4137+
'2020-01-04 08:00', '2020-01-04 20:00',
4138+
'2020-01-05 08:00', '2020-01-05 20:00',
4139+
'2020-01-06 08:00', '2020-01-06 20:00',
4140+
'2020-01-07 08:00', '2020-01-07 20:00'
4141+
]
4142+
}, {
4143+
xaxis: {
4144+
breaks: [
4145+
{pattern: '%H', bounds: [17, 8]}
4146+
]
4147+
}
4148+
});
4149+
_assert('with dflt operation', [
4150+
1577952000000, BADNUM,
4151+
1578038400000, BADNUM,
4152+
1578124800000, BADNUM,
4153+
1578211200000, BADNUM,
4154+
1578297600000, BADNUM,
4155+
1578384000000, BADNUM
4156+
]);
4157+
});
4158+
4159+
it('should discard coords within [values[i], values[i] + dvalue] bounds', function() {
4160+
var x = [
4161+
// Thursday
4162+
'2020-01-02 08:00', '2020-01-02 16:00',
4163+
// Friday
4164+
'2020-01-03 08:00', '2020-01-03 16:00',
4165+
// Saturday
4166+
'2020-01-04 08:00', '2020-01-04 16:00',
4167+
// Sunday
4168+
'2020-01-05 08:00', '2020-01-05 16:00',
4169+
// Monday
4170+
'2020-01-06 08:00', '2020-01-06 16:00',
4171+
// Tuesday
4172+
'2020-01-07 08:00', '2020-01-07 16:00'
4173+
];
4174+
4175+
_calc({x: x}, {
4176+
xaxis: {
4177+
breaks: [{values: ['2020-01-04', '2020-01-05'], dvalue: ONEDAY}],
4178+
}
4179+
});
4180+
_assert('two values', [
4181+
1577952000000, 1577980800000,
4182+
1578038400000, 1578067200000,
4183+
BADNUM, BADNUM,
4184+
BADNUM, BADNUM,
4185+
1578297600000, 1578326400000,
4186+
1578384000000, 1578412800000
4187+
]);
4188+
});
4189+
4190+
it('should discard coords equal to two consecutive open values bounds', function() {
4191+
_calc({
4192+
x: [1, 2, 3, 4, 5]
4193+
}, {
4194+
xaxis: {
4195+
breaks: [{ values: [2, 3], dvalue: 1, operation: '()' }]
4196+
}
4197+
});
4198+
_assert('', [1, 2, BADNUM, 4, 5]);
4199+
});
4200+
4201+
it('should adapt coords generated from x0/dx about breaks', function() {
4202+
_calc({
4203+
x0: 1,
4204+
dx: 0.5,
4205+
y: [1, 3, 5, 2, 4]
4206+
}, {
4207+
xaxis: {
4208+
breaks: [
4209+
{bounds: [2, 3]}
4210+
]
4211+
}
4212+
});
4213+
_assert('generated x=2.5 gets masked', [1, 1.5, 2, BADNUM, 3]);
4214+
});
4215+
});
4216+
});
40144217
});
40154218

40164219
function getZoomInButton(gd) {

0 commit comments

Comments
 (0)
Please sign in to comment.