官术网_书友最值得收藏!

Adding group elements

At this point, we've got the data into a form that we can use. The next step is to create a number of containers, to which we can add the rect that represents the number of times the name was used, and we'll also add a text element there that displays the name:

var bars = chart.selectAll("g").data(both) 
.enter()
.append('g')
.attr('transform', function (d, i) {
var yPos = ((barWidth + barMargin) * i);
return 'translate( 0 ' + yPos + ')';
});

Here, we bind the both array to a number of g elements. We only need to use the enter function here, since we know that there aren't any g elements that can be reused. We position each g element using the translate operation of the transform attribute. We translate the g element along its y-axis based on the barWidth, the barMargin, and the position of the data element (d) in our data (both) array. If you use the Chrome developer tools, you'll see something like this, which nicely shows the calculated translate values:

All that is left to do now, is draw the rectangles and add the names.

主站蜘蛛池模板: 克什克腾旗| 无棣县| 丰宁| 静安区| 土默特左旗| 黔西县| 芜湖市| 贡山| 惠州市| 沂水县| 忻城县| 溧水县| 乐至县| 兴安盟| 海南省| 克拉玛依市| 建瓯市| 分宜县| 四川省| 泽普县| 龙井市| 荣昌县| 屏山县| 嵩明县| 和田县| 越西县| 防城港市| 兴山县| 新兴县| 健康| 库伦旗| 河北省| 南开区| 会理县| 武宣县| 桐城市| 临漳县| 金川县| 慈溪市| 海淀区| 札达县|