【问题标题】:Drawing multiple edges between two nodes with d3使用 d3 在两个节点之间绘制多条边
【发布时间】:2012-07-06 19:13:58
【问题描述】:

我一直在关注 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 代码中正确绘制,但是它们都具有相同的坐标和方向,这导致视觉效果看起来像 1 行。在这个例子中需要进行什么样的代码重组以允许 3 条边不被绘制在彼此之上?

【问题讨论】:

    标签: html graph svg d3.js edges


    【解决方案1】:

    事实上,原始可视化是显示节点之间多条链接的一种方法的主要示例,即 - 使用弧线而不是直接路径,因此您可以看到传入和传出链接。

    通过更改代表链接的后续svg path(arc) 元素的半径值,可以扩展此概念以显示每种类型的链接中的多个。一个基本的例子是

    dr = 75/d.linknum;
    

    其中d.linknum 表示连续链接的编号。 dr 稍后用作绘制弧的 rx 和 ry 量。

    在这里完整实现:​​http://jsfiddle.net/7HZcR/3/

    【讨论】:

    • 谢谢!我正在研究一个涉及二次贝塞尔曲线的解决方案,并使用 Math.random() 来确定反射/控制点的确切 x 和 y 坐标。您的解决方案更加优雅。
    • 这太棒了!但是如果 jsfiddle 出现故障,在此处发布源代码可能会有所帮助。此外,来源有 cmets 可以更好地解释答案,例如 any links with duplicate source and target get an incremented 'linknum'
    • 我是 d3.js 的新手,我用它来显示图表,这个答案有助于显示节点之间的多条边。我面临的问题是链接文本重叠。有什么我必须使用的风格,或者 d3 提供了任何解决方案吗?
    • 这是一个很好的答案,但并不完美。看这个编辑版本:jsfiddle.net/thatOneGuy/7HZcR/502,如果我点击顶部的按钮,它会修复所有节点,这样做之后,拖动节点。您会注意到路径重叠:(烦人,否则它会完美运行
    【解决方案2】:

    如果有人需要,这里是上述答案的来源:

    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

    【讨论】:

    • 我将路径功能代码(直到return "M" + d.source.x + "," + d.source.y + "A" + dr...)复制到我正在处理的页面中,但所有路径都显示为相同的半圆。如果发生这种情况,最大半径(dr = 75/d.linknum; 中的75)太小,应该增加。
    • 做 75*linknum @Ian
    • 这也有效,但从更圆的弧线开始并在其中起作用。
    • @Ian 不知道你是否找到了答案,但请看这个问题的答案stackoverflow.com/questions/37417459/…
    • 我最初的评论包含我找到的问题和解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 2017-04-17
    • 2014-03-20
    • 1970-01-01
    相关资源
    最近更新 更多