2017-07-18 47 views
1

我在構建離子3中的組件,它放置圖像網格並將它們與畫布疊加連接。我已經使用離子的離子網格,但是當我嘗試調整畫布的大小時,我無法找到容器的正確尺寸,因此畫布容器保持其初始尺寸。我想使它動態,所以我需要畫布具有與網格容器相同的大小。這裏是我的代碼:將畫布調整爲其父元素離子網格

<ion-content> 
    <ion-card> 
    <div ion-card-content> 

     <div ion-grid id= "container-graph"> 
      <ion-row responsive-sm> 
      <canvas id="connection-canvas" style="position: absolute;"></canvas> 
      </ion-row> 
      <ion-row responsive-sm> 
      <ion-col col-6 text-center> 
       Generation 
       <img id="generation" src="../../assets/img/solar-panel.png"> 
      </ion-col> 
      <ion-col col-6 text-center> 
       Grid 
       <img id="grid" src="../../assets/img/grid.png"> 
      </ion-col> 
      </ion-row> 
      <ion-row responsive-sm> 
      <img id="inverter" src="../../assets/img/logo.png"> 
      </ion-row> 
      <ion-row responsive-sm> 
      <ion-col col-6 text-center> 
       Battery 
       <img id="battery" src="../../assets/img/battery.png"> 
      </ion-col> 
      <ion-col col-6 text-center> 
       Consumption 
       <img id="consumption" src="../../assets/img/house.png"> 
      </ion-col> 
      </ion-row> 
     </div> 
    </div> 
    </ion-card> 
    <ion-card> 
    <ion-card-content> 
     {{description}} 
    </ion-card-content> 
    </ion-card> 
</ion-content> 





public drawGraphs (center:string, a:string, b:string, c:string, d:string){ 
    var canvas: any = document.getElementById("connection-canvas"); 
    var connectionGraph: any = document.getElementById("container-graph"); 
    canvas.width = connectionGraph.clientWidth; 
    canvas.height = connectionGraph.clientHeight; 
    var elCenter = document.getElementById(center); 
    var contCenter = elCenter.getBoundingClientRect(); 
    var elA = document.getElementById(a); 
    this.drawLine(elCenter,elA); 
    var elB = document.getElementById(b); 
    this.drawLine(elCenter,elB); 
    var elC = document.getElementById(c); 
    this.drawLine(elCenter,elC); 
    var elD = document.getElementById(d); 
    this.drawLine(elCenter,elD); 


    } 

回答

0

我剛剛找到了一個可行的解決方案。我通過使用生命週期鉤子接口ngAfterViewChecked,在視圖完全呈現之後調用函數「drawgraphs」More info

ngAfterViewChecked(){ 
    this.drawGraphs("inverter", "generation", "grid", "battery", "consumption")} 
相關問題