首先在<body>添加图片对象
<svg id="mySvg_ac" width="0" height="0" > <defs id="mdef"> <!-- 图片1 --> <pattern id="img_ac" x="0" y="0" height="128" width="128"> <image x="0" y="0" width="128" height="128" xlink:href="/static/images/ac.png"></image> </pattern> <!-- 图片2 --> <pattern id="img_de" x="0" y="0" height="128" width="128"> <image x="0" y="0" width="128" height="128" xlink:href="/static/images/de.png"></image> </pattern> </defs> </svg>
然后在需要的节点中引用图片对象
var nodes = svg.selectAll("circle") .data(nodes_data) .enter() .append("circle") .attr("fill", function(d){ if(d.type == 'ac'){ return "url(#img_ac)"; }else{ return "url(#img_de)"; }})
标签:ac,return,img,样式,节点,circle,data,D3 From: https://www.cnblogs.com/carriexu/p/16984277.html