2013-03-03 92 views
5

我试图适应由麦克·博斯托克这个和弦图:旋转文本路径:文本

我想有文字标签向外旋转这样的弦图:

http://bost.ocks.org/mike/uberdata/

enter image description here

这里有一个例子

http://bl.ocks.org/mbostock/910126

enter image description here

然而,变换使用SVG完成:文字:

g.append("svg:text") 
     .each(function(d) { d.angle = (d.startAngle + d.endAngle)/2; }) 
     .attr("dy", ".35em") 
     .attr("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; }) 
     .attr("transform", function(d) { 
     return "rotate(" + (d.angle * 180/Math.PI - 90) + ")" 
      + "translate(" + (r0 + 26) + ")" 
      + (d.angle > Math.PI ? "rotate(180)" : ""); 
     }) 
     .text(function(d) { return nameByIndex[d.index]; }); 

的一个我试图适应使用“文本”和“textPath”,我似乎并没有能够简单地添加变换/旋转属性。加入这一行

.attr("transform",function(d,i){return "rotate(90)";}) 

下面的代码什么也不做:

// Add a text label. 
     var groupText = group.append("text") 
      .attr("x", 6) 
      .attr("dy", 15); 

      groupText.append("textPath") 
      .attr("xlink:href", function(d, i) { return "#group" + i; }) 

      .text(function(d, i) { return cities[i].country; }); 

任何想法如何,我可以旋转向外所以小弦组文本标签可以在不被揉成或显示的文本(如原始解决方案)完全关闭?

回答

5

我认为你正在寻找this example by Mike Bostock

至于修改初始弦代码,以下变化应该做你想要什么:

// Add a text label. 
// var groupText = group.append("text") 
//  .attr("x", 6) 
//  .attr("dy", 15); 

//groupText.append("textPath") 
// .attr("xlink:href", function(d, i) { return "#group" + i; }) 
// .text(function(d, i) { return cities[i].name; }); 

// Remove the labels that don't fit. :(
//groupText.filter(function(d, i) { return groupPath[0][i].getTotalLength()/2 - 16 < this.getComputedTextLength(); }) 
// .remove(); 

group.append("text") 
    .each(function(d) { d.angle = (d.startAngle + d.endAngle)/2; }) 
    .attr("dy", ".35em") 
    .attr("transform", function(d) { 
    return "rotate(" + (d.angle * 180/Math.PI - 90) + ")" 
     + "translate(" + (innerRadius + 26) + ")" 
     + (d.angle > Math.PI ? "rotate(180)" : ""); 
    }) 
    .style("text-anchor", function(d) { return d.angle > Math.PI ? "end" : null; }) 
    .text(function(d, i) { return cities[i].name; });