{"id":7630,"date":"2020-09-17T10:00:40","date_gmt":"2020-09-17T07:00:40","guid":{"rendered":"https:\/\/dhtmlx.com\/blog\/?p=7630"},"modified":"2025-03-11T13:24:21","modified_gmt":"2025-03-11T10:24:21","slug":"using-layout-algorithms-arrange-diagram-shapes-automatically","status":"publish","type":"post","link":"https:\/\/dhtmlx.com\/blog\/using-layout-algorithms-arrange-diagram-shapes-automatically\/","title":{"rendered":"Using Layout Algorithms to Arrange Diagram Shapes Automatically"},"content":{"rendered":"<p>Modern diagramming libraries frequently come with a special tool that helps to speed up the creation of different diagram types \u2013 a layout algorithm. This feature is intended for automatic arrangement of diagram elements. Based on specific rules, the algorithm calculates the positioning of diagram shapes and connectors and places them in such a way that even the most complex diagrams become clear and informative.<\/p>\n<p>In this post, we will take a closer look at the most popular auto layout algorithm types, see how they are implemented in DHTMLX Diagram, and consider from a more technical standpoint how web developers can apply the auto layout function using our diagramming component.<\/p>\n<h5>Types of Automatic Layout Algorithms<\/h5>\n<p>Layout algorithms may vary in numerous aspects, but most commonly they differ in approaches to how shapes are interconnected i.e. what kind of connectors are used in the diagram.<\/p>\n<p>Based on this attribute, layout algorithms are divided into many types, but the most popular of them are hierarchical layout and orthogonal layout.<\/p>\n<h6>Hierarchical Layout Algorithm<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/Hierarchical-layout.gif\" alt=\"hierarchical layout algorithm by DHTMLX\" width=\"1336\" height=\"804\" class=\"aligncenter size-full wp-image-7646\" \/><em><\/p>\n<p style=\"text-align:center;\">Hierarchical layout in the org chart by DHTMLX<\/p>\n<p><\/em><\/p>\n<p>A hierarchical layout algorithm uses straight and diagonal connectors for arranging loaded data in a hierarchical order. It helps to clearly show the principal direction (or flow) within a diagram. Here shapes of the diagram are commonly positioned in hierarchically structured layers with most of the connectors oriented in the same direction (left-to-right, top-to-bottom, etc.). Thanks to a small possibility of crossings between connectors, the algorithm brings more clarity to the diagram.<\/p>\n<p>The hierarchical layout algorithm is popular in the application areas where it is necessary to clearly show dependencies between diagram nodes. For instance, it is widely used for visualizing business activities (workflow diagrams, PERT charts, org charts), software engineering (activity diagrams), database modeling, electrical engineering, and other fields with a strong focus on the direction of data flow.<\/p>\n<h6>Orthogonal Layout Algorithm<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/image2.gif\" alt=\"Orthogonal Layout Algorithm\" width=\"940\" height=\"560\" class=\"aligncenter size-full wp-image-7634\" \/><em><\/p>\n<p style=\"text-align:center;\">Orthogonal layout in the block diagram by DHTMLX<\/p>\n<p><\/em><\/p>\n<p>An orthogonal layout utilizes right-angle connectors for presenting directed or undirected graphs allowing you to quickly understand the relationships between nodes. Like the hierarchical layout, the orthogonal layout algorithm helps to minimize the chances of crossings and also ensures a lack of overlaps and as few as possible bends. Thus, diagrams, especially complex ones, become more compact and readable.<\/p>\n<p>An orthogonal layout algorithm is an appropriate option for mid-sized sparse graphs. It can be applied in a database schema, system management, or software engineering. This algorithm can be frequently encountered in ER diagrams or circuit diagrams.<\/p>\n<h5>How to Use Layout Algorithms in DHTMLX Diagram<\/h5>\n<p>When utilizing DHTMLX <a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxDiagram\/\" rel=\"noopener\" target=\"_blank\">JavaScript diagram<\/a> library for data visualization, you can arrange diagram shapes automatically either in the <strong>direct<\/strong> mode or in the <strong>edges<\/strong> mode. The <strong>direct<\/strong> mode allows connecting diagram shapes with diagonal lines in a hierarchical structure. The <strong>edges<\/strong> mode engages right-angle connectors and helps you create orthogonal graphs.<\/p>\n<p>In order to configure the automatic layout algorithm, web developers have two possible options:<\/p>\n<ul>\n<li>Employ the <strong><a href=\"https:\/\/docs.dhtmlx.com\/diagram\/api\/diagram\/autoplace_method\/\" rel=\"noopener\" target=\"_blank\">autoPlace<\/a><\/strong> method. <\/li>\n<\/ul>\n<p>By default, this method will implement the <strong>direct<\/strong> mode of linking shapes.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/image4.png\" alt=\"DHTMLX Diagram -Direct mode\" width=\"602\" height=\"326\" class=\"aligncenter size-full wp-image-7635\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/image4.png 602w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/image4-300x162.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><em><\/p>\n<p style=\"text-align:center;\">Diagonal connectors in the direct mode<\/p>\n<p><\/em><\/p>\n<p>Optionally, you can change it to the <strong>edges<\/strong> mode via the <strong>mode<\/strong> parameter. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/image5.png\" alt=\"DHTMLX Diagram-Edges Mode\" width=\"602\" height=\"326\" class=\"aligncenter size-full wp-image-7637\" srcset=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/image5.png 602w, https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/image5-300x162.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><em><\/p>\n<p style=\"text-align:center;\">Right-angle connectors in the edges mode<\/p>\n<p><\/em><\/p>\n<p>If you want to place several diagrams next to each other on the grid, you can apply the <strong>graphPadding<\/strong> parameter that determines the interval between diagrams (200px, by default).<\/p>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\"><span class=\"kw1\">var<\/span> diagram <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> dhx.<span class=\"me1\">Diagram<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;diagram&quot;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\ndiagram.<span class=\"me1\">data<\/span>.<span class=\"me1\">parse<\/span><span class=\"br0\">&#40;<\/span>data<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\ndiagram.<span class=\"me1\">autoPlace<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp;mode<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;edges&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp;graphPadding<span class=\"sy0\">:<\/span> <span class=\"nu0\">100<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/Hierarchical-layout-algorithm.gif\" alt=\"DHTMLX Auto Layout - Edges Mode\" width=\"1360\" height=\"828\" class=\"aligncenter size-full wp-image-7648\" \/><em><\/p>\n<p style=\"text-align:center;\">Auto layout in the edges mode<\/p>\n<p><\/em><\/p>\n<ul>\n<li>Set the same configuration parameters using the <a href=\"https:\/\/docs.dhtmlx.com\/diagram\/api\/diagram\/autoplacement_property\/\" rel=\"noopener\" target=\"_blank\"><strong>autoplacement <\/strong><\/a>property<\/li>\n<\/ul>\n<div class=\"codecolorer-container javascript default codecolorer-noborder\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"javascript codecolorer\"><span class=\"kw1\">var<\/span> diagram <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> dhx.<span class=\"me1\">Diagram<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;container&quot;<\/span><span class=\"sy0\">,<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; autoplacement<span class=\"sy0\">:<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; mode<span class=\"sy0\">:<\/span> <span class=\"st0\">&quot;edges&quot;<\/span><span class=\"sy0\">,<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; graphPadding<span class=\"sy0\">:<\/span> <span class=\"nu0\">100<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><\/div><\/div>\n<p>The graph layout algorithm can also be used in the Diagram Editor. Its settings can be specified via the <strong>autoplacement<\/strong> config. This advanced feature helps end-users to build elaborate and neat-looking diagrams right from the UI much faster. Users just need to drag all required shapes from the left panel, link them with each other via connectors, and click on the \u201cAuto Layout\u201d button. The connected shapes will be instantly assembled into a well-organized diagram.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/dhtmlx.com\/blog\/wp-content\/uploads\/2020\/09\/Layout-algorithm-in-editor.gif\" alt=\"Diagram Editor with Auto Layout\" width=\"1920\" height=\"880\" class=\"aligncenter size-full wp-image-7649\" \/><em><\/p>\n<p style=\"text-align:center;\">Auto layout function in DHTMLX Diagram editor<\/p>\n<p><\/em><\/p>\n<p>If you need a feature-packed diagramming component with the auto layout functionality for your project and still haven\u2019t tried DHTMLX Diagram, we encourage you to download a <a href=\"https:\/\/dhtmlx.com\/docs\/products\/dhtmlxDiagram\/download.shtml\" rel=\"noopener\" target=\"_blank\">free 30-day trial version<\/a> and estimate its capabilities in a real-case scenario.<\/p>\n<h5>Related Materials:<\/h5>\n<ul>\n<li>Documentation <a href=\"https:\/\/docs.dhtmlx.com\/diagram\/guides\/manipulating_items\/#arranging-shapes-automatically\" rel=\"noopener\" target=\"_blank\">\u201cArranging shapes automatically\u201d<\/a><\/li>\n<li>Sample <a href=\"https:\/\/snippet.dhtmlx.com\/p1ybrkz2?mode=wide\" rel=\"noopener\" target=\"_blank\">\u201cAutoplacement in direct mode\u201d<\/a><\/li>\n<li>Sample <a href=\"https:\/\/snippet.dhtmlx.com\/1i65txcw\" rel=\"noopener\" target=\"_blank\">\u201cAutoplacement in edges mode\u201d<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Modern diagramming libraries frequently come with a special tool that helps to speed up the creation of different diagram types \u2013 a layout algorithm. This feature is intended for &#8230;<\/p>\n","protected":false},"author":101014,"featured_media":7652,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[71,1],"tags":[33,58],"class_list":["post-7630","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","category-uncategorized","tag-all-posts","tag-diagram"],"_links":{"self":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/7630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/users\/101014"}],"replies":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/comments?post=7630"}],"version-history":[{"count":18,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/7630\/revisions"}],"predecessor-version":[{"id":13554,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/posts\/7630\/revisions\/13554"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media\/7652"}],"wp:attachment":[{"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/media?parent=7630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/categories?post=7630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dhtmlx.com\/blog\/wp-json\/wp\/v2\/tags?post=7630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}