Click on the image below to run the sample:
I. References and HTML Settings
Our sample has its JS code in a separate file called Script.js. We place a reference to it as well:
The diagram library needs an HTML Canvas to draw itself onto. We add one in the middle of the web page:
<div style="width: 100%; height: 100%; overflow: auto;">
<canvas id="diagram" width="2100" height="2100">
This page requires a browser that supports HTML 5 Canvas element.
II. The OrgChartNode
In the Script.js file we first add mappings to some enums and classes that we’ll use from the diagram library:
var Diagram = MindFusion.Diagramming.Diagram;
var CompositeNode = MindFusion.Diagramming.CompositeNode;
var Behavior = MindFusion.Diagramming.Behavior;
var Alignment = MindFusion.Drawing.Alignment;
var Rect = MindFusion.Drawing.Rect;
Now we call the classFromTemplate method of CompositeNode that generates a node class using a JSON template that we’ll provide:
var OrgChartNode = CompositeNode.classFromTemplate("OrgChartNode",
columnDefinitions: ["22", "*"],
In this code we indicate the panel that will be used by the CompositeNode is a GridPanel. Then we declare two lists that set the width and height of the grid rows and columns. The number of members in each array indicate how many rows/columns the grid has. In our case we have one row that takes all place and two columns: one is with fixed with of 22 pixels, the other takes the rest of the available space.
The JSON definition of the CompositeNode continues with an array with the children:
The first child uses a Rect component that we call “Background”. It is rendered with a white brush, has a black outline and spans on two columns e.g. it fills all available space or each node.
The second child is an image. Note the row:
That means that we want to be able to access this component as a property. In such cases the library generates automatic set/get methods using the name of the component. In our sample they will be setImage / getImage.
The third child is a StackPanel component. This is the container for the text labels next to the node. This child has its own collection of children nodes:
font: "Arial bold"
text: "full name",
font: "Arial 3"
The children of this new StackPanel are text components, which are called Title, FullName and Details. They have their autoProperty set to true, which means we can access their value through automatic setter and getter methods.
III. The Diagram and the Nodes
in the read() function of the document we create an instance of the Diagram class using a reference to the canvas we’ve created in section I.
// create a Diagram component that wraps the "diagram" canvas
diagram = Diagram.create($("#diagram"));
Then we enable interactive drawing of custom nodes by calling setCustomNodeType and Then we enable interactive drawing of custom nodes by calling setCustomNodeType and setBehavior:
// enable drawing of custom nodes interactively
The behavior o the diagram is set to Custom, which means that when the user starts drawing nodes the library shall draw nodes specified by CustomNodeType. The setCustomNodeType method tells the diagram that these custom nodes are of type OrgChartNode.
Now it is really easy and intuitive to create nodes:
var node1 = new OrgChartNode(diagram);
node1.setBounds(new Rect(25, 15, 60, 25));
"Our beloved leader. \r\n" +
"The CEO of this great corporation.");
We create a few more nodes using the same code and we bind them in a hierarchy. The links among the nodes are created by calling the Diagram Factory createDiagramLink method of the diagram Factory class:
IV. Rounded Images
We want to add now a custom feature to the node – instead of drawing the image as a rectangle we want to clip it and show it as an ellipse. We’ll do this by using a method that replaces the standard setImage method.
The new method is called createImageClip and takes as parameters two objects: one is the image URL and the other is the node that uses this image.
function createImageClip(path, node)
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
img = document.createElement('img');
We create two HTMLElements – canvas and image, and we get the 2D context of the Canvas. Then, in an event handler of the onload event of the image we clip the canvas to an area defined by a Path. The path reads the size of the image and creates a full arc e.g. a circle inside that rectangle. Then the context draws the image and the new canvas is set as an image to the node using the setImage method:
img.src = path;
img.onload = function ()
canvas.width = img.width;
canvas.height = img.height;
var halfSize = img.width / 2;
ctx.arc(halfSize, halfSize, halfSize, 0, Math.PI * 2, true);
ctx.drawImage(img, 0, 0, img.width, img.height);
You can use this approach to create clippings of images with variable shape.
Now instead of calling:
we call our custom method this way:
We do this for all nodes in the org chart.