什么工具可以从 SQL Server 分层数据生成图表?
是否有一个工具可以与 SQL Server 一起使用,从分层数据模型生成树状图?
我正在处理一个大型的地理层次结构,并且希望将其可视化。
这是一个例子。
我有一个 NodeHierarchy 表,用于存储节点之间的层次关系。表中的每一行代表一个节点。除一个节点外,每个节点都有一个父节点。没有父节点的节点是层次结构的根。
以下是我创建表格的方法:
CREATE DATABASE HierarchyTest;
GO
USE HierarchyTest;
GO
CREATE TABLE NodeHierarchy (
PK_NodeID INT NOT NULL
CONSTRAINT PK_NodeHierarchy PRIMARY KEY,
FK_ParentNodeID INT NULL
CONSTRAINT FK_NodeHierarchy_NodeHierarchy FOREIGN KEY
REFERENCES NodeHierarchy(PK_NodeID),
Name NVARCHAR(255) NOT NULL
);
我有一个苏格兰城市和场馆的层次结构示例。苏格兰是等级制度的根源。苏格兰的后裔是城市和场地。在这个层次结构中,父级“包含”一个子级,因此我们说“巴罗兰在格拉斯哥,格拉斯哥在苏格兰”。
此语句使用示例数据填充 NodeHierachy 表:
INSERT INTO NodeHierarchy(PK_NodeID, FK_ParentNodeID, Name)
VALUES
(1, NULL, N'Scotland'),
(2, 1, N'Glasgow'),
(3, 1, N'Edinburgh'),
(4, 1, N'St Andrews'),
(5, 2, N'The Barrowlands'),
(6, 2, N'The Cathouse'),
(7, 2, N'Carling Academy'),
(8, 2, N'SECC'),
(9, 2, N'King Tut''s Wah-Wah Hut'),
(10, 3, N'Henry''s Cellar Bar'),
(11, 3, N'The Bongo Club'),
(12, 3, N'Sneaky Pete''s'),
(13, 3, N'The Picture House'),
(14, 3, N'Potterrow'),
(15, 4, N'Aikman''s'),
(16, 4, N'The Union'),
(17, 4, N'Castle Sands');
SELECT * FROM NodeHierarchy;
的输出:
PK_NodeID FK_ParentNodeID Name
----------- --------------- ---------------------------------
1 NULL Scotland
2 1 Glasgow
3 1 Edinburgh
4 1 St Andrews
5 2 The Barrowlands
6 2 The Cathouse
7 2 Carling Academy
8 2 SECC
9 2 King Tut's Wah-Wah Hut
10 3 Henry's Cellar Bar
11 3 The Bongo Club
12 3 Sneaky Pete's
13 3 The Picture House
14 3 Potterrow
15 4 Aikman's
16 4 The Union
17 4 Castle Sands
(17 row(s) affected)
In Freemind 我画了这个等效图:
什么工具可以以最少的手动操作为我完成此任务?
编辑:最初我说我想可视化层次结构的“全部或部分”。此处发布的解决方案无条件地可视化整个层次结构。这对于小型示例层次结构来说很好,但对于较大的示例层次结构,仅可视化其中的一部分可能更有用。
因为我没有具体说明“部分”的含义,所以我已将其从问题中删除。我在 另一个问题。
Is there a tool that works with SQL Server to generate tree-like diagrams from a hierachical data model?
I am working with a large geographical hierarchy, and would like to visualize it.
Here is an example.
I have a NodeHierarchy table that stores a hierarchical relationship among nodes. Each row in the table represents a node. Each node but one has a parent node. The node that has no parent is the root if the hierarchy.
Here is how I create my table:
CREATE DATABASE HierarchyTest;
GO
USE HierarchyTest;
GO
CREATE TABLE NodeHierarchy (
PK_NodeID INT NOT NULL
CONSTRAINT PK_NodeHierarchy PRIMARY KEY,
FK_ParentNodeID INT NULL
CONSTRAINT FK_NodeHierarchy_NodeHierarchy FOREIGN KEY
REFERENCES NodeHierarchy(PK_NodeID),
Name NVARCHAR(255) NOT NULL
);
I have an example hierachy of Scottish cities and venues. Scotland is the root of the hierachy. The descendants of Scotland are cities and venues. In this hiearchy, a parent 'contains' a child, so we say that e.g. "The Barrowlands is in Glasgow, and Glasgow is in Scotland".
This statement populates the NodeHierachy table with eample data:
INSERT INTO NodeHierarchy(PK_NodeID, FK_ParentNodeID, Name)
VALUES
(1, NULL, N'Scotland'),
(2, 1, N'Glasgow'),
(3, 1, N'Edinburgh'),
(4, 1, N'St Andrews'),
(5, 2, N'The Barrowlands'),
(6, 2, N'The Cathouse'),
(7, 2, N'Carling Academy'),
(8, 2, N'SECC'),
(9, 2, N'King Tut''s Wah-Wah Hut'),
(10, 3, N'Henry''s Cellar Bar'),
(11, 3, N'The Bongo Club'),
(12, 3, N'Sneaky Pete''s'),
(13, 3, N'The Picture House'),
(14, 3, N'Potterrow'),
(15, 4, N'Aikman''s'),
(16, 4, N'The Union'),
(17, 4, N'Castle Sands');
Output of SELECT * FROM NodeHierarchy;
:
PK_NodeID FK_ParentNodeID Name
----------- --------------- ---------------------------------
1 NULL Scotland
2 1 Glasgow
3 1 Edinburgh
4 1 St Andrews
5 2 The Barrowlands
6 2 The Cathouse
7 2 Carling Academy
8 2 SECC
9 2 King Tut's Wah-Wah Hut
10 3 Henry's Cellar Bar
11 3 The Bongo Club
12 3 Sneaky Pete's
13 3 The Picture House
14 3 Potterrow
15 4 Aikman's
16 4 The Union
17 4 Castle Sands
(17 row(s) affected)
In Freemind I drew this equivalent diagram:
What tool can do this for me with a minimum of manual effort?
EDIT: Originally I said that I wanted to visualize "all or part" of the hierarchy. The solution posted here visualizes the entire hierarchy unconditionally. This is fine for the small example hierarchy, but for a larger one, it may be more useful to visualize only part of it.
Because I didn't specify what I meant by "part", I have removed this from the question. I have asked about partial visualization in another question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我研究了 Cade Roux 答案中的线索,并使用 GraphViz 开发了一个解决方案。
要了解 GraphViz,首先我阅读了这篇介绍性文章 和 命令行调用文档。从本文中的示例代码列表成功生成图表后,我对使用自己的数据充满信心。
正如 Cade 所建议的,学习 GraphViz 的 DOT 语言的最佳方法是自己编写它。我研究了本文的示例(清单 1、2 和 6),然后提出了这个
venues.gv
来描述我自己的数据:我将其提供给
circo
,只是GraphViz 中众多图形绘制命令之一,并获得了令人满意的输出:circo -Tpngvenues.gv -ovenues.png
的输出:GraphViz 文件由两个块构成。一个块声明每个节点的标签,另一个块声明图的边。
为了提供每个块的数据,我创建了一个 NodeHierarchy 视图。
该视图提供了用于声明节点标签的数据:
该视图提供了用于声明节点之间的边的数据:
这个名为
generate-graph.ps1
的 Powershell 脚本从视图中选择数据,将其转换为 GraphViz输入,并将其传送到 circo 来生成完整层次结构的可视化,如上所示:脚本必须在 sqlps 中运行,而不是在 powershell 中运行> 以便
Invoke-Sqlcmd
cmdlet 可用。sqlps
的默认工作目录是SQLSERVER
,所以我通过sqlps
运行脚本时必须指定驱动器。这是我用来生成类似上面的图表的命令:
这会在 C 工作目录中输出一个名为
venues.png
的文件。这个 Powershell 解决方案感觉有点不优雅,但这确实满足了我的需要。更有经验的 Powershell 程序员也许能够想出更干净的东西。
I researched the leads in Cade Roux's answer and developed a solution using GraphViz.
To understand GraphViz, first I read this introductory article and the Command-line Invocation documentation. After successfully generating graphs from the example code listing in the article, I felt confident to work with my own data.
As Cade suggested, the best way to learn GraphViz's DOT language is to write it out myself. I studied the article's examples (Listings 1, 2, and 6) and then came up with this
venues.gv
to describe my own data:I fed this to
circo
, just one of the many graph-drawing commands that are part of GraphViz, and got pleasing output:Output of
circo -Tpng venues.gv -o venues.png
:The GraphViz file is structured in two blocks. One block declares a label for each node, and the other block declares the edges of the graph.
To provide the data for each of these blocks, I created a view of
NodeHierarchy
.This view provides the data to declare labels for nodes:
This view provides the data to declare edges between nodes:
This Powershell script called
generate-graph.ps1
selects the data from the views, transforms it into a GraphViz input, and pipes it tocirco
to produce a visualization of the full hierarchy like the one above:The script must be run in
sqlps
instead ofpowershell
so that theInvoke-Sqlcmd
cmdlet is available. The default working directory ofsqlps
isSQLSERVER
, so I have to specify the drive when I run the script throughsqlps
.This is the command I use to generate a graph like the one above:
This outputs a file called
venues.png
in the C working directory.This Powershell solution feels a little inelegant, but this does what I need it to do. A more experienced Powershell programmer might be able to come up with something cleaner.
通过 GraphViz 导出并运行它 - 您甚至不必生成层次结构(只需导出节点和边) - 只需根据您的 NodeID 列分配唯一的节点名称,并在边缘中使用相同的节点名称。
如果你想要一些交互式的东西,微软有一个自动图形布局库,它可以从 .NET 使用。
这是GraphViz 简介。
您要做的是通过使用如下脚本导出 SQL 来输出 DOT 文件: https://data.stackexchange.com/stackoverflow/q/109885/ 它将通过 GraphViz 运行并生成您的图片。
DOT 语法相对简单 - 您可以先手动编写它,然后从 SQL 生成它,然后将其简单地粘贴到文件或其他东西(如 .NET 或 PowerShell)中,后者读取 SQL 集并生成文件。
您可以使用 SSIS 实现自动化。我制作了一个包,写出 DOT 文件并在其上运行 graphviz,并每天保存我们系统的 graphiacl 快照。
Export and run it through GraphViz - you don't even have to generate the hierarchy (just export nodes and edges) - just assign node names which are unique based on your NodeID column and use those same node names in the edges.
If you want something interactive, Microsoft has a Automatic Graph Layout library which can be used from .NET.
Here's an introduction to GraphViz.
What you are going to do is output a DOT file by exporting your SQL using a script like this: https://data.stackexchange.com/stackoverflow/q/109885/ which will run through GraphViz and generate your picture.
The DOT syntax is relatively simple - you can write it by hand first and then generate that from SQL and simply paste it in a file or something else (like .NET or PowerShell) which reads the SQL sets and generates the file.
You can automate that with SSIS. I made a package which wrote out the DOT file and ran graphviz on it and saved a graphiacl snapshot of our system on a daily basis.