[英]D3 circle pack - Adding labels to nodes
While I've seen this question asked a few times, I'm having a bit trouble implementing. 虽然我已经问过几次这个问题,但是在执行时遇到了一些麻烦。 What I'd like to do is have the
label
attribute centered within each circle (as mentioned here ). 我希望做的是有
label
每个圆圈的中心属性(如提到这里 )。 I believe I'd be adding the text attribute to: 我相信我会将text属性添加到:
canvas.selectAll('circles')
.data(nodes)
.enter()
.append('svg:circle')
.attr('cx', function (d) {
return d.x;
})
.attr('cy', function (d) {
return d.y;
})
.attr('r', function (d) {
return d.r;
})
.attr('fill', function (d) {
return d.color;
});
But am confused on why the instructions they gave in the previous example I linked to doesn't work with the setup I currently have. 但是,对于为什么我在与之链接的上一个示例中给出的说明不适用于当前的设置,我感到困惑。 I believe it's the
pack
option that could be throwing me off (about the difference between the two), but any further examples would be a huge help. 我认为可能是
pack
选项让我失望(大约两者之间的差异),但是任何其他示例都将有很大帮助。 Thanks! 谢谢!
Thanks for the answers/suggestions, I updated the Codepen with my progress (as I needed two lines of data; should have clarified) which seems to be working well. 感谢您的回答/建议,我用自己的进度更新了Codepen (因为我需要两行数据;应该澄清),这似乎运行良好。 Now this is packing into a circle - at the end of the day, I'd love for this to be packed in the actual
#canvas
width/height (which is a rectangle). 现在将其包装成一个圆圈-归根结底,我希望将其包装在实际的
#canvas
宽度/高度(这是一个矩形)中。 I saw this treemap example - would that be what I'm going for here? 我看到了这个树形图示例-这就是我要去的地方吗?
Demo of what I have so far 我到目前为止的演示
Perhaps the confusion is that you can't add labels to the circle selection (because in SVG, a circle
element can't contain a text
element). 可能的困惑是,您不能在圆选择中添加标签(因为在SVG中,
circle
元素不能包含text
元素)。 You need to either make a g
element that contains both circle and text, or a separate selection for the text, eg: 您需要制作一个既包含圆圈又包含文本的
g
元素,或者单独选择文本,例如:
canvas.selectAll('text')
.data(nodes)
.enter()
.append('svg:text')
.attr('x', function (d) {
return d.x;
})
.attr('y', function (d) {
return d.y;
})
// sets the horizontal alignment to the middle
.attr('text-anchor', "middle")
// sets the vertical alignment to the middle of the line
.attr('dy', '0.35em')
.text(function(d) {
return d.label;
});
See the updated demo: http://codepen.io/anon/pen/djebv 请参阅更新的演示: http : //codepen.io/anon/pen/djebv
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.