使用d3在两个节点之间绘制多条边

bis0qfac  于 2023-06-06  发布在  其他
关注(0)|答案(3)|浏览(240)

我一直在this example上学习Mike Bostock的代码,以学习如何在d3中绘制有向图,并想知道如何构造代码,以便在图中的两个节点之间添加多条边。例如,如果上面示例中的数据集定义为

var links = [{source: "Microsoft", target: "Amazon", type: "licensing"},
             {source: "Microsoft", target: "Amazon", type: "suit"},
             {source: "Samsung", target: "Apple", type: "suit"},
             {source: "Microsoft", target: "Amazon", type: "resolved"}];

然后运行代码,我只看到一行。所有的路径都在html代码中正确绘制,但是它们都具有相同的坐标和方向,这导致视觉效果看起来像一条线。在这个例子中,需要进行什么样的代码重组,才能使3条边不相互重叠?

rryofs0p

rryofs0p1#

事实上,原始的可视化是一个显示节点之间多个链接的方法的主要示例,即使用弧而不是直接路径,因此您可以看到传入和传出链接。
这个概念可以扩展到通过改变表示链接的后续svg路径(弧)元素的半径值来显示这些类型的链接中的每一种的多个。一个基本的例子是

dr = 75/d.linknum;

其中d.linknum表示连续链路的编号。dr稍后被用作正被绘制的弧的rx和ry量。
在此全面实施:http://jsfiddle.net/7HZcR/3/

beq87vna

beq87vna2#

下面是上面的答案,如果有人需要的话:

var links = [{source: "Microsoft", target: "Amazon", type: "licensing"},
             {source: "Microsoft", target: "Amazon", type: "suit"},
             {source: "Samsung", target: "Apple", type: "suit"},
             {source: "Microsoft", target: "Amazon", type: "resolved"}];
//sort links by source, then target
links.sort(function(a,b) {
    if (a.source > b.source) {return 1;}
    else if (a.source < b.source) {return -1;}
    else {
        if (a.target > b.target) {return 1;}
        if (a.target < b.target) {return -1;}
        else {return 0;}
    }
});
//any links with duplicate source and target get an incremented 'linknum'
for (var i=0; i<links.length; i++) {
    if (i != 0 &&
        links[i].source == links[i-1].source &&
        links[i].target == links[i-1].target) {
            links[i].linknum = links[i-1].linknum + 1;
        }
    else {links[i].linknum = 1;};
};

var nodes = {};

// Compute the distinct nodes from the links.
links.forEach(function(link) {
  link.source = nodes[link.source] || (nodes[link.source] = {name: link.source});
  link.target = nodes[link.target] || (nodes[link.target] = {name: link.target});
});

var w = 600,
    h = 600;

var force = d3.layout.force()
    .nodes(d3.values(nodes))
    .links(links)
    .size([w, h])
    .linkDistance(60)
    .charge(-300)
    .on("tick", tick)
    .start();

var svg = d3.select("body").append("svg:svg")
    .attr("width", w)
    .attr("height", h);

// Per-type markers, as they don't inherit styles.
svg.append("svg:defs").selectAll("marker")
    .data(["suit", "licensing", "resolved"])
  .enter().append("svg:marker")
    .attr("id", String)
    .attr("viewBox", "0 -5 10 10")
    .attr("refX", 15)
    .attr("refY", -1.5)
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("orient", "auto")
  .append("svg:path")
    .attr("d", "M0,-5L10,0L0,5");

var path = svg.append("svg:g").selectAll("path")
    .data(force.links())
  .enter().append("svg:path")
    .attr("class", function(d) { return "link " + d.type; })
    .attr("marker-end", function(d) { return "url(#" + d.type + ")"; });

var circle = svg.append("svg:g").selectAll("circle")
    .data(force.nodes())
  .enter().append("svg:circle")
    .attr("r", 6)
    .call(force.drag);

var text = svg.append("svg:g").selectAll("g")
    .data(force.nodes())
  .enter().append("svg:g");

// A copy of the text with a thick white stroke for legibility.
text.append("svg:text")
    .attr("x", 8)
    .attr("y", ".31em")
    .attr("class", "shadow")
    .text(function(d) { return d.name; });

text.append("svg:text")
    .attr("x", 8)
    .attr("y", ".31em")
    .text(function(d) { return d.name; });

// Use elliptical arc path segments to doubly-encode directionality.
function tick() {
  path.attr("d", function(d) {
    var dx = d.target.x - d.source.x,
        dy = d.target.y - d.source.y,
        dr = 75/d.linknum;  //linknum is defined above
    return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + " 0 0,1 " + d.target.x + "," + d.target.y;
  });

  circle.attr("transform", function(d) {
    return "translate(" + d.x + "," + d.y + ")";
  });

  text.attr("transform", function(d) {
    return "translate(" + d.x + "," + d.y + ")";
  });
}
path.link {
  fill: none;
  stroke: #666;
  stroke-width: 1.5px;
}

marker#licensing {
  fill: green;
}

path.link.licensing {
  stroke: green;
}

path.link.resolved {
  stroke-dasharray: 0,2 1;
}

circle {
  fill: #ccc;
  stroke: #333;
  stroke-width: 1.5px;
}

text {
  font: 10px sans-serif;
  pointer-events: none;
}

text.shadow {
  stroke: #fff;
  stroke-width: 3px;
  stroke-opacity: .8;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
<div id="chart"></div>

关于D3v4,请看这里:https://bl.ocks.org/mbostock/4600693

lnvxswe2

lnvxswe23#

感谢您使用linknum的答案,它真的工作。然而,线在Linkum > 10之后开始重叠。这里是一个函数来生成等距二次曲线

// use it like  'M' + d.source.x + ',' + d.source.y + link_arc2(d) + d.target.x + ',' + d.target.y
        function link_arc2(d) {
            // draw line for 1st link
            if (d.linknum == 1) {
                return 'L';
            }
            else {
                let sx = d.source.x;
                let sy = d.source.y;
                let tx = d.target.x;
                let ty = d.target.y;

                // distance b/w curve paths
                let cd = 30;

                // find middle of source and target
                let cx = (sx + tx) / 2;
                let cy = (sy + ty) / 2;
                
                // find angle of line b/w source and target
                var angle = Math.atan2(ty - sy, tx - sx);

                // add radian equivalent of 90 degree
                var c_angle = angle + 1.5708;

                // draw odd and even curves either side of line
                if (d.linknum & 1) {
                    return 'Q ' + (cx - ((d.linknum - 1) * cd * Math.cos(c_angle))) + ',' + (cy - ((d.linknum - 1) * cd * Math.sin(c_angle))) + ' ';
                }
                else {
                    return 'Q ' + (cx + (d.linknum * cd * Math.cos(c_angle))) + ',' + (cy + (d.linknum * cd * Math.sin(c_angle))) + ' ';
                }
            }
        }

相关问题