how to draw a tree in javascript
Treant contructor
Before stating a wider explanation of all the possible chart configurations, it is first required to briefly describe the Treant Javascript constructor which accepts the configuration object with all the data necessary to draw the wanted chart.
Treant constructor | ||
---|---|---|
parameters | chart_structure {Array or JSON} | Usagevar my_chart = new Treant( chart_structure, callback, $ ); |
As seen above, the chart_structure
parameter should be provided for the Treant constructor. Further documentation explains the possible solutions for generating the chart_structure
which will eventually give the desired result.
Part 1: General chart API
The first part of the API explains all the properties that can be passed to chart config variable.
What is a chart config variable?
Example: Below is a simple example in Construction & Initialization. In the Array example the config variable and in the JSON example the simple_chart_config.chart property are editable withe the properties listed bellow.
List of properties
Property | Values | Short Description |
---|---|---|
container {string} | - (required) | A string that identifies a HTML element that will contain the organizational chart (nodes and connections). A string should be specified as a jQuery ID selector |
callback {object} | onCreateNode onCreateNodeCollapseSwitch onAfterAddNode onBeforeAddNode {function} onAfterPositionNode {function} onBeforePositionNode {function} onToggleCollapseFinished {function} onAfterClickCollapseSwitch {function} onBeforeClickCollapseSwitch {function} onTreeLoaded {function} | Detailed usage and examples coming soon. |
rootOrientation {string} | NORTH EAST WEST SOUTH | This property specifies the position of the root node, and the orientation of its children which depend on it. |
nodeAlign {string} | CENTER TOP BOTTOM | Specifies the vertical alignment of nodes on the same level. Is one node has a bigger height than the others, the ones with the smaller height should be properly vertical aligned. |
levelSeparation {number} | 30(px) | The levelSeparation property defines the separation between chart levels. |
siblingSeparation {number} | 30(px) | The siblingSeparation property defines the separation between two sibling on the same level. |
subTeeSeparation {number} | 30(px) | The siblingSeparation property defines the separation between two neighbor branches of the organizational chart. |
hideRootNode {boolean} | false true | The root node can be hidden by defining this as true . Root node should still be defined in the chart structure but it won't be shown. |
animateOnInit {boolean} | false | Every chart can be animated uppon initialization if this option is set to true . For more animation options see chart config animation. (jQuery required) |
animateOnInitDelay {number} | 500(ms) | This option indicates a number of miliseconds before an init animation is triggered. |
scrollbar {string} | native fancy None | If the chart container is smaller than the chart content a scrollbar will be shown. There are tree possibilities. Use a native browser scrollbar, use a fancy jquery powered scrollbar or don't use scrollbar at all with the "None" property. Usage<!-- include this files if "fancy" is set --> <link href="[path]/perfect-scrollbar.css" rel="stylesheet" type="text/css"/> <script src="[path]/jquery.min.js"></script> <script src="[path]/jquery.mousewheel.js"></script> <script src="[path]/perfect-scrollbar.js"></script> |
padding {number} | 15(px) | If the scrollbar is shown, this defines the padding between the chart structure and the chart container. |
connectors {object} | type {string} style {object} stackIndent | |
node {object} | HTMLclass drawLineThrough collapsable link {object} | A string can be entered under A If you are planning of making a lot of |
animation {object} | nodeSpeed {number} 450(ms) nodeAnimation {string} connectorsSpeed {number} connectorsAnimation {string} | How the chart animates can be fully customised. Here, animation speed and animation functions can be set. jQuery is required in oreder for animations to work. Performance Hint: In order to animate nodes with hardware-accelerated transitions use jquery.transition.js plugin |
Part 2: Individual node configuration
The second part explains the properties which can be passed to every single node.
Where do I define node configuration?
Example: Look at the simple example in Construction & Initialization. In the Array example the parent_node, first_child, second_child variables, and in the JSON example, each node in the simple_chart_config.nodeStructure property are editable withe the properties listed bellow.
List of properties
Property | Values | Short Description |
---|---|---|
text {object} | name {string} or {object} class: .node-name - title {string} or {object} class: .node-title - desc {string} or {object} class: .node-desc - contact {string} or {object} class: .node-contact - data-* {string} class: - - | A Usagenode = { parent: some_parent, text: { name: "Simple node", title: "One of kind", desc: "A basic example", data-foo: " data Attribute for node", contact: { val: "contact me", href: "http://twitter.com/", target: "_self" } } } |
link {object} | href {string} - target {string} - | By default, each node is created as an absolute positioned <div> element. By defining a link property, a node is rendered as an <a> element instead. |
image {string} | - | An image can be inserted into a node container. A relative or absolute path to target image needs to be set as image parameter. The <img> is inserted before the text properties. |
innerHTML {string} | - | A custom HTML structure can be inserted into a node container. A HTML string needs to be specified in order for this to work. Another usage of this property is to pass a jQuery ID selector. In that case, an element with a given id is found and its copy is used as node structure. Suffix "-copy" is added to the id of the node element. |
childrenDropLevel {number} | - | If a certain node has children, a childrenDropLevel can be defined on it. That property specifies how many levels deeper than normal should children be positioned. |
pseudo {boolean} | - | A pseudo property allows the creation of invisible nodes with no content. Such nodes can have children and all the other desired properties, the only thing to keep in mind is that such nodes have NO content. |
connectors {object} | inherits from chart config.connectors | Although all the nodes inherit the connectors property from the chart config, it can be overridden for a single node by redefining it under node config. See chart config connectors for more documentation. |
collapsable {boolean} | inherits from chart config.node.collapsable | Each node can be meade collapsable. (jQuery required) See chart config node.collapsable for more documentation. |
collapsed {boolean} | false | This option can be set to true , if so, effected node is initially collapsed. That node is also considered as collapsable . (jQuery required) |
HTMLclass {string} | inherits from chart config.node.HTMLclass | If specified, all the nodes inherit the HTMLclass from config.node.HTMLclass. But with this property, it is possible to give extra classes to individual nodes. See chart config node.HTMLclass for more documentation. |
HTMLid {string} | - | A custom HTML id attribute can be given to each node element by defineing an id property. |
stackChildren {boolean} | - | |
drawLineThrough {boolean} | inherits from chart config.node .drawLineThrough | The inherited property can be overridden or set by defining this property. See chart config node.drawLineThrough for more documentation. |
Source: https://fperucic.github.io/treant-js/
0 Response to "how to draw a tree in javascript"
Post a Comment