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

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.

主站蜘蛛池模板: 吉首市| 工布江达县| 盐亭县| 夏河县| 图片| 临安市| 巩义市| 新干县| 阿拉善盟| 姜堰市| 兴海县| 渝北区| 乌兰县| 乌兰察布市| 梅州市| 远安县| 乐至县| 城步| 来安县| 开化县| 酉阳| 碌曲县| 通渭县| 黎城县| 宣威市| 道孚县| 汪清县| 香河县| 上犹县| 静乐县| 卢龙县| 香港| 崇左市| 登封市| 湟中县| 新绛县| 建始县| 鄂伦春自治旗| 乌兰察布市| 土默特右旗| 景泰县|