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

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.

主站蜘蛛池模板: 湖州市| 乌海市| 沂水县| 温州市| 华阴市| 佛坪县| 顺义区| 清原| 朝阳市| 即墨市| 上饶市| 老河口市| 台山市| 轮台县| 永济市| 固安县| 北辰区| 固原市| 遵义县| 东安县| 柳河县| 北川| 濮阳市| 阿拉善盟| 秀山| 茌平县| 九龙城区| 桐梓县| 仁化县| 长顺县| 高唐县| 车险| 黄陵县| 林口县| 天全县| 合作市| 扶沟县| 景洪市| 晋州市| 禹城市| 遂溪县|