如何在 d3.js 的强制布局中动态附加箭头标记,如下图所示

d3.select('svg.svgparentTag').selectAll("marker").data(["BLACK", "BLUE"]).enter().append("svg:marker")
.attr("id", String)
.attr("viewBox", "0 -5 10 10")
.attr("markerWidth", 6)
.attr("markerHeight", 6)
.attr("orient", "auto")
.append("svg:path")
.attr("class", function(d) { return "marker_only " + d.type; })
.attr("d", "M0,-5L10,0L0,5"); //marker
var path = d3.select('.pitch').selectAll("path")
.data(force.links())
.enter().append('g').classed('g_path', true).append("svg:path").attr("class", "link");
path.filter(function(d) {
return d.arrow == true;
}).attr("marker-mid", function(d) { return "url(#BLUE)"}).attr("stroke-linecap", "round");
Run Code Online (Sandbox Code Playgroud) 如何在力布局的链接中添加和删除类。考虑一下d.source.x == d.target.x我是否必须添加类,否则我必须从链接中删除该类。
path.attr("d", function(d) {
var x1 = d.source.x,
y1 = d.source.y,
x2 = d.target.x,
y2 = d.target.y,
dx = Math.abs(x2 - x1),
dy = Math.abs(y2 - y1),
dr = dx * dx + dy * dy;
var rotation = 0;
if (x1 === x2) {
var dr = Math.sqrt(dx * dx + dy * dy) / 1.8; // note that this is always equal to Math.abs(dy)
var sweep = 1;
if (y1 > y2) {
sweep …Run Code Online (Sandbox Code Playgroud)