{"id":2258,"date":"2020-02-06T08:11:35","date_gmt":"2020-02-06T08:11:35","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=2258"},"modified":"2020-08-03T13:09:00","modified_gmt":"2020-08-03T13:09:00","slug":"organization-services-chart-with-the-javascript-diagram-library","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/organization-services-chart-with-the-javascript-diagram-library\/","title":{"rendered":"Organization Services Chart with the JavaScript Diagram Library"},"content":{"rendered":"<p>In this blog post we will use <a title=\"MindFusion JavaScript Diagram Library\" href=\"https:\/\/mindfusion.dev\/javascript-diagram.html\">MindFusion JavaScript Diagram library<\/a> to build an application that allows the users to create a diagram with nodes that hold icons and text. We have used this online diagram application to build an infogram for the services portfolio of an imaginary company. However, you can replace the icons and use it as a tool to build diagrams of whatever domain is of interest to you.<\/p>\n<p>In the sample diagram we use SVG images from Google&#8217;s material design icons set. You can download them from <a title=\"Material Design Icons from Google\" href=\"https:\/\/github.com\/google\/material-design-icons\">https:\/\/github.com\/google\/material-design-icons<\/a><a>.<\/a><\/p>\n<p><a><br \/><\/a><a title=\"Organization Services Diagram\" href=\"https:\/\/mindfusion.dev\/samples\/javascript\/diagram\/OrgInfoDiagram\/OrgInfoDiagram.html\"><img decoding=\"async\" title=\"Organization Info Diagram\" src=\"https:\/\/mindfusion.dev\/samples\/javascript\/diagram\/org_info_diagram.png\" \/><\/a><\/p>\n<p>Our sample application uses a <a title=\"MindFusion Diagramming for JavaScript: Diagram\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Diagram_0.htm\">Diagram<\/a> and a <a title=\"MindFusion Diagramming for JavaScript: NodeListView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_NodeListView_Members.htm\">NodeListView<\/a> controls &#8211; both of them part of <a title=\"MindFusion JavaScript Diagram Library\" href=\"https:\/\/mindfusion.dev\/javascript-diagram.html\">JsDiagram<\/a>.<\/p>\n<p><strong>I. General Settings<\/strong><\/p>\n<p>We need to add two HTML Canvas elements to the web page where the sample application will be. One is for the <a title=\"MindFusion Diagramming for JavaScript: NodeListView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_NodeListView_Members.htm\">NodeListView<\/a> and the other is for the <a title=\"MindFusion Diagramming for JavaScript: Diagram\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Diagram_0.htm\">Diagram<\/a><\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">div<\/span> <span class=\"attribute-name\">style<\/span>=\"<a class=\"attribute-value\">width: 70px; height: 100%; overflow-y: auto; overflow-x: hidden; position: absolute; top: 5px; left: 0px; right: 0px; bottom: 0px;<\/a>\"&gt;\n<span id=\"line327\"><\/span>    &lt;<span class=\"start-tag\">canvas<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">nodeList<\/a>\" <span class=\"attribute-name\">width<\/span>=\"<a class=\"attribute-value\">200<\/a>\"&gt;&lt;\/<span class=\"end-tag\">canvas<\/span>&gt;\n<span id=\"line329\"><\/span>&lt;\/<span class=\"end-tag\">div<\/span>&gt;\n<span id=\"line330\"><\/span>...........................\n&lt;<span class=\"start-tag\">div<\/span> <span class=\"attribute-name\">style<\/span>=\"<a class=\"attribute-value\">position: absolute; left: 70px; top: 5px; right: 0px; bottom: 0px; overflow: auto;<\/a>\"&gt;\n<span id=\"line334\"><\/span>    &lt;<span class=\"start-tag\">canvas<\/span> <span class=\"attribute-name\">id<\/span>=\"<a class=\"attribute-value\">diagram<\/a>\" <span class=\"attribute-name\">width<\/span>=\"<a class=\"attribute-value\">2100<\/a>\" <span class=\"attribute-name\">height<\/span>=\"<a class=\"attribute-value\">2100<\/a>\"&gt;\n        This page requires a browser that supports HTML 5 Canvas element.\n    &lt;\/<span class=\"end-tag\">canvas<\/span>&gt;\n<span id=\"line337\"><\/span>&lt;\/<span class=\"end-tag\">div<\/span>&gt;<\/pre>\n<p>We place the two Canvas elements using CSS next to each other. The <a title=\"MindFusion Diagramming for JavaScript: NodeListView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_NodeListView_Members.htm\">NodeListView<\/a> is in a long and narrow Canvas and next to it is the <a title=\"MindFusion Diagramming for JavaScript: Diagram\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Diagram_0.htm\">Diagram<\/a> Canvas, which has a big size of 211 pixels. It is important that we provide the two Canvases with id-s: we will refer to them later in the JavaScript code using these id-s.<\/p>\n<p>Then we add references to the two JavaScript files that we use from the Diagram library &#8211; MindFusion.Diagramming and MindFusion.Common:<\/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\/organization-services-chart-with-the-javascript-diagram-library\/Scripts\/MindFusion.Common.js\">Scripts\/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=\"line341\"><\/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\/organization-services-chart-with-the-javascript-diagram-library\/Scripts\/MindFusion.Diagramming.js\">Scripts\/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;\n<span id=\"line342\"><\/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\/organization-services-chart-with-the-javascript-diagram-library\/OrgInfoDiagram.js\">OrgInfoDiagram.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>The last JavaScript file that we reference is OrgInfoDiagram and there we keep the code for our application.<\/p>\n<p><strong>II. The Diagram<\/strong><\/p>\n<p>We create the <a title=\"MindFusion Diagramming for JavaScript: Diagram\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Diagram_0.htm\">Diagram<\/a> object by using the reference to the Canvas that will render it:<\/p>\n<pre>\/\/ create a Diagram component that wraps the \"diagram\" canvas\ndiagram = Diagram.create(document.getElementById(\"diagram\"));<\/pre>\n<p>Then we use an instance of the <a title=\"MindFusion Diagramming for JavaScript: Style\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Style_0.htm\">Style<\/a> class to create a style that will determine how newly created &#8211;DiagramNode -s are styled:<\/p>\n<pre>\/\/styling\nvar shapeNodeStyle = new Style();\nshapeNodeStyle.setBrush({ type: 'SolidBrush', color: '#e0e9e9' });\nshapeNodeStyle.setStroke(\"Orange\");\nshapeNodeStyle.setFontName(\"Verdana\");\nshapeNodeStyle.setFontSize(9);   \nshapeNodeStyle.setNodeEffects([new GlassEffect()]);<\/pre>\n<p>The <a title=\"MindFusion Diagramming for JavaScript: Style\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Style_0.htm\">Style<\/a> instance applies <a title=\"MindFusion Diagramming for JavaScript: GlassEffect\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_GlassEffect_Members_0.htm\">GlassEffect<\/a> on the new nodes and places a thick orange border around them. We also allow users to edit diagram nodes by calling <a title=\"MindFusion Diagramming for JavaScript: setAllowInplaceEdit\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_Diagram_setAllowInplaceEdit_1_Boolean.htm\">setAllowInplaceEdit<\/a> .<\/p>\n<pre>diagram.setStyle(shapeNodeStyle);\ndiagram.setAllowInplaceEdit(true);<\/pre>\n<p><strong>III. The NodeListView<\/strong><\/p>\n<p>The <a title=\"MindFusion Diagramming for JavaScript: NodeListView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_NodeListView_Members.htm\">NodeListView<\/a> control can hold both text and image nodes. We create it the same way we created the <a title=\"MindFusion Diagramming for JavaScript: Diagram\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_Diagram_0.htm\">Diagram<\/a> : by using the id of its Canvas element:<\/p>\n<pre>\/\/ create a NodeListView component that wraps the \"nodeList\" canvas\nvar nodeList = MindFusion.Diagramming.NodeListView.create(document.getElementById(\"nodeList\"));\nnodeList.setTargetView(document.getElementById(\"diagram\"));\nnodeList.setIconSize(new Size(36, 36));\nnodeList.setDefaultNodeSize (new Size(18, 18));<\/pre>\n<p>It is important that we set the target of the <a title=\"MindFusion Diagramming for JavaScript: NodeListView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_NodeListView_Members.htm\">NodeListView<\/a> &#8211; this is the Canvas, where the newly created <a title=\"MindFusion Diagramming for JavaScript: DiagramNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_DiagramNode_Members_0.htm\">DiagramNode<\/a> will be rendered. We also set the size of the icons in the <a title=\"MindFusion Diagramming for JavaScript: NodeListView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_NodeListView_Members.htm\">NodeListView<\/a> and the size that newly created nodes will have. For this we use the <a title=\"MindFusion Diagramming for JavaScript: setIconSize\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_NodeListView_setIconSize_1_Number.htm\">setIconSize<\/a> and <a title=\"MindFusion Diagramming for JavaScript: setDefaultNodeSize\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_NodeListView_setDefaultNodeSize_1_Number.htm\">setDefaultNodeSize<\/a> methods respectively.<\/p>\n<p>We create the nodes for the <a title=\"MindFusion Diagramming for JavaScript: NodeListView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_NodeListView_Members.htm\">NodeListView<\/a> in a special method called initNodeList:<\/p>\n<pre>function initNodeList(nodeList, diagram)\n{\n    var node = new ShapeNode(diagram);\n    node.setText(\"text\");\n    node.setShape(\"Ellipse\");\n    node.setPen(\"Gray\");\t\t\n    node.setBrush(\"White\");\n    node.setBounds(new Rect(0, 0, 48, 48));\n    node.setEnableStyledText(true);\n    node.setFont(new Font(\"sans-serif\", 12, true));\n    node.setStrokeThickness(2);\n    nodeList.addNode(node);\n    .......................................<\/pre>\n<p>The first <a title=\"MindFusion Diagramming for JavaScript: ShapeNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_ShapeNode_0.htm\">ShapeNode<\/a> that we create is a standard text node with the difference that we enable styled text for it: that means the user can enter bold, italic or underline text using the standard HTML tags &lt;b&gt;, &lt;i&gt;, &lt;u&gt;. We specify this with the method <a title=\"MindFusion Diagramming for JavaScript: setEnableStyledText\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_ShapeNode_setEnableStyledText_1_Boolean.htm\">setEnableStyledText<\/a> . We also add the node to the <a title=\"MindFusion Diagramming for JavaScript: NodeListView\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_NodeListView_Members.htm\">NodeListView<\/a> that we have provided to the method.<\/p>\n<p>In order to create nodes with SVG images, we need to create instances of the SvgNode class:<\/p>\n<pre>\/\/ add some nodes to the NodeListView\nvar svgImages = [\"call\", \"reader\", \"computer\", \"data\", \"car\", \"text\", \"music\", \"movie\", \"nature\", \"calendar\", \"chart\", \"router\",  \"account\", \"alarm\", \"announcement\", \"book\", \"calls\", \"copyright\", \"event\", \"grade\", \"group_work\", \"info\", \"key\", \"list\", \"payment\", \"phone\", \"shop\", \"sign\", \"textsms\", \"theaters\", \"work\"];\nfor (var i = 1; i &lt;= svgImages.length; ++i)\n{\n    node = new MindFusion.Diagramming.SvgNode(new Rect(0, i * 48, 48, 48));\n    node.setTransparent(true);<br \/>\n    var content = new SvgContent();\n    content.parse(svgImages[i] + \".svg\");\n    node.setContent(content);<br \/>\n    nodeList.addNode(node);\n}<\/pre>\n<p>We have copied the desired icons in the directory of the application and we have listed their names in a list. Then we cycle through all members of the list and create instances of the SvgNode class. We set their background to be transparent and we render the images thanks to the <a title=\"MindFusion Diagramming for JavaScript: SvgContent\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_SvgContent_Members.htm\">SvgContent<\/a> class. Finally we also add the newly created <a title=\"MindFusion Diagramming for JavaScript: SvgNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Diagramming_SvgNode_Members_0.htm\">SvgNode<\/a> -s to the node list.<\/p>\n<p>We call the initNodeList method providing the nodeList and diagram instances:<\/p>\n<pre>initNodeList(nodeList, diagram);<\/pre>\n<p><strong>IV. Events<\/strong><\/p>\n<p>Though the code so far is enough to provide the desired functionality of our application, we want to customize the newly created diagram nodes and links. This can be achieved through events. We handle two of them onNodeCreated and LinkCreated.<\/p>\n<pre>diagram.addEventListener(Events.nodeCreated, onNodeCreated);\ndiagram.addEventListener(Events.linkCreated, onLinkCreated);<\/pre>\n<p>In the NodeCreated event handler we get the <a title=\"MindFusion Diagramming for JavaScript: ShapeNode\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_ShapeNode_Members_0.htm\">ShapeNode<\/a> that is being created and make its shape to a round one with the <a title=\"MindFusion Diagramming for JavaScript: setShape\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Diagramming_ShapeNode_setShape_1_Shape.htm\">setShape<\/a> method. We assign to it a thick orange border and add some padding to the images &#8211; in case it is an SvgNode:<\/p>\n<pre>function onNodeCreated(sender, args)\n{\n    var node = args.getNode();\n    node.setTransparent(false);\n    node.setShape(\"Ellipse\");\n    node.setStroke(\"orange\");\n    node.setStrokeThickness(3);\n    node.setFont(new Font(\"sans-serif\", 4));\n    node.setImagePadding(new Thickness(1.5, 1.5, 1.5, 1.5));\n}<\/pre>\n<p>The <a title=\"MindFusion Diagramming for JavaScript: linkCreated\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?F_MindFusion_Diagramming_Events_linkCreated.htm\">linkCreated<\/a> event handler applies some styling, but to newly created <a title=\"MindFusion Diagramming for JavaScript: DiagramLink\" href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?CC_T_MindFusion_Diagramming_DiagramLink_Members_0.htm\">DiagramLink<\/a> instances&#8221;<\/p>\n<pre>function onLinkCreated(sender, e)\n{\n    var link = e.getLink();\n    link.setBaseShape(null);\n    link.setHeadShape(null);\n    link.setStrokeThickness(3.0);\n}<\/pre>\n<p>We remove the default base and head shapes of the diagram links and leave the connectors to be just straight lines. We also draw them with a thick orange pen.<\/p>\n<p>And these were the last lines of code that we had to add to make the application complete. You can download the source code, together with all all Diagramming libraries used from this link:<\/p>\n<p align=\"center\"><a href=\"http:\/\/mindfusion.dev\/samples\/javascript\/diagram\/OrgInfoDiagram.zip\">Download Organigram Application in JavaScript Source Code<\/a><\/p>\n<p>You can use <a title=\"Js Diagram Discussion Board and Technical Support\" href=\"https:\/\/mindfusion.dev\/Forum\/YaBB.pl?board=jsdiag_disc\">MindFusion Diagramming for JavaScript<\/a> forum to post your questions, comments and recommendations about this sample or the Js Diagram library.<\/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 and 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\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post we will use MindFusion JavaScript Diagram library to build an application that allows the users to create a diagram with nodes that hold icons and text. We have used this online diagram application to build an &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/organization-services-chart-with-the-javascript-diagram-library\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/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,513,74],"tags":[630,3,628,631,627,629],"class_list":["post-2258","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-javascript","category-sample-code","tag-company-mission-diagram","tag-diagram","tag-javascript-organization-diagram","tag-js-code","tag-organigram","tag-services-diagram"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-Aq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2258","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=2258"}],"version-history":[{"count":6,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2258\/revisions"}],"predecessor-version":[{"id":2324,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2258\/revisions\/2324"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=2258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=2258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=2258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}