recharts
Version:
React charts
167 lines (165 loc) • 6.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.axisViewportToPixelWindow = axisViewportToPixelWindow;
exports.centerViewportAroundRatio = centerViewportAroundRatio;
exports.centerZoomStateAtPoint = centerZoomStateAtPoint;
exports.hitTestViewportWindow = hitTestViewportWindow;
exports.panZoomStateByPixels = panZoomStateByPixels;
exports.pixelWindowToAxisViewport = pixelWindowToAxisViewport;
exports.rectToZoomState = rectToZoomState;
exports.resizeZoomStateRect = resizeZoomStateRect;
exports.zoomStateToRect = zoomStateToRect;
var _viewport = require("./viewport");
var MIN_PIXEL_SIZE = 1;
function clampPixel(value, start, length) {
return Math.min(Math.max(value, start), start + length);
}
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
};
}
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 (0, _viewport.clampViewport)({
startRatio: 1 - high,
endRatio: 1 - low
});
}
return (0, _viewport.clampViewport)({
startRatio: low,
endRatio: high
});
}
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)
};
}
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)
};
}
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: (0, _viewport.panViewport)(zoom.x, xDelta),
y: (0, _viewport.panViewport)(zoom.y, yDelta)
};
}
function centerViewportAroundRatio(viewport, centerRatio) {
var width = (0, _viewport.getViewportWidth)(viewport);
var targetStart = (0, _viewport.clampRatio)(centerRatio) - width / 2;
return (0, _viewport.panViewport)(viewport, targetStart - viewport.startRatio);
}
function centerZoomStateAtPoint(zoom, area, flipped, x, y) {
var xRatio = area.width === 0 ? 0.5 : (0, _viewport.clampRatio)((x - area.x) / area.width);
var yRatio = area.height === 0 ? 0.5 : (0, _viewport.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)
};
}
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);
}
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';
}