Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions draftlogs/8183_add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Make Cartesian plot zoom direction configurable [[#8183](https://github.com/plotly/plotly.js/pull/8183)]
11 changes: 11 additions & 0 deletions src/components/fx/layout_attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,5 +230,16 @@ module.exports = {
'*v* only vertical, *d* only diagonal and *any* sets no limit.'
].join(' '),
editType: 'none'
},
zoomdirection: {
valType: 'flaglist',
flags: ['h', 'v', 'd'],
dflt: 'h+v+d',
description: [
'When `dragmode` is set to *zoom*, this limits the zoombox drag.',
'Combine *h* for horizontal, *v* for vertical, and *d* for diagonal with *+*.',
'The default *h+v+d* sets no limit. Also applies when Shift turns *pan* into a zoombox drag.'
].join(' '),
editType: 'none'
}
};
1 change: 1 addition & 0 deletions src/components/fx/layout_defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = function supplyLayoutDefaults(layoutIn, layoutOut) {

var dragMode = coerce('dragmode');
if(dragMode === 'select') coerce('selectdirection');
if(dragMode === 'zoom' || dragMode === 'pan') coerce('zoomdirection');

// if only map or geo subplots is present on graph,
// reset 'zoom' dragmode to 'pan' until 'zoom' is implemented,
Expand Down
74 changes: 58 additions & 16 deletions src/plots/cartesian/dragbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
var corners;
// zoom takes over minDrag, so it also has to take over gd._dragged
var zoomDragged;
// allowed zoom directions
var allowH, allowV, allowD;

function zoomPrep(e, startX, startY) {
var dragBBox = dragger.getBoundingClientRect();
Expand All @@ -340,10 +342,39 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
dimmed = false;
zoomMode = 'xy';
zoomDragged = false;

var zoomDirectionFlags = (gd._fullLayout.zoomdirection || 'h+v+d').split('+');
allowH = zoomDirectionFlags.indexOf('h') !== -1;
allowV = zoomDirectionFlags.indexOf('v') !== -1;
allowD = zoomDirectionFlags.indexOf('d') !== -1;

zb = makeZoombox(zoomlayer, lum, xs, ys, path0);
corners = makeCorners(zoomlayer, xs, ys);
}

function applyZoomDirection(zoomModeCandidate, dx, dy) {
if (allowD && !allowH && !allowV) {
return dx >= MINDRAG && dy >= MINDRAG ? 'xy' : '';
}
if (zoomModeCandidate === 'xy' && !allowD) {
if (allowH && allowV) return dx / pw > dy / ph ? 'x' : 'y';
if (allowH) return 'x';
if (allowV) return 'y';
return '';
}
if (zoomModeCandidate === 'x' && !allowH) {
if (allowD && dx >= MINDRAG && dy >= MINDRAG) return 'xy';
if (allowV && !allowD && dy >= MINDRAG) return 'y';
return '';
}
if (zoomModeCandidate === 'y' && !allowV) {
if (allowD && dx >= MINDRAG && dy >= MINDRAG) return 'xy';
if (allowH && !allowD && dx >= MINDRAG) return 'x';
return '';
}
return zoomModeCandidate;
}

function zoomMove(dx0, dy0) {
if(gd._transitioningWithDuration) {
return false;
Expand All @@ -367,7 +398,7 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
}

if(links.isSubplotConstrained) {
if(dx > MINZOOM || dy > MINZOOM) {
if((dx > MINZOOM || dy > MINZOOM) && allowD) {
zoomMode = 'xy';
if(dx / pw > dy / ph) {
dy = dx * ph / pw;
Expand All @@ -383,7 +414,7 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
noZoom();
}
} else if(matches.isSubplotConstrained) {
if(dx > MINZOOM || dy > MINZOOM) {
if((dx > MINZOOM || dy > MINZOOM) && allowD) {
zoomMode = 'xy';

var r0 = Math.min(box.l / pw, (ph - box.b) / ph);
Expand All @@ -397,26 +428,37 @@ function makeDragBox(gd, plotinfo, x, y, w, h, ns, ew) {
} else {
noZoom();
}
} else if(!yActive || dy < Math.min(Math.max(dx * 0.6, MINDRAG), MINZOOM)) {
// look for small drags in one direction or the other,
// and only drag the other axis
} else {
let zoomModeCandidate = '';
if(!yActive || dy < Math.min(Math.max(dx * 0.6, MINDRAG), MINZOOM)) {
// look for small drags in one direction or the other,
// and only drag the other axis

if(dx < MINDRAG || !xActive) {
noZoom();
if(dx < MINDRAG || !xActive) {
zoomModeCandidate = '';
} else {
zoomModeCandidate = 'x';
}
} else if(!xActive || dx < Math.min(dy * 0.6, MINZOOM)) {
zoomModeCandidate = 'y';
} else {
zoomModeCandidate = 'xy';
}

zoomMode = applyZoomDirection(zoomModeCandidate, dx, dy);
if(zoomMode === 'x') {
box.t = 0;
box.b = ph;
zoomMode = 'x';
corners.attr('d', xCorners(box, y0));
} else if(zoomMode === 'y') {
box.l = 0;
box.r = pw;
corners.attr('d', yCorners(box, x0));
} else if(zoomMode === 'xy') {
corners.attr('d', xyCorners(box));
} else {
noZoom();
}
} else if(!xActive || dx < Math.min(dy * 0.6, MINZOOM)) {
box.l = 0;
box.r = pw;
zoomMode = 'y';
corners.attr('d', yCorners(box, x0));
} else {
zoomMode = 'xy';
corners.attr('d', xyCorners(box));
}
box.w = box.r - box.l;
box.h = box.b - box.t;
Expand Down
5 changes: 5 additions & 0 deletions src/types/generated/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16710,6 +16710,11 @@ export interface Layout {
width?: number;
xaxis?: LayoutAxis;
yaxis?: LayoutAxis;
/**
* When `dragmode` is set to *zoom*, this limits the zoombox drag. Combine *h* for horizontal, *v* for vertical, and *d* for diagonal with *+*. The default *h+v+d* sets no limit. Also applies when Shift turns *pan* into a zoombox drag.
* @default 'h+v+d'
*/
zoomdirection?: 'h' | 'v' | 'd' | (string & {});

// Subplot index signatures for numbered variants
[key: `coloraxis${number}`]: ColorAxis;
Expand Down
94 changes: 94 additions & 0 deletions test/jasmine/tests/cartesian_interact_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,100 @@ describe('axis zoom/pan and main plot zoom', function() {
.then(done, done.fail);
});

it('limits zoombox drags with zoomdirection', async() => {
let x0;
let y0;

async function resetPlot(zoomdirection) {
await Plotly.relayout(gd, {zoomdirection});
await Plotly.relayout(gd, {'xaxis.range': [0, 4], 'yaxis.range': [0, 4]});
x0 = gd.layout.xaxis.range.slice();
y0 = gd.layout.yaxis.range.slice();
}

async function dragBy(dx, dy) {
gd._mouseDownTime = 0;
await drag({node: getDragger('xy', 'nsew'), dpos: [dx, dy]});
}

function assertAxes(msg, xZoomed, yZoomed) {
const xRange = gd.layout.xaxis.range;
if(xZoomed) {
expect(xRange[1] - xRange[0]).withContext(msg + ' x zoomed').toBeLessThan(x0[1] - x0[0]);
} else {
expect(xRange).toBeCloseToArray(x0, 5, msg + ' x unchanged');
}

const yRange = gd.layout.yaxis.range;
if(yZoomed) {
expect(yRange[1] - yRange[0]).withContext(msg + ' y zoomed').toBeLessThan(y0[1] - y0[0]);
} else {
expect(yRange).toBeCloseToArray(y0, 5, msg + ' y unchanged');
}
}

const cases = [
{zoomdirection: 'h', dx: 80, dy: 0, xZoomed: true, yZoomed: false},
{zoomdirection: 'h', dx: 80, dy: 40, xZoomed: true, yZoomed: false},
{zoomdirection: 'h', dx: 0, dy: 80, xZoomed: false, yZoomed: false},
{zoomdirection: 'h', dx: 40, dy: 80, xZoomed: true, yZoomed: false},

{zoomdirection: 'v', dx: 80, dy: 0, xZoomed: false, yZoomed: false},
{zoomdirection: 'v', dx: 80, dy: 40, xZoomed: false, yZoomed: true},
{zoomdirection: 'v', dx: 0, dy: 80, xZoomed: false, yZoomed: true},
{zoomdirection: 'v', dx: 40, dy: 80, xZoomed: false, yZoomed: true},

{zoomdirection: 'd', dx: 80, dy: 0, xZoomed: false, yZoomed: false},
{zoomdirection: 'd', dx: 80, dy: 40, xZoomed: true, yZoomed: true},
{zoomdirection: 'd', dx: 0, dy: 80, xZoomed: false, yZoomed: false},
{zoomdirection: 'd', dx: 40, dy: 80, xZoomed: true, yZoomed: true},

{zoomdirection: 'h+v', dx: 80, dy: 0, xZoomed: true, yZoomed: false},
{zoomdirection: 'h+v', dx: 80, dy: 40, xZoomed: true, yZoomed: false},
{zoomdirection: 'h+v', dx: 0, dy: 80, xZoomed: false, yZoomed: true},
{zoomdirection: 'h+v', dx: 40, dy: 80, xZoomed: false, yZoomed: true},

{zoomdirection: 'h+d', dx: 80, dy: 0, xZoomed: true, yZoomed: false},
{zoomdirection: 'h+d', dx: 80, dy: 40, xZoomed: true, yZoomed: true},
{zoomdirection: 'h+d', dx: 0, dy: 80, xZoomed: false, yZoomed: false},
{zoomdirection: 'h+d', dx: 5, dy: 80, xZoomed: false, yZoomed: false},
{zoomdirection: 'h+d', dx: 15, dy: 80, xZoomed: true, yZoomed: true},
{zoomdirection: 'h+d', dx: 40, dy: 80, xZoomed: true, yZoomed: true},

{zoomdirection: 'v+d', dx: 80, dy: 0, xZoomed: false, yZoomed: false},
{zoomdirection: 'v+d', dx: 80, dy: 5, xZoomed: false, yZoomed: false},
{zoomdirection: 'v+d', dx: 80, dy: 15, xZoomed: true, yZoomed: true},
{zoomdirection: 'v+d', dx: 80, dy: 40, xZoomed: true, yZoomed: true},
{zoomdirection: 'v+d', dx: 0, dy: 80, xZoomed: false, yZoomed: true},
{zoomdirection: 'v+d', dx: 40, dy: 80, xZoomed: true, yZoomed: true},

{zoomdirection: 'h+v+d', dx: 80, dy: 0, xZoomed: true, yZoomed: false},
{zoomdirection: 'h+v+d', dx: 80, dy: 40, xZoomed: true, yZoomed: true},
{zoomdirection: 'h+v+d', dx: 0, dy: 80, xZoomed: false, yZoomed: true},
{zoomdirection: 'h+v+d', dx: 40, dy: 80, xZoomed: true, yZoomed: true},
];

await Plotly.newPlot(gd, [{y: [1, 2, 3, 2, 1]}], {
width: 500,
height: 500,
margin: {l: 50, r: 20, t: 20, b: 40},
dragmode: 'zoom',
xaxis: {range: [0, 4]},
yaxis: {range: [0, 4]}
});
expect(gd._fullLayout.zoomdirection).toBe('h+v+d');

for(const {zoomdirection, dx, dy, xZoomed, yZoomed} of cases) {
const msg = zoomdirection + ' drag ' + dx + ',' + dy;

await resetPlot(zoomdirection);
expect(gd._fullLayout.zoomdirection).withContext(msg).toBe(zoomdirection);

await dragBy(dx, dy);
assertAxes(msg, xZoomed, yZoomed);
}
});

it('handles y-only to xy back to y-only in single zoombox drag motion', function(done) {
function _assert(msg, evtData, xrng, yrng) {
expect([evtData['xaxis.range[0]'], evtData['xaxis.range[1]']])
Expand Down
11 changes: 11 additions & 0 deletions test/plot-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16029,6 +16029,17 @@
"editType": "ticks",
"valType": "number"
}
},
"zoomdirection": {
"description": "When `dragmode` is set to *zoom*, this limits the zoombox drag. Combine *h* for horizontal, *v* for vertical, and *d* for diagonal with *+*. The default *h+v+d* sets no limit. Also applies when Shift turns *pan* into a zoombox drag.",
"dflt": "h+v+d",
"editType": "none",
"flags": [
"h",
"v",
"d"
],
"valType": "flaglist"
}
}
},
Expand Down