如何在 d3.js 中填充我的 svg 圆圈内的图像

Yog*_*ogi 5 javascript svg d3.js

这是我在 svg 中填充圆圈的一段代码。

   var svgContainer = d3.select("body").append("svg")
                                 .attr("width", 1000)
                             .attr("height", 1000);
    var circles = svgContainer.selectAll("circle")
                      .data(nodes)
                      .enter()
                      .append("circle");

     var circleAttributes = circles
                   .attr("cx", function (d) { return d.x_axis; })
                   .attr("cy", function (d) { return d.y_axis; })
                   .attr("r", function (d) { return d.radius; })
                   .attr('fill', 'green')
Run Code Online (Sandbox Code Playgroud)

但不是在我的圆圈内填充绿色,我想在每个圆圈内填充不同的图像,其中 url 在我的 json 数据中。我一直在尝试使用 .attr('fill', url(function(d) {return d.url})) 但它不起作用。我是 d3 的新手,谁能帮我解决这个任务?

小智 5

从图像创建图案会占用太多内存,并且使用多个图像会产生严重的性能问题。因此,为了避免这种情况,我们可以在图像上使用剪辑路径。

像这样:

var config = {
  "avatar_size": 100
}

var body = d3.select("body");
var svg = body.append("svg")
  .attr("width", 500)
  .attr("height", 500);

var defs = svg.append('svg:defs');

data = [{
  posx: 100,
  posy: 100,
  img: "https://cdn4.iconfinder.com/data/icons/seo-and-data/500/pencil-gear-128.png",
}, {
  posx: 200,
  posy: 200,
  img: "https://cdn4.iconfinder.com/data/icons/seo-and-data/500/gear-clock-128.png"
}, {
  posx: 300,
  posy: 300,
  img: "https://cdn4.iconfinder.com/data/icons/seo-and-data/500/magnifier-data-128.png"
}];


svg .append('clipPath')
   .attr('id','clipObj')  
        .append('circle')
         .attr('cx',config.avatar_size/2)
          .attr('cy',config.avatar_size/2)
         .attr('r',config.avatar_size/2);

data.forEach(function(d,i){
  svg.append('image')
     .attr('xlink:href',d.img)
     .attr('width',config.avatar_size)
     .attr('height',config.avatar_size)
 .attr('transform','translate('+parseInt(d.posx+config.avatar_size/2)+','+parseInt(d.posy+config.avatar_size/2)+')')
     .attr('clip-path','url(#clipObj)');
});
Run Code Online (Sandbox Code Playgroud)

我们还可以根据需要轻松地用新的剪切区域替换剪切区域。这是代码笔的链接:http://codepen.io/anon/pen/VagxKp ?editors=0010


Cyr*_*ian 4

想象一下你有一个像这样的数据集:

data = [{
  posx: 100,
  posy: 100,
  img: "https://cdn0.iconfinder.com/data/icons/flat-round-system/512/android-128.png",

}, {
  posx: 200,
  posy: 200,

  img: "https://cdn1.iconfinder.com/data/icons/social-media-set/24/Reverbnation-128.png"
}, {
  posx: 300,
  posy: 300,

  img: "https://cdn1.iconfinder.com/data/icons/user-pictures/100/male3-128.png"
}]
Run Code Online (Sandbox Code Playgroud)

在 svg 中制作像这样的 def:

var defs = svg.append('svg:defs');
Run Code Online (Sandbox Code Playgroud)

迭代所有数据并使用图像和圆圈创建尽可能多的定义。圆圈内的填充像这样传递 def 的 id.style("fill", "url(#grump_avatar" + i + ")");

data.forEach(function(d, i) {
  defs.append("svg:pattern")
    .attr("id", "grump_avatar" + i)
    .attr("width", config.avatar_size) 
    .attr("height", config.avatar_size)
    .attr("patternUnits", "userSpaceOnUse")
    .append("svg:image")
    .attr("xlink:href", d.img)
    .attr("width", config.avatar_size)
    .attr("height", config.avatar_size)
    .attr("x", 0)
    .attr("y", 0);

  var circle = svg.append("circle")
    .attr("transform", "translate(" + d.posx + "," + d.posy + ")")
    .attr("cx", config.avatar_size / 2)
    .attr("cy", config.avatar_size / 2)
    .attr("r", config.avatar_size / 2)
    .style("fill", "#fff")
    .style("fill", "url(#grump_avatar" + i + ")");

})
Run Code Online (Sandbox Code Playgroud)

工作代码在这里

受到这个SO答案的启发