Why are my embedded JointJS elements overlapping?

故事扮演 提交于 2019-12-05 12:00:31

This is a known issue of Dagre-D3: Automatic layout does not work on hierarchical diagrams with links with parent.

As a workaround, you can either omit the links between the regions (suggested in the edition of your question) or you can do a bit of extra work that will work nicely for any number of nodes:

  1. Layout each group of children nodes separately:

    var fooChildren = [nodes[0], nodes[1], nodes[2]];
    var barChildren = [nodes[3], nodes[4], nodes[5]];
    var hmmChildren = [nodes[6], nodes[7], nodes[8]];
    var children = [fooChildren, barChildren, hmmChildren];
    
    for(var i = 0; i < children.length; i++)
        joint.layout.DirectedGraph.layout(children[i]);
    
  2. For each region, create an auxiliary clone region.

    var clones = [];
    for(var i = 0; i < regions.length; i++) {
        var clone = regions[i].clone();
        graph.addCell(clone);
        clones.push(clone);
    }
    
  3. Set the corresponding nodes to their cloned region.

    for(var i = 0; i < children.length; i ++)
        for(var k = 0; k < children[i].length; k++)
            clones[i].embed(children[i][k]);
    
  4. Fit each cloned region to the size of its children and resize the original region with the size of the cloned region.

    for(var i = 0; i < clones.length; i++) {
        clones[i].fitEmbeds(padding: { top: 30, left: 10, right: 10, bottom: 10 });
        regions[i].resize(clones[i].getBBox().width, clones[i].getBBox().height);
    }
    
  5. Layout the graph.

    joint.layout.DirectedGraph.layout(graph, {
        rankDir: 'LR',
        marginX: 30,
        marginY: 30,
        clusterPadding: {
            top: 30,
            left: 10,
            right: 10,
            bottom: 10
        }
    });
    
  6. Translate the cloned regions to the position of its original region (the children will be translated accordingly).

    for(var i = 0; i < clones.length; i++) {
        var dx = regions[i].getBBox().x - clones[i].getBBox().x;
        var dy = regions[i].getBBox().y - clones[i].getBBox().y;
        clones[i].translate(dx, dy);
    }
    
  7. Remove the cloned regions and set the children to the original regions.

    for(var i = 0; i < regions.length; i++) {
        clones[i].remove();
    
        for(var j = 0; j < children[i].length; j++)
            regions[i].embed(children[i][j]);
    }
    

Hope this can help to provide an alternative solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!