C# 有向图生成库

发布于 2024-12-17 05:07:15 字数 287 浏览 0 评论 0原文

我注意到 Visual Studio 可以使用称为 DGML 的东西生成图形。

我想在我的 C# 应用程序中生成如下图所示的图表。

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

不必像VS一样互动。我只想生成一个静态的这样的图像并将其保存为通用图形文件,例如PNG。

有没有免费的 .NET 库可以实现这个目的?

I noticed that Visual Studio can generate graphs using something called DGML.

I would like to generate a graph like the following one in my C# application.

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

It does not have to be interactive like the VS. I just want to generate a static such image and save it as a general graphics file, such as PNG.

Is there any free .NET library for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

入怼 2024-12-24 05:07:15

有点晚了,但实际上自己实现起来相对容易:

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;
        using (XmlWriter xmlWriter = XmlWriter.Create(xmlpath, settings))
        {
            serializer.Serialize(xmlWriter, g);
        }
    }
}

A little late, but it's actually relatively easy to implement yourself:

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;
        using (XmlWriter xmlWriter = XmlWriter.Create(xmlpath, settings))
        {
            serializer.Serialize(xmlWriter, g);
        }
    }
}
十二 2024-12-24 05:07:15

对于事后的任何人来说,现在有一个 NuGet 包 (GitHub),可以更轻松地创建 DGML 文件,包括支持样式和其他属性元数据。我最初使用了 Simon 的答案(它非常适合我最初的需求),然后在寻找改进输出的方法时偶然发现了 NuGet 包。希望有帮助。

对于任何对方便的工具组合感兴趣的人,我一直将其与 DgmlImage (NuGet 包;注意,这会将自身公开为工具目录中的可执行文件包的,所以我手动将其提取到我可以轻松调用的地方),NDepend (商业但令人难以置信方便的软件,我相信免费开源项目),以及 LINQPad (免费,但值得我的许可证意见)生成图表在尝试设计主要重构时针对相当复杂的代码库进行临时查询。能够使用简单的 LINQ 查询询问有关代码库的问题、生成图表并快速连续显示结果,真是太棒了。请注意,我不隶属于任何这些项目/产品,我刚刚使用这些工具组合获得了如此美妙的体验,我想我会向任何尝试类似东西的人推荐它。

For anybody coming along after the fact, there is now a NuGet Package (GitHub) that makes it easier to create DGML files, including support styling and additional property metadata. I used Simon's answer initially (which worked great for my initial needs) and then stumbled on the NuGet package when looking for ways to improve my output. Hope it helps.

For anybody interested in a handy combination of tools, I've been using this in conjunction with DgmlImage (NuGet Package; note, this exposes itself as an executable in the tools directory of the package, so I manually extracted it to somewhere I could call it easily), NDepend (commercial but incredibly handy software, I believe free to open source projects), and LINQPad (free, but worth the license in my opinion) to generate diagrams from ad-hoc queries against a fairly complex codebase while trying to design a major refactor. It has been fantastic being able to ask questions about the codebase using simple LINQ queries, generate diagrams, and display the results in rapid succession. Note that I am not affiliated with any of these projects/products, I've just had such a wonderful experience using this combination of tools that I thought I'd recommend it to anybody trying to something similar.

我喜欢麦丽素 2024-12-24 05:07:15

我过去曾使用 NodeXL 在 Web 应用程序中生成工作流程图,但它适用于桌面应用程序以及互动。

该描述可能会让您有点困惑,让您认为它仅适用于 Excel。完全不是,您可以直接使用它的对象模型,并从 .NET 中绘制任何您想要的图形。

I have used NodeXL in the past, for generating workflow graphs within a web application, but it is suitable for desktop applications and interaction as well.

The description might confuse you a bit, making you think it's just for Excel. Not at all, you can use it's object model directly and graph whatever you want from .NET.

回忆追雨的时光 2024-12-24 05:07:15

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

原始代码以前位于 Codeplex,但由于该代码已于 01/07/2021 关闭,因此这里是找到多个分支的 Github 链接:

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

(感谢 @ergohack 提供它。)

Did not try it by myself, but read some recommendations for Graph#.

The original code was formerly at Codeplex, but since this is closed at 01/07/2021, here is a Github link which finds several forks:

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

(Thanks to @ergohack for providing it.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文