【问题标题】:Change orientation of Arrow Marker in D3 oriented force graph在 D3 定向力图中更改箭头标记的方向
【发布时间】:2016-05-11 11:06:39
【问题描述】:

我正在尝试在我的每个链接中插入一个箭头标记。但我想改变箭头的方向,使它们面向每个链接的另一端。

代码如下:

    svg.append("svg:defs").selectAll("marker")
        .data(["end"])      // Different link/path types can be defined here
      .enter().append("svg:marker")
        .attr("id", "end")    // This section adds in the arrows
        .attr("viewBox", "0 -5 10 10")
        .attr("refX", 15)
        .attr("refY", -1.5)
        .attr("markerWidth", 6)
        .attr("markerHeight", 6)
        .attr("orient", 180)
      .append("svg:path")
        .attr("d", "M0,-5L10,0L0,5");

    // add the links and the arrows
    var pg = svg.append("svg:g").selectAll("path")
        .data(force.links())
      .enter().append("g");

    var path = pg.append("path")
        .attr("class", "link")
        .attr("marker-end", "url(#end)")

这怎么可能?

我已尝试更改.attr("marker-end", "url(#end)");

.attr("marker-end", "url(#start)");

但随后标记就消失了。我想知道的另一件事是代码的哪一部分定义了链接的曲率以及如何更改它。

谢谢。

【问题讨论】:

    标签: javascript jquery d3.js


    【解决方案1】:

    您应该查看标记的"orient" 属性:https://www.w3.org/TR/svg-markers/#OrientAttribute

    在你的情况下,.attr("orient", "auto-start-reverse").attr("orient", 180) 会起作用。

    可以在这篇文章中找到一个示例:Display an arrow head in the middle of D3 force layout link

    至于曲率,相关部分是

    dr = Math.sqrt(dx * dx + dy * dy);
    

    dr,半径越大,曲线越多。

    【讨论】:

    • .attr("orient", "auto-start-reverse") 无效):
    • 它确实有效,但仅适用于.attr("marker-start", "url(#end)")。当我尝试.attr("marker-mid", "url(#end)") 时,它消失了):
    • 我已将我的代码插入到原始帖子中。你会在你的环境中尝试一下,看看它是否有效?谢谢
    • 为什么有这么多的追加? var path = svg.append("svg:g").selectAll("path") .data(force.links()) .enter().append("svg:path")
    • 即使我删除 pg 和文本并粘贴您的代码,箭头也不会出现。
    【解决方案2】:
    svg.select("defs").append("marker")
        .attr("id", "start")
        .attr("viewBox", "0 -5 10 10")
        .attr("refX", -5)
        .attr("refY", 0)
        .attr("markerWidth", 6)
        .attr("markerHeight", 6)    
        .attr("orient", "auto")
        .append("path")
        .attr("d", "M0,0L10,-5L10,5");
    
    var path = svg.append("svg:g").selectAll("path")
        .data(force.links())
        .enter().append("svg:path")
        .attr("class", "link")
        .attr("marker-start", "url(#start)")
        .attr("marker-end", "url(#end)");
    

    var links= [
      {
        "source": "Harry",
        "target": "Sally",
        "value": 1.2
      },
      {
        "source": "Harry",
        "target": "Mario",
        "value": 1.3
      },
      {
        "source": "Sarah",
        "target": "Alice",
        "value": 0.2
      },
      {
        "source": "Eveie",
        "target": "Alice",
        "value": 0.5
      },
      {
        "source": "Peter",
        "target": "Alice",
        "value": 1.6
      },
      {
        "source": "Mario",
        "target": "Alice",
        "value": 0.4
      },
      {
        "source": "James",
        "target": "Alice",
        "value": 0.6
      },
      {
        "source": "Harry",
        "target": "Carol",
        "value": 0.7
      },
      {
        "source": "Harry",
        "target": "Nicky",
        "value": 0.8
      },
      {
        "source": "Bobby",
        "target": "Frank",
        "value": 0.8
      },
      {
        "source": "Alice",
        "target": "Mario",
        "value": 0.7
      },
      {
        "source": "Harry",
        "target": "Lynne",
        "value": 0.5
      },
      {
        "source": "Sarah",
        "target": "James",
        "value": 1.9
      },
      {
        "source": "Roger",
        "target": "James",
        "value": 1.1
      },
      {
        "source": "Maddy",
        "target": "James",
        "value": 0.3
      },
      {
        "source": "Sonny",
        "target": "Roger",
        "value": 0.5
      },
      {
        "source": "James",
        "target": "Roger",
        "value": 1.5
      },
      {
        "source": "Alice",
        "target": "Peter",
        "value": 1.1
      },
      {
        "source": "Johan",
        "target": "Peter",
        "value": 1.6
      },
      {
        "source": "Alice",
        "target": "Eveie",
        "value": 0.5
      },
      {
        "source": "Harry",
        "target": "Eveie",
        "value": 0.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});
        link.value = +link.value;
    });
    
    var width = 960,
        height = 500;
    
    var force = d3.layout.force()
        .nodes(d3.values(nodes))
        .links(links)
        .size([width, height])
        .linkDistance(60)
        .charge(-300)
        .on("tick", tick)
        .start();
    
    var svg = d3.select("body").append("svg")
        .attr("width", width)
        .attr("height", height);
    
    // build the arrow.
    svg.append("svg:defs").selectAll("marker")
        .data(["end"])      // Different link/path types can be defined here
      .enter().append("svg:marker")    // This section adds in the arrows
        .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");
    
    svg.select("defs").append("marker")
        .attr("id", "start")
        .attr("viewBox", "0 -5 10 10")
        .attr("refX", -5)
        .attr("refY", 0)
        .attr("markerWidth", 6)
        .attr("markerHeight", 6)    
        .attr("orient", "auto")
        .append("path")
        .attr("d", "M0,0L10,-5L10,5");
    
    // add the links and the arrows
    var path = svg.append("svg:g").selectAll("path")
        .data(force.links())
        .enter().append("svg:path")
        .attr("class", "link")
        .attr("marker-start", "url(#start)")
        .attr("marker-end", "url(#end)");
    
    // define the nodes
    var node = svg.selectAll(".node")
        .data(force.nodes())
      .enter().append("g")
        .attr("class", "node")
        .call(force.drag);
    
    // add the nodes
    node.append("circle")
        .attr("r", 5);
    
    // add the text 
    node.append("text")
        .attr("x", 12)
        .attr("dy", ".35em")
        .text(function(d) { return d.name; });
    
    // add the curvy lines
    function tick() {
        path.attr("d", function(d) {
            var dx = d.target.x - d.source.x,
                dy = d.target.y - d.source.y,
                dr = Math.sqrt(dx * dx + dy * dy);
            return "M" + 
                d.source.x + "," + 
                d.source.y + "A" + 
                dr + "," + dr + " 0 0,1 " + 
                d.target.x + "," + 
                d.target.y;
        });
    
        node
            .attr("transform", function(d) { 
      	    return "translate(" + d.x + "," + d.y + ")"; });
    }
    path.link {
      fill: none;
      stroke: #666;
      stroke-width: 1.5px;
    }
    
    circle {
      fill: #ccc;
      stroke: #fff;
      stroke-width: 1.5px;
    }
    
    text {
      fill: #000;
      font: 10px sans-serif;
      pointer-events: none;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 2013-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多