2017-06-17 71 views
1

我試圖製作一個手風琴列表,當用戶點擊它時,它會顯示數據圖表。問題是最初的圖表顯示,但是當我關閉手風琴列表時,它在重新打開時不再顯示。所以基本打開應用,顯示圖表,點擊手風琴元素,圖表消失,點擊手風琴元素,它展開,但圖表不顯示。帶有本地元素圖表的Ionic 3手風琴列表

排行榜page.html中

<ion-header> 
    <ion-navbar> 
    <ion-title>Health Summary</ion-title> 
    </ion-navbar> 
</ion-header> 


<ion-content padding> 
    <ion-list> 
    <ion-item padding (click)="toggleChartThisWeek(thisWeek)"> 
     This Week 
     <div *ngIf="thisWeek"> 
     <br> 
     <canvas #lineCanvas></canvas> 
     <br> 
     </div> 
    </ion-item> 
    </ion-list> 
</ion-content> 

排行榜page.ts

import { Component, ViewChild } from '@angular/core'; 
import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular'; 
import { Chart } from 'chart.js'; 


@IonicPage() 
@Component({ 
    selector: 'page-chart-page', 
    templateUrl: 'chart-page.html', 
}) 
export class ChartPage { 


    @ViewChild('lineCanvas') lineCanvas; 
    lineChart: any; 
    chartLabels: any = ["January", "February", "March", "April", "May", "June", "July"]; 
    chartData: any = [65, 59, 80, 81, 56, 55, 40]; 
    thisWeek: any = true; 


    constructor(public navCtrl: NavController, public navParams: NavParams, public platform: Platform) { 

    } 

    ionViewDidLoad() { 
    console.log('ionViewDidLoad HealthSummaryPage'); 
     this.lineChart = new Chart(this.lineCanvas.nativeElement, { 

      type: 'line', 
      data: { 
       labels: this.chartLabels, 
       datasets: [ 
        { 
         label: "My First dataset", 
         fill: false, 
         lineTension: 0.1, 
         backgroundColor: "rgba(75,192,192,0.4)", 
         borderColor: "rgba(75,192,192,1)", 
         borderCapStyle: 'butt', 
         borderDash: [], 
         borderDashOffset: 0.0, 
         borderJoinStyle: 'miter', 
         pointBorderColor: "rgba(75,192,192,1)", 
         pointBackgroundColor: "#fff", 
         pointBorderWidth: 1, 
         pointHoverRadius: 5, 
         pointHoverBackgroundColor: "rgba(75,192,192,1)", 
         pointHoverBorderColor: "rgba(220,220,220,1)", 
         pointHoverBorderWidth: 2, 
         pointRadius: 1, 
         pointHitRadius: 10, 
         data: this.chartData, 
         spanGaps: false, 
        } 
       ] 
      } 

     }); 
    } 

    ionViewWillEnter() { 
    } 

    toggleChartThisWeek() { 
    if(this.thisWeek) { 
     this.thisWeek = false; 
    } else { 
     this.thisWeek = true; 
    } 
    } 
} 

如何獲得它出現每一次?我是否需要將其存儲到變量中,如果是這樣,我該怎麼做?

回答

0

檢查低於希望迫使角至完全重繪模板代碼:

import { Component, ViewChild } from '@angular/core'; 
import { IonicPage, NavController, NavParams, Platform } from 'ionic-angular'; 
import { Chart } from 'chart.js'; 


@IonicPage() 
@Component({ 
    selector: 'page-chart-page', 
    templateUrl: 'chart-page.html', 
}) 
export class ChartPage { 


    @ViewChild('lineCanvas') lineCanvas; 
    lineChart: any; 
    chartLabels: any = ["January", "February", "March", "April", "May", "June", "July"]; 
    chartData: any = [65, 59, 80, 81, 56, 55, 40]; 
    thisWeek: any = true; 


    constructor(
     public navCtrl: NavController, 
     public navParams: NavParams, 
     public platform: Platform) { 

    } 

    ionViewDidEnter() { 
    console.log('ionViewDidEnter() HealthSummaryPage'); 
    } 

    drawChart(){ 
    if (! this.lineCanvas) { return; } 
    this.lineChart = new Chart(this.lineCanvas.nativeElement, { 
     type: 'line', 
     data: { 
      labels: this.chartLabels, 
      datasets: [ 
       { 
        label: "My First dataset", 
        fill: false, 
        lineTension: 0.1, 
        backgroundColor: "rgba(75,192,192,0.4)", 
        borderColor: "rgba(75,192,192,1)", 
        borderCapStyle: 'butt', 
        borderDash: [], 
        borderDashOffset: 0.0, 
        borderJoinStyle: 'miter', 
        pointBorderColor: "rgba(75,192,192,1)", 
        pointBackgroundColor: "#fff", 
        pointBorderWidth: 1, 
        pointHoverRadius: 5, 
        pointHoverBackgroundColor: "rgba(75,192,192,1)", 
        pointHoverBorderColor: "rgba(220,220,220,1)", 
        pointHoverBorderWidth: 2, 
        pointRadius: 1, 
        pointHitRadius: 10, 
        data: this.chartData, 
        spanGaps: false, 
       } 
      ] 
     } 
    }); 
} 

    toggleChartThisWeek() { 
    if(this.thisWeek) { 
     this.thisWeek = false; 
    } else { 
     this.thisWeek = true; 
     this.drawChart(); 
    } 
    } 
} 
+0

現在我得到的錯誤: '運行時錯誤 無法讀取屬性「nativeElement」的undefined' –

+0

給它一個鏡頭。我改變了'ionViewDidLoad()' - >'ionViewDidEnter()'並添加了一個'if(!this.lineCanvas){return; }'在'drawChart()'中。 – maninak

+0

現在它沒有出錯,但它仍然沒有顯示。 –