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

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

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 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