Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

D3, SVG and textPath: How to move the text down?

Tags:

text

path

svg

d3.js

I've got a diagram with some text written along one of the paths using a textPath. However, my problem is: I need the text to be on the other side of the text path, ie, sitting below it.

Here's an example:

Currently Sits on the Outside

I need the text here to be within the solid blue area. Ie, so you can actually read it. The blue arc here being the textPath. In other words, I just want to move the text down about 20px.

Whats really confusing me, is that I can set an "x" property on the text and move it left and right , but I can't set a "y" property to move it up or down.

I can't figure it out. Can anyone help?

Here's my code

  var labels = svg.selectAll("text.label")
      .data(partition.nodes(data))
     .enter()
      .append("text")
      .attr("class", "label")
      .attr("x", 10)
      .attr("stroke","black")
      .style("background-color",'white')

    labels.append("textPath")
      .attr("xlink:href", function(d) { return '#' + d.name })
      .text(function(d) { return d.name.capitalize() })
like image 414
Cheyne Avatar asked Apr 26 '13 22:04

Cheyne


2 Answers

You can use the dy property to change the vertical alignment.

like image 172
Lars Kotthoff Avatar answered Nov 18 '22 00:11

Lars Kotthoff


Created JS fiddle example showing labels over links in D3 Forced layout chart

See working demo in JS Fiddle: http://jsfiddle.net/bc4um7pc/

var linktext = svg.append("svg:g").selectAll("g.linklabelholder").data(force.links());

linktext.enter().append("g").attr("class", "linklabelholder")
 .append("text")
 .attr("class", "linklabel")
 .style("font-size", "13px")
 .attr("dx", "30")
 .attr("dy", "-5")
 .attr("text-anchor", "start")
 .style("fill","#000")
 .append("textPath")
 .attr("xlink:href",function(d,i) { return "#linkId_" + i;})
 .text(function(d) { 
 return d.type; 
 });
like image 37
Sanjeev Avatar answered Nov 17 '22 23:11

Sanjeev