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

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.

主站蜘蛛池模板: 万安县| 小金县| 庆城县| 于都县| 娄烦县| 宜州市| 重庆市| 定南县| 广南县| 明水县| 内黄县| 平定县| 普宁市| 博野县| 思茅市| 镶黄旗| 龙海市| 桂平市| 若尔盖县| 五原县| 福建省| 禹州市| 沈丘县| 疏附县| 石首市| 上犹县| 南陵县| 岳阳市| 蓬溪县| 南华县| 贞丰县| 福贡县| 四会市| 舞阳县| 库伦旗| 南平市| 开原市| 民乐县| 遂溪县| 阳信县| 山阴县|