2014-06-18 42 views
0

我有一个条形图和我在一些贴标(后已经有创建的酒吧)是这样的:D3选择在SVG对象的多个元素

svg.selectAll("text").data(labels).enter().append("text").text(function(d) { return d; }) 
.attr("text-anchor", "middle") 
.attr("x", parseFloat(d3.selectAll("#bar").attr("x")) + 
      parseFloat(d3.selectAll("#bar").attr("width"))/2) 
.attr("y", 90); 

}; 
//when I am creating the bars using selectAll("rect").... at the end of the attr() 
//function I have specified an id called bar so I can reference it later that way. 

但这仅选择第一并将所有标签放在彼此的顶部。为什么是这样的,如何选择svg中的所有矩形?

+0

你能否提供JS代码来绘制你的条形图以及你想添加为标签的文本到每一列? (如果我理解正确) – Fered

回答

0

#barid selector并且每个ID在文档中必须是唯一的或者您的文件无效。

如果你想要有多个称为bar的元素,你可以给他们一类吧,然后用class selector来代替。

如果你想选择所有<rect>元素,那么你可以使用type selector

+0

所以一旦我给他们一个类,我怎么能调用他们的.attr来获得矩形的x和宽度值? – user3281466