我正在可视化链接列表。只是为了好玩和学习 d3.js。我能够为圆圈(代表一个节点)创建组元素。但我不明白为什么在那之后没有添加新的组元素。我希望这个新组包含线/箭头(.next 参考表示)。我通过 CDT 调试了代码正在执行但没有添加到 DOM 中。
var g = svg.selectAll("g")
.data(dataArr)
.enter().append("g")
.attr("transform", function() {
if (addMethod === "headAdd") {
return "translate(" + 50 + " ," + y + ")";
} else {
return "translate(" + x + " ," + y + ")";
}
})
.attr("class", "nodes")
.attr("id", function(d) { return d.getData(); });
// Create a circle representing the node.
g.append("circle")
.attr("r", 40)
.style("fill", "#7DC24B")
.style("stroke", "#122A0A")
.style("stroke-width", 5)
.style("opacity", 0)
.style("stroke-opacity", 0.8)
.transition().duration(1000).style("opacity", 1);
// Add data to the node.
g.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.style("fill", "#ffffff")
.text(function(d) { return d.getData(); });
// Create the "next" arrow.
// l is the line group that will enclose the line
// and the text.
// This doesn't seem to be working from here below.
var l = svg.selectAll("g")
.data(["1"])
.enter().append("g")
.attr("class", "lines")
.attr("transform", "translate(" + (x + 40) + ", " + y + ")");
l.append("line")
.attr("x1", x + 40)
.attr("y1", y)
.attr("x2", x + 100)
.attr("y2", y)
.style("stroke", "#8EBD99")
.style("stroke-width", 30);