2014-11-01 42 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