Reputation: 12494
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.
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?
Upvotes: 34
Views: 25657
Reputation: 20054
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.)
Upvotes: 1
Reputation: 1149
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.
Upvotes: 4
Reputation: 1495
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);
}
}
}
Upvotes: 41
Reputation: 11406
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.
Upvotes: 3