有没有办法在这里为 d3-cola 强制布局示例添加标签到组
我添加了工具提示:
var group = svg.selectAll(".group")
.data(groups)
.enter().append("rect")
.classed("group", true)
.attr("rx",20)
.attr("ry",20)
.style("fill", function (d) { return color(d.id); })
.call(cola.drag);
group.append("title")
.text(function (d) { return d.id; });
我尝试添加这部分:
var groupText = svg.selectAll(".group")
.append("text")
.attr("class", "link-label")
.attr("font-family", "Arial, Helvetica, sans-serif")
.attr("fill", "Black")
.style("font", "normal 9px Arial")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.text(function(d) {
return (d.id);
});
然后在刻度函数中:
groupText
.attr("x", function(d) {
return ((d.bounds.x + d.bounds.width())/2);
})
.attr("y", function(d) {
return ((d.bounds.y + d.bounds.height())/2);
});
但它没有出现。