UNPKG

recharts

Version:
153 lines (152 loc) • 5.54 kB
import { clampRatio, clampViewport, getViewportWidth, panViewport } from './viewport'; var MIN_PIXEL_SIZE = 1; function clampPixel(value, start, length) { return Math.min(Math.max(value, start), start + length); } export function axisViewportToPixelWindow(viewport, start, length, flipped) { if (length <= 0) { return { start, end: start }; } var pixelStartRatio = flipped ? 1 - viewport.endRatio : viewport.startRatio; var pixelEndRatio = flipped ? 1 - viewport.startRatio : viewport.endRatio; return { start: start + pixelStartRatio * length, end: start + pixelEndRatio * length }; } export function pixelWindowToAxisViewport(pixelStart, pixelEnd, start, length, flipped) { if (length <= 0) { return { startRatio: 0, endRatio: 1 }; } var low = (Math.min(pixelStart, pixelEnd) - start) / length; var high = (Math.max(pixelStart, pixelEnd) - start) / length; if (flipped) { return clampViewport({ startRatio: 1 - high, endRatio: 1 - low }); } return clampViewport({ startRatio: low, endRatio: high }); } export function zoomStateToRect(zoom, area, flipped) { var xWindow = axisViewportToPixelWindow(zoom.x, area.x, area.width, flipped.x); var yWindow = axisViewportToPixelWindow(zoom.y, area.y, area.height, flipped.y); return { x: xWindow.start, y: yWindow.start, width: Math.max(xWindow.end - xWindow.start, MIN_PIXEL_SIZE), height: Math.max(yWindow.end - yWindow.start, MIN_PIXEL_SIZE) }; } export function rectToZoomState(rect, area, flipped) { return { x: pixelWindowToAxisViewport(rect.x, rect.x + rect.width, area.x, area.width, flipped.x), y: pixelWindowToAxisViewport(rect.y, rect.y + rect.height, area.y, area.height, flipped.y) }; } export function panZoomStateByPixels(zoom, area, flipped, deltaX, deltaY) { var xDelta = area.width === 0 ? 0 : (flipped.x ? -deltaX : deltaX) / area.width; var yDelta = area.height === 0 ? 0 : (flipped.y ? -deltaY : deltaY) / area.height; return { x: panViewport(zoom.x, xDelta), y: panViewport(zoom.y, yDelta) }; } export function centerViewportAroundRatio(viewport, centerRatio) { var width = getViewportWidth(viewport); var targetStart = clampRatio(centerRatio) - width / 2; return panViewport(viewport, targetStart - viewport.startRatio); } export function centerZoomStateAtPoint(zoom, area, flipped, x, y) { var xRatio = area.width === 0 ? 0.5 : clampRatio((x - area.x) / area.width); var yRatio = area.height === 0 ? 0.5 : clampRatio((y - area.y) / area.height); return { x: centerViewportAroundRatio(zoom.x, flipped.x ? 1 - xRatio : xRatio), y: centerViewportAroundRatio(zoom.y, flipped.y ? 1 - yRatio : yRatio) }; } export function resizeZoomStateRect(zoom, area, flipped, handle, pointerX, pointerY, limits) { var rect = zoomStateToRect(zoom, area, flipped); var left = rect.x; var right = rect.x + rect.width; var top = rect.y; var bottom = rect.y + rect.height; var clampedX = clampPixel(pointerX, area.x, area.width); var clampedY = clampPixel(pointerY, area.y, area.height); // The zoom limits in pixel terms: a window narrower than minSize would exceed maxZoom, wider // than maxSize would fall below minZoom. Only the dragged edge is clamped, so the anchor holds. var minSizeX = limits ? Math.max(1 / limits.maxZoom * area.width, MIN_PIXEL_SIZE) : MIN_PIXEL_SIZE; var maxSizeX = limits ? Math.min(1, 1 / limits.minZoom) * area.width : area.width; var minSizeY = limits ? Math.max(1 / limits.maxZoom * area.height, MIN_PIXEL_SIZE) : MIN_PIXEL_SIZE; var maxSizeY = limits ? Math.min(1, 1 / limits.minZoom) * area.height : area.height; if (handle.includes('left')) { left = Math.min(Math.max(clampedX, right - maxSizeX), right - minSizeX); } else if (handle.includes('right')) { right = Math.max(Math.min(clampedX, left + maxSizeX), left + minSizeX); } if (handle.includes('top')) { top = Math.min(Math.max(clampedY, bottom - maxSizeY), bottom - minSizeY); } else if (handle.includes('bottom')) { bottom = Math.max(Math.min(clampedY, top + maxSizeY), top + minSizeY); } return rectToZoomState({ x: left, y: top, width: right - left, height: bottom - top }, area, flipped); } export function hitTestViewportWindow(rect, x, y) { var handleSize = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 8; var left = rect.x; var right = rect.x + rect.width; var top = rect.y; var bottom = rect.y + rect.height; var inX = x >= left && x <= right; var inY = y >= top && y <= bottom; if (!inX || !inY) { return 'outside'; } var distanceLeft = Math.abs(x - left); var distanceRight = Math.abs(x - right); var distanceTop = Math.abs(y - top); var distanceBottom = Math.abs(y - bottom); var onLeft = distanceLeft <= handleSize && distanceLeft <= distanceRight; var onRight = distanceRight <= handleSize && distanceRight < distanceLeft; var onTop = distanceTop <= handleSize && distanceTop <= distanceBottom; var onBottom = distanceBottom <= handleSize && distanceBottom < distanceTop; if (onTop && onLeft) { return 'top-left'; } if (onTop && onRight) { return 'top-right'; } if (onBottom && onLeft) { return 'bottom-left'; } if (onBottom && onRight) { return 'bottom-right'; } if (onLeft) { return 'left'; } if (onRight) { return 'right'; } if (onTop) { return 'top'; } if (onBottom) { return 'bottom'; } return 'body'; }