奇怪的看箭头

时间:2013-10-29 07:09:36

标签: c# graphviz

我有以下代码(使用graphviz4net从C#转换):

digraph g {
graph [rankdir="LR" ,compound="true" ];
    subgraph cluster0 {
        graph [label="Ready" ];
        1 [  ];
    };
    subgraph cluster2 {
        graph [label="Paused" ];
        3 [  ];
    };
    1 -> 3 [ ltail="cluster0" ,lhead="cluster2" ,comment="4"  ];
    3 -> 1 [ ltail="cluster2" ,lhead="cluster0" ,comment="5"  ];
}

我正在http://www.graphviz-dev.appspot.com/检查已转换的图片。

图像是这样的:

Image converted with Graphviz

我正在用C#编程。我有两个问题:

1 - 如何修复C#中的箭头?

2 - 如何使C#中的省略号消失?

*我知道我可以使用node [shape = none],但我不知道如何在C#中设置它。

更新:

现在我有以下代码:

digraph g {
graph [rankdir="LR" ,compound="true" ];
    subgraph cluster0 {
        graph [label="Ready\n\nPurchaser:\noperation1,operation2,Supplier:\noperation1,operation3," ];
        1 [ shape="none" ,fontcolor="white"  ];
    };
    subgraph cluster2 {
        graph [label="Paused\n\nPurchaser:\noperation1,operation3,Supplier:\noperation2,operation3," ];
        3 [ shape="none" ,fontcolor="white"  ];
    };
    subgraph cluster4 {
        graph [label="Completed\n\nPurchaser:\noperation4,Supplier:\noperation4," ];
        5 [ shape="none" ,fontcolor="white"  ];
    };
    1 -> 3 [ ltail="cluster0" ,lhead="cluster2" ,comment="6"  ];
    1 -> 5 [ ltail="cluster0" ,lhead="cluster4" ,comment="7"  ];
    3 -> 1 [ ltail="cluster2" ,lhead="cluster0" ,comment="8"  ];
    3 -> 5 [ ltail="cluster2" ,lhead="cluster4" ,comment="9"  ];
}

这给了我:

Graphviz4net

不要担心标签问题,我会解决这个问题。

C#代码如下:

Graph<State> Graph = new Graph<State> { Rankdir = RankDirection.LeftToRight };

stringBuilder生成子图

// returns new SubGraph<State>{ Label = stringBuilder.ToString()};
var stringNewBlock = ConvertBlockToSubgraph(state); 

ConvertBlockToSubgraph内部

foreach (var allowedOperation in allowedOperationList)
            {
                stringBuilder.Append(allowedOperation.Key +":\\n");

                foreach (var operation in allowedOperation.Value)
                {
                    stringBuilder.Append(!operation.Equals(lastAllowedOperation) ? operation + ",": operation);
                }
            }

回到外面的世界:

var subgraphNewBlock = new SubGraph<State>{ Label = stringBuilder.ToString()};


stringNewBlock.AddVertex(state);
Graph.AddSubGraph(stringNewBlock);

然后我用:

生成边缘
public override IBlockHandling<State> GenerateLinks()
{
    foreach (var state in statesDictionary.Values)
    {
        foreach (var nextPossibleState in state.GetNextPossibleStateList())
        {
            if (statesDictionary.ContainsKey(nextPossibleState))
            {
                var sourceSubGraph = Graph.SubGraphs.Single(x => x.Label.Contains(state.GetMainHeaderName()));
                var destinationSubGraph = Graph.SubGraphs.Single(x => x.Label.Contains(nextPossibleState));
                var edge = new Edge<SubGraph<State>>(sourceSubGraph, destinationSubGraph);
                Graph.AddEdge(edge);
            }

        }
    }

    return this;
}

然后我转换为DOT格式:

    public override IBlockHandling<State> ConvertDtoToDot()
    {
        var writer = new StringWriter();
        new GraphToDotConverter().Convert(writer, Graph, new AttributesProvider());
        var result = writer.GetStringBuilder().ToString().Trim();

        Console.WriteLine(result);

        return this;
    }

我仍然遇到的问题是箭头看起来很奇怪。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

如果你给你的边缘更多的空间,箭头看起来会很好。实现此目的的最简单方法是使用nodesep attribute

graph [nodesep=2];

结果将是:

enter image description here

如果您在使用的Graphviz C#库中遇到有关如何执行此操作的问题,则应考虑让代码生成DOT代码并将其提供给graphviz。由于某种原因,DOT是DSL; - )