admin管理员组文章数量:1356304
I am working with the following fiddle: /
I would like the connections between the nodes to have arrow-ends, like in this one: /
I added a marker element to the svg like this:
svg.append("defs")
.append("marker")
.attr("id", "arrow")
.attr("markerWidth", 10)
.attr("markerHeight", 10)
.attr("refX", 5)
.attr("refY", 5)
.attr("orient", "auto")
.attr("markerUnits", "strokeWidth")
.append("path")
.attr("d", "M0,0 L0,6 L9,3 z")
.attr("fill", "#000");
and then added the marker-end attribute to the lines like this:
links = svg.selectAll( "line.link" )
.data( json.edges )
.enter().append( "line" )
.attr( "class", "link" )
.attr("marker-end", "url(#arrow)")
.attr("markerWidth", 5)
.attr("stroke", "#000")
.attr("stroke-width", 5)
.style( "stroke", "#000" )
.style( "stroke-width", 2 );
This does not show any arrow-ends.
If I add the marker-end attribute to a normal line like this:
svg.append("line")
.attr("x1", 50)
.attr("y1", 50)
.attr("x2", 100)
.attr("y2", 100)
.attr("stroke", "#000")
.attr("stroke-width", 5)
.attr("marker-end", "url(#arrow)");
it displays.
I am working with the following fiddle: https://jsfiddle/4ovue3s8/12/
I would like the connections between the nodes to have arrow-ends, like in this one: http://jsfiddle/maxl/mNmYH/2/
I added a marker element to the svg like this:
svg.append("defs")
.append("marker")
.attr("id", "arrow")
.attr("markerWidth", 10)
.attr("markerHeight", 10)
.attr("refX", 5)
.attr("refY", 5)
.attr("orient", "auto")
.attr("markerUnits", "strokeWidth")
.append("path")
.attr("d", "M0,0 L0,6 L9,3 z")
.attr("fill", "#000");
and then added the marker-end attribute to the lines like this:
links = svg.selectAll( "line.link" )
.data( json.edges )
.enter().append( "line" )
.attr( "class", "link" )
.attr("marker-end", "url(#arrow)")
.attr("markerWidth", 5)
.attr("stroke", "#000")
.attr("stroke-width", 5)
.style( "stroke", "#000" )
.style( "stroke-width", 2 );
This does not show any arrow-ends.
If I add the marker-end attribute to a normal line like this:
svg.append("line")
.attr("x1", 50)
.attr("y1", 50)
.attr("x2", 100)
.attr("y2", 100)
.attr("stroke", "#000")
.attr("stroke-width", 5)
.attr("marker-end", "url(#arrow)");
it displays.
Share Improve this question asked Oct 24, 2017 at 9:23 JarkobJarkob 782 silver badges7 bronze badges1 Answer
Reset to default 8First make a def for the arrow:
svg.append("svg:defs").append("svg:marker")
.attr("id", "arrow")
.attr("viewBox", "0 -5 10 10")
.attr('refX', -20)//so that it es towards the center.
.attr("markerWidth", 5)
.attr("markerHeight", 5)
.attr("orient", "auto")
.append("svg:path")
.attr("d", "M0,-5L10,0L0,5");
Instead of line make a path
links = svg.selectAll( "line.link" )
.data( json.edges )
.enter().append( "path" )//append path
.attr( "class", "link" )
.style( "stroke", "#000" )
.attr('marker-start', (d) -> "url(#arrow)")//attach the arrow from defs
.style( "stroke-width", 2 );
lastly give d attribute for path in tick function
links
.attr( "d", (d) -> "M" + d.source.x + "," + d.source.y + ", " + d.target.x + "," + d.target.y)
working code here
本文标签: javascriptAdding arrowends to D3js linesStack Overflow
版权声明:本文标题:javascript - Adding arrow-ends to D3.js lines - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743962584a2569301.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论