UNPKG

recharts

Version:
93 lines (91 loc) • 3.53 kB
import { clampViewport, FULL_VIEWPORT, getViewportWidth, isFullViewport } from './viewport'; /* * Camera zoom is a chart capability, not a consequence of whether an axis component happened to * register. Cartesian charts commonly omit visible axes while still using implicit x/y scales, so * treating an empty axis registry as a camera chart would apply both range zoom and a second SVG * transform. */ var cameraZoomModes = { FunnelChart: 'independent', PieChart: 'uniform', RadarChart: 'uniform', RadialBarChart: 'uniform', Sankey: 'independent', Sunburst: 'uniform', Treemap: 'independent' }; function getViewportCenter(viewport) { return (viewport.startRatio + viewport.endRatio) / 2; } export function resizeViewportAroundCenter(viewport, targetWidth) { var width = Math.max(Math.min(targetWidth, 1), 0); if (!Number.isFinite(width) || width <= 0) { return clampViewport(viewport); } var center = getViewportCenter(viewport); var startRatio = center - width / 2; var endRatio = center + width / 2; return clampViewport({ startRatio, endRatio }); } export function getUniformZoomState(zoom) { var xWidth = getViewportWidth(zoom.x); var yWidth = getViewportWidth(zoom.y); var targetWidth = Math.min(xWidth, yWidth); if (!Number.isFinite(targetWidth) || targetWidth >= 1) { return { x: FULL_VIEWPORT, y: FULL_VIEWPORT }; } return { x: resizeViewportAroundCenter(zoom.x, targetWidth), y: resizeViewportAroundCenter(zoom.y, targetWidth) }; } /** * Returns the render-time viewport projection for a camera-zoom chart, or `undefined` for charts * whose x/y scales already apply zoom. This never mutates or normalizes the public/store viewport. */ export function getCameraZoomState(chartName, zoom) { var cameraZoomMode = chartName == null ? undefined : cameraZoomModes[chartName]; if (cameraZoomMode == null) { return undefined; } return cameraZoomMode === 'uniform' ? getUniformZoomState(zoom) : zoom; } export function getZoomTransformForPlot(plot, zoom) { if (plot.width <= 0 || plot.height <= 0 || isFullViewport(zoom.x) && isFullViewport(zoom.y)) { return null; } var scaleX = 1 / getViewportWidth(zoom.x); var scaleY = 1 / getViewportWidth(zoom.y); var translateX = -plot.x - zoom.x.startRatio * plot.width; var translateY = -plot.y - zoom.y.startRatio * plot.height; return "translate(".concat(plot.x, " ").concat(plot.y, ") scale(").concat(scaleX, " ").concat(scaleY, ") translate(").concat(translateX, " ").concat(translateY, ")"); } export function transformCoordinateByZoom(coordinate, plot, zoom) { if (coordinate == null) { return undefined; } var transform = getZoomTransformForPlot(plot, zoom); if (transform == null) { return coordinate; } var scaleX = 1 / getViewportWidth(zoom.x); var scaleY = 1 / getViewportWidth(zoom.y); return { x: plot.x + (coordinate.x - plot.x - zoom.x.startRatio * plot.width) * scaleX, y: plot.y + (coordinate.y - plot.y - zoom.y.startRatio * plot.height) * scaleY }; } /** * Projects a raw SVG coordinate into wrapper/screen space for camera-zoom charts. Scale-based * cartesian charts are returned unchanged because their coordinates already include range zoom. */ export function transformCoordinateByCameraZoom(coordinate, plot, chartName, zoom) { var cameraZoomState = getCameraZoomState(chartName, zoom); return cameraZoomState == null ? coordinate : transformCoordinateByZoom(coordinate, plot, cameraZoomState); }