csv to array in d3.js

2019-01-03 04:29发布

I am using this to parse a csv file and create an array data as specified in d3 docs:

d3.csv("afile.csv", function(data) {
    data.forEach(function(d) {
    d.date = formatDate.parse(d.date);
    d.price = +d.price;
});

However, if after this method I try to call data[0] it says it is undefined. Is this because data is a reference and once d3.csv() is out of scope is destroyed? If this is the case how can I overcome this. I need to reference data out of d3.csv()

标签: csv d3.js
4条回答
萌系小妹纸
2楼-- · 2019-01-03 05:02

You could declare a variable outside the callback function and then assign it with the values from the csv:

var csv_data;
d3.csv("afile.csv", function(data) {
  data.forEach(function(d) {
    d.date = formatDate.parse(d.date);
    d.price = +d.price;
  csv_data = data;
});

and then use csv_data outside the callback

查看更多
我命由我不由天
3楼-- · 2019-01-03 05:11

I think your problem is timing because it is an async call. So, load the data as you have but call the function within the d3 code (where Mike has 'doSomethingWithRows()'). Don't follow your d3 code with any more processing (where Mike has 'second()'), move that code into the 'doSomethingWithRows()' function. It will have the data available and away you go...

查看更多
霸刀☆藐视天下
4楼-- · 2019-01-03 05:11

I think the problem is already solved but I faced a similar problem and the above discussion wasn't as helpful. So posting how I figured a way out of this problem: Here, the reason why data[0] is undefined is probably because the data itself is not read by the browser. This failure to read is likely due to directly loading the data (csv) file, i.e., using the following command d3.csv("myCSVfile.csv",....). This approach may not work because web applications normally require loading files from web servers (not sure why this is the case). So a local web server needs to be put in place. Use this forum to learn how: How do I setup a local HTTP server using Python. The updated code, if you use Python 3 for creating a local web server will be: d3.csv("http://localhost:8000/myCSVfile.csv",.....).

查看更多
Anthone
5楼-- · 2019-01-03 05:14

d3.csv is an asynchronous method. This means that code inside the callback function is run when the data is loaded, but code after and outside the callback function will be run immediately after the request is made, when the data is not yet available. In other words:

first();
d3.csv("path/to/file.csv", function(rows) {
  third();
});
second();

If you want to use the data that is loaded by d3.csv, you either need to put that code inside the callback function (where third is, above):

d3.csv("path/to/file.csv", function(rows) {
  doSomethingWithRows(rows);
});

function doSomethingWithRows(rows) {
  // do something with rows
}

Or, you might save it as a global variable on the window that you can then refer to later:

var rows;

d3.csv("path/to/file.csv", function(loadedRows) {
  rows = loadedRows;
  doSomethingWithRows();
});

function doSomethingWithRows() {
  // do something with rows
}

If you want, you can also assign the loaded data explicitly to the window object, rather than declaring a variable and then managing two different names:

d3.csv("path/to/file.csv", function(rows) {
  window.rows = rows;
  doSomethingWithRows();
});

function doSomethingWithRows() {
  // do something with rows
}
查看更多
登录 后发表回答