无法从d3中的csv文件导入数据 [英] Cannot import data from csv file in d3

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

问题描述

我只是学习d3,我正在尝试从CSV文件导入数据,但我不断收到错误XMLHttpRequest无法加载文件:///Users/Laura/Desktop/SampleECG.csv。请求只支持HTTP。我搜索了如何解决这个错误,并运行在本地Web服务器上,但我还没有找到一个解决方案适用于d3.v2.js.以下是代码示例:

I'm just learning d3, and I'm attempting to import data from a CSV file, but I keep getting the error "XMLHttpRequest cannot load file:///Users/Laura/Desktop/SampleECG.csv. Cross origin requests are only supported for HTTP. ". I've searched for how to fix this error and have ran it on a local web server, but I haven't found a solution that works for d3.v2.js. Here's a sample of the code:

var Time = []
    ECG1 = []

d3.csv("/Desktop/d3Project/Sample.csv", function(data) 
      {
      Time = data.map(function(d) {return [+d["Time"]];});
      ECG1 = data.map(function(d) {return [+d["ECG1"]];});
      console.log(Time)
      console.log(ECG1)
      });

任何帮助将非常感激。

推荐答案

这也让我困惑我(我也是一个d3初学者)。

This confused me too (I am also a d3 beginner).

因此,由于某些原因,网络浏览器对您加载本地数据不满意,这可能是出于安全原因或其他原因。无论如何,要解决这个问题,你必须运行本地Web服务器。

So, for some reason, web browsers are not happy about you loading local data, probably for security reasons or something. Anyways, to get around this, you have to run a local web server. This is easy.

在您的终端中, cd 到您网站的文档根目录(感谢@daixtr) ,type:

In your terminal, after cd-ing to your website's document root (thanks @daixtr), type:

python -m SimpleHTTPServer 8888 &

好了,现在只要终端窗口打开并运行,您的本地8888 Web服务器将运行。

Okay, now as long as that terminal window is open and running, your local 8888 web server will be running.

所以在我的例子中,最初我工作的网页叫做

So in my case, originally the web page I was working on was called

file://localhost/Users/hills/Desktop/website/visualizing-us-bls-data-inflation-and-prices.html

当我用chrome打开它。要在我的本地网络服务器上打开我的网页,只需输入(进入Chrome搜索栏):

When I opened it in chrome. To open up my page on my local web server, I just typed (into the chrome search bar):

http://localhost:8888/Desktop/website/visualizing-us-bls-data-inflation-and-prices.html

现在,读取CSV应该工作。很奇怪,我知道。

Now, reading in CSVs should work. Weird, I know.

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

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