Ion segments that do not display data after switching tabs

I'm having trouble displaying charts after moving from segments . I currently have 2 segments, index and editing. In the index, I successfully display a chart with Chart.js .

Code for chart:

diagram = function() {
    var finished = 0;
    var pending = 0;

    for (var i = 0; i < this.goal.activities.length; i++) {
      var current = this.goal.activities[i];
      if (current.completed == true) { finished++ } else { pending++ }
    }
    this.piChart = new Chart(this.piCanvas.nativeElement, {
      type: 'doughnut',
      data: {
          labels: ["finished activities", "pending activities"],
          datasets: [{
            label: '# of Votes',
            data: [
              finished
              ,
              pending
            ],
            backgroundColor: [
              'rgba(255, 99, 132, 0.2)',
              'rgba(54, 162, 235, 0.2)'
            ],
            hoverBackgroundColor: [
              "#FF6384",
              "#36A2EB"
            ]
          }]
      }
    })
 }

And the function call:

ionViewDidEnter() {
  this.diagram();
}

Now on the page, the initial view of the diagram loads successfully and leads to a beautiful donut. The problem is that I click on the segment and then return to the original page (with the chart). Now the chart is gone. I still see this in HTML, but it just doesn't appear in the view.

Code in view:

<ion-header>
  <ion-navbar color="primary">
    <ion-title>{{ goal.title }}</ion-title>
  </ion-navbar>
</ion-header>

<ion-content>
  <ion-segment [(ngModel)]="goalTab">
    <ion-segment-button value="progress">
      Progress
    </ion-segment-button>
    <ion-segment-button value="activities">
      Actitivies
    </ion-segment-button>
  </ion-segment>

  <div [ngSwitch]="goalTab">
    <ion-card *ngSwitchCase="'progress'">
      <ion-card-header>
        Overview of activities for: {{ goal.title }}
      </ion-card-header>
      <ion-card-content>
        <canvas #piCanvas></canvas>
      </ion-card-content>
    </ion-card>

    <ion-list *ngSwitchCase="'activities'">
      <ion-item>
        <ion-card-header>
          Something other segment
        </ion-card-header>
      </ion-item>
    </ion-list>
  </div>
</ion-content>

Hope someone can find the problem and help me, thanks!

UPDATE

HTML. ( ): enter image description here

, (): enter image description here

, - .

+4
1

:

   <ion-card [style.display]="goalTab == 'progress' ? 'block' : 'none'">
      <ion-card-header>
         Overview of activities for: {{ goal.title }}
      </ion-card-header> 
      <ion-card-content> 
         <canvas #piCanvas></canvas>
      </ion-card-content> 
   </ion-card>
0

Source: https://habr.com/ru/post/1679516/


All Articles