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

Add the lines from the donut to the text

For the lines, we once again follow the same principle. We add the lines and use an attrTween function to determine how to draw the lines at each step of the animation:

// add the lines which point to the labels 
var lineElements = pieContainer.selectAll(".lines").data(arcs);
lineElements.enter()
.append("path")
.attr("class", "lines")
.merge(lineElements)
.transition()
.ease(d3.easeCircle)
.duration(2000)
.attrTween("d", tweenLines)

The interesting stuff happens in the tweenLines function:

function tweenLines(d) { 
var interpolator = getArcInterpolator(this, d);
var lineGen = d3.line();
return function (t) {
var dInt = interpolator(t);
var start = arc.centroid(dInt);
var xy = labelsArc.centroid(dInt);
var textXy = [xy[0],xy[1]];
// Change the final line a little bit to
// make sure we can tween nicely, and we have
// a little bit of extra space
textXy[0]= textXy[0] * 1.15
return lineGen([start,xy,textXy]);
}
}

What we do here is we use the d3.line() function (which we'll explore in more depth in the following example) to draw a line from the center of the donut (arc.centroid) to the position of the text (labelsArc.centroid). To make it look better, we add an additional corner just before the text. The result of this looks like this:

The final step we're going to take is add some interactivity to the donut.

主站蜘蛛池模板: 喀什市| 堆龙德庆县| 内江市| 府谷县| 兴化市| 巴楚县| 抚宁县| 天长市| 隆昌县| 宣化县| 嘉禾县| 边坝县| 石楼县| 台湾省| 江山市| 龙门县| 乌什县| 奇台县| 体育| 瑞安市| 确山县| 鹿邑县| 天全县| 中山市| 绵竹市| 和林格尔县| 扎兰屯市| 朝阳区| 内黄县| 松滋市| 无棣县| 诸暨市| 农安县| 娄底市| 门头沟区| 青川县| 黑龙江省| 乐东| 施秉县| 依安县| 钦州市|