0

我正在尝试在 MSAGL (WinForms) 版本 1.1.3 中强制未连接节点之间的最小距离。用户从头开始创建图表。这涉及创建节点,以及在节点之间拖动以创建边。但是所有没有任何边缘的节点都会像聚苯乙烯球和羊毛套头衫一样粘在其他节点上。

未连接的节点 - 与其他节点没有距离

我希望所有有或没有边缘的节点都有最小间距。这可能有一个简单的答案,到目前为止我还没有找到。在 GitHub、here 和 Google 上搜索相关材料后,我尝试过但没有成功:

LayoutAlgorithmSettings.NodeSeparation = 200; //works well between nodes that have edges between
LayoutAlgorithmSettings.ClusterMargin = 200;
LayoutAlgorithmSettings.PackingMethod = Microsoft.Msagl.Core.Layout.PackingMethod.Columns;
LayoutAlgorithmSettings.PackingAspectRatio = 2;
LayoutAlgorithmSettings.EdgeRoutingSettings.Padding = 100;

这是图形生成代码(省略了一些声明和其他细节)。我正在使用 MDS 布局:

Graph graph = new Microsoft.Msagl.Drawing.Graph("graph");

graph.LayoutAlgorithmSettings = new Microsoft.Msagl.Layout.MDS.MdsLayoutSettings();
            
//add steps
foreach (Step step in config.Steps)
{
    Node node = new Node(step.Name);
    node.UserData = step;
    UpdateNodeColour(node);
    graph.AddNode(node);
}
                
//add transitions (edges)
foreach(Transition t in config.Transitions)
    graph.AddEdge(t.FromStep.Name, t.Name, t.ToStep.Name);

gViewer.Graph = graph;

顺便说一句 - MSAGL 太棒了!用于可视化和编辑状态机的绝佳工具。

4

1 回答 1

1

我有同样的重叠问题。我用下面的方法解决了这个问题。

GeometryGraph填充图表后,使用via添加节点分隔Microsoft.Msagl.Core.Layout.GraphConnectedComponents.CreateComponents()

if (graph.NodeCount > 1)
{
    GeometryGraph geomGraph = graph.GeometryGraph;
    IEnumerable< GeometryGraph> geomGraphComponents =
        GraphConnectedComponents.CreateComponents(geomGraph.Nodes, geomGraph.Edges);

    SugiyamaLayoutSettings settings = new SugiyamaLayoutSettings();
    foreach (GeometryGraph subgraph in geomGraphComponents)
    {
        LayeredLayout layout = new LayeredLayout(subgraph, settings);
        subgraph.Margins = settings.NodeSeparation / 2;
        layout.Run();
    }
    
    MdsGraphLayout.PackGraphs(geomGraphComponents, settings);
    geomGraph.UpdateBoundingBox();
}

请参阅 LayeredLayout.Run 示例中的示例 #1:

https://csharp.hotexamples.com/examples/Microsoft.Msagl.Layout.Layered/LayeredLayout/Run/php-layeredlayout-run-method-examples.html

注意:从源示例中不要设置 'gViewer1.NeedToCalculateLayout = false;' 如果您打算刷新图表。

于 2021-10-12T10:47:17.197 回答