[英]Labels not displaying on d3 sunburst chart
I'm trying to make labels display on a sunburst chart using D3. 我正在尝试使用D3使标签显示在朝阳图上。 I tried adding labels for each slice, but it doesn't work. 我尝试为每个切片添加标签,但这不起作用。 What am I doing wrong? 我究竟做错了什么? I looked up several similar problems but I'm not able to pin it down. 我查了几个类似的问题,但无法确定。 Thanks for your time. 谢谢你的时间。
Here's the fiddle: https://jsfiddle.net/4mx4jsdw/ 这是小提琴: https : //jsfiddle.net/4mx4jsdw/
And here's the code I'm using to push in the text, temporarily I'm setting the rotation as 30 deg: 这是我用来推入文本的代码,暂时将旋转度设置为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;
}
})
So I read up some d3 documentation and realised that if we are using circle elements, we need to bind the svg and text separately with the "g" tag. 因此,我阅读了一些d3文档,并意识到,如果我们使用圆元素,则需要分别将svg和文本与“ g”标记绑定。 So I just need to add one more line of code: 所以我只需要再添加一行代码:
.append("g")
into this block: 进入此块:
var path = vis.data([json]).selectAll("path")
.data(nodes)
.enter()
.append("svg:path")
So the final block looks like this: 所以最后一块看起来像这样:
var path = vis.data([json]).selectAll("path")
.data(nodes)
.enter()
.append("g")
path.append("svg:path")
Here's the working fiddle . 这是工作中的小提琴 。 I'm still trying to rotate the text for perfection, but hey, it works! 我仍在尝试旋转文本以求完美,但是,嘿,行得通!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.