彩票走势图

MindFusion.Diagramming for WinForms使用教程:加载分层数据

翻译|使用教程|编辑:莫成敏|2020-06-11 11:59:06.863|阅读 211 次

概述:本教程说明如何从XML加载图形数据并创建与图形节点和边相对应的图形对象。

# 慧都年终大促·界面/图表报表/文档/IDE等千款热门软控件火热促销中 >>

MindFusion.Diagramming for WinForms是一个能帮助你创建工作流和进程图表的.NET控件;数据库实体关系图表;组织图表;对象层次和关系图表;图表和树。它是基于对象-图表框,表格和箭头类型,将其归类分派给其他并结合成复杂的结构。该控件提供超过预先定义的50多种图表框,如自定义设计样式和对图表框着色等。MindFusion.Diagramming for WinForms现已加入在线订购,现在抢购立享优惠,查看详情>>

点击下载MindFusion.Diagramming for WinForms最新试用版

本教程说明如何从XML加载图形数据并创建与图形节点和边相对应的图形对象。

1、在解决方案资源管理器中右键单击该项目,然后从上下文菜单中选择“添加”->“新建项”。 创建一个名为SampleTree.xml的新XML文件,并以以下形式添加内容:

XML

<?xml version="1.0" encoding="utf-8" ?> 
<Project>
    <Activity Name="Activity 1">
        <Activity  Name="sub-activity 1">
            <Activity  Name="sub-activity 1-1" />
            <Activity  Name="sub-activity 1-2" />
        </Activity>
        <Activity  Name="sub-activity 2">
            <Activity  Name="sub-activity 2-1" />
            <Activity  Name="sub-activity 2-2" />
            <Activity  Name="sub-activity 2-3" />
        </Activity>
        <Activity  Name="sub-activity 3">
            <Activity  Name="sub-activity 3-1" />
            <Activity  Name="sub-activity 3-2" />
        </Activity>
    </Activity>
    <Activity Name="Activity 2">
        <Activity  Name="sub-activity 1">
            <Activity  Name="sub-activity 1-1" />
            <Activity  Name="sub-activity 1-2" />
            <Activity  Name="sub-activity 1-3" />
        </Activity>
        <Activity  Name="sub-activity 2">
            <Activity  Name="sub-activity 2-1" />
            <Activity  Name="sub-activity 2-2" />
        </Activity>
    </Activity>
    <Activity Name="Activity 3">
        <Activity  Name="sub-activity 1">
            <Activity  Name="sub-activity 1-1" />
            <Activity  Name="sub-activity 1-2" />
            <Activity  Name="sub-activity 1-3" />
            <Activity  Name="sub-activity 1-4" />
        </Activity>
        <Activity  Name="sub-activity 2" />
        <Activity  Name="sub-activity 3">
            <Activity  Name="sub-activity 3-1" />
            <Activity  Name="sub-activity 3-2" />
        </Activity>
    </Activity>
</Project>

2、将文件的“生成操作”属性设置为“内容”,将“复制到输出目录”设置为“如果更新则复制”。

3、覆盖以下形式的OnLoad方法:

C#

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
}

Visual Basic

Protected Overrides Sub OnLoad(ByVal e As EventArgs)

    MyBase.OnLoad(e)

End Sub

4、添加窗口类的以下成员以保留默认节点大小。

C#

RectangleF nodeBounds = new RectangleF(0, 0, 24, 6);

Visual Basic

Dim nodeBounds As New RectangleF(0, 0, 24, 6)

5、创建树的根节点,并调用CreateChildren方法,该方法以递归方式构建树。

C#

ShapeNode root = diagram.Factory.CreateShapeNode(nodeBounds);
root.Text = "Project";
XmlDocument document = new XmlDocument();
document.Load("SampleTree.xml");
CreateChildren(root, document.SelectSingleNode("Project"));

Visual Basic

Dim root As ShapeNode = diagram.Factory.CreateShapeNode(nodeBounds)
root.Text = "Project"
Dim document As New XmlDocument()
document.Load("SampleTree.xml")
CreateChildren(root, document.SelectSingleNode("Project"))

6、如下定义CreateChildren。 它以父DiagramNode和父XML元素为参数,迭代子XML元素以构建层次结构的下一层,并通过调用CreateShapeNode和CreateDiagramLink创建相应的图项目。

C#

private void CreateChildren(DiagramNode parentDiagNode, XmlNode parentXmlNode)
{
    foreach (XmlElement element in parentXmlNode.SelectNodes("Activity"))
    {
        ShapeNode node = diagram.Factory.CreateShapeNode(nodeBounds);
        node.Text = element.GetAttribute("Name");
        diagram.Factory.CreateDiagramLink(parentDiagNode, node);
        CreateChildren(node, element);
    }
}

Visual Basic

Private Sub CreateChildren(ByVal parentDiagNode As DiagramNode, ByVal parentXmlNode As XmlNode)

    For Each element As XmlElement In parentXmlNode.SelectNodes("Activity")

        Dim node As ShapeNode = diagram.Factory.CreateShapeNode(nodeBounds)
        node.Text = element.GetAttribute("Name")
        diagram.Factory.CreateDiagramLink(parentDiagNode, node)
        CreateChildren(node, element)

    Next

End Sub

7、使用TreeLayout排列图。 将以下代码添加到OnLoad覆盖的末尾。

C#

TreeLayout layout = new TreeLayout();
layout.Type = TreeLayoutType.Cascading;
layout.Direction = TreeLayoutDirections.LeftToRight;
layout.LinkStyle = TreeLayoutLinkType.Cascading2;
layout.NodeDistance = 3;
layout.LevelDistance = -8; // let horizontal positions overlap
layout.Arrange(diagram);

Visual Basic

Dim layout As New TreeLayout()
layout.Type = TreeLayoutType.Cascading
layout.Direction = TreeLayoutDirections.LeftToRight
layout.LinkStyle = TreeLayoutLinkType.Cascading2
layout.NodeDistance = 3
layout.LevelDistance = -8
' let horizontal positions overlap
layout.Arrange(diagram)

8、生成并运行项目。 如果一切正常,您应该看到此层次结构的表示形式:



想要购买该产品正版授权请点击【商城购买】,想了解更多产品信息请点击


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@cahobeh.cn

文章转载自:

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP