在 C# 中可视化数据结构,例如树和图

Visualize data structures such as trees and graphs in C#

我正在使用 C# 和 LINQPad。是否有任何库或 NuGet 包可以让我可视化图形和树?

例如,如果我创建一个这样的树数据结构:

public class BinaryTree<T>()
{
    public T Value { get; set; }
    public BinaryTree<T> LeftChild { get; set; }
    public BinaryTree<T> RightChild { get; set; }

    //Other methods:
}

我想要一个简单的方法来在我执行程序时显示它,例如,像这样:

什么 library/package 允许我执行此操作(或者是 LINQPad 内置的功能 and/or Visual Studio)?

您最好的选择可能是使用 Microsoft Automatic Graph Layout。这就是 LINQPad 用于语法树可视化工具的内容。

在 LINQPad 5 中,您只需添加对 Microsoft.MSAGL.GraphViewerGDIGraph NuGet 包的引用即可使用此库。然后你可以这样做:

var graph = new Graph ("graph");

graph.AddEdge ("A", "B");
graph.AddEdge ("B", "C");
graph.AddEdge ("A", "C").Attr.Color = Microsoft.Msagl.Drawing.Color.Green;

new GViewer() { Graph = graph, ToolBarIsVisible = false }.Dump();

不幸的是,这在 LINQPad 6(来自 .NET Core 3.1)中不起作用,因为 Microsoft.MSAGL.GraphViewerGDIGraph 是一个 .NET Framework 包,它依赖于 .NET Core 3.1 中删除的 WinForms 工具栏控件.但是,MSAGL 是开源的,并且很容易删除依赖项,正如我在此处所做的那样:

https://github.com/albahari/automatic-graph-layout