Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an arrow marker on an SVG <line> element?

I need to create an arrow in d3.js, but all I find are examples with diagrams of nodes. What I need is to simply make an arrow that goes from point A to point B.

I tried implementing part of the code in the following example: http://bl.ocks.org/1153292

This specific part:

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"); 

But as I mentioned earlier, I do not find the way to create the arrow with a svg:line greatly appreciate the help you can give me.

like image 883
Cristian G Avatar asked Oct 01 '12 19:10

Cristian G


People also ask

What is marker in SVG?

<marker> The <marker> element defines the graphic that is to be used for drawing arrowheads or polymarkers on a given <path> , <line> , <polyline> or <polygon> element. Markers are attached to shapes using the marker-start , marker-mid , and marker-end properties.

How does SVG path work?

It is often placed at the end of a path node, although not always. There is no difference between the uppercase and lowercase command. The path will move to point ( 10 , 10 ) and then move horizontally 80 points to the right, then 80 points down, then 80 points to the left, and then back to the start.

What is Marker-end?

The marker-end attribute defines the arrowhead or polymarker that will be drawn at the final vertex of the given shape. For all shape elements, except <polyline> and <path> , the last vertex is the same as the first vertex.


1 Answers

If you meant 'how do I use an arrow marker on a <line> element?' then here's how you do that:

<line x1="100" y1="230" x2="300" y2="230"   marker-end="url(#yourMarkerId)" stroke="black" stroke-width="10"/> 

Here's a full example. And note that marker-end is a css property, so you can also put that part in a stylesheet if you want.

If you meant you want to draw your marker with lines, then just add whatever lines you need as a child of the marker element (and make sure to use the coordinate system defined by the marker's viewBox attribute).

like image 107
Erik Dahlström Avatar answered Oct 03 '22 04:10

Erik Dahlström