2013-03-03 5 views
5

staram się dostosować ten schemat akordów Mike Bostock:obrotowe ścieżki tekstowe w d3.js schemat akordów bez zwykłej SVG: text

chcę mieć etykiety tekstowe obracające się na zewnątrz jak ten schemat akordów:

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

enter image description here

jest przykładem tutaj

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

enter image description here

Jednak transformacja odbywa się za pomocą SVG: text:

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]; }); 

Ten Staram się przystosować używa "tekst" i "textPath" i nie wydaje aby móc po prostu dodać atrybut transformacji/rotacji. Dodanie tej linii

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

na poniższym kodzie nic nie robi:

// 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; }); 

Jakieś pomysły jak mogę obrócić tekst na zewnątrz tak, mniejszych grup akord etykiety tekstowe mogą być wyświetlane bez wiązek lub (jak to oryginalne rozwiązanie) całkowicie wyłączone?

Odpowiedz

5

myślę szukasz this example by Mike Bostock

Jeśli chodzi o modyfikację początkowego kodu akord następujące zmiany powinny robić to, co chcesz:

// 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; });