Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!