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

201
Visualizações
How to load NG2Charts when all data is ready

I am working on an Angular project, where I want to populate some charts with data that I recieve from Firebase. Since I have to do an async function, the data comes AFTER the chart is loaded, so it is displayinh nothing.

My code is the following:

detail.component.html

<div class="container mt-4">
  <div class="card">
    <div class="card-body">
      <div class="container d-flex">
        <span class="h3">Employee detail</span>
      </div>
      <div>
        <div>
          <div style="display: block">
            <canvas baseChart
              [data]="doughnutChartData"
      
              [type]="doughnutChartType">
            </canvas>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

detail.component.ts

import class DetailComponent implements OnInit {
  empHoursLearning!: number;
  empHProject1!: number;
  empHProject2!: number;

  public doughnutChartLabels: string[] = [
    'Hours learning',
    'Hours Project 1',
    'Hours Project 2'
  ];
  public doughnutChartData: ChartData<'doughnut'> = {
    labels: this.doughnutChartLabels,
    datasets: [
      {
        data: [this.empHoursLearning, this.empHProject1, this.empHProject2],
        backgroundColor: ['#7DFF93', '#36A2EB', '#FFCE56'],
        hoverBackgroundColor: '#E6E6E6',
        hoverBorderColor: '#E6E6E6',
      },
    ],
  };
  public doughnutChartType: ChartType = 'doughnut';

  @ViewChild(BaseChartDirective) chart?: BaseChartDirective;

  ngOnInit(): void {
    this.getEmpleados();   
  }

  async getEmployee() {
    await this._employeeService.getEmployee().then((employees) => {
      this.empHoursLearning= employees!['hl'];
      this.empHProject1= empleados!['p1'];
      this.empHProject2= empleados!['p2'];
    });
  }
}

My question is how to make the chart to render once all the data is received.

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

0

As Mentioned in the similar thread:

Use a loading flag combined with *ngIf to wait for the data from the service to be loaded before initializing the chart.

I.e

<canvas *ngIf="loaded" baseChart 
  [data]="doughnutChartData" 
  [type]="doughnutChartType"> 
</canvas>
about 4 years ago · Santiago Gelvez 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