1

I'm using D3 and SVG and am attempting to wrap some text by using attributes and chaining. This is my current code:

vis.append("svg:text")
    .attr("x", "0")
    .attr("y", "0")
    .attr("text-anchor", "middle")
    .append("svg.tspan")
    .attr("dy", ".35em")
    .text("Revenue Split")
    .text("for Current Month")
    .attr("transform", "translate(50,50)")
    .attr("class", "title");

The above is an attempt to translate this answer to using attr: https://stackoverflow.com/a/16701952/1179880

My attempt results in nothing showing on the screen.


update

With the help of the current two answers I've amended my code to the following:

vis.append("svg:text")
    .attr("x", "0")
    .attr("y", "0")
    .attr("class", "title")
    .attr("text-anchor", "middle")
    //.attr("transform", "translate(50,50)")
    .text("Revenue Split")
    .append("svg:tspan")
    .attr("dy", ".35em")
    .attr("text-anchor", "middle")
    .text("for Current Month")
    .attr("class", "title");

It appears to be heading in the right direction - although not quite as expected and on two distinct lines. The text appears as follows...

enter image description here

Community
  • 1
  • 1
whytheq
  • 34,466
  • 65
  • 172
  • 267

3 Answers3

2
  • You're using a . when you need a : as the d3 namespace separator for the tspan element (you get it right for the text element)
  • The transform presumably applies to the text element so needs to come before the tspan creation
  • The two text contents will overwrite, you can only have one per element.

Overall you probably want something like this...

vis.append("svg:text")
    .attr("x", "0")
    .attr("y", "0")
    .attr("text-anchor", "middle")
    .attr("transform", "translate(50,50)")
    .text("Revenue Split")
    .append("svg:tspan")
    .attr("dy", ".35em")
    .text("for Current Month")
    .attr("class", "title");
Robert Longson
  • 118,664
  • 26
  • 252
  • 242
1

You have to define the text for the text element and then define the text for the tspan element:

vis.append("text")
.attr("x", "0")
.attr("y", "0")
.attr("text-anchor", "middle")
.text("Revenue Split")
.append("tspan")
.attr("x", "0")
.attr("dy", ".35em")
.text("for Current Month")
.attr("class", "title");
Gerardo Furtado
  • 100,839
  • 9
  • 121
  • 171
  • this has helped a lot but still misbehaving - please see screenshot I've added to the original post – whytheq Oct 10 '16 at 13:26
  • If you want "for current month" to be under "revenue split", you'll have to set the `x`. value again, and probably increase that `dy`. I just edited the answer, please check it. – Gerardo Furtado Oct 10 '16 at 14:29
  • once this `".35em"` is adjusted to this `1.1em` things are happy. – whytheq Oct 10 '16 at 15:57
0

This code by Mark in the comments above worked:

vis.append("svg:text")
      .attr("x", "0")
      .attr("y", "0")
      .attr("class", "title")
      .attr("text-anchor", "middle")
      .text("Revenue Split")
      .append("svg:tspan")
      .attr("dy", "1.1em")
      .attr("x", 0)
      .attr("text-anchor", "middle")
      .text("for Current Month")
      .attr("class", "title");
whytheq
  • 34,466
  • 65
  • 172
  • 267