使用d3.js的.csv导入数据(Importing data from .csv using d3

2019-08-18 20:24发布

我试图使用d3.js.的.csv导入一些数据 我有这样的麻烦,并想知道是否有人能伸出援助之手。 我的.csv文件的格式如下所示:

max_i,min_i,max_f,min_f
-122.1430195,-122.1430195,-122.415278,37.778643
-122.1430195,-122.1430195,-122.40815,37.785034
-122.4194155,-122.4194155,-122.4330827,37.7851673
-122.4194155,-122.4194155,-122.4330827,37.7851673
-118.4911912,-118.4911912,-118.3672828,33.9164666
-121.8374777,-121.8374777,-121.8498415,39.7241178
-115.172816,-115.172816,-115.078011,36.1586877
-82.5618186,-82.5618186,-79.2274115,37.9308282
-79.9958864,-79.9958864,-80.260396,40.1787544
-74.1243063,-74.1243063,-74.040948,40.729688
-106.609991,-106.609991,-106.015897,35.640949

我尝试使用下面的代码加载数据:

var dataset = []
d3.csv("data.csv", function(data) {
   dataset = data.map(function(d) { return [ +d["max_i"], +d["min_i"] ]; });
});
console.log(dataset)

不过,我只是得到一个空的[]在控制台中。 任何人都可以指出我的错误?

Answer 1:

将其更改为:

var dataset = []
d3.csv("data.csv", function(data) {
   dataset = data.map(function(d) { return [ +d["max_i"], +d["min_i"] ]; });
   console.log(dataset)
});

您需要检查回调内部数据集,一旦返回数据。



文章来源: Importing data from .csv using d3.js