Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

260
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda