1

I used d3.layout.force() to draw a force layout graph since there is no layout available for Directed Acrylic Graph (DAG). The code is at the end of this post.

  1. How I can modify the node positions to draw as shown in the picture? (the diagram after the red arrow)?

enter image description here

A final layout should like this below:

enter image description here

  1. Should I be using d3.layout.tree() instead of the d3.layout.force() to draw a tree like this?

The code to draw the graph is below:

var width = 640;
var height = 480;

var nodes = [
    { name: 'V' },
    { name: 'A' },
    { name: 'M' },
    { name: 'C' },
    { name: 'A' }
];

var links = [
    { source: 0, target: 1 },
    { source: 0, target: 2 },
    { source: 0, target: 3 },
    { source: 0, target: 4 }
];

var svg = d3.select('body')
    .append('svg')
    .attr('width', width)
    .attr('height', height);

var force = d3.layout.force()
    .size([width, height])
    .nodes(nodes)
    .links(links);

force.linkDistance(width / 4);

var link = svg.selectAll('.link')
    .data(links)
    .enter()
    .append('line')
    .attr('class', 'link');

var node = svg.selectAll('.node')
    .data(nodes)
    .enter()
    .append('circle')
    .attr('class', 'node');

force.on('end', function () {

  node.attr('r', width / 50)
      .attr('cx', function(d) { return d.x; })
      .attr('cy', function(d) { return d.y; });

  link.attr('x1', function (d) { return d.source.x; })
      .attr('y1', function (d) { return d.source.y; })
      .attr('x2', function (d) { return d.target.x; })
      .attr('y2', function (d) { return d.target.y; });
});

force.start();
wonderful world
  • 10,969
  • 20
  • 97
  • 194
  • 1
    If we go with force directed, you may not be able to achieve this may be tree layout can help you out http://bl.ocks.org/d3noob/8375092. Other option is my answer here http://stackoverflow.com/questions/31245751/how-do-you-create-a-family-tree-in-d3-js/31367673#31367673 – Cyril Cherian Sep 05 '15 at 02:14
  • 1
    [Cola.js](http://marvl.infotech.monash.edu/webcola/) is probably a better fit for this. – Lars Kotthoff Sep 05 '15 at 08:43
  • I looked at Cola, and it says it's better for displaying 100 nodes. I assume the performance will degrade for more than 100 nodes. In my real example, I may have more than 100 nodes. – wonderful world Sep 05 '15 at 10:43

0 Answers0