2014-07-26 7 views
1

d3.js에서 초보자이고 일부 텍스트를 추가하고 rect 내부를 rect로 추가하려고합니다. 나는 텍스트와 RECT의 SVG 내부 mainBox를 추가 할d3 SVG rect에 텍스트 추가

  if (!_svg) { 
      _svg = d3.select("#col1").append("svg") 
        .attr("height", _height - 2) 
        .attr("width", _width - 2); 
     } 

     _svg.append("text").attr({ 
      id: "leftDomainName", 
      x: d3.select("svg").attr("width")/14, 
      y: d3.select("svg").attr("height")/2, 
      class: "vertical-text" 
     }).text("Business Development"); 

     var g = _svg.append("g").attr("transform", function (d, i) { 
      return "translate(0,0)"; 
     }).attr("clip-path", "url(#body-clip)"); 


     var mainBox = g.append("rect").attr({ 
      id: "mainBox", 
      x: d3.select("svg").attr("width")/8, 
      y: 3, 
      width: _width - 60, 
      height: _height/3, 
      fill: "#D7FFEC" 
     }); 

     mainBox.append("text").text("sample!!!"); 
     mainBox.append("rect").attr({ 
      x: 50, 
      y: 50, 
      width: _width - 60, 
      height: _height/3, 
      fill: "red" 
     }); 

:

여기 내 코드입니다. 크롬 develper 도구에서 나는 자신이 추가되었습니다 볼 수 있지만

enter image description here

내 잘못 무엇 페이지

에 존재하지 않습니다? 내가 mainBox에 g을 추가하고 해당 g 내 요소를 추가

은 감사하지만

  var innerG = mainBox.append("g"); 

     innerG.append("text").text("sample!!!"); 
     innerG.append("rect").attr({ 
      x: 50, 
      y: 50, 
      width: _width - 60, 
      height: _height/3, 
      fill: "red" 
     }); 

enter image description here

답변

6

당신은 recttext를 추가 할 수 없습니다 작동하지 않았다 SVG의 요소.

g.append("text").text("sample!!!"); 

대신

mainBox.append("text").text("sample!!!"); 
+0

의 당신이 mainBox의 내부 의미 : 당신이 원하는 것을 달성하기 위해, 모두 textrect을 포함하는 g 요소를 추가? 나는 그것을했지만 작동하지 않았다. 내 새로운 코드를 qustion 섹션에 추가합니다. – farhad

+0

아,이 모든 것을 다른 'rect'요소에 추가 한 것처럼 보입니다. 같은 이유로 작동하지 않을 것입니다. 요소를 그룹화하려면'g' 요소를 사용하십시오. –

+0

귀하의 의견을 후, 그들을 그룹화했지만 작동하지 않았다. 질문 섹션에서 굵게 기울임 꼴 라인을 참조하십시오 – farhad