简体   繁体   English

在d3 v5的npm版本中使用CSV文件

[英]Using csv file in npm version of d3 v5

I am working on a react project and using d3. 我正在做一个反应项目,并使用d3。 I have used npm to install d3 module in my project. 我已经使用npm在我的项目中安装d3模块。 I am having version 5.9.2 of d3 currently in my project. 我的项目中目前拥有d3的5.9.2版本。 I have a csv file which I want to use in the d3. 我有一个要在d3中使用的csv文件。 I have tried searching a way to use it but couldn't get even one resource for the same. 我尝试过寻找一种使用它的方法,但是却无法获得相同的资源。 Every post is using the csv file like this: 每个帖子都使用csv文件,如下所示:

d3.csv("sp500.csv", type, function(error, data) {
    if (error) throw error;

    //do something here
}

But I don't have any module named d3.csv in my installed modules. 但是我安装的模块中没有任何名为d3.csv的模块。 All I have is d3-dsv 我只有d3-dsv

Currently I am trying to use it in this way 目前,我正在尝试以这种方式使用它

import {dsvFormat, csvParse, csvParseRows, csvFormat, csvFormatBody, csvFormatRows} from 'd3-dsv'

csvFormat("sp500.csv", this.type.bind(this), function(error, data) {

            console.log("data is")

            //do something here
}

But I have no luck here. 但是我在这里没有运气。 Seems like csvFormat is not the equivalent fuction of d3.csv. 似乎csvFormat不是d3.csv的等效功能。 How can I use csv file? 如何使用csv文件?

Your d3.csv syntax is from old versions if D3. 如果D3,则您的d3.csv语法来自旧版本。 In latest versions D3 uses fetch for downloading resources and d3.csv (and other similar methods like d3.json and d3.text ) syntax is changed to be similar to fetch syntax: 在最新版本中,D3使用fetch来下载资源,并且d3.csv (以及d3.jsond3.text等其他类似方法)的语法已更改为类似于fetch语法:

 let url = "https://raw.githubusercontent.com/oncletom/coursera-ml/master/week-1/people-example.csv"; d3.csv(url).then(data => { console.log(data) }).catch(error => { console.error(error) }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script> 

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

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