Skip to content Skip to sidebar Skip to footer

Network Multi-route Orthogonal Graph In D3.js

We want to use d3 to draw a network route graph that has fixed start and end node but different paths in between that might share some nodes, for example: I read comments from Co

Solution 1:

See my demo here。

http://jsfiddle.net/doraeimo/JEcdS/

The main idea is to make a connectivity based on a tree.

//1)temporarily convert a connectivity to a tree        var tree = conv2tree(data);

    //2)calculate for nodes' coords with <code>cluster.nodes(tree);</code>var nodes = buildNodes(tree);

    //3)append all the edges(links) of the connectivityvar links = buildLinks(data);

Solution 2:

I built an example using Dagre-d3, which integrates Dagre (suggested by @frank) with D3.

// Display optionsvar options = {
  rankdir: "LR"
};

// Create the input graphvar g = new dagreD3.graphlib.Graph()
  .setGraph(options)
  .setDefaultEdgeLabel(function() {
    return {};
  });

// Set nodesvar n_nodes = 8;
for (var i = 0; i < n_nodes; i++) {
  g.setNode(i, {
    label: i
  });
}

g.nodes().forEach(function(v) {
  var node = g.node(v);
  node.shape = "circle";
});

// Set edges
g.setEdge(0, 1);
g.setEdge(1, 2);
g.setEdge(2, 3);
g.setEdge(3, 4);
g.setEdge(4, 5);
g.setEdge(1, 1);
g.setEdge(1, 6);
g.setEdge(6, 7);
g.setEdge(7, 0);
g.setEdge(7, 4);

// Create the renderervar render = new dagreD3.render();

// Set up an SVG group so that we can translate the final graph.var svg = d3.select("svg");

// Run the renderer. This is what draws the final graph.render(svg, g);
.node {
  pointer-events: none;
}

.node circle {
  stroke: darkgray;
  fill: lightgray;
}

.edgePath path {
  stroke: black;
}
<scriptsrc="https://d3js.org/d3.v4.min.js"charset="utf-8"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/dagre-d3/0.6.1/dagre-d3.min.js"></script><svgwidth=960height=600></svg>

Solution 3:

Dagre solved our problem. It does exactly what we need.

Post a Comment for "Network Multi-route Orthogonal Graph In D3.js"