mribot
mribot

Reputation: 529

NaN error while retrieving element from json data

I am beginer in javascript

I have following data:

data[i] = '{"Name":"'+car[i]+'","Price":"'+retail[i]+'"}';

I want to use value of retail[i] as y position of svg('.dot')

 svg.selectAll(".dot")
  .data(data)
.enter().append("circle")
  .attr("class", "dot")
  .attr("r", 3.5)
  .attr("cx", function(d) { return 10; })
  .attr("cy", function(d) { return y(d.Price); })
  .style("fill", function(d) { return 'Red'; });

but i am getting

Error: Invalid value for <circle> attribute cy="NaN" 

I have also tried .attr("cy", function(d) { return y(Number(d.Price)); }) What mistake i am doing?

Upvotes: 0

Views: 979

Answers (3)

deeban
deeban

Reputation: 149

It shows , you are passing NaN value (Not a Number). So you have to pass the value as integer value..

Use parseInt() to convert to integer..

Upvotes: 0

mido
mido

Reputation: 25034

may be the data is in string format, did you try .attr("cy", function(d) { return y(Number(JSON.parse(d).Price)); })

Upvotes: 0

leopik
leopik

Reputation: 2351

data[i] variable is a string as far as it is shown in your question. To fix this, either make that variable an object (the better solution) or use JSON.parse to parse that string and convert it to an object

I'd construct object for data[i] like so

data[i] = {"Name":car[i],"Price":retail[i]};

Upvotes: 1

Related Questions