{"id":2393,"date":"2020-12-17T10:04:57","date_gmt":"2020-12-17T10:04:57","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=2393"},"modified":"2020-12-17T10:16:08","modified_gmt":"2020-12-17T10:16:08","slug":"create-and-arrange-a-diagram-in-xamarin-forms","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/create-and-arrange-a-diagram-in-xamarin-forms\/","title":{"rendered":"Create and Arrange a Diagram in Xamarin.Forms"},"content":{"rendered":"<p>In this tutorial we demonstrate how to create a sample diagram and arrange it with the LayeredLayout. The diagram is generated with the Xamarin flowchart library. Here is a screenshot of the final application on an Android phone:<\/p>\n<p align=\"center\"><img decoding=\"async\" src=\"https:\/\/mindfusion.dev\/samples\/xamarin\/diagram\/layeredLayout_android.png\" title=\"The LayeredLayout in Diagramming for Xamarin\" align=\"middle\"><\/p>\n<p><!--more--><\/p>\n<p><strong>I. Project Setup<\/strong><br \/>\nWe start Visual Studio and type &#8220;Xamarin.Forms&#8221; in the search box for project templates. Among the search results we choose &#8220;Mobile App (Xamarin.Forms)&#8221; and press &#8220;Next&#8221;.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/mindfusion.dev\/samples\/xamarin\/diagram\/new_project.png\" title=\"New Xamarin.Forms Project\"><\/p>\n<p>Then we choose a name for our application and press &#8220;Create&#8221;. Select the platforms you want to target: iOS, Android and UWP. Our sample application supports them all.<\/p>\n<p>When the new application is created we create a new folder called References and copy in it all assembly references that are needed by the project. These are:<\/p>\n<ul>\n<li>MindFusion.Common<\/li>\n<li>MindFusion.Diagramming<\/li>\n<li>MindFusion.Licensing<\/li>\n<\/ul>\n<p>for the main, common project, shared by all projects for the platforms.<\/p>\n<p>Then in the Android you need to add references to:<\/p>\n<ul>\n<li>MindFusion.Common<\/li>\n<li>MindFusion.Common.Android<\/li>\n<li>MindFusion.Diagramming<\/li>\n<li>MindFusion.Diagramming.Android<\/li>\n<\/ul>\n<p>For the iOS project you need references to:<\/p>\n<ul>\n<li>MindFusion.Common<\/li>\n<li>MindFusion.Common.iOS<\/li>\n<li>MindFusion.Diagramming<\/li>\n<li>MindFusion.Diagramming.iOS<\/li>\n<\/ul>\n<p>and for the UWP project, you need to add references to:<\/p>\n<ul>\n<li>MindFusion.Common<\/li>\n<li>MindFusion.Common.Universal<\/li>\n<li>MindFusion.Diagramming<\/li>\n<li>MindFusion.Diagramming.Universal<\/li>\n<\/ul>\n<p>Then, in the xaml page of the common project, you need to add reference to the diagramming assembly:<\/p>\n<pre>&lt;ContentPage xmlns=\"http:\/\/xamarin.com\/schemas\/2014\/forms\"\n             xmlns:x=\"http:\/\/schemas.microsoft.com\/winfx\/2009\/xaml\"\n             xmlns:d=\"http:\/\/xamarin.com\/schemas\/2014\/forms\/design\"\n             xmlns:mc=\"http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006\"\n             xmlns:diag=\"clr-namespace:MindFusion.Diagramming;assembly=MindFusion.Diagramming\"\n             mc:Ignorable=\"d\"\n             x:Class=\"LayeredLayout.MainPage\"&gt;&lt;\/ContentPage&gt;\n<\/pre>\n<p>We add the mapping xmlns:diag to the MindFusion.Diagramming namespace in the MindFusion.Diagramming assembly. Then we need to declare an instance of the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?T_MindFusion_Diagramming_Xamarin_DiagramView.htm\" title=\"MindFusion Diagramming Library for Xamarin: DiagramView\">DiagramView<\/a> class, and we do it in XAML:<\/p>\n<pre>&gt;diag:DiagramView x:Name=\"diagramView\"\n\t\t\tHorizontalOptions=\"FillAndExpand\"\n\t\t\tVerticalOptions=\"FillAndExpand\"&gt;<\/pre>\n<p><strong>II. Diagram Initialization<\/strong><\/p>\n<p>In the code-behind file for the MainPage in the common project, we declare a class variable Diagram. We assign it to the diagram, associated with the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?T_MindFusion_Diagramming_Xamarin_DiagramView.htm\" title=\"MindFusion Diagramming Library for Xamarin: DiagramView\">DiagramView<\/a><\/p>\n<pre>Diagram diagram;\n\npublic MainPage()\n{\n\tInitializeComponent();\n\tdiagram = diagramView.Diagram;\n...........................\n...........................\n...........................\n}<\/pre>\n<p>We will create a diagram on button click, so we add a button control to the XAML code:<\/p>\n<pre>&lt;StackLayout\n\tOrientation=\"Horizontal\"\n\tHorizontalOptions=\"Center\"\n\tSpacing=\"5\" Padding=\"5\"&gt;\n&lt;Button\nText=\"Random\"\nBorderColor=\"Black\"\nBackgroundColor=\"Silver\"\nClicked=\"OnRandomClick\"\n\/&gt;<\/pre>\n<p>We will handle the Click event of the button to generate the graph. We do this in a method called RandomGraph. We first, clear all items from the diagram, if there are any:<\/p>\n<pre>  private void RandomGraph()\n  {\n     diagram.ClearAll();\n     ....................\n}<\/pre>\n<p>We generate the nodes with the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?O_T_MindFusion_Diagramming_Xamarin_Factory_CreateShapeNode.htm\" title=\"MindFusion Diagramming Library for Xamarin: CreateShapeNode\">CreateShapeNode<\/a> method of the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?T_MindFusion_Diagramming_Xamarin_Factory.htm\" title=\"MindFusion Diagramming Library for Xamarin: Factory\">Factory<\/a> class that is accessible through a property of diagram.<\/p>\n<pre>ShapeNode node = diagram.Factory.CreateShapeNode(0, 0, 40, 40);\nnode.AnchorPattern = AnchorPattern.TopInBottomOut;<\/pre>\n<p>The node constructor takes as a parameter the location and size of the node with its four values: top, left, width and height. We don&#8217;t care about the location, because we will auto-arrange the nodes with a layout algorithm.<\/p>\n<p>The <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?F_MindFusion_Diagramming_Xamarin_Constants_NoAnchorPattern.htm\" title=\"MindFusion Diagramming Library for Xamarin: AnchorPattern\">AnchorPattern<\/a> property is important and determines the points, where links can dock to the node. We use one of the values of the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?P_MindFusion_Diagramming_Xamarin_DiagramNode_AnchorPattern_0.htm\" title=\"MindFusion Diagramming Library for Xamarin: AnchorPattern\">AnchorPattern<\/a> enumeration. The member TopInBottomOut means that incoming links will enter through the top side of the node and outgoing links will start from its bottom side.<\/p>\n<p>Finally, we create randomly links between nodes. We use again the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?T_MindFusion_Diagramming_Xamarin_Factory_Methods.htm\" title=\"MindFusion Diagramming Library for Xamarin: Factory\">Factory<\/a> class, this time it is the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?O_T_MindFusion_Diagramming_Xamarin_Factory_CreateDiagramLink.htm\" title=\"MindFusion Diagramming Library for Xamarin: CreateDiagramLink\">CreateDiagramLink<\/a> method. We pick random nodes and provide them as parameters to the method:<\/p>\n<pre>private void RandomGraph()\n {\n      diagram.ClearAll();\n\n      for (int i = 0; i &lt; 30; ++i)\n      {\n            int c = diagram.Nodes.Count;\n            int g = 2 + random.Next(15);\n            for (int j = 0; j &lt; g; ++j)\n            {\n             ShapeNode node = diagram.Factory.CreateShapeNode(0, 0, 40, 40);\n             node.AnchorPattern = AnchorPattern.TopInBottomOut;\n               if (j &gt; 0)\n                   diagram.Factory.CreateDiagramLink(diagram.Nodes[diagram.Nodes.Count - 2], node);\n              }\n              if (i &gt; 0)\n              {\n                  for (int j = 0; j &lt; 1 + random.Next(3); ++j)\n                      diagram.Factory.CreateDiagramLink(\n                       diagram.Nodes[random.Next(c)],\n                       diagram.Nodes[c + random.Next(g)]);\n           }\n       }\n  }<\/pre>\n<p><strong>III. Arranging the Diagram<\/strong><\/p>\n<p>We choose the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?T_MindFusion_Diagramming_Xamarin_Layout_LayeredLayout_Members.htm\" title=\"MindFusion Diagramming Library for Xamarin: LayeredLayout\">LayeredLayout<\/a> for the automatic arrangement. Like all other algorithms, it is a member of the Layout namespace and is applied with a single method: arrange The arrange method is a member of the Diagram class. It is called with the instance of the layout, that you want to have applied:<\/p>\n<pre>layout.Arrange(diagram);<\/pre>\n<p>The different algorithms have different options that fine-tune the arranged graph. We apply the Reassign <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?P_MindFusion_Diagramming_Xamarin_Layout_Layout_Anchoring_0.htm\" title=\"MindFusion Diagramming Library for Xamarin: Anchoring\">Anchoring<\/a> type to the graph, which means the links will be reassigned to the positions, where the algorithms find most appropriate. The <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?P_MindFusion_Diagramming_Xamarin_Layout_LayeredLayout_NodeDistance_0.htm\" title=\"MindFusion Diagramming Library for Xamarin: NodeDistance\">NodeDistance<\/a> and <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?P_MindFusion_Diagramming_Xamarin_Layout_LayeredLayout_LayerDistance_0.htm\" title=\"MindFusion Diagramming Library for Xamarin: LayerDistance\">LayerDistance<\/a> properties allow us to control the spacing between the nodes and the layers of the graph. They are used by many of the algorithms.<\/p>\n<p>We apply two more layout-specific properties: <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?P_MindFusion_Diagramming_Xamarin_Layout_LayeredLayout_EnforceLinkFlow_0.htm\" title=\"MindFusion Diagramming Library for Xamarin: EnforceLinkFlow\">EnforceLinkFlow<\/a> and <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/xamarin.diagram\/index.htm?P_MindFusion_Diagramming_Xamarin_Layout_LayeredLayout_StraightenLongLinks_0.htm\" title=\"MindFusion Diagramming Library for Xamarin: StraightenLongLinks\">StraightenLongLinks<\/a> . As their names suggest, they try to make links follow one direction and straighten those links that cross layers.<\/p>\n<p>And with that our tutorial is finished. You can download the sample project from this link:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/xamarin\/diagram\/LayeredLayout.zip\" title=\"LayeredLayout in the Diagram Control for Xamarin: Download Code\">A Sample Diagram in Xamarin with the LayeredLayout: Download Code<\/a><\/p>\n<p>Diagramming for Xamarin: The diagramming component provides all Xamarin-applications with the complete feature-set to create, edit and customize flowcharts, diagrams, graphs, hierarchies, schemes and much more. The control&#8217;s API is intuitive and easy to use with plenty of properties that allow you to control every aspect of the appearance and behavior of the diagram. You have a rich set of predefined nodes and links to choose from, as well table nodes, composite nodes, different brush and pen types, various layout algorithms. Learn more about Diagramming for Xamarin at <a href=\"https:\/\/mindfusion.dev\/xamarin-diagram.html\" title=\"Xamarin Diagram Library\">https:\/\/mindfusion.dev\/xamarin-diagram.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial we demonstrate how to create a sample diagram and arrange it with the LayeredLayout. The diagram is generated with the Xamarin flowchart library. Here is a screenshot of the final application on an Android phone:<\/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,656],"tags":[3,6,349,657],"class_list":["post-2393","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-xamarin","tag-diagram","tag-layout","tag-xamarin","tag-xamarin-tutorial"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-CB","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2393","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=2393"}],"version-history":[{"count":9,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2393\/revisions"}],"predecessor-version":[{"id":2402,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2393\/revisions\/2402"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=2393"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=2393"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=2393"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}