我正在尝试使用D3使标签显示在朝阳图上。我尝试为每个切片添加标签,但这不起作用。我究竟做错了什么?我查了几个类似的问题,但无法确定。谢谢你的时间。
这是小提琴:https : //jsfiddle.net/4mx4jsdw/
这是我用来推入文本的代码,暂时将旋转度设置为30度:
var path = vis.data([json]).selectAll("path")
.data(nodes)
.enter()
.append("svg:path")
.attr("display", function(d) { return d.depth ? null : "none"; })
.attr("d", arc)
.attr("fill-rule", "evenodd")
.style("fill", function(d) { return colors(d.name); })
.style("opacity", 1)
.on("mouseover", mouseover)
.append("text")
.text(function(d) { console.log("Q", d.name); return d.name})
.attr("x", function(d) { return d.x; })
.attr("text-anchor", "middle")
// translate to the desired point and set the rotation
.attr("transform", function(d) {
if (d.depth > 0) {
return "translate(" + arc.centroid(d) + ")" +
"rotate(" + 30 + ")";
}
else {
return null;
}
})
因此,我阅读了一些d3文档,并意识到,如果我们使用的是圆形元素,则需要将svg和文本分别与“ g”标签绑定在一起。所以我只需要再添加一行代码:
.append("g")
进入此块:
var path = vis.data([json]).selectAll("path")
.data(nodes)
.enter()
.append("svg:path")
所以最后一块看起来像这样:
var path = vis.data([json]).selectAll("path")
.data(nodes)
.enter()
.append("g")
path.append("svg:path")
这是工作中的小提琴。我仍在尝试旋转文本以求完美,但是,嘿,行得通!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句