2016-11-28 52 views
1

您可以在此問題中包含的jsFiddle演示中找到如下所示的內容,並且我在想如何將圖像分配到每個節點。D3圖:如何在節點內分配圖像

var graph = { 
    "nodes":[ 
     {"name":"1","rating":90,"id":2951}, 
    ] 
} 

我的jsfiddle演示在這個鏈接:http://jsfiddle.net/JSDavi/qvco2Ljy/

更新1:以上

node.append("circle") 
     .attr("r", function(d) { return d.weight * 2+ 12; }) 
     .style("fill", function(d) { return color(1/d.rating); }); 

node.append("image") 
     .attr("xlink:href", d=> d.url) 

     //people icon's location (x,y) 

     .attr("x", function(d) { return d.weight * 2+ 12; }) 
     .attr("y", function(d) { return d.height * 2+ 12; }) 

     //people icon's size 
     .attr("width", width/10) 
     .attr("height", height/10); 

的代碼是如何設置的圓和圖像

enter image description here

但他們從不粘在一起。

這是我更新的演示:http://jsfiddle.net/qvco2Ljy/116/

+0

你想在mouseover上顯示圖像嗎? – Afsar

回答

2

對於每一個對象,設置圖像的URL:

{name: "1", rating: 90, id: 2951, url: "someUrl"}, 

然後,而不是附加圈,追加一個image

node.append("image") 
    .attr("xlink:href", d=> d.url) 
    .attr("x", -width/2) 
    .attr("y", -height/2) 
    .attr("width", width) 
    .attr("height", height); 

其中widthheight是相應的圖像寬度和高度。

PS:記住,我只是回答你的問題(「我怎麼可以分配圖像到每個節點?」)。如果放棄圈子不是您的選擇,您將不得不使用pattern

+0

我能夠追加圖像,但我很難將圖像放入節點,我的意思是圓形的東西 – anson920520

+0

你想要每個節點的相同圖像或特定圖像? –

+0

具體圖片請 – anson920520