{"id":2271,"date":"2020-03-02T13:50:55","date_gmt":"2020-03-02T13:50:55","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=2271"},"modified":"2021-01-25T16:11:34","modified_gmt":"2021-01-25T16:11:34","slug":"tree-with-nodes-that-have-multiple-parents","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/tree-with-nodes-that-have-multiple-parents\/","title":{"rendered":"Tree with Nodes that Have Multiple Parents"},"content":{"rendered":"<p>Most tree structures are built as hierarchies: the number of nodes increases at each level. In our sample we will look at a tree, where not all branches have a higher number of nodes than their predecessors. In our tree some nodes will have multiple parents e.g. there are nodes that have several ancestors.<\/p>\n<p>You can try the sample online:<\/p>\n<p><a title=\"Layered Layout with Nodes that Have Multiple Parents\" href=\"https:\/\/mindfusion.dev\/javascript-demo.html?sample=multiple-parent-nodes\"><img decoding=\"async\" title=\"Node with Multiple Parents\" src=\"https:\/\/mindfusion.dev\/samples\/javascript\/diagram\/multiple_parents_app.png\" \/><\/a><\/p>\n<p>In order to build this application we use <a title=\"JavaScript Diagram Library\" href=\"https:\/\/mindfusion.dev\/javascript-diagram.html\">MindFusion Diagramming for JavaScript library.<\/a><\/p>\n<p><strong>I. General Settings<\/strong><\/p>\n<p>In a web page we add the code that initializes a Canvas. We give the Canvas an id:<\/p>\n<pre>&lt;div style=\"overflow: visible; height: 100%; margin: 1px; padding: 0px;\"&gt;\n    &lt;canvas id=\"diagram\" width=\"2100\" height=\"2500\"&gt;\n        This page requires a browser that supports HTML 5 Canvas element.\n    &lt;\/canvas&gt;\n&lt;\/div&gt;<\/pre>\n<p>We add references to the two JavaScipt files that provide the diagramming functionality: MindFusion.Diagramming and MindFusion.Common. We also add a reference to a code-behind file that contains the JavaScript code for our application:<\/p>\n<pre>&lt;script src=\"Scripts\/MindFusion.Common.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;\n&lt;script src=\"Scripts\/MindFusion.Diagramming.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;\n&lt;script src=\"MultipleParentNodes.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;<\/pre>\n<p>We have placed those files in a subfolder called Scripts, which is located in our main application folder.<\/p>\n<p><strong>II. Diagram Settings<\/strong><\/p>\n<p>We create the diagram in the window.onload event handler. We want to be sure that all scripts are loaded:<\/p>\n<pre>window.onload = function(e)\n{\n    var diagramEl = document.getElementById('diagram');\n    \/\/ create a Diagram component that wraps the \"diagram\" canvas\n    diagram = AbstractionLayer.createControl(Diagram, null, null, null, diagramEl);\n    diagram.setAllowInplaceEdit(true);\n    diagram.setRouteLinks(true);\n    diagram.setShowGrid(true);\n    diagram.setRoundedLinks(true);\n    diagram.setBounds(new Rect(0, 0, 2000,2500));\n}<\/pre>\n<p>We create the <a title=\"MindFusion Diagramming for JavaScript and React: Diagram\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Diagram_0.htm\">Diagram<\/a> using a reference to the DOM element of the Canvas from the web page. We set its <a title=\"MindFusion Diagramming for JavaScript and React: allowInplaceEdit\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?P_MindFusion_Scheduling_Calendar_allowInplaceEdit_0.htm\">allowInplaceEdit<\/a> property to true, which lets users edit interactively nodes and links. We use <a title=\"MindFusion Diagramming for JavaScript and React: showGrid\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_Diagram_setShowGrid_1_Boolean.htm\">showGrid<\/a> to render a background grid that helps to align nodes and links. We <a title=\"MindFusion Diagramming for JavaScript and React: setRoundedLinks\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_Diagram_setRoundedLinks_1_Boolean.htm\">setRoundedLinks<\/a> and give the diagram a big work are with the <a title=\"MindFusion Diagramming for JavaScript and React: setBounds\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_DiagramNode_setBounds_2_Rect_Boolean.htm\">setBounds<\/a> method.<\/p>\n<p><strong>II. Creating the Diagram Nodes<\/strong><\/p>\n<p>We create the <a title=\"MindFusion Diagramming for JavaScript and React: DiagramNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_DiagramNode_Members_0.htm\">DiagramNode<\/a> -s in a separate method, which we call after the control is created and all settings are made:<\/p>\n<pre>function onLoaded()\n{\n    var nodes = {};\n\n    for(var i = 0; i &lt; 5; i++)\n    {\n        nodes[i] = diagram.getFactory().createShapeNode(new Rect(20, 20, 20, 12));\n        nodes[i].setShape('Rectangle');\n        nodes[i].setBrush({ type: 'SolidBrush', color: '#567939' });\n    };<\/pre>\n<p>We initialize a list, where we will store dynamically those nodes that we want to have reference to. At first we create 5 <a title=\"MindFusion Diagramming for JavaScript and React: ShapeNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_ShapeNode_Members_0.htm\">ShapeNode<\/a> -s that are the first level of the tree. We use the <a title=\"MindFusion Diagramming for JavaScript and React: createShapeNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_Factory_createShapeNode_5_Rect_Number_Number_Number_Number.htm\">createShapeNode<\/a> method of <a title=\"MindFusion Diagramming for JavaScript and React: Factory\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_Diagram_getFactory_0.htm\">Factory<\/a> to create the <a title=\"MindFusion Diagramming for JavaScript and React: ShapeNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_ShapeNode_Members_0.htm\">ShapeNode<\/a> -s and <a title=\"MindFusion Diagramming for JavaScript and React: DiagramLink\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_DiagramLink_Members_0.htm\">DiagramLink<\/a> -s. Note that we will create all nodes with equal bounds. We don&#8217;t have to worry about their location because we will apply an automatic layout at the end.<\/p>\n<p>Factory is available through the <a title=\"MindFusion Diagramming for JavaScript and React: getFactory\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_Diagram_getFactory_0.htm\">getFactory<\/a> method of <a title=\"MindFusion Diagramming for JavaScript and React: Diagram\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Diagram_0.htm\">Diagram<\/a> You do not usually create the class but get an instance of it through the <a title=\"MindFusion Diagramming for JavaScript and React: Diagram\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Diagram_0.htm\">Diagram<\/a> instance.<\/p>\n<p>We use setShape to provide the id af the diagram shape that we want the node to take. A list with the diagram shapes available, together with their id-s can be found in <a title=\"List with Predefined Diagram Shapes in JavaScript\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?CC_refTable_of_Predefined_Shapes_4.htm\">the online help.<\/a><\/p>\n<p>We also use <a title=\"MindFusion Diagramming for JavaScript and React: setBrush\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_DiagramItem_setBrush_1_Object.htm\">setBrush<\/a> to specify the fill of the ShapeNode . In our case we use a <a title=\"MindFusion Diagramming for JavaScript and React: SolidBrush\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Charting_Drawing_Brush.htm\">SolidBrush<\/a> ,but there are other options to choose from.<\/p>\n<p>We create then a single <a title=\"MindFusion Diagramming for JavaScript and React: ShapeNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_ShapeNode_Members_0.htm\">ShapeNode<\/a> that will be the next level:<\/p>\n<pre>var node5 = diagram.getFactory().createShapeNode(new Rect(20, 20, 20, 12 ));\nnode5.setShape('Rectangle');\nnode5.setBrush({ type: 'SolidBrush', color: '#6f9c49' });<\/pre>\n<p>We color it in a slightly lighter shade of green than the nodes at the first level. We then use the <a title=\"MindFusion Diagramming for JavaScript and React: Factory\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_Diagram_getFactory_0.htm\">Factory<\/a> class once again to create the <a title=\"MindFusion Diagramming for JavaScript and React: DiagramLink\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_DiagramLink_Members_0.htm\">DiagramLink<\/a> -s between the 5 nodes at the first level and this one single node at the second level:<\/p>\n<pre>    for(var i = 0; i &lt; 5; i++)\n    { \n        var link = diagram.getFactory().createDiagramLink(nodes[i], node5);\t\n        link.setHeadShape(\"Triangle\");\n        link.setText(\"20%\");\n        link.setHeadShapeSize(3.0);\n        link.setHeadBrush({ type: 'SolidBrush', color: '#7F7F7F' });\n    };<\/pre>\n<p>The <a title=\"MindFusion Diagramming for JavaScript and React: setText\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_DiagramLink_setText_1_String.htm\">setText<\/a> and <a title=\"MindFusion Diagramming for JavaScript and React: setHeadShape\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_DiagramLink_setHeadShape_1_Shape.htm\">setHeadShape<\/a> methods of the <a title=\"MindFusion Diagramming for JavaScript and React: DiagramLink\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_DiagramLink_Members_0.htm\">DiagramLink<\/a> class allow us to specify the label of the link and its shape at the end. There is also <a title=\"MindFusion Diagramming for JavaScript and React: setBaseShape\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_DiagramLink_setBaseShape_1_Shape.htm\">setBaseShape<\/a> that allows us the specify the shape at the start of the <a title=\"MindFusion Diagramming for JavaScript and React: DiagramLink\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_DiagramLink_Members_0.htm\">DiagramLink<\/a>.<\/p>\n<p>The lovely thing about the <a title=\"MindFusion Diagramming for JavaScript and React: Factory\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_Diagram_getFactory_0.htm\">Factory<\/a> class is that it adds automatically the newly created <a title=\"MindFusion Diagramming for JavaScript and React: DiagramItem\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_DiagramItem_0.htm\">DiagramItem<\/a> -s, such as nodes and links, to the <a title=\"MindFusion Diagramming for JavaScript and React: items\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?F_MindFusion_Diagramming_Diagram_items.htm\">items<\/a> collection of the diagram. You can find the newly created DiagramNode -s and DiagramLink -s also as members of the <a title=\"MindFusion Diagramming for JavaScript and React: nodes\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?F_MindFusion_Diagramming_Diagram_nodes.htm\">nodes<\/a> and <a title=\"MindFusion Diagramming for JavaScript and React: links\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?F_MindFusion_Diagramming_Diagram_links.htm\">links<\/a> collections respectively.<\/p>\n<p>Now we have 5 links coming from all 5 nodes from the first level that point to the second level:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/mindfusion.dev\/samples\/javascript\/diagram\/node_multiple_parents.png\" alt=\"Node with Multiple Parents\" \/><\/p>\n<p>We go on doing the rest of the diagram in the same way. We create <a title=\"MindFusion Diagramming for JavaScript and React: ShapeNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_ShapeNode_Members_0.htm\">ShapeNode<\/a> -s with <a title=\"MindFusion Diagramming for JavaScript and React: Factory\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Factory_Members_0.htm\">Factory<\/a> and then bind the nodes with <a title=\"MindFusion Diagramming for JavaScript and React: Factory\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_Diagram_getFactory_0.htm\">Factory<\/a> .<\/p>\n<p><strong>III. Layout<\/strong><\/p>\n<p>We use the <a title=\"MindFusion Diagramming for JavaScript and React: LayeredLayout\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Graphs_LayeredLayout_Members.htm\">LayeredLayout<\/a> to arrange all nodes of the diagram. Since the diagram is not a typical tree, we prefer the <a title=\"MindFusion Diagramming for JavaScript and React: LayeredLayout\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Graphs_LayeredLayout_Members.htm\">LayeredLayout<\/a> to the <a title=\"MindFusion Diagramming for JavaScript and React: TreeLayout\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Graphs_TreeLayout_Members.htm\">TreeLayout<\/a><\/p>\n<pre>var lLayout = new MindFusion.Graphs.LayeredLayout();\ndiagram.arrange(lLayout);<\/pre>\n<p>It is really easy to apply any other algorithm on the diagram &#8211; you just need to create an instance of it and call the arrange method of your diagram to apply this instance. You can quickly change layouts and experiment to see which one provides the best result.<\/p>\n<p>In our case the <a title=\"MindFusion Diagramming for JavaScript and React: LayeredLayout\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Graphs_LayeredLayout_Members.htm\">LayeredLayout<\/a> looks fine and with this we are done building the tree.<\/p>\n<p>You can download the sample together with all libraries used from the following link:<\/p>\n<p align=\"center\"><a title=\"Download Js Graph with Nodes that Have Multiple Parents\" href=\"https:\/\/mindfusion.dev\/samples\/javascript\/diagram\/MultipleParentNodes.zip\">A JavaScript Graph with Nodes that Have Multiple Parents<\/a><\/p>\n<p>Technical support is available through MindFusion <a title=\"Js Diagram Discussion Board\" href=\"https:\/\/mindfusion.dev\/Forum\/YaBB.pl?board=jsdiag_disc\">forum here.<\/a><\/p>\n<p><i>About Diagramming for JavaScript:<\/i> This native JavaScript library provides developers with the ability to create and customize any type of diagram, decision tree, flowchart, class hierarchy, graph, genealogy tree, BPMN diagrams and much more. The control offers rich event set, numerous customization options, animations, graph operations, styling and themes. You have more than 100 predefined nodes, table nodes and more than 15 automatic layout algorithms. Learn more about Diagramming for JavaScript at <a title=\"JavaScript Diagram Library\" href=\"https:\/\/mindfusion.dev\/javascript-diagram.html\">https:\/\/mindfusion.dev\/javascript-diagram.html<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most tree structures are built as hierarchies: the number of nodes increases at each level. In our sample we will look at a tree, where not all branches have a higher number of nodes than their predecessors. In our tree &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/tree-with-nodes-that-have-multiple-parents\/\">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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[95,513,74],"tags":[643,641,640,491,639,642],"class_list":["post-2271","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-javascript","category-sample-code","tag-diagrams","tag-graph-layout","tag-javascript-tree","tag-js-flowchart","tag-js-tree","tag-layered-layout"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-AD","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2271","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=2271"}],"version-history":[{"count":5,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2271\/revisions"}],"predecessor-version":[{"id":2655,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2271\/revisions\/2655"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=2271"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=2271"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=2271"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}