Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw line with arrow using d3.js

Tags:

I have trouble in drawing the line with arrow using d3.js. I did see some tutorials and wrote this code, but I just see the line with no arrow mark. Could anyone please look at it and tell me where am I missing. Thanks in advance.

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

//arrow
svg.append("svg:defs")
  .append("svg:marker")
  .attr("id", "triangle")
  .attr("viewBox", "0 -5 10 10")
  .attr("refX", 15)
  .attr("refY", -1.5)
  .attr("markerWidth", 6)
  .attr("markerHeight", 6)
  .attr("orient", "auto");

//line              
svg.append("line")
  .attr("x1", 100)
  .attr("y1", 100)
  .attr("x2", 200)
  .attr("y2", 100)          
  .attr("stroke-width", 1)
  .attr("stroke", "black")
  .attr("marker-end", "url(#triangle)");
like image 861
spod Avatar asked Apr 12 '16 16:04

spod


People also ask

What is the syntax to draw a line in D3?

To make a line, we will use the line generator of d3 by invoking d3. line() . This line generator can then be used to compute the d attribute of an SVG path element. We append path to our SVG and then specify the class as line .

How do I create a circle in JavaScript D3?

#Selecting Elements selectAll method takes a selector string, such as "circle" , and returns a selection representing all elements that match the selector: var circle = d3. selectAll("circle"); With a selection, we can make various changes to selected elements.

What can you use D3 js for?

D3 is a JavaScript library and framework for creating visualizations. D3 creates visualizations by binding the data and graphical elements to the Document Object Model. D3 associates (binding) the data (stuff you want to visualize) with the DOM. This allows the user to manipulate, change or add to the DOM.

What does D3 mean in JavaScript?

D3. js (data-driven documents) is an open source JavaScript library that allows users to apply prebuilt data visualizations to their data. The code creates interactive graphics and data visualizations in common Web standards like HTML, CSS and SVG.


1 Answers

Change your marker creation to the following:

svg.append("svg:defs").append("svg:marker")
    .attr("id", "triangle")
    .attr("refX", 15)
    .attr("refY", -1.5)
    .attr("markerWidth", 6)
    .attr("markerHeight", 6)
    .attr("orient", "auto")
    .append("path")
    .attr("d", "M 0 -5 10 10")
    .style("stroke", "black");

Once you do this, you'll see a line being drawn. You may want to use fill instead of stroke if you want a filled in arrow, which you can get by using the following code:

svg.append("svg:defs").append("svg:marker")
    .attr("id", "triangle")
    .attr("refX", 6)
    .attr("refY", 6)
    .attr("markerWidth", 30)
    .attr("markerHeight", 30)
    .attr("orient", "auto")
    .append("path")
    .attr("d", "M 0 0 12 6 0 12 3 6")
    .style("fill", "black");
like image 166
JSBob Avatar answered Sep 27 '22 17:09

JSBob