Search code examples
javascriptd3.jsgraphnodesedges

D3.js Edge Bundling without Hierachy


Holten's hierarchical edge bundling algorithm in D3 depends on hierarchical data.

Example: http://bl.ocks.org/mbostock/7607999

Is there a way to implement a similar circular edge bundling graph with those nice splines for non-hierarchical data?

Example: http://bl.ocks.org/sjengle/5432087 (Like this, but with splines...)


Solution

  • If your data really is non-hierarchical, you could change the drawCurves function of your example into something like this:

    function drawCurves(links) {
    
      d3.select("#plot").selectAll(".link")
        .data(links)
        .enter()
        .append("path")
        .attr("class", "link")
        .attr("d", function(d){
          var lineData = [
          {
            x: Math.round(d.target.x),
            y: Math.round(d.target.y)
          }, {
          x: Math.round(d.target.x) - Math.round(d.target.x)/3,
            y: Math.round(d.target.y) - Math.round(d.target.y)/3
          }, 
          {
          x: Math.round(d.source.x) - Math.round(d.source.x)/3,
            y: Math.round(d.source.y) - Math.round(d.source.y)/3
          },{
            x: Math.round(d.source.x),
            y: Math.round(d.source.y)
          }];
          return `M${lineData[0].x},${lineData[0].y}C${lineData[1].x},${lineData[1].y},${lineData[2].x},${lineData[2].y},${lineData[3].x},${lineData[3].y} `;
        });
    }
    

    To draw the lines with the d attribute the SVG specification provides. Consult https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d for further reference.