2011-11-20 56 views
23

我注意到Visual Studio可以使用稱爲DGML的東西生成圖。C#有向圖生成庫

我想在我的C#應用​​程序中生成如下圖形。

http://bishoponvsto.files.wordpress.com/2010/02/dgml-graph1.jpg

它不必是互動類的VS.我只是想生成一個靜態的圖像,並將其保存爲一般的圖形文件,如PNG。

是否有任何免費的.NET庫?

回答

3

我已經在過去使用NodeXL,爲Web應用程序中生成的工作流圖表,但很適合桌面應用程序和交互。

該說明可能會讓你困惑,讓你覺得它只適用於Excel。一點也不,你可以直接使用它的對象模型,並從.NET中繪製任何你想要的圖形。

+0

+1 NodeXL的WPF控件是非常強大的。以圖形方式表示相關數據非常棒。感謝您的參考! –

+0

描述是在契約混淆。這是否意味着它可以在沒有安裝Excel的系統上工作? –

+0

是的,這是正確的,我沒有安裝Excel。現在,這是幾年前的情況。現在我可能會使用D3爲這個或使用D3的組件,如Dagre,推薦在這裏:http://stackoverflow.com/a/20342129/1373170 –

29

有點晚,但它實際上是比較容易實現自己:

public class DGMLWriter 
{ 
    public struct Graph 
    { 
     public Node[] Nodes; 
     public Link[] Links; 
    } 

    public struct Node 
    { 
     [XmlAttribute] 
     public string Id; 
     [XmlAttribute] 
     public string Label; 

     public Node(string id, string label) 
     { 
      this.Id = id; 
      this.Label = label; 
     } 
    } 

    public struct Link 
    { 
     [XmlAttribute] 
     public string Source; 
     [XmlAttribute] 
     public string Target; 
     [XmlAttribute] 
     public string Label; 

     public Link(string source, string target, string label) 
     { 
      this.Source = source; 
      this.Target = target; 
      this.Label = label; 
     } 
    } 

    public List<Node> Nodes { get; protected set; } 
    public List<Link> Links { get; protected set; } 

    public DGMLWriter() 
    { 
     Nodes = new List<Node>(); 
     Links = new List<Link>(); 
    } 

    public void AddNode(Node n) 
    { 
     this.Nodes.Add(n); 
    } 

    public void AddLink(Link l) 
    { 
     this.Links.Add(l); 
    } 

    public void Serialize(string xmlpath) 
    { 
     Graph g = new Graph(); 
     g.Nodes = this.Nodes.ToArray(); 
     g.Links = this.Links.ToArray(); 

     XmlRootAttribute root = new XmlRootAttribute("DirectedGraph"); 
     root.Namespace = "http://schemas.microsoft.com/vs/2009/dgml"; 
     XmlSerializer serializer = new XmlSerializer(typeof(Graph), root); 
     XmlWriterSettings settings = new XmlWriterSettings(); 
     settings.Indent = true; 
     XmlWriter xmlWriter = XmlWriter.Create(xmlpath, settings); 
     serializer.Serialize(xmlWriter, g); 
    } 
} 
+1

我正在尋找簡單的方法來從我的工作流生成基本的狀態機圖文件。這是迄今爲止最簡單的方法。 – Filip

+2

這個答案很有用! – gerodim

+1

同意這是一個很好的答案!我可以在Visual Studio中打開生成的dgml,但沒有人知道如何在Windows窗體上顯示dgml?謝謝! –