Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

259
Visualizações
The chart is not getting shown when page is loaded

I am facing problem in loading charts in angular 11.the chart is not getting shown when page is loaded but it is getting displayed when we click on some label "student count". I used canvas in html page to display the chart.

I am using chart.js version 2.9.4

<div style="height:250px;">
  <canvas id="myChart" width="750" height="250" #mychart>
  </canvas>
  </div>

my code in typescript file looks something like this

import { Chart } from 'chart.js';

   canvas: any;
   ctx: any;

    @ViewChild('mychart') mychart:any;
   
    
    ngAfterViewInit() {
      this.canvas = this.mychart.nativeElement; 
      this.ctx = this.canvas.getContext('2d');
  
      new Chart(this.ctx, {
        type: 'line',
        data: {
            datasets: [{
                label: 'Students Count',
                data: this.row_studnet,//this values received from the backend 
                backgroundColor: "rgb(115 185 243 / 65%)",
                borderColor: "red",
                fill: true,
            },
        
        ],
            labels: this.row_feeid//this values received from the backend
        },
        
    });


I am not getting the actual issue .please help me to solve this issue.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

It's because ngAfterViewInit is called after change detection is completed and the view has already been build. When you click on same label you execute new change detection cycle.

You can try to add this line of code after new Chart(), where cd is ChangeDetectorRef injected in constructor.

setTimeout(() => {

        this.cd.markForCheck();

    });

which will mark this component as a dirty for change detection.

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda