【问题标题】:Load one column of csv into javascript variable将一列csv加载到javascript变量中
【发布时间】:2013-01-22 18:55:24
【问题描述】:

我有一个格式为 String1、String2、Int1、Int2 的 CSV 文件。我想使用 D3 将每一行放在一个散点图上,并将整数作为轴。所以我想出了如何使用 D3 将 CSV 数据加载到表中,但后来我意识到,为什么不直接将 CSV 列加载到变量中呢?

那么,有谁知道如何使用 D3 或 jQuery 一次加载 CSV 的单列?或者更好的是,有没有更好的解决方案我应该考虑?

【问题讨论】:

标签: jquery csv d3.js


【解决方案1】:

首先,感谢 thecodingtutorials.blogspot.com 上的 Andrew Davis,他对使用 D3 尤其是使用 D3 加载 CSV 文件非常有帮助。

D3教程:http://www.thecodingtutorials.blogspot.com/2012/07/introduction-to-d3.html

正在加载 CSV:http://thecodingtutorials.blogspot.com/2012/07/using-csv-and-text-files-with-d3.html

多列 CSV:http://thecodingtutorials.blogspot.com/2012/08/using-multi-column-data-with-d3-part-1.html

无论如何,简短的回答是 CSV 列可以被视为对象中的属性。

以前在哪里

var vis = d3.select("#tryHere").append("svg:svg").attr("width", w).attr("height", h);
vis.selectall("circle")
   .data(data).enter().append("svg:circle")
   .attr("cx", function(d) { return x(d.x) })
   .attr("cy", function(d) { return y(d.y) })
   ....

我需要将绘图代码封装在

 d3.csv("MyFile.csv", function(rawData) {

并将最后两行替换为:

 .attr("cx", function(d) {return x(d["XColumnName"] )}
 .attr("cy", function(d) {return y(d["YColumnName"] )}
 ....

【讨论】:

    猜你喜欢
    • 2017-07-13
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多