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

158
Visualizações
number string NaN error correction workaround

I have a member variable total explicitely of type number (in a strict Angular app) which is initialized in the constructor but then shows as type any in the drawSlices() method, resulting in my tooltip label showing NaN.

export class SomeComponent implements OnInit {
         
  total: number;
  dataSource: Item[];

  constructor(private dataService: DataService) {
    this.dataSource = this.dataService.getData();
    this.total = this.dataSource.reduce((sum, element) => sum += element.abs, 0);
  }
    
  drawSlices() {   
    const mousemove = function (event, d) {
      const percent = (Math.abs(d.data.abs / this.total) * 100).toFixed(2) + "%";
      tooltip
        .style("left", (event.pageX) + "px")
        .style("top", (event.pageY - 80) + "px")
        .style("display", "block")
        .style("opacity", 1)
        .style("height", "40px");
      tooltip.html(`name: ${d.data.name}<br>value: ${d.data.value}<br>share: ${percent}`);
    }
}

I have tried forcing the type, and similar workarounds, to no avail...

let t: number = this.total;
const percent = (Math.abs(d.data.abs / t) * 100).toFixed(2) + "%";

How do I fix this to get a number followed by %?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The issue is due to the scope of 'this' in mousemove function. Convert the normal function to an ES6 (arrow) function and you should be good to go. It will get the scope of the component, and would return the correct 'total' variable.

const mousemove = (event, d) => {
  const percent = (Math.abs(d.data.abs / this.total) * 100).toFixed(2) + "%";
  ...
}

Put a debug point for the line you are using this.total and see the value assinged to it.

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