5

I am currently trying to use the d3 framework for a university visualisation approach. For testing purpose I want to read a csv-file and parse the rows to objects inside an array.

My csv looks like:

 ID, Referred To, TimeStamp, Votes, Comment

So I want to read it with the following lines:

d3.csv("test_comments.csv", function(data) {
  commentlist = data.map(function(d) {
    return[+d["ID"],
           +d["Referred To"],
           +d["TimeStamp"],
           +d["Votes"],
           +d["Comment"]
          ]
  });
});

But if I want to readout the values afterwards I am just getting "undefined" I also tried the way mbostock described in this thread: csv to array in d3.js

but working with a global variable is not working either.

var commentlist;
d3.csv("test_comments.csv", function(data) {
  commentlist = data.map(function(d) {
    return[+d["ID"],
           +d["Referred To"],
           +d["TimeStamp"],
           +d["Votes"],
           +d["Comment"]
          ]
  });
});
console.log(commentlist);

Am I understanding something wrong? Maybe you have a solution for me.

7
  • Do you have spaces in your CSV like in the headers? Then remove those. Also I'm guessing converting "comment" to a number doesn't make sense. Commented Jul 21, 2014 at 12:54
  • yes but just in the comment section, the Idea is a new visualisation of social network comments. So I wanted to store the comments inside of a csv, isnt that possible ? Commented Jul 21, 2014 at 12:57
  • Sure, but your code is converting everything in the CSV into numbers, which doesn't make sense if you have strings in there. Commented Jul 21, 2014 at 13:05
  • OkayI understand, which char do I have to use to convert to Strings? Commented Jul 21, 2014 at 13:13
  • You're getting strings already, no need to convert. Commented Jul 21, 2014 at 13:15

2 Answers 2

1
var commentlist=[];
d3.csv("test_comments.csv", function(data) {
  commentlist=data;
});
console.log(commentlist);

What I know is, In the call back data object will contain array of JSON objects of csv file's all rows of data, that each row data is pushed as a JSON format into the data array. As below

[{"ID": valueFromtheRow, "Referred To": value, "TimeStamp": value, "Votes":value, "Comment":value}]

The call back function is called by passing the array of JSONs. So data object will look like

data=[{"ID": valueFromtheRow, "Referred To": value, "TimeStamp": value, "Votes":value, "Comment":value}];

Hope you understood...If not ask me.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks that is another try, but I want to access for instance console.log(commentlist[1]) I get undefined as return.
@hGen CSV function in d3 is asynchronous, commentlist[1] is undefined, probably the csv file is not loaded yet and data in csv file is yet to be assigned to commentlist variable.
1

I think the reason you got { console.log(commentlist) } undefined is that inside d3.csv the callback function is parsed and called last by browser, and { console.log(commentlist) } is called earlier even though it appears at the bottom of your code. So the moment when { console.log(commentlist) } is called, { commentlist } is actually undefined (only declared). That being said, just try putting { console.log(commentlist) } inside the callback and it should do the job.

1 Comment

I had this problem, too. But outside d3.csv I need the "commentlist"-variable for other usage. Is there a possibility to run the d3.csv before another function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.