Skip to content

Commit fe80cad

Browse files
committed
adapt dragbox logic for axis breaks
1 parent 6bec94d commit fe80cad

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

src/plots/cartesian/dragbox.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,20 @@ function zoomAxRanges(axList, r0Fraction, r1Fraction, updates, linkedAxes) {
986986
var axi = axList[i];
987987
if(axi.fixedrange) continue;
988988

989-
var axRangeLinear0 = axi._rl[0];
990-
var axRangeLinearSpan = axi._rl[1] - axRangeLinear0;
991-
updates[axi._name + '.range[0]'] = axi.l2r(axRangeLinear0 + axRangeLinearSpan * r0Fraction);
992-
updates[axi._name + '.range[1]'] = axi.l2r(axRangeLinear0 + axRangeLinearSpan * r1Fraction);
989+
if(axi.breaks) {
990+
if(axi._id.charAt(0) === 'y') {
991+
updates[axi._name + '.range[0]'] = axi.l2r(axi.p2l((1 - r0Fraction) * axi._length));
992+
updates[axi._name + '.range[1]'] = axi.l2r(axi.p2l((1 - r1Fraction) * axi._length));
993+
} else {
994+
updates[axi._name + '.range[0]'] = axi.l2r(axi.p2l(r0Fraction * axi._length));
995+
updates[axi._name + '.range[1]'] = axi.l2r(axi.p2l(r1Fraction * axi._length));
996+
}
997+
} else {
998+
var axRangeLinear0 = axi._rl[0];
999+
var axRangeLinearSpan = axi._rl[1] - axRangeLinear0;
1000+
updates[axi._name + '.range[0]'] = axi.l2r(axRangeLinear0 + axRangeLinearSpan * r0Fraction);
1001+
updates[axi._name + '.range[1]'] = axi.l2r(axRangeLinear0 + axRangeLinearSpan * r1Fraction);
1002+
}
9931003
}
9941004

9951005
// zoom linked axes about their centers
@@ -1003,10 +1013,17 @@ function dragAxList(axList, pix) {
10031013
for(var i = 0; i < axList.length; i++) {
10041014
var axi = axList[i];
10051015
if(!axi.fixedrange) {
1006-
axi.range = [
1007-
axi.l2r(axi._rl[0] - pix / axi._m),
1008-
axi.l2r(axi._rl[1] - pix / axi._m)
1009-
];
1016+
if(axi.breaks) {
1017+
axi.range = [
1018+
axi.l2r(axi._rl[0] - (axi.p2l(pix) - axi.p2l(0))),
1019+
axi.l2r(axi._rl[1] - (axi.p2l(axi._length + pix) - axi.p2l(axi._length)))
1020+
];
1021+
} else {
1022+
axi.range = [
1023+
axi.l2r(axi._rl[0] - pix / axi._m),
1024+
axi.l2r(axi._rl[1] - pix / axi._m)
1025+
];
1026+
}
10101027
}
10111028
}
10121029
}

test/jasmine/tests/cartesian_interact_test.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,6 +2029,70 @@ describe('axis zoom/pan and main plot zoom', function() {
20292029
.catch(failTest)
20302030
.then(done);
20312031
});
2032+
2033+
describe('with axis breaks', function() {
2034+
it('should compute correct range updates - x-axis case', function(done) {
2035+
function _assert(msg, xrng) {
2036+
expect(gd.layout.xaxis.range).toBeCloseToArray(xrng, 2, 'xrng - ' + msg);
2037+
}
2038+
2039+
Plotly.plot(gd, [{
2040+
mode: 'lines',
2041+
x: [0, 10, 50, 90, 100, 150, 190, 200]
2042+
}], {
2043+
xaxis: {
2044+
breaks: [
2045+
{bounds: [11, 89]},
2046+
{bounds: [101, 189]}
2047+
]
2048+
},
2049+
dragmode: 'zoom'
2050+
})
2051+
.then(function() { _assert('base', [0, 200]); })
2052+
.then(doDrag('xy', 'nsew', 50, 0))
2053+
// x range would be ~ [100, 118] w/o breaks
2054+
.then(function() { _assert('after x-only zoombox', [95, 98.148]); })
2055+
.then(doDblClick('xy', 'nsew'))
2056+
.then(function() { _assert('back to base', [0, 200]); })
2057+
.then(function() { return Plotly.relayout(gd, 'dragmode', 'pan'); })
2058+
.then(doDrag('xy', 'nsew', 50, 0))
2059+
// x range would be ~ [-18, 181] w/o breaks
2060+
.then(function() { _assert('after x-only pan', [-3.148, 196.851]); })
2061+
.catch(failTest)
2062+
.then(done);
2063+
});
2064+
2065+
it('should compute correct range updates - y-axis case', function(done) {
2066+
function _assert(msg, yrng) {
2067+
expect(gd.layout.yaxis.range).toBeCloseToArray(yrng, 2, 'yrng - ' + msg);
2068+
}
2069+
2070+
Plotly.plot(gd, [{
2071+
mode: 'lines',
2072+
y: [0, 10, 50, 90, 100, 150, 190, 200]
2073+
}], {
2074+
yaxis: {
2075+
breaks: [
2076+
{bounds: [11, 89]},
2077+
{bounds: [101, 189]}
2078+
]
2079+
},
2080+
dragmode: 'zoom'
2081+
})
2082+
.then(function() { _assert('base', [-1.888, 201.888]); })
2083+
.then(doDrag('xy', 'nsew', 0, 50))
2084+
// y range would be ~ [62, 100] w/o breaks
2085+
.then(function() { _assert('after y-only zoombox', [10.004, 95.00]); })
2086+
.then(doDblClick('xy', 'nsew'))
2087+
.then(function() { _assert('back to base', [-1.888, 201.888]); })
2088+
.then(function() { return Plotly.relayout(gd, 'dragmode', 'pan'); })
2089+
.then(doDrag('xy', 'nsew', 0, 50))
2090+
// y range would be ~ [35, 239] w/o breaks
2091+
.then(function() { _assert('after y-only pan', [5.106, 208.884]); })
2092+
.catch(failTest)
2093+
.then(done);
2094+
});
2095+
});
20322096
});
20332097

20342098
describe('Event data:', function() {

0 commit comments

Comments
 (0)