0

私はこのコードを持っています

.text(function(d){return d.name});

今、d.nameの長さを確認したいのですが、10を超えると2行に分かれます。このコードを書きましたが、機能しません

    var nodeEnter = node.enter().append("g")
      .attr("class", "node")
      .attr("transform", function(d) { return "translate(" + source.x0 + "," + source.y0 + ")"; })
      .on("click", click);

  nodeEnter.append("circle")
      .attr("r", 1e-6)
      .style("fill", function(d) { return d._children ? "lightsteelblue" : "#fff"; });
    var name1="asdsdadasdasdas";
  nodeEnter.append("text")
      .attr("x", function(d) { return d.children || d._children ? -40 : -25; })
      .attr("dy",function(d){return d.children || d._children ? "-2em" : "2em"})
      .attr("text-anchor", "end")
      .attr("height",50)
      .attr("width",50)
      .text(function(d){
          var str=d.name;
          if(str.length>10){
            var txt1=str.slice(0,10);
            txt1+="<br/>";
            txt2=str.slice(10,str.length);
            return txt1+txt2;
          }else{
          return d.name;
          }
      })
      .attr("transform",function(d){
          return "rotate(45)";
          })
      .style("fill-opacity", 1e-6);
4

2 に答える 2

0

elseif ステートメントで欠落しています。

$(document).ready(function () {
    var str = 'somelongtesxtthat';
    if (str.length > 10) {
        var txt1 = str.slice(0, 10);
        txt1 += "<br/>";
        var txt2 = str.slice(10, str.length);
        alert(txt1 + txt2);
    } else {
        alert(str);
    }
});
于 2013-05-01T06:55:22.347 に答える
0

htmlの代わりに選択方法を使用してくださいtext。そう:

selection.html(function(d){
  var str=d.name;
  if(str.length>10){
        var txt1=str.slice(0,10);
    txt1+="<br/>";
    txt2=str.slice(10,str.length);
    return txt1+txt2;
   }
  return d.name;
});
于 2013-05-01T06:53:25.267 に答える