{"id":1860,"date":"2017-06-09T10:26:52","date_gmt":"2017-06-09T10:26:52","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=1860"},"modified":"2021-01-21T14:16:38","modified_gmt":"2021-01-21T14:16:38","slug":"a-javascript-application-for-server-load-monitoring-continued","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/a-javascript-application-for-server-load-monitoring-continued\/","title":{"rendered":"A JavaScript Application for Server Load Monitoring (Continued)"},"content":{"rendered":"<p>We continue the ServerLoad tutorial with the diagram.<\/p>\n<p><strong>I. Create and Style The Diagram<\/strong><\/p>\n<p>We create a new JavaScript file named diagram.js in the Scripts folder of the project and reference it in the HTML.<\/p>\n<pre id=\"line1\">&lt;<span class=\"start-tag\">a<\/span> <span class=\"attribute-name\">href<\/span>=\"<a class=\"attribute-value\" href=\"view-source:http:\/\/scripts\/MindFusion.Diagramming.js\">http:\/\/Scripts\/MindFusion.Diagramming.js<\/a>\"&gt;http:\/\/Scripts\/MindFusion.Diagramming.js&lt;\/<span class=\"end-tag\">a<\/span>&gt;<\/pre>\n<p>Now, in this file we make some namespace mapping to access easy the classes we need:<\/p>\n<pre>var Diagram = MindFusion.Diagramming.Diagram;\nvar DiagramLink = MindFusion.Diagramming.DiagramLink;\nvar ShapeNode = MindFusion.Diagramming.ShapeNode;\nvar Style = MindFusion.Diagramming.Style;\nvar DashStyle = MindFusion.Drawing.DashStyle;\nvar Alignment = MindFusion.Diagramming.Alignment;\n\nvar Rect = MindFusion.Drawing.Rect;\nvar LayeredLayout = MindFusion.Graphs.LayeredLayout;\nvar LayoutDirection = MindFusion.Graphs.LayoutDirection;\n\n\/\/ a shortcut to the Events class\nvar Events = MindFusion.Diagramming.Events;\n\n<\/pre>\n<p>The code for the diagram does not need to be in a single method call, so we&#8217;ll use the document.ready event handler:<\/p>\n<pre>$(document).ready(function () {\n\n\/\/ create a Diagram component that wraps the \"diagram\" canvas\ndiagram = MindFusion.AbstractionLayer.createControl(Diagram, null, null, null, $(\"#diagram\")[0]);\n\/\/set both base and head link shape size\ndiagram.setLinkHeadShapeSize(2);\ndiagram.setLinkBaseShapeSize(2);\ndiagram.setLinkBaseShape(MindFusion.Diagramming.Shape.fromId(\"Arrow\"));\n................\n\n});\n\n<\/pre>\n<p>As with the line chart, we create a diagram object using the canvas id from the html page. Then we make some link customization: we set the base and head shape with <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Diagramming_DiagramLink_setBaseShape_1_Shape.htm\">setBaseShape<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Diagramming_DiagramLink_setHeadShape_1_Shape.htm\">setHeadShape<\/a> to &#8220;Arrow&#8221; to indicate that data flows in two directions between servers.<\/p>\n<p>Now let&#8217;s use a <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?CC_T_MindFusion_Diagramming_Style_0.htm\">Style<\/a> instance to set the stroke, text color and font name. Then we set the link style this way:<\/p>\n<pre>\/\/customize the link appearance\nvar linkStyle = new Style();\nlinkStyle.setStroke(\"#c0c0c0\");\nlinkStyle.setTextColor(\"#585A5C\");\nlinkStyle.setFontName(\"Verdana\");\nlinkStyle.setFontSize(3);\ndiagram.setStyle(linkStyle);\n\n<\/pre>\n<p>When users click on the diagram it is important not to allow them to create new links and nodes. That&#8217;s why we use <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Diagramming_Diagram_setBehavior_1_Behavior.htm\">setBehavior<\/a> to change the default way the diagram responds to user actions:<\/p>\n<pre>\/\/diagram items can only be selected\ndiagram.setBehavior(MindFusion.Diagramming.Behavior.SelectOnly);\n\n<\/pre>\n<p>We create the graph in the buildDiagram method. First, we call <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?CC_M_MindFusion_Diagramming_Diagram_clearAll_0_0.htm\">clearAll<\/a> to remove all diagram items to make sure only nodes we&#8217;ve created are visible:<\/p>\n<pre>\/\/generate diagram nodes\nfunction buildDiagram() {\n diagram.clearAll();\n\n};\n\n<\/pre>\n<p><strong>II. Diagram Items<\/strong><\/p>\n<p>Let&#8217;s create the diagram nodes. We use png icons we&#8217;ve saved in an &#8220;icons&#8221; folder in the website. The background of each node is transparent ( <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Diagramming_ShapeNode_setTransparent_1_Boolean.htm\">setTransparent<\/a> ), which means only the image will be visible. Then we add the node to the items of the diagram:<\/p>\n<pre>var rect = new Rect(0, 0, 15, 15);\n\nvar node = new ShapeNode(diagram);\nnode.setBounds(rect);\n\/\/the web server\nnode.setImageLocation(\"icons\/web_server.png\");\nnode.setTransparent(true);\ndiagram.addItem(node);\n\n<\/pre>\n<p>We create similar nodes for the data server, the clients and the network servers. The links are created with the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?CC_T_MindFusion_Diagramming_DiagramLink_0.htm\">DiagramLink<\/a> class. The constructor takes the origin and target node of the link as parameters. We set an <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Diagramming_DiagramItem_setId_1_Object.htm\">setId<\/a> to the links, which is important and we <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Diagramming_DiagramLink_addLabel_1_String_0.htm\">add a label<\/a> :<\/p>\n<pre>\/\/add a link between the client and the server\nvar link = new DiagramLink(\n       diagram, node, diagram.nodes[0]);\n\/\/same as the title of a given chart series\nlink.setId(\"Client\" + i);\nlink.addLabel(\"Client\" + i);\ndiagram.addItem(link);\n\n<\/pre>\n<p>Let&#8217;s not forget to <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Diagramming_Style_setStrokeThickness_1_Number.htm\">emphasize the two links<\/a> that correspond to the two series that are initially emphasized on the chart:<\/p>\n<pre>\/\/bolden the two major links\ndiagram.links[5].setStrokeThickness(2.0);\ndiagram.links[8].setStrokeThickness(2.0);\n\n<\/pre>\n<p><strong>III. Layout<\/strong><\/p>\n<p>We&#8217;ve created the diagram items but we need to arrange them. We use the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?T_MindFusion_Graphs_LayeredLayout.htm\">LayeredLayout<\/a> algorithm:<\/p>\n<pre>\/\/the layeredLayout arranges the diagram properly - into layers\nfunction applyLayeredLayout() {\n    var layout = new LayeredLayout();\n    layout.direction = LayoutDirection.TopToBottom;\n    layout.siftingRounds = 0;\n    layout.nodeDistance = 20;\n    layout.layerDistance = 20;\n    diagram.arrange(layout);\n    diagram.resizeToFitItems();\n}\n\n<\/pre>\n<p>As you see it is very easy to apply a layout with the diagramming control. You just create an instance of the layout, set the properties of your choice and call <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Graphs_LayeredLayout_arrange_1_Graph.htm\">arrange<\/a> (). In our case we need the layout direction to be <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?T_MindFusion_Graphs_LayoutDirection.htm\">LayoutDirection.TopToBottom<\/a> We also adjust the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?F_MindFusion_Graphs_LayeredLayout_nodeDistance.htm\">nodeDistance<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?F_MindFusion_Graphs_LayeredLayout_layerDistance.htm\">layerDistance<\/a> and set the number of <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?F_MindFusion_Graphs_LayeredLayout_siftingRounds.htm\">siftingRounds<\/a> (attempts to unwind split links) to 0.<\/p>\n<p><strong>IV. Events<\/strong><\/p>\n<p>The diagram is meant to be interactive. We use the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?F_MindFusion_Diagramming_Events_linkSelected.htm\">linkSelected<\/a> and <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?F_MindFusion_Diagramming_Events_clicked.htm\">clicked<\/a> events to handle selection of links and click on an area of the diagram, unoccupied by any items.<\/p>\n<pre>\/\/ add listeners\ndiagram.addEventListener(Events.linkSelected, onLinkSelected);\ndiagram.addEventListener(Events.clicked, onClicked);\n\n<\/pre>\n<p>When a link is selected, we need to emphasize the line graphic that corresponds to this link. We also emphasize the link itself. In addition, we must adjust the stroke and thickness of the other line graphs and diagram links. We do this in the onLinkSelected method:<\/p>\n<pre>\/\/handle the linkSelected event\nfunction onLinkSelected(sender, args) {\n\n    \/\/get the style of the series\n    var seriesStyle = lineChart.plot.seriesStyle;\n    seriesStyle.strokeThicknesses.clear();\n\n    \/\/thicken just the selected series, the others should be transparent\n    for (var j = 0; j &lt; lineChart.series.count() ; j++) {\n        seriesStyle.strokeThicknesses.add(0.15);\n        diagram.links[j].setStrokeThickness(1.0);\n    }\n.....\n}\n\n\n<\/pre>\n<p>First we get the series style and then we use <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Diagramming_Style_setStrokeThickness_1_Number.htm\">setStrokeThickness<\/a> to reset the thickness of diagram links and series to their default values. After we&#8217;ve done that we need to get the selected links and emphasize them:<\/p>\n<pre>\/\/bolden all selected links in the diagram as well\nfor (var m = 0; m &lt; diagram.selection.links.length; m++) {\n     var clickedLinkId = diagram.selection.links[m].getId();\n     diagram.selection.links[m].setStrokeThickness(3.0);\n\n<\/pre>\n<p>When we&#8217;ve done that we need to find the series that correspond to these links and emphasize them as well:<\/p>\n<pre>\/\/find the series that correspond to the selected links\nfor (var i = 0; i &lt; lineChart.series.count() ; i++) {\n      var _series = lineChart.series.item(i);\n\n\n      \/\/adjust the stroke thicknesses\n      if (_series.title == clickedLinkId) {\n\n          seriesStyle.strokeThicknesses.removeAt(i);\n          seriesStyle.strokeThicknesses.insert(i, 3);\n      }\n\n   }\n\n<\/pre>\n<p>All this is followed by a call to the draw method that repaints the chart.<\/p>\n<pre>\/\/repaint the chart\nlineChart.draw();\n\n<\/pre>\n<p>The next event handler &#8211; the onClicked method resets the thicknesses to their default values:<\/p>\n<pre>\/\/reset the chart thicknesses\nfunction onClicked(sender, args) {\n    resetThicknesses();\n\n}\n\n<\/pre>\n<p>This is done in the resetThicknesses method, which uses the seriesStyle field of the line chart:<\/p>\n<pre>\/* bolden the two major series, the others should be very thin.\nbolden the two major diaglinks as well. *\/\nfunction resetThicknesses() {\n   var seriesStyle = lineChart.plot.seriesStyle;\n   seriesStyle.strokeThicknesses.clear();\n\n   for (var j = 0; j &lt; 5; j++) {\n      seriesStyle.strokeThicknesses.add(0.15);\n      diagram.links[j].setStrokeThickness(1.0);\n   }\n.......\n}\n\n\n<\/pre>\n<p><strong>V. Tyre Separators<\/strong><\/p>\n<p>The diagram is divided into three parts by three separator lines. These lines are unconnected <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?CC_T_MindFusion_Diagramming_DiagramLink_0.htm\">DiagramLink<\/a> instances that have no head and base shape and have a custom position for the label. We use absolute positioning to locate the arrows. To do this we need to know the current size of the diagram:<\/p>\n<pre>\/\/add the separators for the tyres\n\/\/first get the size of the diagram\nvar width = diagram.getBounds().width;\nvar height = diagram.getBounds().height;\n\n<\/pre>\n<p>Then the separator link is created with both origin and destination node being null:<\/p>\n<pre>\/\/separator for the Clients tyre\n\/\/the link starts from the left edge and eds to the right edge of the digram\nvar link = new DiagramLink(\n\t\tdiagram, new MindFusion.Drawing.Point(2, (height \/ 3.5)),\n        new MindFusion.Drawing.Point(width, (height \/ 3.5)));\nlink.setShadowOffsetX(0);\nlink.setShadowOffsetY(0);\nlink.setStrokeDashStyle(DashStyle.Dash);\nlink.setStroke(\"#DCDCDC\");\n\/\/remove the shapes at both ends\nlink.setHeadShape(null);\nlink.setBaseShape(null);\n\/\/do not allow this link to be selected\nlink.setLocked(true);\n\/\/move the link label to the right\nvar linkLabel = link.addLabel(\"Clients\");\nlinkLabel.setControlPointPosition(1, -5, 0);\ndiagram.addItem(link);\n\n<\/pre>\n<p>Note that we&#8217;ve used the <a href=\"http:\/\/www.mindfusion.dev\/onlinehelp\/jsdiagram\/index.htm?M_MindFusion_Diagramming_DiagramItem_setLocked_1_Boolean.htm\">setLocked<\/a> property of the link and have set it to true. This means the link cannot participate in user interaction &#8211; it can&#8217;t be selected, moved, resized. That&#8217;s what we want.<\/p>\n<p>And with this our sample server load application is ready. Once again, here is how it looks:<\/p>\n<div id=\"attachment_1851\" style=\"width: 1260px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/05\/server-load-monitor.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1851\" class=\"size-full wp-image-1851\" src=\"http:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/05\/server-load-monitor.png\" alt=\"Server Load Application in JavaScript\" width=\"1250\" height=\"898\" srcset=\"https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/05\/server-load-monitor.png 1250w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/05\/server-load-monitor-300x216.png 300w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/05\/server-load-monitor-768x552.png 768w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/05\/server-load-monitor-1024x736.png 1024w, https:\/\/mindfusion.dev\/blog\/wp-content\/uploads\/2017\/05\/server-load-monitor-418x300.png 418w\" sizes=\"auto, (max-width: 1250px) 100vw, 1250px\" \/><\/a><p id=\"caption-attachment-1851\" class=\"wp-caption-text\">Server Load Application in JavaScript<\/p><\/div>\n<p align=\"center\"><a href=\"http:\/\/mindfusion.dev\/samples\/javascript\/chart\/ServerLoad\/index.html\">Run The Application<\/a><\/p>\n<p>Use this link to download the full sample with all necessary libraries and scripts.<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/javascript\/chart\/ServerLoad.zip\">Download Source Code<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/MindFusionComponents\/JavaScript-Chart-Samples\/tree\/master\/ServerLoad\">You can also fork it from GitHub.<\/a><\/p>\n<p>Visit the web pages of the <a href=\"http:\/\/mindfusion.dev\/javascript-diagram.html\">diagramming<\/a> and <a href=\"http:\/\/mindfusion.dev\/javascript-chart.html\">charting (with gauges)<\/a> JavaScript libraries to learn more about these tools. Visit the <a href=\"http:\/\/mindfusion.dev\/Forum\/YaBB.pl\">MindFusion forums<\/a> if you need technical support or want to ask further questions about MindFusion developer tools.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We continue the ServerLoad tutorial with the diagram. I. Create and Style The Diagram We create a new JavaScript file named diagram.js in the Scripts folder of the project and reference it in the HTML. &lt;a href=&#8221;http:\/\/Scripts\/MindFusion.Diagramming.js&#8221;&gt;http:\/\/Scripts\/MindFusion.Diagramming.js&lt;\/a&gt; Now, in this &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/a-javascript-application-for-server-load-monitoring-continued\/\">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":[61,95,74],"tags":[432,479,478,443,424],"class_list":["post-1860","post","type-post","status-publish","format-standard","hentry","category-charting-2","category-diagramming-2","category-sample-code","tag-diagram-library","tag-flowchart-library","tag-interactive-diagrams","tag-javascript-chart-library","tag-js-chart"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-u0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1860","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=1860"}],"version-history":[{"count":5,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1860\/revisions"}],"predecessor-version":[{"id":2623,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1860\/revisions\/2623"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}