首先:请不要将此问题标记为已经回答,我知道在d3中设置类存在很多问题,但我以相同的方式进行了操作,但无济于事。
这是我的代码:
const node = svg.append("g")
.attr("stroke", "#fff")
.attr("stroke-width", 3)
.selectAll("circle")
.data(nodes)
.attr("class", function (d) {
console.log("I'm in creating classes for nodes")
for (var i = 0; i < links.length; i++) {
for (var j = 0; j < nodes.length; j++) {
if (links[i].relation_type === "SHAREHOLDER") {
console.log("found shareholder in links")
if (links[i].source === nodes[j].id || links[i].target === nodes[j].id) {
console.log("found shareholder in nodes" + i)
return "shareholder"
}
}
}
}
}
})
而且我什至没有让控制台记录我在类函数中编写的任何代码。所以对我来说,他似乎并没有参加function(d) { for ....}
似乎您正在向圈子中添加数据,但是我们需要选择并创建一些“新”圈子,因为它们尚不存在。在现有的“数据”调用之后添加以下几行应该会有所帮助。
.data(nodes)
.enter()
.append('circle')
您可以在d3中找到有关输入/更新模式的更多信息:https://www.d3indepth.com/enterexit/