无法在 D3.js 代码中加载服务器上的 .csv 文件
Can't load .csv file on server in D3.js code
因此,以下用于加载 .csv 文件的代码不起作用:
<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>
我将上述代码托管在文件 new_event_list.html
的 github 的第 104 行
我也将这段代码推送到 heroku 并在这里有一个实时网站:https://neweventdetection.herokuapp.com/
当您打开网站的控制台时,它显示 data = null
来自文件中的代码行console.log("data = ", data);
new_event_list.html第108行
您在 function(data, error)
中的参数顺序错误。
交换它们,事情会好很多:
d3.csv("final_news_events.csv", function(error, data) {
但请记住,加载仍然是一个异步操作。因此,与其将数据分配给更全局范围的变量 (final_news_events
),不如执行 d3 的所有样本所做的事情:对回调函数内的数据进行操作:https://github.com/mbostock/d3/wiki/CSV
因此,以下用于加载 .csv 文件的代码不起作用:
<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>
我将上述代码托管在文件 new_event_list.html
的 github 的第 104 行我也将这段代码推送到 heroku 并在这里有一个实时网站:https://neweventdetection.herokuapp.com/
当您打开网站的控制台时,它显示 data = null
来自文件中的代码行console.log("data = ", data);
new_event_list.html第108行
您在 function(data, error)
中的参数顺序错误。
交换它们,事情会好很多:
d3.csv("final_news_events.csv", function(error, data) {
但请记住,加载仍然是一个异步操作。因此,与其将数据分配给更全局范围的变量 (final_news_events
),不如执行 d3 的所有样本所做的事情:对回调函数内的数据进行操作:https://github.com/mbostock/d3/wiki/CSV