如何用d3 v4读取CSV? [英] How to read in CSV with d3 v4?
问题描述
我只是在理解带有D3的CSV解析。我目前有:
d3.parse(data.csv,function(data){
salesData = data ;
});
但我一直收到错误:
< blockquote>
未捕获TypeError:d3.parse不是函数
这应该是什么样子?我只是有点困惑,我能找到的唯一例子就是这个。
我也尝试过这样的事情:
d3.dsv.parse (data.csv,函数(数据){
salesData = data;
});
并得到:
Uncaught TypeError:无法读取未定义的属性'parse'
为什么会发生这种情况?任何帮助都会有很大的帮助,谢谢!!
这里有一些误解:你很困惑 d3.csv
,这是一个请求,带有 d3.csvParse
,它解析一个字符串(并将D3 v3函数与D3 v4函数混合)。这是区别:
d3.csv(D3 v4)
d3.csv 函数,它以为参数(url [ [,row],callback])
:
返回CSV 文件<的新请求/ strong>在指定的 url 上使用默认的mime类型text / csv。 (强调我的)
所以,正如你所看到的,你使用 d3.csv
当您想要在给定网址上请求给定的CSV文件时。
例如,下面的代码段会在引号之间的网址上获取CSV,如下所示...
姓名,父母
等级2:A,顶级
顶级,null
A之子,等级2:A
A女儿,等级2:A
等级2:B,顶级
...并记录解析后的CSV文件,检查它:
d3.csv(https://gist.githubusercontent.com/d3noob/fa0f16e271cb191ae85f/raw/bf896176236341f56a55b36c8fc40e32c73051ad/treedata.csv,function(data){console.log(data) ;});
< script src =https: / /d3js.org/d3.v4.min.js\"></script>
d3.csvParse
另一方面, d3.csvParse (或D3 v3中的 d3.csv.parse
), arguments (string [,row])
:
解析指定的字符串,必须使用适当的分隔符以分隔符分隔的值格式,返回表示已解析行的对象数组。
所以,你当你想解析一个字符串时,使用 d3.csvParse
。
这是一个演示,假设你有这个字符串:
var data =foo,bar,baz \ nn42,33,42 \ nn12,76,54 \ n13,42,17\" ;
如果要解析它,你将使用 d3.csvParse
,而不是 d3.csv
:
var data =foo,bar,baz \ nn42,33,42 \ n12,76,54 \ nn13,42,17; var parsed = d3.csvParse(data ); console.log(已解析);
< script SRC = https://d3js.org/d3.v4.min.js >< /脚本>
I'm just having a little trouble understanding the documentation for CSV Parse with D3. I currently have:
d3.parse("data.csv",function(data){
salesData = data;
});
But I keep on getting the error:
Uncaught TypeError: d3.parse is not a function
What is this supposed to look like? I'm just a little confused, and the only examples that I could find was something like this.
I also tried something like:
d3.dsv.parse("data.csv",function(data){
salesData = data;
});
and got:
Uncaught TypeError: Cannot read property 'parse' of undefined
Why is this happening? Any help would be greatly appreaciated, thanks!!
There is some misunderstanding here: you're confusing d3.csv
, which is a request, with d3.csvParse
, which parses a string (and also mixing D3 v3 functions with D3 v4 functions). This is the difference:
d3.csv (D3 v4)
The d3.csv function, which takes as arguments (url[[, row], callback])
:
Returns a new request for the CSV file at the specified url with the default mime type text/csv. (emphasis mine)
So, as you can see, you use d3.csv
when you want to request a given CSV file at a given url.
For example, the snippet below gets the CSV at the url between quotes, which looks like this...
name, parent
Level 2: A, Top Level
Top Level, null
Son of A, Level 2: A
Daughter of A, Level 2: A
Level 2: B, Top Level
... and logs the parsed CSV file, check it:
d3.csv("https://gist.githubusercontent.com/d3noob/fa0f16e271cb191ae85f/raw/bf896176236341f56a55b36c8fc40e32c73051ad/treedata.csv", function(data){
console.log(data);
});
<script src="https://d3js.org/d3.v4.min.js"></script>
d3.csvParse
On the other hand, d3.csvParse (or d3.csv.parse
in D3 v3), which takes as arguments (string[, row])
:
Parses the specified string, which must be in the delimiter-separated values format with the appropriate delimiter, returning an array of objects representing the parsed rows.
So, you use d3.csvParse
when you want to parse a string.
Here is a demo, suppose you have this string:
var data = "foo,bar,baz\n42,33,42\n12,76,54\n13,42,17";
If you want to parse it, you'll use d3.csvParse
, not d3.csv
:
var data = "foo,bar,baz\n42,33,42\n12,76,54\n13,42,17";
var parsed = d3.csvParse(data);
console.log(parsed);
<script src="https://d3js.org/d3.v4.min.js"></script>
这篇关于如何用d3 v4读取CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!