使用 d3.js 从 .csv 导入数据 [英] Importing data from .csv using d3.js

查看:40
本文介绍了使用 d3.js 从 .csv 导入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 d3.js 从 .csv 导入一些数据.我在这样做时遇到了麻烦,想知道是否有人可以伸出援手.我的 .csv 文件格式如下:

I am trying to import some data from a .csv using d3.js. I am having trouble doing this, and was wondering if anyone could lend a hand. My .csv file is formatted like so:

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

我正在尝试使用以下代码加载数据:

I am trying to load the data using the following code:

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

但是,我只是在控制台中得到一个空的 [].谁能指出我的错误?

However, I just get an empty [] in the console. Can anyone point out my mistake?

推荐答案

改为:

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

返回数据后,您需要检查回调中的数据集.

You need to inspect dataset inside the callback, once your data is returned.

这篇关于使用 d3.js 从 .csv 导入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆