Can I use images as the background rectangles for d3 treemaps?

前端 未结 3 1411
难免孤独
难免孤独 2020-12-30 08:36

Is it possible to make a treemap in d3 with the background of each rectangle be an image? I am looking for something similar to what was done in Silverlight here, but for d3

3条回答
  •  渐次进展
    2020-12-30 09:00

    Yes, there are several ways of using images in SVGs. You probably want to define the image as a pattern and then use it to fill the rectangle. For more information, see e.g. this question (the procedure is the same regardless of the element you want to fill).

    In D3 code, it would look something like this (simplified).

    svg.append("defs")
       .append("pattern")
       .attr("id", "bg")
       .append("image")
       .attr("xlink:href", "image.jpg");
    
    svg.append("rect")
       .attr("fill", "url(#bg)");
    

提交回复
热议问题