2017-07-28 75 views
0

我是d3js的新手,目前我正忙於一個項目上,這個項目我被困住了。這裏是我的語法將svg圈子更改爲圖像

var node = svg.selectAll(".node") 
        .data(data.nodes) 
        .enter() 
       //.append("circle") 
       //.attr("r",5) 
        .append("img") 
        .attr("class", function(d) { return "flag flag-" + d.code; }) 




    node.style('left', d => d.x + "px") 
      .style('top', d => d.y + "px"); 

    /* 
    node.attr("cx", function(d) { return d.x; }) 
     .attr("cy", function(d) { return d.y; }); 
*/ 

基本上,我想要做的是改變圈子(我已經評論它)圖像。你能幫我弄清楚我錯過了什麼嗎?

回答

0

您可以使用下面的代碼添加圖像和調整「X」和「Y」相應座標:

var node = svg.selectAll(".node") 
      .data(data.nodes) 
      .enter() 
      .append('image') 
      .attr('xlink:href', 'http://www.charbase.com/images/glyph/9899') 
      .attr('width', '50px') 
      .attr('height', '50px') 
      .attr("class", function(d) { return "flag flag-" + d.code; }); 

force.on("tick", function() { 
. 
. 
// node.style('left', d => d.x + "px") 
//  .style('top', d => d.y + "px"); 

node.attr("x", function(d) { return d.x - 25; }) 
    .attr("y", function(d) { return d.y - 25; }); 
. 
. 
}); 

我在https://jsfiddle.net/o81reo7a/

+1

與更新你的代碼,我只能追加一個單圖片。我的目標是追加與節點數量一樣多的圖像。 – Lucas

+0

@AvaLucas我已經更新了我的回覆。你必須包括force.on(「tick」)函數的變化,以及我在我的回覆中提到的內容。 –