2014-11-01 90 views

回答

3

添加圖片只是用

... 
.append("svg:image") 
.attr("xlink:href","myimage.png") 
.attr("width", "32") 
.attr("height", "32"); 

您還需要位置(xy)的情況。這將默認爲0,所以你可以或者使用一個translate的,像這樣找圓弧的中心:

.attr("transform", function(d){ 
    return "translate(" + arc.centroid(d) + ")"; 
}); 

然而,這只是錨圖像的右上角到的中心弧,所以要正確居中它需要使用圖像的大小。下面是完整的東西:

var image_width = 32; 
var image_height = 32; 

// add the image 
arcs.append("svg:image").attr("transform", function(d){ 
    // Reposition so that the centre of the image (not the top left corner) 
    // is in the centre of the arc 
    var x = arc.centroid(d)[0] - image_width/2; 
    var y = arc.centroid(d)[1] - image_height/2; 
    return "translate(" + x + "," + y + ")"; 
}) 
.attr("xlink:href",function(d) { return d.data.icon;}) 
.attr("width", image_width) 
.attr("height", image_height); 

下面是一個例子:http://jsfiddle.net/henbox/88t18rqg/6/

請注意,我已經包括在圖表數據成像路徑。你只需要去找到適當的圖標!

+0

「圖像路徑是否嵌入到數據中」是什麼意思? – 2014-11-01 19:58:51

+0

對不起,我不清楚 - 我的意思是圖像路徑是用於創建圖表的數據值的一部分。我已經更新了答案,現在改進了解決方案,以更好地定位圖像中的圖像 – 2014-11-01 20:08:23

+0

好吧!謝謝! – 2014-11-01 20:10:10