Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3 csv readin to objects in array

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.

like image 773
hGen Avatar asked Nov 01 '22 20:11

hGen


1 Answers

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.

like image 172
saikiran.vsk Avatar answered Nov 09 '22 05:11

saikiran.vsk