无法在D3.js代码中在服务器上加载.csv文件 [英] Can't load .csv file on server in D3.js code

查看:292
本文介绍了无法在D3.js代码中在服务器上加载.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,加载.csv文件的以下代码无效:

 < script type =text / javascript > 
var final_news_events = [];
d3.csv(final_news_events.csv,function(data,error){
//你的函数就在这里
console.log(data =,data) ;
final_news_events = data;
});
< / script>

我有上面的代码托管在github一行104在文件 new_event_list.html



我也推送这段代码到heroku,并在这里有一个直播网站: https://neweventdetection.herokuapp.com/



当您打开网站的控制台时,它会显示 data = null



从文件new_event_list.html第108行的 console.log(data =,data);


< c> b
$ b

交换它们,事情会更好:

  d3.csv(final_news_events .csv,function(error,data){

但是请记住,加载仍然是异步操作。因此,不要将数据分配给更全局范围的变量( final_news_events ),对d3执行所有的样本:对回调函数内的数据进行操作: https://github.com/mbostock/d3/wiki/CSV


So the following code for loading the .csv file is not working:

<script type="text/javascript">
  var final_news_events = [];
  d3.csv("final_news_events.csv", function(data, error) {
    // You're function goes in here.
    console.log("data = ", data);
    final_news_events = data;
  });
</script>

I have the above code hosted on github one line 104 in file new_event_list.html

I also pushed this code to heroku and have a live website here: https://neweventdetection.herokuapp.com/

When you open the console for the website it shows data = null

From the line of code console.log("data = ", data); in the file new_event_list.html line 108

解决方案

You have the arguments in the wrong order in function(data, error).

Swap them and things will work a lot better:

  d3.csv("final_news_events.csv", function(error, data) {

But keep in mind that the loading is still an asynchronous operation. So instead of assigning the data to a more globally scoped variable (final_news_events), do what all the samples for d3 do: act on the data inside the callback function: https://github.com/mbostock/d3/wiki/CSV

这篇关于无法在D3.js代码中在服务器上加载.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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