使用Force Layout从两个不同的csv文件中读取节点和边
reading nodes and edges from two distinct csv files using Force Layout
我在尝试使用力布局显示图形时遇到问题。我使用了两个csv文件,一个用于顶点,另一个用于边。我不确定,但我认为由于d3.csv方法是异步的,并且我正在使用其中两个方法,我需要将一个插入到另一个中以避免"并发"问题(最初我尝试分别调用d3.csv两次,但遇到了问题)。
我的csv的结构如下:
对于边缘:
source,target
2,3
2,5
2,6
3,4
对于节点:
index,name
1,feature1
2,feature2
3,feature3
我最初的尝试是:
// create the force layout
var force = d3.layout.force()
.charge(-120)
.linkDistance(30)
.size([width, height]);
var node, linked;
// we read the edges
d3.csv("csvEdges.csv", function(error, data) {
edg = data;
// we read the vertices
d3.csv("csvVertices.csv", function(error2, data2) {
ver = data2;
force.nodes(data2)
.links(data)
.start();
node = svg.selectAll(".node")
.data(data2)
.enter()
.append("circle")
.attr("class", "node")
.attr("r", 12)
.style("fill", function(d) {
return color(Math.round(Math.random()*18));
})
.call(force.drag);
linked = svg.selectAll(".link")
.data(data)
.enter()
.append("line")
.attr("class", "link");
force.on("tick", function() {
linked.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; });
node.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
});
但我得到了:
"TypeError: Cannot call method 'push' of undefined."
我不知道出了什么问题;我认为这与链接的推送有关。我读到这个问题可能与d3如何将链接与节点的对象匹配有关(在这种情况下,我的节点有两个字段)。所以我尝试了以下方法(我在这里的另一个问题中看到了这一点):
// we read the edges
d3.csv("csvEdges.csv", function(error, data) {
edg = data;
// we read the vertices
d3.csv("csvVertices.csv", function(error2, data2) {
ver = data2;
force.nodes(data2)
.start();
//.links(data);
var findNode = function(id) {
for (var i in force.nodes()) {
if (force.nodes()[i]["index"] == id) return force.nodes()[i]
};
return null;
};
var pushLink = function (link) {
//console.log(link)
if(findNode(link.source)!= null && findNode(link.target)!= null) {
force.links().push ({
"source":findNode(link.source),
"target":findNode(link.target)
})
}
};
data.forEach(pushLink);
[...]
但在这种情况下,我得到了一堆:
Error: Invalid value for <circle> attribute cy="NaN"
我不知道这个案子出了什么问题!
我看到的方法是使用queue.js对函数进行排队,如Elijah Meeks的《D3.js在行动》一书中所述。Manning网站上有第6章的示例代码,见清单6.7。(买这本书,它很好)以下是稍微适应您的用例的基本结构:
<script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>
queue()
.defer(d3.csv, "csvVertices.csv")
.defer(d3.csv, "csvEdges.csv")
.await(function(error, file1, file2) {createForceLayout(file1, file2);});
function createForceLayout(nodes, edges) {
var nodeHash = {};
for (x in nodes) {
nodeHash[nodes[x].id] = nodes[x];
}
for (x in edges) {
edges[x].weight = 1; //you have no weight
edges[x].source = nodeHash[edges[x].source];
edges[x].target = nodeHash[edges[x].target];
}
force = d3.layout.force()
.charge(-1000)
.gravity(.3)
.linkDistance(50)
.size([500,500])
.nodes(nodes)
.links(edges);
//etc.
相关文章:
- 通过CSV文件上载更新数据库表
- 在.csv文件中写入学位符号
- PERL-下载CSV文件不完整
- 将CSV文件从URL导入Node.js
- Get方法获取csv文件的内容
- 将JSON转换为放入Sdcard中的CSV文件
- 如何使用d3.js从csv文件中绘制areaplot?请任何人分享代码
- 正在以javascript读取本地csv文件
- 处理字段中带有换行符的csv文件-node.js
- 如何将CSV数据从变量下载到CSV文件
- Safari 5.1.7下载文件名未知的csv文件
- 是否可以从Html本地存储输出到.csv或类似文件
- CSV、PDF、Excel文件在dataTables的导出扩展中作为blob下载
- 如何读取csv文件并将其转换为javascript中的对象
- 尝试从csv文件中检索单个值时出现问题
- 如何用javascript下载巨大的CSV文件
- 在将csv文件中的数据分配给数组变量时增强了d3代码
- 无法使用Behat Mink扩展框架在输入字段中附加文件(csv文件)
- 如何读取文件.csv非常大的FileReader API javascript, html/jquery只有
- 我如何创建和写入一个新文件(csv),并让浏览器用javascript或D3.js下载它