{"id":386,"date":"2013-06-19T13:18:12","date_gmt":"2013-06-19T13:18:12","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=386"},"modified":"2021-01-08T15:43:49","modified_gmt":"2021-01-08T15:43:49","slug":"pert-network-charts-in-wpf","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/pert-network-charts-in-wpf\/","title":{"rendered":"PERT network charts in WPF"},"content":{"rendered":"<p>In this post we show how to build a PERT network chart using MindFusion.Diagramming for WPF.<\/p>\n<p>First, create a new WPF project and add a mindfusion.diagramming.wpf.dll reference. Add the appropriate Xaml namespace to the window Xaml file:<\/p>\n<pre>xmlns:diag=\"http:\/\/mindfusion.dev\/diagramming\/wpf\"<\/pre>\n<p>Add a ScrollViewer and Diagram to the window markup:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">scrollviewer<\/span>&gt;\n<span id=\"line246\"><\/span>\t&lt;<span class=\"start-tag\">diag:diagram<\/span> <span class=\"attribute-name\">x:name<\/span>=\"<a class=\"attribute-value\">diagram<\/a>\"&gt;\n<span id=\"line247\"><\/span>&lt;\/<span class=\"end-tag\">diag:diagram<\/span>&gt;&lt;\/<span class=\"end-tag\">scrollviewer<\/span>&gt;<\/pre>\n<p>There are two conventions for drawing PERT networks: activity on arrow and activity on node. Activity on arrow shows activity information on arrows, and the nodes they connect represent project milestones. Activity on node shows activity information on nodes and the arrows between them display relationships. We will use the first convention.<\/p>\n<p>Let\u2019s define a model class to hold activity data:<\/p>\n<pre>public class Activity\n{\n\t\/\/ activity name\n\tpublic string Name { get; set; }\n\n\t\/\/ start milestone name\n\tpublic string From { get; set; }\n\n\t\/\/ end milestone  name\n\tpublic string To { get; set; }\n\n\t\/\/ time in months\n\tpublic int Time { get; set; }\n}\n<\/pre>\n<p>Define a simple PERT project consisting of five milestones and six activities:<\/p>\n<pre>var network = new[]\n{\n\tnew Activity { Name = \"A\", From = \"10\", To = \"30\", Time = 3 },\n\tnew Activity { Name = \"B\", From = \"10\", To = \"20\", Time = 4 },\n\tnew Activity { Name = \"C\", From = \"20\", To = \"50\", Time = 3 },\n\tnew Activity { Name = \"D\", From = \"30\", To = \"40\", Time = 1 },\n\tnew Activity { Name = \"E\", From = \"30\", To = \"50\", Time = 3 },\n\tnew Activity { Name = \"F\", From = \"40\", To = \"50\", Time = 3 }\n};\n<\/pre>\n<p>Create a diagram node for each milestone in the project:<\/p>\n<pre>\/\/ create diagram nodes for each milestone\nforeach (var activity in network)\n{\n\tif (diagram.FindNodeById(activity.From) == null)\n\t{\n\t\tvar node = diagram.Factory.CreateShapeNode(bounds, Shapes.Ellipse);\n\t\tnode.Id = node.Text = activity.From;\n\t}\n\n\tif (diagram.FindNodeById(activity.To) == null)\n\t{\n\t\tvar node = diagram.Factory.CreateShapeNode(bounds, Shapes.Ellipse);\n\t\tnode.Id = node.Text = activity.To;\n\t}\n}\n<\/pre>\n<p>Create a diagram link for each activity and display the activity name and estimated time as link labels:<\/p>\n<pre>\/\/ create a diagram link for each activity\nforeach (var activity in network)\n{\n\tvar link = diagram.Factory.CreateDiagramLink(\n\t\tdiagram.FindNodeById(activity.From),\n\t\tdiagram.FindNodeById(activity.To));\n\n\tvar label1 = link.AddLabel(activity.Name);\n\tlabel1.RelativeTo = RelativeToLink.Segment;\n\tlabel1.Index = 0;\n\tlabel1.VerticalOffset = -20;\n\tlabel1.TextBrush = new SolidColorBrush(Colors.Red);\n\n\tvar label2 = link.AddLabel(string.Format(\"t={0}mo\", activity.Time));\n\tlabel2.RelativeTo = RelativeToLink.Segment;\n\tlabel2.Index = 0;\n\tlabel2.VerticalOffset = 10;\n\tlabel2.TextBrush = new SolidColorBrush(Colors.Blue);\n}\n<\/pre>\n<p>Use automatic graph layout algorithm to arrange the PERT network chart:<\/p>\n<pre>\/\/ arrange the diagram from left to right\nvar layout = new LayeredLayout();\nlayout.Margins = new Size(100, 100);\nlayout.EnforceLinkFlow = true;\nlayout.Orientation = Orientation.Horizontal;\nlayout.Arrange(diagram);\n<\/pre>\n<p>The final result is displayed below.<br \/>\n<img decoding=\"async\" src=\"http:\/\/mindfusion.dev\/_samples\/PertNetworkWpf.png\" alt=\"PERT network chart\" \/><\/p>\n<p>For more information on the PERT methodology, see this Wikipedia article:<br \/>\n<a href=\"http:\/\/en.wikipedia.org\/wiki\/PERT\" target=\"_blank\" rel=\"noopener noreferrer\">http:\/\/en.wikipedia.org\/wiki\/PERT<\/a><\/p>\n<p>The complete sample project is available for download here:<br \/>\n<a href=\"https:\/\/mindfusion.dev\/_samples\/PertNetwork.zip\">https:\/\/mindfusion.dev\/_samples\/PertNetwork.zip<\/a><\/p>\n<p>All MindFusion.Diagramming libraries expose the same programming interface, so most of the sample code shown above will work with only a few modifications in Windows Forms, ASP.NET, Silverlight and Java versions of the control.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we show how to build a PERT network chart using MindFusion.Diagramming for WPF. First, create a new WPF project and add a mindfusion.diagramming.wpf.dll reference. Add the appropriate Xaml namespace to the window Xaml file: xmlns:diag=&#8221;http:\/\/mindfusion.dev\/diagramming\/wpf&#8221; Add a &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/pert-network-charts-in-wpf\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95,74],"tags":[],"class_list":["post-386","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-6e","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/386","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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=386"}],"version-history":[{"count":7,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/386\/revisions"}],"predecessor-version":[{"id":2450,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/386\/revisions\/2450"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=386"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=386"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=386"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}