我在 d3 力有向图中有以下代码,我试图根据一个值(从 1 到 3)改变链接的大小及其关联的箭头。笔画粗细会随值而变化,但箭头不会保持在正确的位置。当笔画粗细从 1 变为 3 时,它往往会从末尾移回。关于在更改笔画值时如何保持箭头(标记)正确对齐的任何想法?非常感谢!
var link = vis.selectAll("line.link")
.data(json.links)
.enter().append("svg:line")
.attr("class", "link")
.style("stroke-width", function(d) { return Math.sqrt(d.value); })
.attr("x1", function(d) { return d.source.x; })
.attr("y1", function(d) { return d.source.y; })
.attr("x2", function(d) { return d.target.x; })
.attr("y2", function(d) { return d.target.y; })
.attr("marker-end", "url(#arrowGray)")
.on("click", function(d) {
link.style("stroke","#dddddd");
node.style("stroke","#FFFFFF");
d3.select(this).style("stroke","red");
link.attr("marker-end", null);
link.attr("marker-end", "url(#arrowGray)");
d3.select(this).attr("marker-end", null);
d3.select(this).attr("marker-end", "url(#arrowRed)");
clickLink(d);
});
defs.append("svg:marker")
.attr("id", "arrowGray")
.attr("viewBox","0 0 10 10")
.attr("refX","20")
.attr("refY","5")
.attr("markerUnits","strokeWidth")
.attr("markerWidth","9")
.attr("markerHeight","5")
.attr("orient","auto")
.append("svg:path")
.attr("d","M 0 0 L 10 5 L 0 10 z")
.attr("fill", "#BBBBBB");