svg - How can I add different images instead of text inside d3.js piechart slices -
Now I have only one and it is hard work instead of pulling it out of grape drops.
I also need an image in the center of the circle. Please consider
If all images Continuous width and height, you only add image URLs to your data array and dynamically set the xlink: href attribute using the data function.
var piedata = [[label: "test", image: "http://placeimg.com/40/40/any", value: 50}, {label: "", Image: "http://placeimg.com/42/42/any", value: 50}, {label: "Jonathan", image: "http://placeimg.com/44/44/any", Value: 50}, {label: "Lorenzo", image: "http://placeimg.com / 46/46 / any", value: 50}, {label: "Hilary", image: "http: // placeimg. Com / 38/38 / any ", value: 50}] .... g.append (" g ") .attr (" transform ", function (d) {return" translation "(+ + arc.centroid (d) + ")";}) .append ("svg: image") .attr ("xlink: href", function (d) {return d.data.image;}) .attr ("width", image_width) .attr ("Height", image_height) .attr ("x", - 1 * image_width / 2). TR ("y", - 1 * IMAGE_HEIGHT / 2);
Here's a working belle:
If, on the other hand, each image is a different shape and you do not know what the width and height values are before, something Additional work is involved. One approach is to preload the images so that the width and height values can be detected after images are loaded, and can be used to put a callback function inside the pie chart.
Here is a dynamic image dimension: