@devexperts/dxcharts-lite
Version:
38 lines (37 loc) • 1.81 kB
JavaScript
/*
* Copyright (C) 2019 - 2025 Devexperts Solutions IE Limited
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { at } from '../../utils/array.utils';
import { pixelsToUnits } from './viewport.model';
/**
* Return constraited state that handled zooming and moving chart near first/last candles
* this and other constraits that works with state should mutate and return state
* @param initialState
* @param state
* @param visualCandlesCoordinates
* @param candleLimit
* @param bounds
* @returns
* @doc-tags viewport,zoom,scaling
*/
export const candleEdgesConstrait = (state, visualCandlesCoordinates, candleLimit, bounds) => {
var _a, _b, _c, _d;
const newState = Object.assign({}, state);
const leftConstraitCoordinate = (_b = (_a = visualCandlesCoordinates[candleLimit]) === null || _a === void 0 ? void 0 : _a.startUnit) !== null && _b !== void 0 ? _b : 0;
const rightConstraintCoordinate = (_d = (_c = at(-candleLimit, visualCandlesCoordinates)) === null || _c === void 0 ? void 0 : _c.startUnit) !== null && _d !== void 0 ? _d : 0;
let normalizedXStart = state.xStart;
let normalizedXEnd = state.xEnd;
if (newState.xStart >= rightConstraintCoordinate) {
normalizedXStart = rightConstraintCoordinate;
normalizedXEnd = normalizedXEnd - (newState.xStart - rightConstraintCoordinate);
}
if (newState.xEnd < leftConstraitCoordinate) {
normalizedXStart = leftConstraitCoordinate - pixelsToUnits(bounds.width, newState.zoomX);
normalizedXEnd = leftConstraitCoordinate;
}
newState.xStart = normalizedXStart;
newState.xEnd = normalizedXEnd;
return newState;
};