简体   繁体   中英

How to move labels to outside pie chart in D3

I'm trying to recreate this in D3 Tableau示例

I've got my D3 code here: http://codepen.io/jpezninjo/pen/XpoVwQ

I can't figure out how to move labels to outside my pie chart. I know it's this line

.attr("transform", function(d) {
      return "translate(" + labelArc.centroid(d) + ")"; })

but I'm having a hard time looking for information about centroid. I'm guessing it's taking the center between labelArc's inner and outer radius, but I tried messing with that and got no difference.

Try this

.attr("transform", function(d) {  
    var c = labelArc.centroid(d);
    return "translate(" + c[0]*1.2 +"," + c[1]*1.2 + ")";
 })

You can play with 1.2 which allows you to position the labels outside the pie chart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM