【问题标题】:Graphviz (4net) to DOT conversionGraphviz (4net) 到 DOT 的转换
【发布时间】:2013-10-23 10:41:29
【问题描述】:

我有以下代码(使用 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/ 上检查转换后的图像。

图片是这样的:

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

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

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

*我知道我可以使用节点 [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"  ];
}

这给了我:

别担心标签问题,我会解决的。

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;
    }

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

有什么建议吗?

【问题讨论】:

  • 您能否提供您输入到 Graphviz4Net 的图形的 C# 代码,以便它产生此输出?
  • @Steves ,我无法解决这个问题。我用我目前的状态编辑了这个问题。请看一下,如果你知道如何解决它,请告诉我。我的代码中充满了系统详细信息

标签: c# graphviz


【解决方案1】:

如果你的问题是箭头形状,请看下面的代码:

var edge = new Edge<SubGraph<State>>(sourceSubGraph, destinationSubGraph , 
     new Arrow(),  new DiamondArrow());

欲了解更多信息,请查看以下内容:

Graphviz4Net

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-19
    • 2021-06-04
    • 2011-12-10
    • 2015-12-03
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 2011-11-29
    相关资源
    最近更新 更多