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

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

问题描述

我刚刚在学习 d3,我正在尝试从 CSV 文件导入数据,但我不断收到错误消息XMLHttpRequest 无法加载文件:///Users/Laura/Desktop/SampleECG.csv.Cross origin请求仅支持 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 浏览器对您加载本地数据不满意,可能是出于安全原因或其他原因.无论如何,要解决这个问题,您必须运行本地 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-ing 到您网站的文档根目录后(感谢 @daixtr),输入:

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 打开它时.要在本地 Web 服务器上打开我的页面,我只需输入(进入 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天全站免登陆