D3.js - How can I add a new line to the text in this Collapsible Tree?

前端 未结 2 1885
长情又很酷
长情又很酷 2021-01-18 11:19

I have a Collapsible Tree built by D3. Here is the JSFIDDLE:http://jsfiddle.net/mEyQW/57/

As you can see, when the nodes fully expended, the text were all overlapped

2条回答
  •  暖寄归人
    2021-01-18 11:34

    I had a hard time to split a line on comma and place the text pieces in a list format. The following adaptation worked.

     function splitoncomma(text) {
       var lines=text.split(",") //splits text on comma
       return lines
    }
    
       label.append("div")
              .attr("class", "class_name")
              .each(function (d) {
                if (textValue(d)!==undefined) {
                  var lines = splitoncomma(textValue(d))
                  for (var i = 0; i < lines.length; i++) {
                    d3.select(this)
                    .append("text")
                    .append("tspan")
                    .style("float","left")// the float:left avoids default inline positioning
                    .text(lines[i])
           }
        }
    

    The style float left is required to avoid default inline placement.

提交回复
热议问题