C#有向图生成库

Dam*_*les 24 c# visualization graph

我注意到Visual Studio可以使用DGML生成图形.

我想在我的C#应用​​程序中生成如下图形.

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

它不必像VS那样具有交互性.我只是想生成一个静态的这样的图像并将其保存为一般的图形文件,如PNG.

有没有免费的.NET库?

Sim*_*ing 35

有点晚了,但实际上相对容易实现:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案摇滚! (3认同)
  • xmlWriter 应该被处理掉! (3认同)

Doc*_*own 1

我自己没有尝试过,但阅读了 Graph# 的一些建议。

原始代码以前位于Codeplex,但由于该代码已于 2021 年 1 月 7 日关闭,因此这里有一个 Github 链接,其中找到了几个分支:

https://github.com/search?p=1&q=graphsharp&type=Repositories

(感谢@ergohack 提供它。)