Skip to content
Advertisement

Display values of variables taken from div html by means of d3.js

I am trying to display the values of an array, but for some reason it shows only NaN… can somebody help with the job? thanks in advance.

JavaScript
JavaScript

enter image description here

{{ Value1 }}, {{ Value2 }} and {{ Value 3 }} are being deployed correctly, this is a picture from the html:

enter image description here

Advertisement

Answer

Your recent question used text input elements to create a pie chart. These elements have a property called value. A div does not.

When you use this, where d refers to an element:

JavaScript

Input elements will return a value, and the divs will return undefined. Hence your NaN error:

JavaScript
JavaScript

But if your div elements only contain the relevant text, then we can use something like element.innerHtml to get the data.

JavaScript
JavaScript

If you swap:

JavaScript

With:

JavaScript

You should have a working solution.

Note, in both cases (input or div) the input is a string, this can cause issues depending on how you use the data, it’s often a good idea to coerce the data to a number, which you can do with a unary +:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement