UNPKG

recharts

Version:
183 lines (182 loc) • 8.29 kB
import { ZoomDimension, ZoomState } from '../../state/zoomSlice'; import { AxisId } from '../../state/cartesianAxisSlice'; import { AxisViewport } from './viewport'; /** * Which dimensions a chart can be zoomed and panned along. */ export type ZoomAxis = 'x' | 'y' | 'xy'; /** * 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). */ export type AxisWindow = { start: number; end: number; }; /** * A per-dimension viewport in public form. Omitting a dimension means it is not zoomed. */ export type ZoomViewport = { x?: AxisWindow; y?: AxisWindow; }; /** * The gesture/limit part of the zoom configuration - everything that has a sensible default. */ type ZoomGestureOptions = { /** Which dimensions can be zoomed and panned. @defaultValue 'xy' */ axis?: ZoomAxis; /** * The id of XAxis which is corresponding to the data. Required when there are multiple XAxes. * @defaultValue 0 */ xAxisId?: AxisId; /** * The id of YAxis which is corresponding to the data. Required when there are multiple YAxes. * @defaultValue 0 */ yAxisId?: AxisId; /** Furthest zoom-out (1 = cannot zoom out past the full data). @defaultValue 1 */ minZoom?: number; /** Deepest zoom-in (e.g. 25 shows ~1/25th of the data). @defaultValue 25 */ maxZoom?: number; /** Wheel / trackpad to zoom. @defaultValue true */ wheel?: boolean; /** * Zoom factor applied per wheel notch (and per keyboard zoom step). `1.15` means each notch zooms * in by 15%. Larger is faster/coarser. @defaultValue 1.15 */ wheelStep?: number; /** Drag to pan. @defaultValue true */ pan?: boolean; /** * Interactions on the axis bands around the plot: wheel over an axis zooms only that axis, drag * over an axis pans only that axis. @defaultValue true */ axisInteractions?: boolean; /** * Drag-to-zoom a selected region. Triggered by Shift + drag (or a plain drag when `pan` is off). * @defaultValue true */ dragToZoom?: boolean; /** Double-click resets to the full view. @defaultValue true */ doubleClickReset?: boolean; /** * Keyboard zoom/pan when the chart is focused: `+`/`-` to zoom, Shift + arrows to pan, `0` to * reset. Requires the chart to be focusable (the default `accessibilityLayer`). @defaultValue true */ keyboard?: boolean; /** * Touch gestures: pinch to zoom, two-finger drag to pan, double-tap to reset, and double-tap then * drag up/down to zoom in/out (Google-Maps style). @defaultValue true */ touch?: boolean; /** * What a one-finger drag on the plot does on touch devices. * `tooltip` leaves it to the chart cursor/tooltip; `pan` pans the zoom viewport. * A single tap still updates the cursor/tooltip in both modes. @defaultValue 'tooltip' */ touchDrag?: 'tooltip' | 'pan'; /** * Show on-canvas scrollbars (one per zoomed axis). Drag the thumb or track to pan; wheel over the * scrollbar pans, and Shift+wheel zooms. Only visible while that axis is zoomed. @defaultValue true */ scrollbars?: boolean; /** * Base fraction of the visible window used for keyboard panning. Shift + arrow keys pan by * `panStep * panFastMultiplier`. @defaultValue 0.1 */ panStep?: number; /** Multiplier applied to the base `panStep` for Shift + arrow-key panning. @defaultValue 2.5 */ panFastMultiplier?: number; /** Finger-spread (px) before a pinch starts zooming, so small two-finger moves stay pure pan. @defaultValue 12 */ pinchThreshold?: number; /** Fraction of the visible window panned per unit of wheel delta (Shift / scrollbar wheel). @defaultValue 0.0015 */ wheelPanStep?: number; }; /** * Public configuration for the cartesian zoom/pan feature. See the "Zoom and Pan" guide. */ export type ZoomOptions = ZoomGestureOptions & { /** * Controlled viewport. When provided, the chart shows exactly this window and you are responsible * for updating it (typically from {@link ZoomOptions.onZoomChange}). A dimension left out is shown * un-zoomed. */ viewport?: ZoomViewport; /** * Initial (uncontrolled) viewport, applied once on mount. Ignored when `viewport` is set. */ initialZoom?: ZoomViewport; /** * Called whenever an uncontrolled viewport changes. In controlled mode, called once with each * proposed viewport; applying the `viewport` prop or restoring a rejected proposal stays silent. * Both dimensions are provided in `[0, 1]` window form. */ onZoomChange?: (viewport: Required<ZoomViewport>) => void; /** Localized accessible names for the optional x/y scrollbars. */ scrollbarAriaLabels?: Partial<Record<ZoomDimension, string>>; }; /** * The `zoom` prop accepts `true` (enable with defaults), an axis shorthand (`"x"`), or full options. */ export type ZoomProp = boolean | ZoomAxis | ZoomOptions; export type ResolvedZoomOptions = Required<ZoomGestureOptions> & Pick<ZoomOptions, 'viewport' | 'initialZoom' | 'onZoomChange' | 'scrollbarAriaLabels'> & { /** * Internal: restrict the wheel gesture to these pointer regions (`'plot'`, `'xAxis'`, `'yAxis'`, * `'xScrollbar'`, `'yScrollbar'`). Lets the composable components share the one wheel module * without double-counting (e.g. `<MouseWheelZoom/>` = plot, `<AxisZoom/>` = axes). Default: all. */ wheelRegions?: readonly string[]; /** * Internal: restrict the pointer drag gesture - `'pan'` (plain drag pans), `'select'` (plain drag * selects), or `'selectShift'` (Shift + drag selects, so it can coexist with a `'pan'` component). * Default: legacy (plain drag pans, Shift / no-pan selects). */ pointerMode?: 'pan' | 'select' | 'selectShift'; /** * Internal: when set, a completed drag selection emits the selected window here (per axis, in * `[0, 1]` fractions) instead of zooming into it. Used by `<DragToSelect/>`. */ onSelectRegion?: (selection: ZoomViewport) => void; /** * Internal: same shape as `onSelectRegion`, but reserved for the touch double-tap-drag select * mode so it never changes desktop Shift + drag behavior. */ onTouchSelectRegion?: (selection: ZoomViewport) => void; /** * Internal: controls the touch double-tap-then-drag gesture. Default zooms like a maps UI; * selectZoom draws a region and zooms into it; selectCallback emits a viewport through * `onTouchSelectRegion` instead; none leaves that gesture to another component. */ touchDoubleTapDrag?: 'zoom' | 'selectZoom' | 'selectCallback' | 'none'; /** * Internal: disable two-finger pinch handling while keeping the touch double-tap-drag selection * path installed. Used by `<DragToSelect/>`, which must not zoom the chart itself. */ touchPinch?: boolean; /** * Internal: disable one-finger axis panning while keeping other touch gestures installed. * Used by selection-only controls. */ touchAxisPan?: boolean; }; /** * 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 declare function resolveZoomOptions(zoom: ZoomOptions | ZoomAxis | true): ResolvedZoomOptions; export declare function resolveZoomOptions(zoom: ZoomProp | undefined): ResolvedZoomOptions | null; /** * Whether a given dimension is enabled for zoom/pan under these options. */ export declare function zoomEnabledForDimension(options: ResolvedZoomOptions, dimension: ZoomDimension): boolean; export declare function windowToViewport(window: AxisWindow): AxisViewport; export declare function viewportToWindow(viewport: AxisViewport): AxisWindow; /** * Converts a public viewport into a full {@link ZoomState}; omitted dimensions become the full view. */ export declare function zoomStateFromViewport(viewport: ZoomViewport): ZoomState; export declare function zoomStateToViewport(state: ZoomState): Required<ZoomViewport>; export {};