UNPKG

recharts

Version:
118 lines (105 loc) • 4.37 kB
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import { defaultAxisId } from '../../state/cartesianAxisSlice'; import { clampViewport, FULL_VIEWPORT } from './viewport'; /** * Which dimensions a chart can be zoomed and panned along. */ /** * A visible window along a single axis, expressed as fractions in `[0, 1]` of the axis. * `{ start: 0, end: 1 }` is the whole axis (not zoomed). */ /** * A per-dimension viewport in public form. Omitting a dimension means it is not zoomed. */ /** * The gesture/limit part of the zoom configuration - everything that has a sensible default. */ /** * Public configuration for the cartesian zoom/pan feature. See the "Zoom and Pan" guide. */ /** * The `zoom` prop accepts `true` (enable with defaults), an axis shorthand (`"x"`), or full options. */ var ZOOM_DEFAULTS = { axis: 'xy', xAxisId: defaultAxisId, yAxisId: defaultAxisId, minZoom: 1, maxZoom: 25, wheel: true, wheelStep: 1.15, pan: true, axisInteractions: true, dragToZoom: true, doubleClickReset: true, keyboard: true, touch: true, touchDrag: 'tooltip', scrollbars: true, panStep: 0.1, panFastMultiplier: 2.5, pinchThreshold: 12, wheelPanStep: 0.0015 }; function isZoomAxis(value) { return value === 'x' || value === 'y' || value === 'xy'; } /** * Normalizes the `zoom` prop into resolved options, or `null` when zoom is disabled. * Only an absent/`false` prop disables: any options object, axis shorthand or `true` resolves, * which the first overload encodes for callers that pass one. */ export function resolveZoomOptions(zoom) { if (zoom == null || zoom === false) { return null; } if (zoom === true) { return _objectSpread({}, ZOOM_DEFAULTS); } if (isZoomAxis(zoom)) { return _objectSpread(_objectSpread({}, ZOOM_DEFAULTS), {}, { axis: zoom }); } return _objectSpread(_objectSpread({}, ZOOM_DEFAULTS), zoom); } /** * Whether a given dimension is enabled for zoom/pan under these options. */ export function zoomEnabledForDimension(options, dimension) { return options.axis === 'xy' || options.axis === dimension; } /* ---------------------------------------------------------------------------------------------- * * Mapping between the public `[0, 1]` window form and the internal AxisViewport / ZoomState. * ---------------------------------------------------------------------------------------------- */ export function windowToViewport(window) { return clampViewport({ startRatio: window.start, endRatio: window.end }); } export function viewportToWindow(viewport) { return { start: viewport.startRatio, end: viewport.endRatio }; } /** * Converts a public viewport into a full {@link ZoomState}; omitted dimensions become the full view. */ export function zoomStateFromViewport(viewport) { return { x: viewport.x ? windowToViewport(viewport.x) : FULL_VIEWPORT, y: viewport.y ? windowToViewport(viewport.y) : FULL_VIEWPORT }; } export function zoomStateToViewport(state) { return { x: viewportToWindow(state.x), y: viewportToWindow(state.y) }; }