diff --git a/draftlogs/8183_add.md b/draftlogs/8183_add.md new file mode 100644 index 00000000000..5de3f97f09d --- /dev/null +++ b/draftlogs/8183_add.md @@ -0,0 +1 @@ +- Make Cartesian plot zoom direction configurable [[#8183](https://github.com/plotly/plotly.js/pull/8183)] \ No newline at end of file diff --git a/src/components/fx/layout_attributes.js b/src/components/fx/layout_attributes.js index 180dc8b32db..79af99d501c 100644 --- a/src/components/fx/layout_attributes.js +++ b/src/components/fx/layout_attributes.js @@ -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' } }; diff --git a/src/components/fx/layout_defaults.js b/src/components/fx/layout_defaults.js index ef727cddde0..271612597d3 100644 --- a/src/components/fx/layout_defaults.js +++ b/src/components/fx/layout_defaults.js @@ -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, diff --git a/src/plots/cartesian/dragbox.js b/src/plots/cartesian/dragbox.js index 6f7c87a4576..5296d6bb776 100644 --- a/src/plots/cartesian/dragbox.js +++ b/src/plots/cartesian/dragbox.js @@ -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(); @@ -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; @@ -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; @@ -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); @@ -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; diff --git a/src/types/generated/schema.d.ts b/src/types/generated/schema.d.ts index 7724577052f..cddd9a57bfa 100644 --- a/src/types/generated/schema.d.ts +++ b/src/types/generated/schema.d.ts @@ -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; diff --git a/test/jasmine/tests/cartesian_interact_test.js b/test/jasmine/tests/cartesian_interact_test.js index c3817896b41..90dbdf6ac45 100644 --- a/test/jasmine/tests/cartesian_interact_test.js +++ b/test/jasmine/tests/cartesian_interact_test.js @@ -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]']]) diff --git a/test/plot-schema.json b/test/plot-schema.json index 127a5fe337e..6498d9dd3b8 100644 --- a/test/plot-schema.json +++ b/test/plot-schema.json @@ -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" } } },