简体   繁体   English

如何使用 d3 v4 读取 CSV?

[英]How to read in CSV with d3 v4?

I'm just having a little trouble understanding the documentation for CSV Parse with D3.我只是在理解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未捕获的类型错误:d3.parse 不是函数

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 syntax with D3 v4 syntax).这里存在一些误解:您将请求d3.csvParse与解析字符串的d3.csv混淆了(并且还将 D3 v3 语法与 D3 v4 语法混合)。 This is the difference:这是区别:

d3.csv (D3 v4) d3.csv (D3 v4)

The d3.csv function, which takes as arguments (url[[, row], callback]) : d3.csv函数,它以(url[[, row], callback])作为参数:

Returns a new request for the CSV file at the specified url with the default mime type text/csv.返回对指定url处的 CSV文件的新请求,默认 mime 类型为 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.因此,正如您所见,当您想要在给定的 url 请求给定的 CSV 文件时,您可以使用d3.csv

For example, the snippet below gets the CSV at the url between quotes, which looks like this...例如,下面的代码片段在引号之间的 url 中获取 CSV,看起来像这样......

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: ...并记录已解析的 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.csv解析

On the other hand, d3.csvParse (or d3.csv.parse in D3 v3), which takes as arguments (string[, row]) :另一方面, d3.csvParse (或 D3 v3 中的d3.csv.parse )作为参数(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.所以,当你想解析一个字符串时,你可以使用d3.csvParse

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 :如果你想解析它,你将使用d3.csvParse ,而不是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>

You can get csv data into d3 like the following -您可以像下面这样将 csv 数据导入 d3 -

// get the data
d3.csv("data.csv", function(error, data) {
  if (error) throw error;
      console.log(data);
      //format data if required...
      //draw chart
});

I also could not get the d3.csv("csv_file.csv", function(data) { //modifying code } to work.我也无法让 d3.csv("csv_file.csv", function(data) { //modifying code } 工作。

A classmate recommended using the following, which has worked so far:一位同学推荐使用以下方法,目前有效:

d3.csv("data.csv").then(function(data){
//modifying code
}

As noted in the comments below, this is a fix if you're running v5 instead of v4.如以下评论所述,如果您运行的是 v5 而不是 v4,则这是一个修复。

Use d3.csv("data.csv", function(data){...}) to get CSV from url and parse, or use d3.csv.parse() to parse a CSV-formatted string.使用d3.csv("data.csv", function(data){...})从 url 获取 CSV 并解析,或使用d3.csv.parse()解析 CSV 格式的字符串。

Here is the code you can use to read csv file using d3.js这是您可以使用 d3.js 读取 csv 文件的代码

<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
 d3.csv("csv/cars.csv", function(data) {
 console.log(data[0]);
});
</script>

Note that, the csv file name is "cars.csv", and the file is saved in a folder called csv.请注意,csv 文件名为“cars.csv”,该文件保存在名为 csv 的文件夹中。

console.log(data[0])

will help you to see the data output in the browser debug window.将帮助您在浏览器调试窗口中查看数据输出。 Where, you can also find if there is any error as well.在哪里,你也可以找到是否有任何错误。

Inside csv function, if i give the location of the csv file, it is not able to find read it.在 csv 函数中,如果我给出 csv 文件的位置,它无法找到读取它。 It assumes http://localhost:3001/data.csv;它假设 http://localhost:3001/data.csv; instead of local directory address而不是本地目录地址

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM