【发布时间】:2019-05-08 10:06:12
【问题描述】:
试图找到一种在 D3 中将图像置于圆圈内的方法。
img 已加载到 DOM 中,但我无法让它在圆圈内呈现。
重点是将图片放在顶部,因此圆圈背景颜色必须保持原样(颜色编码目的)。
我在这里做错了什么?
期望的输出:
我的代码:
function render(name) {
let markerCircles = svg.selectAll("circle")
.data([1, 2, 3, 4, 5])
.enter()
.append("circle")
.style("fill", "none")
.attr("stroke", "#ff97c4")
.style("stroke-width", "1.5px")
.attr("cy", fullSVGHeight / 2)
.attr("cx", markerCirclesScale(name) + 330)
.attr("r", 0)
markerCircles
.transition()
.duration(1000)
.attr("r", function(d) {
return d * 65;
});
let personCircles = svg.selectAll("a")
.data(data)
.enter()
.append("a")
.attr("id", function(d) {
console.log(d["Person Name"]);
if (d && d.length !== 0) {
return d["Person Name"].replace(/ |,|\./g, '_');
}
})
.style("opacity", 1)
.call(d3.drag()
.on("start", dragstarted)
.on("drag", dragged)
.on("end", dragended));
//Define defs
let defs = personCircles.append("defs");
defs.append('rect')
.attr('id', 'pic')
.attr('x', function(d){
return markerCirclesScale(name)
})
.attr('y', function(d){
return fullSVGHeight / 2;
})
.attr('width', 120)
.attr('height', 30)
.attr('rx', 10)
.style('fill', 'blue')
defs.append("clipPath")
.append("use")
.attr('xlink:href', '_')
.attr("z-index", 1000)
d3.timeout(function() {
personCircles
.append("use")
.attr('xlink:href', "_")
.append('image')
.attr('xlink:href', function(d){
return 'https://vignette.wikia.nocookie.net/ideas/images/8/82/Donald_Trump.png/revision/latest/scale-to-width-down/640?cb=20170512015233'})
.attr("clip-path", "url(#pic)")
.attr("width", 120)
.attr("height", 30)
.attr("y", fullSVGHeight / 2)
.attr("x", markerCirclesScale(name))
.attr("opacity", .8)
.append("circle")
.attr("cy", fullSVGHeight / 2)
.attr("cx", markerCirclesScale(name) + 330)
.attr("r", 20)
.append("title")
.text(function(d) {
return "Name: " + d["Person Name"] + ", " + "Time in the company: " + (+d["Period"] + " years");
});
simulation.restart().on("tick", ticked);
}, 2000)
【问题讨论】:
标签: javascript image d3.js svg