Replace d3.js symbols with images

坚强是说给别人听的谎言 提交于 2019-12-21 21:29:18

问题


Referring to this fiddle example:

I need to replace the symbols with images...or possibly a single image at first..like for instance, this image:

https://github.com/favicon.ico

What I am trying in the code is as follows:

vis.selectAll("path")
     .data(nodes)
   .enter().append("path")
     .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
     .append("image")
   .attr("xlink:href", "https://github.com/favicon.ico")
       .attr("x", -8)
       .attr("y", -8)
       .attr("width", 16)
       .attr("height", 16)
       .style("stroke", "black")
       .style("stroke-width", "1px")
       .call(force.drag);

It is appending image tag to each of the path tags, but not showing up the image itself. Any hints?


回答1:


You're appending each image as a child of each path. What you probably want is to append a group that contains both a path and an image (which I think is what you were asking in your first comment).

Something like this:

var groups = vis.selectAll("g")
    .data(nodes).enter()
    .append("g")
    .attr("transform", function(d) { return "translate(" + d.x + "," + d.y + ")"; })
    .call(force.drag);

groups.append("path")
    .attr("d", d3.svg.symbol()
        .size(function(d) { return d.size; })
        .type(function(d) { return d.type; }))
    .style("fill", "steelblue")
    .style("stroke", "black")
    .style("stroke-width", "1.5px");

groups.append("image")
   .attr("xlink:href", "https://github.com/favicon.ico")
       .attr("x", -8)
       .attr("y", -8)
       .attr("width", 16)
       .attr("height", 16)
       .style("stroke", "black")
       .style("stroke-width", "1px");

See: http://jsfiddle.net/findango/a7as6/4/



来源:https://stackoverflow.com/questions/17976895/replace-d3-js-symbols-with-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!