1

Sorry if this seems obvious, I'm new to D3 and computer programming in general... I have a CSV file that has number values, but when they show up as strings in my console.

I tried putting the parseFloat function in my code, but then the data shows up as NaN on my console for each entry.

This is my code:

var dataset =d3.csv("file.csv", function(d){return{data: parseFloat(d)}; },
 function(data){    
 console.log(data);     
 });

My data looks like this in one column from my csv file:

9.96E-001
9.95E-001
9.95E-001
9.95E-001
9.96E-001
9.95E-001
9.96E-001
9.94E-001
9.96E-001
9.95E-001
9.94E-001
9.96E-001
1.00E+000

Obviously I do need to make them have integer values to then get them to work as a dataset, but I can't even get them to have number values instead of strings?

aonthemoon
  • 50
  • 1
  • 11

1 Answers1

6

javascript should be able to understand numbers formatted like that if you cast them as numbers:

var dataset = d3.csv("file.csv", function(data){
  data.forEach(function(d){ d['columnName'] = +d['columnName']; });   
  console.log(data);     
});

where 'columnName' refers to the title of the column with the the numbers.

If that doesn't work, try posting your csv so we can see if something else is going wrong.

Adam Pearce
  • 9,243
  • 2
  • 38
  • 35