{"id":1655,"date":"2016-08-16T07:42:09","date_gmt":"2016-08-16T07:42:09","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=1655"},"modified":"2021-01-21T13:57:31","modified_gmt":"2021-01-21T13:57:31","slug":"lane-diagram-in-javascript","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/lane-diagram-in-javascript\/","title":{"rendered":"Lane diagram in JavaScript"},"content":{"rendered":"<p>In this post we will show how to use the JavaScript diagram library to create a lane diagram. The complete example is available here:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/samples\/javascript\/diagram\/Lanes.zip\">Lanes.zip<\/a><\/p>\n<p>Create a new HTML page and add references to the jQuery library and to the MindFusion.Diagramming library:<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/lane-diagram-in-javascript\/jquery.min.js\">jquery.min.js<\/a>\" <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;\n<span id=\"line250\"><\/span>&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/lane-diagram-in-javascript\/MindFusion.Common.js\">MindFusion.Common.js<\/a>\" <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;\n<span id=\"line251\"><\/span>&lt;<span class=\"start-tag\">script<\/span> <span class=\"attribute-name\">src<\/span>=\"<a class=\"attribute-value\" href=\"view-source:https:\/\/mindfusion.dev\/blog\/lane-diagram-in-javascript\/MindFusion.Diagramming.js\">MindFusion.Diagramming.js<\/a>\" <span class=\"attribute-name\">type<\/span>=\"<a class=\"attribute-value\">text\/javascript<\/a>\"&gt;&lt;\/<span class=\"end-tag\">script<\/span>&gt;<\/pre>\n<p>Create shortcuts to some classes from the diagram model:<\/p>\n<pre>var Events = MindFusion.Diagramming.Events;\nvar Diagram = MindFusion.Diagramming.Diagram;\nvar AnchorPattern = MindFusion.Diagramming.AnchorPattern;\nvar AnchorPoint = MindFusion.Diagramming.AnchorPoint;\nvar Alignment = MindFusion.Diagramming.Alignment;\nvar MarkStyle = MindFusion.Diagramming.MarkStyle;\nvar Style = MindFusion.Diagramming.Style;\nvar Theme = MindFusion.Diagramming.Theme;\nvar LinkShape = MindFusion.Diagramming.LinkShape;\nvar Shape = MindFusion.Diagramming.Shape;\nvar LaneGrid = MindFusion.Diagramming.Lanes.Grid;\nvar LaneHeader = MindFusion.Diagramming.Lanes.Header;\nvar LaneStyle = MindFusion.Diagramming.Lanes.Style;\nvar Rect = MindFusion.Drawing.Rect;\nvar Point = MindFusion.Drawing.Point;\nvar HandlesStyle = MindFusion.Diagramming.HandlesStyle;<\/pre>\n<p>Next, add a canvas the the page and create a diagram from it by using the Diagram.create() method:<\/p>\n<pre>diagram = Diagram.create($(\"#diagram\")[0]);<\/pre>\n<p>You can obtain a reference to the diagram lane grid by calling the Diagram.getLaneGrid() method. You can use the returned object to add rows and columns to the grid and customize its appearance. Finally, to display the grid, call Diagram.setShowLaneGrid(). The customization is omitted here for brevity, but the full code is available in the associated sample project.<\/p>\n<p>The lane grid implies some restrictions to the node and links inside of it. For example, the nodes can be moved only inside the row lanes of the grid. To enforce those restrictions, we will handle several diagram events:<\/p>\n<pre>diagram.addEventListener(Events.nodeCreated, onNodeCreated);\ndiagram.addEventListener(Events.nodeModified, onNodeModified);\ndiagram.addEventListener(Events.linkCreated, onLinkCreated);<\/pre>\n<p>In the nodeCreated event handler, get the gird cell at the top left of the node&#8217;s bounding rectangle and align the node to this cell:<\/p>\n<pre>function onNodeCreated(sender, e) {\n    var node = e.getNode();\n    node.setAnchorPattern(pattern);\n    node.setHandlesStyle(HandlesStyle.HatchHandles3);\n\n    \/\/ Place the box within the grid\n    var bounds = node.getBounds();\n    var topLeft = new Point(bounds.x, bounds.y);\n\n    var cellBoundsReciever = {};\n    if (!grid.getCellFromPoint(topLeft, cellBoundsReciever))\n        return;\n    var cellBounds = cellBoundsReciever.cellBounds;\n\n    var pixel = 1;\n\n    bounds.y = cellBounds.y + pixel;\n    bounds.height = cellBounds.height - 2 * pixel;\n    node.setBounds(bounds);\n}<\/pre>\n<p>Similar rules can be applied to the links in the linkCreated event handler.<\/p>\n<p>The following image illustrates the grid in action:<\/p>\n<p><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/08\/JsDiagramLanes.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1657\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/08\/JsDiagramLanes.png\" alt=\"JavaScript Swimlane Diagram\" width=\"948\" height=\"296\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/08\/JsDiagramLanes.png 948w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/08\/JsDiagramLanes-300x94.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/08\/JsDiagramLanes-768x240.png 768w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2016\/08\/JsDiagramLanes-500x156.png 500w\" sizes=\"auto, (max-width: 948px) 100vw, 948px\" \/><\/a><\/p>\n<p>For more information on MindFusion JavaScript diagram library, see its <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm\">help reference<\/a> and <a href=\"http:\/\/mindfusion.dev\/javascript-diagram.html\">overview page<\/a>.<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post we will show how to use the JavaScript diagram library to create a lane diagram. The complete example is available here: Lanes.zip Create a new HTML page and add references to the jQuery library and to the &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/lane-diagram-in-javascript\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"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":[3,80,423,44,182,138],"class_list":["post-1655","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-sample-code","tag-diagram","tag-javascript","tag-lanes","tag-library","tag-swimlane","tag-swimlane-grid"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-qH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1655","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=1655"}],"version-history":[{"count":6,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1655\/revisions"}],"predecessor-version":[{"id":2601,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1655\/revisions\/2601"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}