- Expert Data Visualization
- Jos Dirksen
- 211字
- 2021-07-09 18:22:47
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.
推薦閱讀
- C/C++算法從菜鳥到達人
- Mastering OpenCV 4
- 精通API架構(gòu):設(shè)計、運維與演進
- Object-Oriented JavaScript(Second Edition)
- Java持續(xù)交付
- Practical Game Design
- EPLAN實戰(zhàn)設(shè)計
- VMware虛擬化技術(shù)
- SQL Server 2016數(shù)據(jù)庫應用與開發(fā)
- Building Serverless Web Applications
- Django 3.0應用開發(fā)詳解
- Illustrator CC平面設(shè)計實戰(zhàn)從入門到精通(視頻自學全彩版)
- Python自然語言理解:自然語言理解系統(tǒng)開發(fā)與應用實戰(zhàn)
- 寫給大家看的Midjourney設(shè)計書
- Python+Office:輕松實現(xiàn)Python辦公自動化