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

135
Visualizações
Updating Chart Data on Charts.js with Ng2-Charts

So im using chart.js and ng2-charts to create some gauges charts for my platform to monitore the level of fluids inside a tank of a machine.

I get those values by a external API and Im runing into a problem that the Charts are being rendered before I get the values form the API.

Is there a way I can render only after I get the value from the API or force the chart to re render?

<canvas id="hotGauge"
  baseChart
  [labels]="GaugeLabels"
  [chartType]="GaugeType"
  [options]="hotGaugeOptions"
  [colors]="hotGaugeColors"
  [legend]="GaugeLegend"
  [data]="hotGaugeData">


@Component({
  selector: 'app-machine-details',
  templateUrl: './machine-details.component.html',
  styleUrls: ['./machine-details.component.scss'],
  providers: []
})

export class MachineDetailsComponent implements OnInit {
 
 //This the value the Chart gets rendered with, if I equal it to 23 the chart will render 23% and not the expected value from the API
 hot :number;

 hotGaugeData: MultiDataSet=[];  

  constructor(private machinesService: MachinesService) { }

  ngOnInit() {
    //How I get the value from the API
    this.route.params.subscribe(params => {
      this.machinesService.getLastMachineStatusByMac(params.mac).subscribe((res) =>{
        this.hot = Math.floor((res.hotWaterDistance * 100) / 200);
      });
    });

    this.setGraphData();
  }

  setGraphData() {
   this.hotGaugeData=[];
   this.hotGaugeData[0]=[this.hot, 100-this.hot];
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can just place a ngIf on your canvas to check if there is any data like so:

<canvas id="hotGauge"
  *ngIf="hotGaugeData"
  baseChart
  [labels]="GaugeLabels"
  [chartType]="GaugeType"
  [options]="hotGaugeOptions"
  [colors]="hotGaugeColors"
  [legend]="GaugeLegend"
  [data]="hotGaugeData">

Example: https://codesandbox.io/s/gallant-wiles-c8w4p?file=/src/app/app.component.ts

about 4 years ago · Juan Pablo Isaza 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