{"id":1569,"date":"2016-06-28T12:07:02","date_gmt":"2016-06-28T12:07:02","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=1569"},"modified":"2021-01-20T17:40:40","modified_gmt":"2021-01-20T17:40:40","slug":"asp-net-org-chart-with-the-diagram-control","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/asp-net-org-chart-with-the-diagram-control\/","title":{"rendered":"ASP.NET Org Chart with the Diagram Control"},"content":{"rendered":"<p>In this post you&#8217;ll learn how to create a beautiful organization chart where most of the work is done by the ASP.NET flowchart control. We&#8217;ll use the capabilities of the control to create nodes, where we&#8217;ll put an image and formatted text. We&#8217;ll create automatically links among the nodes and apply the tree layout algorithm to arrange the diagram. Let&#8217;s start.<\/p>\n<p><strong>I. Add the Necessary DLLs.<\/strong><\/p>\n<p>Create a new blank ASP.NET project and add those of the dll-s of the control that we need for this project:<\/p>\n<div id=\"attachment_1578\" style=\"width: 279px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1578\" class=\"size-full wp-image-1578\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/06\/asp.net-diagram-control-dlls.png\" alt=\"ASP.NET Diagram dll-s needed to build the the org chart\" width=\"269\" height=\"400\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/06\/asp.net-diagram-control-dlls.png 269w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/06\/asp.net-diagram-control-dlls-202x300.png 202w\" sizes=\"auto, (max-width: 269px) 100vw, 269px\" \/><p id=\"caption-attachment-1578\" class=\"wp-caption-text\">ASP.NET Diagram dll-s needed to build the the org chart<\/p><\/div>\n<p>MindFusion.Diagramming, MindFusion.Diagramming.WebForms, MindFusion.Common, MindFusion.Common.WebForms. The asp.net diagram control needs the MindFusion.Diagramming.js file to render its contents. If you add it directly into the project&#8217;s folder you won&#8217;t have to add a reference to it. Since we create a ew project folder &#8220;Scripts&#8221; where we put it, we&#8217;ll have to reference it in code. We&#8217;ll talk about this in the next step.<\/p>\n<p><strong>II. Setup of the Diagram Control.<\/strong><\/p>\n<p>We add a new WebForm file to the project and into the HTML code we register the diagramming dll with the tag &#8220;ndiag&#8221;.<\/p>\n<pre>&lt;%@ Register Assembly=\"MindFusion.Diagramming.WebForms\" Namespace=\"MindFusion.Diagramming.WebForms\" TagPrefix=\"ndiag\" %&gt;\n\n<\/pre>\n<p>We need a reference to a few *.js and *.css files as shown below:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">link<\/span> <span class=\"attribute-name\">href<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/asp-net-org-chart-with-the-diagram-control\/common\/jquery-ui.min.css\">common\/jquery-ui.min.css<\/a>\" <span class=\"attribute-name\">rel\\<\/span>=\"<a class=\"attribute-value\">stylesheet<\/a>\"&gt;\n<span id=\"line260\"><\/span>&lt;<span class=\"start-tag\">link<\/span> <span class=\"attribute-name\">href<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/asp-net-org-chart-with-the-diagram-control\/common\/samples.css\">common\/samples.css<\/a>\" <span class=\"attribute-name\">rel<\/span>=\"<a class=\"attribute-value\">stylesheet<\/a>\"&gt;\n<span id=\"line261\"><\/span>&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\" <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/asp-net-org-chart-with-the-diagram-control\/common\/jquery.min.js\">common\/jquery.min.js<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;\n<span id=\"line262\"><\/span>&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\" <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/asp-net-org-chart-with-the-diagram-control\/common\/jquery-ui.min.js\">common\/jquery-ui.min.js<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;\n<span id=\"line263\"><\/span>&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\" <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/asp-net-org-chart-with-the-diagram-control\/common\/samples.js\">common\/samples.js<\/a>\"&gt;<\/pre>\n<p>The *.js files are located in a folder &#8220;common&#8221; in the project. Of course, you can reference them from their respective online locations.<\/p>\n<p>Don&#8217;t forget the ScriptManager:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">asp:scriptmanager<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">ScriptManager1<\/a>\" <span class=\"attribute-name\">runat<\/span>=\"<a class=\"attribute-value\">server<\/a>\"&gt;\n<span id=\"line270\"><\/span>\n<span id=\"line271\"><\/span>&lt;\/<span class=\"end-tag\">asp:scriptmanager<\/span>&gt;<\/pre>\n<p>Now it&#8217;s time for the diagram &#8211; we add it in the HTML of the page using the &#8220;ndiag&#8221; tag that we&#8217;ve defined above.<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">ndiag:diagramview<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">fc<\/a>\" <span class=\"attribute-name\">runat<\/span>=\"<a class=\"attribute-value\">server<\/a>\" <span class=\"attribute-name\">clientsidemode<\/span>=\"<a class=\"attribute-value\">Canvas<\/a>\" <span class=\"attribute-name\">style<\/span>=\"<a class=\"attribute-value\">position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;<\/a>\" <span class=\"attribute-name\">jslibrarylocation<\/span>=\"<a class=\"attribute-value\">Scripts\/MindFusion.Diagramming.js<\/a>\" <span class=\"attribute-name\">treeexpandedscript<\/span>=\"<a class=\"attribute-value\">onTreeExpanded<\/a>\"&gt;\n<span id=\"line275\"><\/span>      &lt;<span class=\"start-tag\">diagram<\/span> <span class=\"attribute-name\">autoresize<\/span>=\"<a class=\"attribute-value\">RightAndDown<\/a>\" <span class=\"attribute-name\">defaultshape<\/span>=\"<a class=\"attribute-value\">Rectangle<\/a>\" <span class=\"attribute-name\">shapehandlesstyle<\/span>=\"<a class=\"attribute-value\">DashFrame<\/a>\"&gt;\n<span id=\"line276\"><\/span>&lt;\/<span class=\"end-tag\">diagram<\/span>&gt;&lt;\/<span class=\"end-tag\">ndiag:diagramview<\/span>&gt;<\/pre>\n<p>The Diagram is in a DiagramView control that will render it contents. Node the &#8220;JsLibraryLocation&#8221; attribute &#8211; we use it to point to the location of the Diagramming.js file which we&#8217;ve placed in the Scripts folder in step 1.<\/p>\n<p><strong>III. Building the Diagram<\/strong><\/p>\n<p>We get the diagram object that we&#8217;ve defined in HTML like that:<\/p>\n<pre>Diagram diagram = fc.Diagram;\n\n<\/pre>\n<p>The data for the org chart is located in a XML file. The structure is something like this:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">employee<\/span>&gt;\n<span id=\"line289\"><\/span>  &lt;<span class=\"start-tag\">name<\/span>&gt;Martin Larovsky&lt;\/<span class=\"end-tag\">name<\/span>&gt;  \n<span id=\"line290\"><\/span>  &lt;<span class=\"start-tag\">photourl<\/span>&gt;http:\/\/mindfusion.dev\/_samples\/ASP.NET_OrgChart\/martin-larovsky.png&lt;\/<span class=\"end-tag\">photourl<\/span>&gt;\n<span id=\"line291\"><\/span>  &lt;<span class=\"start-tag\">title<\/span>&gt;COO&lt;\/<span class=\"end-tag\">title<\/span>&gt;\n<span id=\"line292\"><\/span>  &lt;<span class=\"start-tag\">level<\/span>&gt;Executive&lt;\/<span class=\"end-tag\">level<\/span>&gt;\n<span id=\"line293\"><\/span>  &lt;<span class=\"start-tag\">date<\/span>&gt;Aug 2015&lt;\/<span class=\"end-tag\">date<\/span>&gt;  \n<span id=\"line294\"><\/span>    &lt;<span class=\"start-tag\">employee<\/span>&gt;\n<span id=\"line295\"><\/span>      &lt;<span class=\"start-tag\">name<\/span>&gt;Tanja Orsy&lt;\/<span class=\"end-tag\">name<\/span>&gt;\n<span id=\"line296\"><\/span>..........................................\n<span id=\"line297\"><\/span>\n<span id=\"line298\"><\/span>&lt;\/<span class=\"end-tag\">employee<\/span>&gt;&lt;\/<span class=\"end-tag\">employee<\/span>&gt;<\/pre>\n<p>Each new hierarchy level is a list of Employee tags contained within the upper Employee node.<\/p>\n<p>The first node is the top of the tree and we read it separately. First we open the XML document and then we get the first employee:<\/p>\n<pre>XmlDocument xmlDoc = new XmlDocument();\nxmlDoc.Load(\"http:\/\/mindfusion.dev\/_samples\/ASP.NET_OrgChart\/employees.xml\");\nXmlNode ceo = xmlDoc.SelectSingleNode(\"Employee\");\n\n<\/pre>\n<p>We use the methods of the diagram control to create a new shape node and assign its properties to the data that we&#8217;ve read from the XML file:<\/p>\n<pre>ShapeNode root = diagram.Factory.CreateShapeNode(nodeBounds);\nroot.Text = \"<b>\" + ceo[\"Name\"].InnerText + \"<\/b> (\" + ceo[\"Title\"].InnerText + \") since \" +\nceo[\"Date\"].InnerText + \"\"; \nroot.Shape = Shape.FromId(\"RoundRect\"); \nroot.TextPadding = new Thickness(0, 0, 3, 0); \nroot.Brush = new MindFusion.Drawing.SolidBrush(Color.FromArgb(121, 109, 173)); \nroot.TextFormat.Alignment = StringAlignment.Far; \nroot.EnableStyledText = true; \nroot.ImageUrl = ceo[\"PhotoUrl\"].InnerText; \nroot.ImageAlign = MindFusion.Drawing.ImageAlign.MiddleLeft;\n\n<\/pre>\n<p>The <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/netdiagram\/index.htm?T_MindFusion_Diagramming_Factory.htm\">Factory class<\/a> enables us to create different nodes within given bounds. HTML formatting of the text is supported because we have turned on the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/netdiagram\/index.htm?P_MindFusion_Diagramming_ShapeNode_EnableStyledText_0.htm\">EnableStyledText<\/a> property. The node shape is rounded rectangle for aesthetic reasons. The texi is aligned to the right and the image &#8211; to the left of the node. We use the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/netdiagram\/index.htm?P_MindFusion_Diagramming_ShapeNode_ImageUrl_0.htm\">ImageUrl<\/a> property because the image is uploaded online and we just tell the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/netdiagram\/index.htm?T_MindFusion_Diagramming_ShapeNode.htm\">ShapeNode<\/a> from where to read it.<\/p>\n<p>The nodes are read and created in a method that uses recurrence to cycle through all of them &#8211; you can see it in the sample. It has one line that&#8217;s worth noting:<\/p>\n<pre>\/\/cycles through all nodes using recurrence and creates a DiagramNode for all employees\nprivate void CreateChildren(Diagram diagram, DiagramNode parentDiagNode, XmlNode parentXmlNode)\n{\nforeach (XmlElement element in parentXmlNode.SelectNodes(\"Employee\"))\n{\n.................................................................\ndiagram.Factory.CreateDiagramLink(parentDiagNode, node);\n\n<\/pre>\n<p>Note how we instruct the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/netdiagram\/index.htm?T_MindFusion_Diagramming_Factory.htm\">Factory<\/a> to create a link between each node and its parent. This way we have the diagram ready.<\/p>\n<p><strong>IV. The Layout<\/strong><\/p>\n<p>If you run the application you&#8217;ll see just one node. It&#8217;s actually a pile of nodes. We need to arrange it but luckily the ASP.NET Diagram control has an impressive selection of layout algorithms. In our case the Tree layout is the perfect choice:<\/p>\n<pre>TreeLayout layout = new TreeLayout();\nlayout.Type = TreeLayoutType.Cascading;\nlayout.Direction = TreeLayoutDirections.LeftToRight;\nlayout.LinkStyle = TreeLayoutLinkType.Cascading2;\nlayout.NodeDistance = 3;\nlayout.LevelDistance = -8; \/\/ let horizontal positions overlap\nlayout.Arrange(diagram);\n\n<\/pre>\n<p>The <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/netdiagram\/index.htm?T_MindFusion_Diagramming_Layout_TreeLayout.htm\">TreeLayout<\/a> class has plenty of customization options, here we have selected those that would make an org chart look best. You can experiment with the full set of options and see what looks right in your particular scenario.<\/p>\n<p>Here is the final result:<\/p>\n<div id=\"attachment_1577\" style=\"width: 710px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1577\" class=\"size-full wp-image-1577\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/06\/asp.net-org-chart.png\" alt=\"ASP.NET Org Chart\" width=\"700\" height=\"513\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/06\/asp.net-org-chart.png 700w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/06\/asp.net-org-chart-300x220.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/06\/asp.net-org-chart-409x300.png 409w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><p id=\"caption-attachment-1577\" class=\"wp-caption-text\">ASP.NET Org Chart<\/p><\/div>\n<p>The source code of the project together with all necessary libraries and scripts can be downloaded from here:<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/mindfusion.dev\/_samples\/ASP.NET Org Browser.zip\" data-wplink-url-error=\"true\">Download ASP.NET Organization Browser Sample<\/a><\/p>\n<p>You are welcome to ask any questions about the ASP.NET Diagram control at <a href=\"http:\/\/mindfusion.dev\/Forum\/YaBB.pl?board=netdg_disc\">MindFusion discussion board<\/a> or per e-mail at <a href=\"mailto:support@mindfusion.dev\">support@mindfusion.dev.<\/a><\/p>\n<p><a href=\"http:\/\/mindfusion.dev\/netdiagram.html\">Click here to visit the official page of MindFusion ASP.NET Diagram Control.<\/a><\/p>\n<p>We hope you find this tutorial useful and thank you for your interest in MindFusion developer tools.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post you&#8217;ll learn how to create a beautiful organization chart where most of the work is done by the ASP.NET flowchart control. We&#8217;ll use the capabilities of the control to create nodes, where we&#8217;ll put an image and &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/asp-net-org-chart-with-the-diagram-control\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95,74],"tags":[],"class_list":["post-1569","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-sample-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-pj","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=1569"}],"version-history":[{"count":8,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1569\/revisions"}],"predecessor-version":[{"id":2594,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1569\/revisions\/2594"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}