UNPKG

recharts

Version:
123 lines (116 loc) • 4.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.clampDimensionToLimits = clampDimensionToLimits; exports.clampDimensionToLimitsAnchored = clampDimensionToLimitsAnchored; exports.panDimension = panDimension; exports.resetDimensionWithLimits = resetDimensionWithLimits; exports.selectDimension = selectDimension; exports.zoomDimension = zoomDimension; var _viewport = require("./viewport"); /** * Pure zoom "actions": given the current viewport of one axis and a gesture, compute the next * viewport. These hold all the zoom maths so the gesture modules (wheel, pointer, keyboard, ...) * stay thin DOM-only wrappers and the maths can be unit tested in isolation. * * `plotFocus` / `plotFraction` arguments are positions as a fraction `0..1` of the *visible* window * (e.g. the pointer's position across the plotting area), which is what the DOM layer can measure. */ /** * Zooms one axis by `factor` (>1 in, <1 out) keeping the point at `plotFocus` visually fixed. * The factor is clamped so the resulting width stays within the `[1/maxZoom, 1/minZoom]` bounds. */ function zoomDimension(current, factor, plotFocus, limits) { var width = (0, _viewport.getViewportWidth)(current); // plotFocus is a fraction of the visible window; map it to an absolute axis fraction. var absoluteFocus = current.startRatio + plotFocus * width; var minWidth = 1 / limits.maxZoom; var maxWidth = Math.min(1, 1 / limits.minZoom); var clampedFactor = Math.min(Math.max(factor, width / maxWidth), width / minWidth); return (0, _viewport.zoomViewportAround)(current, clampedFactor, absoluteFocus); } /** * Pans one axis by `deltaPlotFraction`, a fraction of the *visible* window * (e.g. dragging by half the plot width is `0.5`). Positive moves towards the end of the axis. */ function panDimension(current, deltaPlotFraction) { return (0, _viewport.panViewport)(current, deltaPlotFraction * (0, _viewport.getViewportWidth)(current)); } /** * Clamps one axis viewport into the width bounds the limits allow, keeping its centre (shifted * only as needed to stay inside `[0, 1]`). Use it on viewports that arrive from outside the * gesture maths - initial / controlled values, direct window edits - so they obey the same * `minZoom`/`maxZoom` as every gesture. */ function clampDimensionToLimits(current, limits) { var width = (0, _viewport.getViewportWidth)(current); var minWidth = 1 / limits.maxZoom; var maxWidth = Math.min(1, 1 / limits.minZoom); var targetWidth = Math.min(Math.max(width, minWidth), maxWidth); if (targetWidth === width) { return current; } var center = (current.startRatio + current.endRatio) / 2; return (0, _viewport.clampViewport)({ startRatio: center - targetWidth / 2, endRatio: center + targetWidth / 2 }); } /** * Same clamp, but anchored: whichever edge moved least compared to `previous` stays fixed and only * the other edge gives. This is what window editors (brush travellers, minimap resize handles) * want - the handle being dragged hits the limit while the opposite edge does not jump around. */ function clampDimensionToLimitsAnchored(next, previous, limits) { var width = (0, _viewport.getViewportWidth)(next); var minWidth = 1 / limits.maxZoom; var maxWidth = Math.min(1, 1 / limits.minZoom); var targetWidth = Math.min(Math.max(width, minWidth), maxWidth); if (targetWidth === width) { return next; } var startMoved = Math.abs(next.startRatio - previous.startRatio); var endMoved = Math.abs(next.endRatio - previous.endRatio); if (startMoved <= endMoved) { return (0, _viewport.clampViewport)({ startRatio: next.startRatio, endRatio: next.startRatio + targetWidth }); } return (0, _viewport.clampViewport)({ startRatio: next.endRatio - targetWidth, endRatio: next.endRatio }); } /** * The most zoomed-out viewport the limits allow: the full axis, or a centered `1/minZoom` window * when `minZoom > 1`. This is what "reset" and an un-zoomed mount must land on. */ function resetDimensionWithLimits(limits) { return clampDimensionToLimits({ startRatio: 0, endRatio: 1 }, limits); } /** * Zooms one axis into the sub-window between two fractions of the current visible window * (drag-to-zoom selection). Order-independent; respects `maxZoom`. */ function selectDimension(current, fromPlotFraction, toPlotFraction, limits) { var width = (0, _viewport.getViewportWidth)(current); var lo = Math.min(fromPlotFraction, toPlotFraction); var hi = Math.max(fromPlotFraction, toPlotFraction); var start = current.startRatio + lo * width; var end = current.startRatio + hi * width; var minWidth = 1 / limits.maxZoom; if (end - start < minWidth) { var mid = (start + end) / 2; start = mid - minWidth / 2; end = mid + minWidth / 2; } return (0, _viewport.clampViewport)({ startRatio: start, endRatio: end }); }