Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotating text paths in d3.js chord diagram without usual svg:text

I am trying to adapt this Chord diagram by Mike Bostock:

I want to have the text labels rotating outwards like this chord diagram:

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

enter image description here

There is an example here

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

enter image description here

However, the transformation is done using the 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]; });

The one I am trying to adapt uses "text" and "textPath" and I don't seem to be able to simply add a transform/rotate attribute. Adding this line

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

to the code below does nothing:

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

Any ideas how I can rotate the text outwards so smaller chord groups text labels can be displayed without being bunched up or (as is the orginal solution) turned off altogether?

like image 893
Jon Scrut Avatar asked Mar 03 '13 21:03

Jon Scrut


1 Answers

I think you are looking for this example by Mike Bostock

As far as modifying the initial chord code, the following change should do what you want:

// 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; });
like image 50
zeFrenchy Avatar answered Oct 27 '22 03:10

zeFrenchy