Загрузка данных


private setRectangleBounds(left: number, right: number, top: number, bottom: number): boolean {
  const bounds = normalizeBounds(left, right, top, bottom, this.container);

  const snapshot = this.dragStateSnapshot;
  const dragTarget = this.activeDragTarget;

  if (this.mode === 'dragging' && snapshot && dragTarget && dragTarget !== 'body') {
    if (
      snapshot.startTime === null ||
      snapshot.endTime === null ||
      snapshot.startPrice === null ||
      snapshot.endPrice === null
    ) {
      return false;
    }

    let nextStartTime = snapshot.startTime;
    let nextEndTime = snapshot.endTime;
    let nextStartPrice = snapshot.startPrice;
    let nextEndPrice = snapshot.endPrice;

    const startIsLeft = this.isStartTimeOnLeft(snapshot);
    const startIsTop = this.isStartPriceOnTop(snapshot);

    if (dragTarget.includes('w')) {
      const nextTime = getTimeFromXCoordinate(this.chart, bounds.left);

      if (nextTime === null) {
        return false;
      }

      if (startIsLeft) {
        nextStartTime = nextTime;
      } else {
        nextEndTime = nextTime;
      }
    }

    if (dragTarget.includes('e')) {
      const nextTime = getTimeFromXCoordinate(this.chart, bounds.right);

      if (nextTime === null) {
        return false;
      }

      if (startIsLeft) {
        nextEndTime = nextTime;
      } else {
        nextStartTime = nextTime;
      }
    }

    if (dragTarget.includes('n')) {
      const nextPrice = getPriceFromYCoordinate(this.series, bounds.top);

      if (nextPrice === null) {
        return false;
      }

      if (startIsTop) {
        nextStartPrice = nextPrice;
      } else {
        nextEndPrice = nextPrice;
      }
    }

    if (dragTarget.includes('s')) {
      const nextPrice = getPriceFromYCoordinate(this.series, bounds.bottom);

      if (nextPrice === null) {
        return false;
      }

      if (startIsTop) {
        nextEndPrice = nextPrice;
      } else {
        nextStartPrice = nextPrice;
      }
    }

    this.startTime = nextStartTime;
    this.endTime = nextEndTime;
    this.startPrice = nextStartPrice;
    this.endPrice = nextEndPrice;

    return true;
  }

  const startTime = getTimeFromXCoordinate(this.chart, bounds.left);
  const endTime = getTimeFromXCoordinate(this.chart, bounds.right);
  const startPrice = getPriceFromYCoordinate(this.series, bounds.top);
  const endPrice = getPriceFromYCoordinate(this.series, bounds.bottom);

  if (startTime === null || endTime === null || startPrice === null || endPrice === null) {
    return false;
  }

  this.startTime = startTime;
  this.endTime = endTime;
  this.startPrice = startPrice;
  this.endPrice = endPrice;

  return true;
}


private isStartTimeOnLeft(state: RectangleState): boolean {
  if (state.startTime === null || state.endTime === null) {
    return true;
  }

  if (typeof state.startTime === 'number' && typeof state.endTime === 'number') {
    return state.startTime <= state.endTime;
  }

  const startX = getXCoordinateFromTime(this.chart, state.startTime, this.series);
  const endX = getXCoordinateFromTime(this.chart, state.endTime, this.series);

  if (startX === null || endX === null) {
    return true;
  }

  return Number(startX) <= Number(endX);
}

private isStartPriceOnTop(state: RectangleState): boolean {
  if (state.startPrice === null || state.endPrice === null) {
    return true;
  }

  const startY = getYCoordinateFromPrice(this.series, state.startPrice);
  const endY = getYCoordinateFromPrice(this.series, state.endPrice);

  if (startY === null || endY === null) {
    return state.startPrice >= state.endPrice;
  }

  return Number(startY) <= Number(endY);
}