{"id":1646,"date":"2016-07-25T17:13:41","date_gmt":"2016-07-25T17:13:41","guid":{"rendered":"http:\/\/mindfusion.eu\/blog\/?p=1646"},"modified":"2021-01-20T17:46:31","modified_gmt":"2021-01-20T17:46:31","slug":"class-inheritance-javascript-diagram","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/class-inheritance-javascript-diagram\/","title":{"rendered":"Class inheritance diagram in JavaScript"},"content":{"rendered":"<p>In this post we will show how to use the JavaScript diagram library to generate a class inheritance diagram. The complete example is available here:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/samples\/javascript\/diagram\/InheritanceDiagram.zip\">InheritanceDiagram.zip<\/a><\/p>\n<p>and a live version here:<\/p>\n<p><a href=\"https:\/\/mindfusion.dev\/demos\/jsdiagram\/Inheritance.html\">http:\/\/mindfusion.dev\/demos\/jsdiagram\/Inheritance.html<\/a><\/p>\n<p>Let&#8217;s start by creating shortcuts to some classes from the diagram model:<\/p>\n<pre>var Diagram = MindFusion.Diagramming.Diagram;\n\nvar DiagramItem = MindFusion.Diagramming.DiagramItem;\nvar DiagramLink = MindFusion.Diagramming.DiagramLink;\nvar DiagramNode = MindFusion.Diagramming.DiagramNode;\nvar ShapeNode = MindFusion.Diagramming.ShapeNode;\nvar TableNode = MindFusion.Diagramming.TableNode;\nvar ContainerNode = MindFusion.Diagramming.ContainerNode;\nvar FreeFormNode = MindFusion.Diagramming.FreeFormNode;\nvar SvgNode = MindFusion.Diagramming.SvgNode;\n\nvar ScrollBar = MindFusion.Diagramming.ScrollBar;\nvar Rect = MindFusion.Drawing.Rect;\nvar Font = MindFusion.Drawing.Font;\nvar TreeLayout = MindFusion.Graphs.TreeLayout;\n<\/pre>\n<p>Next, create a function that takes a Diagram instance and a list of class names as parameters. It will create\u00a0a TableNode\u00a0for each class. Each property of the class prototype is listed in a TableNode cell. If the getBaseType function detects a class inherits another one from the list, we&#8217;ll create a link between their nodes. Finally, the diagram is arranged using the TreeLayout algorithm.<\/p>\n<pre>function createClassDiagram(diagram, classes)\n{\n    var classConstructors = [];\n\n    \/\/ create a table node for each class\n    for (var i = 0; i &lt; classes.length; i++)\n    {\n        var className = classes[i];\n        var node = diagram.getFactory().createTableNode(20, 20, 42, 42);\n        node.redimTable(1, 0);\n        node.setText(className);\n        node.setBrush(\"white\");\n        node.setCaptionBackBrush(\"lightgray\");\n        node.setCaptionFont(\n            new Font(\"sans-serif\", 3, true \/*bold*\/, true \/*italic*\/));\n        node.setScrollable(true);\n\n        var ctor = eval(className);\n        for (var property in ctor.prototype)\n        {\n            node.addRow();\n            node.getCell(0, node.rows.length - 1).setText(property);\n        }\n        classConstructors.push(ctor);\n        ctor.classNode = node;\n    }\n\t\n    \/\/ create a diagram link for each prototype inheritance\n    classConstructors.forEach(function(ctor)\n    {\n        var base = getBaseType(ctor);\n        if (base &amp;&amp; base.classNode)\n        {\n            var link = diagram.factory.createDiagramLink(\n                base.classNode,\n                ctor.classNode);\n            link.setHeadShape(null);\n            link.setBaseShape(\"Triangle\");\n            link.setBaseShapeSize(3);\n        }\n    });\n\n    \/\/ arrange as a tree\n    var treeLayout = new TreeLayout();\n    treeLayout.linkType = MindFusion.Graphs.TreeLayoutLinkType.Cascading;\n    diagram.arrange(treeLayout);\n}<\/pre>\n<p>The\u00a0getBaseType implementation checks if a class was registered as a base for the argument using MindFusion.registerClass method or the common prototype inheritance pattern.<\/p>\n<pre>function getBaseType(ctor)\n{\n    \/\/ if class registered using MindFusion.registerClass\n    if (ctor.__baseType)\n        return ctor.__baseType;\n\n    \/\/ if  prototypical inheritance with Child.prototype = new Parent()\n    if (ctor.prototype &amp;&amp; ctor.prototype.constructor != ctor)\n        return ctor.prototype.constructor;\n\t\n    return null;\n}<\/pre>\n<p>The ready handler creates a Diagram instance binding it to a #diagram canvas element.\u00a0It then calls createClassDiagram with a list of DiagramItem -derived classes as argument:<\/p>\n<pre>$(document).ready(function ()\n{\n    TableNode.prototype.useScrollBars = true;\n    ScrollBar.prototype.background = \"Lavender\";\n    ScrollBar.prototype.foreground = \"DarkGray\";\n\n    \/\/ create a Diagram component that wraps the \"diagram\" canvas\n    var diagram = Diagram.create($(\"#diagram\")[0]);\n\n    createClassDiagram(diagram,\n    [\n        \"DiagramItem\",\n        \"DiagramLink\",\n        \"DiagramNode\",\n        \"ShapeNode\",\n        \"TableNode\",\n        \"ContainerNode\",\n        \"FreeFormNode\",\n        \"SvgNode\"\n    ]);\n});<\/pre>\n<p>If you run the sample now, you should see this nice visualization of MindFusion classes \ud83d\ude42<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/mindfusion.dev\/samples\/javascript\/diagram\/inheritance_diagram.png\" alt=\"JavaScript class inheritance diagram\" \/><\/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 generate a class inheritance diagram. The complete example is available here: InheritanceDiagram.zip and a live version here: http:\/\/mindfusion.dev\/demos\/jsdiagram\/Inheritance.html Let&#8217;s start by creating shortcuts to some classes &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/class-inheritance-javascript-diagram\/\">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,74],"tags":[3,422,80,44],"class_list":["post-1646","post","type-post","status-publish","format-standard","hentry","category-diagramming-2","category-sample-code","tag-diagram","tag-inheritance","tag-javascript","tag-library"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-qy","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1646","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=1646"}],"version-history":[{"count":7,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1646\/revisions"}],"predecessor-version":[{"id":2599,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/1646\/revisions\/2599"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=1646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=1646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=1646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}