jQuery Org Chart – a plugin for visualising data in a tree-like structure

jQuery OrgChart is a plugin that allows you to render structures with nested elements in an easy-to-read tree structure. To build the tree all you need is to make a single line call to the plugin and supply the HTML element ID for a nested unordered list element that is representative of the data you’d like to display. Features include:

  • Very easy to use given a nested unordered list element.
  • Drag-and-drop reorganisation of elements.
  • Showing/hiding a particular branch of the tree by clicking on the respective node.
  • Nodes can contain any amount of HTML except <li> and <ul>.
  • Easy to style.
jQuery OrgChart

Expected Markup & Example Usage

All documentation can be found on GitHub.


Demo

You can view a demo of this here.


Source code

The source code with an example is available here.

320 thoughts on “jQuery Org Chart – a plugin for visualising data in a tree-like structure”

  1. Ok, this is just slick!

    I’ve got some ideas to make the collapse and expanse of the nodes. More of a smooth transition. Not even sure if it would work but maybe I’ll do my first github fork.

    Heads up that in the demo, firebug is throwing this error

    handleDragStart is not defined
    start : handleDragStart,

    and the View Underlying List doesn’t seem to work. Using FF7.0.1

    Reply
    • Thanks J! Fork away, I’m accepting all pull requests 🙂 There are quite a few other niceties that I think are missing e.g. pulling the style class names/ element ids from the original ul/li and making the plug-in work with other data formats e.g. JSON. Hopefully I get some time to build them out. I’ll have a look at the bugs, thanks for the heads up.

      Reply
      • Thank you for your great job, but I would suggest more functions be added to make it more flexible, for example, addNodes/removeNodes function to add/delete nodes dynamically, showDepth to make if some
        level of nodes should be displayed/showed initially, onClick to fire event when some one do not want the
        default show/hide nodes behavior…
        For now, I have to modify your code code to accomplish these….
        anyway, thank you very much for your efforts

        Reply
  2. Hi,

    honorable efforts !!!

    can you help me with a little problem?

    In fact, the horizontal rows, those black ones are not showing up when I test you script.

    any advise or solution?

    Reply
  3. Any chance you’d be willing to dual-license this? CC-BY-SA and the GPL family of licenses are mutually incompatible. (They both say “You must release derivative works under the same license and can’t apply additional restrictions”)

    Reply
      • For now, just using it.

        From what little I’ve been able to find on the legal interpretations of copyleft licensing in the context of Javascript, I fear using BY-SA Javascript and GPL-family Javascript in the same application since either license could quite easily be interpreted to count the entire body of the Javascript loaded in a given page as the derivative work.

        That’d mean that I’d be violating either your license or the other license were I to either distribute an open-source app containing your plugin and anything GPLed or LGPLed in the same page (and any apps I haven’t open-sourced will be eventually) or to simply use an AGPLed application which contains BY-SA code.

        (The GPL’s provisions only kick in if you distribute a copy of your app to someone else (hence why Google has no problem using GPLed code on their servers) but the AGPL requires you to offer copies to anyone who uses it in. The AGPL is for when programmers want GPL-like protections on software that never needs to be distributed to the customer like Google Docs or Twitter. Hence why StatusNet, an open-source Twitter clone, uses it.)

        I’ve got various potential apps which could use an editable org chart as a graphical representation of hierarchical data, but I normally don’t even trust BY-SA code to be on my list of useful resources because it’s too much extra bother to keep track of licensing compatibility. Yours, I’m making a minor exception for just to have on the list of potential resources because it’s the first editable org chart plugin I’ve ever run across.

        Reply
    • I just realised the other license you’re asking for is GPL, that’s my bad I meant to do that from the start :/ Oh and yes, that’s good enough. I find it rewarding to see my projects being used 🙂

      Reply
  4. Hi Wes,
    I simply bow to you and your development skills. This is exactly what I have been looking for. Could you please give me a hint on how I could modify nodes programatically…I know you mentioned we’d have to do it on our own but I’m begging for a bone here… please, Help me Obi Wan Kenobe, you’re my only hope.

    Again, fantastic piece of brilliance. Thank you.

    Reply
    • The great difficulty here is identifying the nodes and the easiest way I can think of doing that is assigning the list items id-values and then translating these into ids for the equivalent nodes. It’s not that hard to do but I’m running a business and maintaining several open source projects so I have very little time at the moment. If you could take a stab at this and issue a pull request I’d gladly send a telepathic-5 your way 🙂

      Reply
      • Yes, you’re right. So, I have been giving this a lot of thought – I’m not a great developer BTW – and I’ve managed to give the list items and id. You see, my list is brought in from a MySQL DB so I modified your script simply by making a DB call and creating an array which is then parsed out in the UL list format that your plugin requires. That works pretty good. I’ve attached a context menu to each node with another jquery plugin. The probem lies with the type of brilliance probably only you can provide surrounding the add/delete part. I’m also trying to save the list back to the DB but I can’t serialize the list properly. I’ve tried making the UL list ‘sortable’ and using the serialize function but I get no output… it’s frustrating not being so good at stuff. If I happen to get this to work – you have my word I’ll share it back to you… although you’d probably re-write my terribly lengthy code in a couple of lines 😛 which would be a wonderful learning lesson to me… anyway I appreciate your input and let me know if you’d like me to share what I mentioned above.

        Reply
  5. Really solid plug-in!

    I see that others have been interested in add/delete children. You might want to consider that functionality as an option. I’m going to add functions for my project, unless someone has already done so. Anybody done this elegantly?

    Again, nice work.

    Reply
  6. Really solid plug-in!

    I see that others have been interested in add/delete children. You might want to consider that functionality as an option. I’m going to add functions for my project, unless someone has already done so. Anybody done this elegantly?

    Again, nice work.

    Reply
  7. Hi,
    I have included the diagram on my GSP page, no matter what the chart always gets displayed on the bottom part. Any Div I include below it, still gets displayed above in the generated gsp page.?

    Reply
  8. Hello, I absolutely love this plug-in. I have a pretty stupid question, which I thought would be an easy fix… But, for the life of it, I can’t figure it out… Well, here it goes… IF I add an HTML link into a node that has child nodes, by clicking on the HTML link inside the node, it opens/closes it.

    I really don’t want it to open/close (toggle) if I click on an link within the node… IS there an easy fix for this? Thank you in advance!

    nc

    Reply
  9. Hi,

    Is it possible to draw hierarchy in left-to-right orientation? Currently it is top-to-bottom and i don’ t get any handle for this

    Plz help

    Reply
  10. Hello,

    Your tool is really interesting and well designed.
    I wish I could make a change to a database when I do a drag and drop.
    Is it possible to display the id of the “source” and “target” in the list
    example: Beer returns me a 1.
    The only information that appears is that between tags that can appear with .html() in the jquery.jOrgChart.js file.

    Thank you!

    Reply
    • Ideally the plugin needs be extended to:

      1. Work with JSON
      2. Handle events

      That would be ideal but it will take some time to create those features, and unfortunately I don’t have time right now 🙁

      Reply
      • Hi Wes,
        Well, I may not be the best developer but I have come to realize that when someone is kind enough to put out there what they’ve done to help others it’s only the right thing to do to pass that on. I have been working feverishly on your #1 point, Work with JSON. I believe I have something that will help. I’ve got it to return a JSON object but the code isn’t rock solid yet. When I’ve got it good I’ll email you and let you have a look and then hopefully you can use it to further this project.

        Thanks.

        Reply
  11. Hello,

    Your tool is really interesting and well designed.
    I wish I could make a change to a database when I do a drag and drop.
    Is it possible to display the id of the “source” and “target” in the list
    example: Beer returns me a 1.
    The only information that appears is that between tags that can appear with .html() in the jquery.jOrgChart.js file.

    Thank you!

    Reply
  12. Ideally the plugin needs be extended to:

    1. Work with JSON
    2. Handle events

    That would be ideal but it will take some time to create those features, and unfortunately I don’t have time right now 🙁

    Reply
    • Pranath, that should be easily doable by giving your parent node (that is, your very first LI element) a class=”collapsed” before rendering it with the plugin. That way only that node will be initially displayed.

      Reply
  13. Why is it that when I drag and drop a node, it renders out the thus making it so that it wont render a second drop? Please help!

    Reply
      • So i put a blank div tag underneath the whole and changed the defaults chart list from body to div and that worked but if I leave it as body it deletes everything inside the body after the first drag and drop. Anyway. I’ve never worked with and orgchart plugin before. Would there be any chance in you making a short tutorial on making something that uses this dynamically. I really like the functionality of it but I can’t figure out how to actually use it dynamically.

        Reply
        • It depends what you mean by dynamic. If you have a code sample or (even better) code that I can see in action that would help me understand.

          Reply
      • Yeah sure, check this out. http://jsfiddle.net/8VGVG/2/
        I put this in the end of the plug-ing where the (link not collapse) code is so the “+” button wouldn’t collapse the tree:

        $nodeDiv.children(‘input{class=\”add_child\”‘).click(function(e){
        console.log(e);
        e.stopPropagation();
        });

        And then the other stuff I want to accomplish is just at the VERY end of the plug-in. Basically I just want a “+” button in each node that lets the user make a new child node. Thats the first step. Later I want to add a functionality that lets the user enter the input for the node (the text, a link, picture, yada yada). But i think if you show me how i could just do the first step I could do the rest myself.

        Thank You for the speedy responses, let alone any responses at all!

        Reply
        • Had a quick look and I can the issue. The button is being copied from the ULs into the new tree mark up so you have 2 copies of the button. One that you can’t see that’s in the UL structure and the other one that is visible in the tree. This is a tricky approach and I’d advise against it. It’d be better to modify the plugin to add the buttons to the tree markup as it builds itself. These buttons then need to node where to put the new LI in the current UL so I’d suggest using IDs on the LIs. In short you need:

          – to generate the buttons in the tree from within the plugin
          – develop a way in which the buttons know which UL they’re adding LIs to
          – “reload” the plugin once the new LI is added

          This isn’t trivial so I can’t help you more than this in the shortterm.

          Reply
          • Hey thanks for the reply. Yeah I am new to all of this. I figured it was just making a copy. Ill give what you said a try.

          • So, I have been trying to get something different to come out on the screen but still no luck. Where in the plug-in should I edit stuff to show up in the nodes?
            I used:
            .append(“input{type=\”button\” value=\”+\”}”);
            at the end of the $nodeDiv = …

            could you maybe hold my hand a little longer? 🙁

          • hey, so i got the “+” and “-” button to show up by appending
            “”
            before and after the .append(content).

            First, why does this stop the drag and drop from working?

            Second, is there a way to position the “+” button at the very bottom of the node?

            Thanks man and sorry fro so many questions

  14. Why is it that when I drag and drop a node, it renders out the thus making it so that it wont render a second drop? Please help!

    Reply
  15. Hey Honorable Efforts… But can u plz tell me how can i add the data below the heading eg.fruit,banana etc. such as characteristics etc. on the page…. Nice anyway! 🙂

    Reply
  16. Is it possible to develop the tree in vertically instead of horizontally?. Mecesitaría a tree of structure of a primary level, 5 secondary ones and of each of these, other 5 salesmen, so that in horizontal it would be very broad. Thank you.

    Reply
  17. Is it possible to develop the tree in vertically instead of horizontally?. Mecesitaría a tree of structure of a primary level, 5 secondary ones and of each of these, other 5 salesmen, so that in horizontal it would be very broad. Thank you.

    Reply
  18. This is awesome, a little too awesome. Now my people want to print it out and since it is really wide the browser cuts off the pages instead of printing more pages. Any suggestions. My best thought was to generate an image then they could save it. But then I looked at what would be involved in creating an image with java and my head started hurt especially when trying to do it with this plugin.

    Reply
    • Printing is a natural step 🙂 Unfortunately it’s not that easy to generate an image, but what if you printed the HTML page to PDF? Most (all?) browsers have this capability.

      Wes

      Reply
      • I’m not having any luck finding a pdf generator that will do what I am looking for. How hard would it be to get the tree to generate left to right instead of top down?

        Thanks for the quick response

        Reply
    • Never mind its working now, i moved my other javascripts before ”
      jQuery(document).ready(function() {
      $(“#org”).jOrgChart(…”.
      NICE Plugin!!! i was planing to do it from Scratch before i found this plugin, saved me a lot of time!
      thanks.

      Reply
      • Had a quick look and I can the issue. The button is being copied from the ULs into the new tree mark up so you have 2 copies of the button. One that you can’t see that’s in the UL structure and the other one that is visible in the tree. This is a tricky approach and I’d advise against it. It’d be better to modify the plugin to add the buttons to the tree markup as it builds itself. These buttons then need to node where to put the new LI in the current UL so I’d suggest using IDs on the LIs. In short you need:

        – to generate the buttons in the tree from within the plugin
        – develop a way in which the buttons know which UL they’re adding LIs to
        – “reload” the plugin once the new LI is added

        This isn’t trivial so I can’t help you more than this in the shortterm.

        Reply
        • So, I have been trying to get something different to come out on the screen but still no luck. Where in the plug-in should I edit stuff to show up in the nodes?
          I used:
          .append(“input{type=\”button\” value=\”+\”}”);
          at the end of the $nodeDiv = …

          could you maybe hold my hand a little longer? 🙁

          Reply
        • hey, so i got the “+” and “-” button to show up by appending
          “”
          before and after the .append(content).

          First, why does this stop the drag and drop from working?

          Second, is there a way to position the “+” button at the very bottom of the node?

          Thanks man and sorry fro so many questions

          Reply
      • Had a quick look and I can the issue. The button is being copied from the ULs into the new tree mark up so you have 2 copies of the button. One that you can’t see that’s in the UL structure and the other one that is visible in the tree. This is a tricky approach and I’d advise against it. It’d be better to modify the plugin to add the buttons to the tree markup as it builds itself. These buttons then need to node where to put the new LI in the current UL so I’d suggest using IDs on the LIs. In short you need:

        – to generate the buttons in the tree from within the plugin
        – develop a way in which the buttons know which UL they’re adding LIs to
        – “reload” the plugin once the new LI is added

        This isn’t trivial so I can’t help you more than this in the shortterm.

        Reply
        • So, I have been trying to get something different to come out on the screen but still no luck. Where in the plug-in should I edit stuff to show up in the nodes?
          I used:
          .append(“input{type=\”button\” value=\”+\”}”);
          at the end of the $nodeDiv = …

          could you maybe hold my hand a little longer? 🙁

          Reply
        • hey, so i got the “+” and “-” button to show up by appending
          “”
          before and after the .append(content).

          First, why does this stop the drag and drop from working?

          Second, is there a way to position the “+” button at the very bottom of the node?

          Thanks man and sorry fro so many questions

          Reply
  19. i don’t know why but i can’t select anchore element inside a node( like in the “Vegetables Click me” node from the jOrgChart example) with $(‘a’).hover(function(e) { console.log(‘Hovered’); });
    but the $(‘a’) selector works for anchores that are out side of a node, any resone why this isn’t working when using this plugin?

    Reply
    • Never mind again lol, its working now
      if any one is having problem selecting elements from the tree nodes use $(window).load(…);
      example:
      $(window).load(function () {
      $(‘a’).hover(function(e) { console.log(‘Hovered’); });
      });

      it took me 2 days to figure it out and i hope this can be helpful to anyone who is having the same problem

      Reply
  20. This is a great plugin. Thanks for this.

    Is there a way to generate the tree for a data source? I wonder how could it be done?

    Reply
  21. This is a great plugin. Thanks for this.

    Is there a way to generate the tree for a data source? I wonder how could it be done?

    Reply
  22. Hay Wes, Thanks for the great efforts, you really saved my day 🙂
    I’m wondering if the tree can be built vertically instead of horizontally?

    Wes your response in highly appreciated

    Thank you

    Reply
  23. First of all – great plugin!

    I was just wondering if you knew of or have a way to allow the tree to go horizontally instead of vertically. Our website only has limited width, but with the OrgChart going vertically, when there are too many categories in one row, it goes too wide.

    So we were trying to see if there was a way to make it go sideways instead?

    Reply
  24. First of all – great plugin!

    I was just wondering if you knew of or have a way to allow the tree to go horizontally instead of vertically. Our website only has limited width, but with the OrgChart going vertically, when there are too many categories in one row, it goes too wide.

    So we were trying to see if there was a way to make it go sideways instead?

    Reply
  25. First of all – great plugin!

    I was just wondering if you knew of or have a way to allow the tree to go horizontally instead of vertically. Our website only has limited width, but with the OrgChart going vertically, when there are too many categories in one row, it goes too wide.

    So we were trying to see if there was a way to make it go sideways instead?

    Reply
    • this is great plugin , saves lot of time , i was working on this plugin and tried to extend its functionality by enabling the user to use the JSON data using jquery using $getJSON method and then parse that data and change it to the HTML CODE in form of a unordered list and append it to the BODY of the page using jquery like $(“body”).append(“”);…..but the HTML code is appeneded to the BODY but nothing is displayed , even there is no error while inspecting the element , also otherwise it works …dont know what the problem…?

      Reply
  26. I added a mod to add ids to each div so I could save out the new configuration after drag and drop. Here is the simple process…

    First, add ids to each LI in your source. In my case, these are the database unique IDs for each element the chart was build from. This is so I can update that ID later.

    Second, find the line that looks like this (in buildNode):

    $nodeDiv = $(”).addClass(“node”).data(“tree-node”, nodeCount).append($nodeContent);

    I changed it like this:

    $nodeDiv = $(”).addClass(“node”).data(“tree-node”, nodeCount).append($nodeContent);

    note the id of the div. I created a composite ID that is a combination fo the level you are currently on and the id of the original element, separated by a | character.

    Now I can iterate through each node like…

    $(“.node”).each(function() {

    myid = $this.attr(“id”);

    });

    and then split myid on the | character and do whatever is necessary to save the location back. You can keep track of the parent ID for each level nested so if I am on level 2, foe example, I can have saved the last level 1 id for reference. Also, the level items come in in order so if your database has a field for ‘order’ it can be updated.

    The result? You can load a chart, drag and drop it and then save it back. If someone could make this mod to the source in GitHub, it would be available for everyone. Then folks would only have to add ids to their source and parse the results on button click as I suggest above.

    Hope this helps!

    Reply
    • just realized my post removed the divs… inside the $(“”) quotes the first line had a div and the second one had a div with an id that consists of level + | + $(this).attr(“id”);

      Reply
  27. To squeeze up those invisible boxes on contraction I’m using:

    tr.contracted + tr,
    tr.contracted + tr + tr,
    tr.contracted + tr + tr + tr {
    display: none;
    }

    .contracted div: after {
    content: ‘+’
    }

    It’s not very pretty, but it seems to work alright.

    Reply
  28. Help!!!

    I’ve downloaded the whole source code but somehow the drag-n-drop feature just won’t work on any kind of browser, but your demo works perfectly fine for me…

    Did I miss any step?

    Reply
  29. Pingback: Quora
  30. Pingback: Quora
  31. The demo comes up a blank green checked screen. I am not sure if something else is required to see a demo of this plugin

    Reply
  32. The demo comes up a blank green checked screen. I am not sure if something else is required to see a demo of this plugin

    Reply
  33. I am willing to pay for someone to integrate this into WordPress/BuddyPress… In a nutshell I would like the top tier (parent) to be the registered WP user, he can then (preferably by drag and drop) or by clicking on 10 circles below to add or invite via email 5 other users (or drag 5 existing users) into 2nd tier, they will be his “children”, who in return can do the same and so fort build a “tree” that links all parents to the children.

    If you are willing to discuss please contact me on greg@imediate.co.za or skype greg.mcewan.marriott, Urgent is someone is willing

    Reply
  34. I am willing to pay for someone to integrate this into WordPress/BuddyPress… In a nutshell I would like the top tier (parent) to be the registered WP user, he can then (preferably by drag and drop) or by clicking on 10 circles below to add or invite via email 5 other users (or drag 5 existing users) into 2nd tier, they will be his “children”, who in return can do the same and so fort build a “tree” that links all parents to the children.

    If you are willing to discuss please contact me on greg@imediate.co.za or skype greg.mcewan.marriott, Urgent is someone is willing

    Reply
  35. This is excellent work – thank you. I would like to know if anyone has been successful in implementing a dynamic version that includes add and remove? For example, add nodes from another tree (or from a list), remove / delete nodes, either by dragging away or clicking on a button.

    Reply
  36. This is excellent work – thank you. I would like to know if anyone has been successful in implementing a dynamic version that includes add and remove? For example, add nodes from another tree (or from a list), remove / delete nodes, either by dragging away or clicking on a button.

    Reply
  37. I was having problems with getting it to work for my company’s IE8/IE7 browser… after a long search I found that if you add:

    if (!window.console) console = {log: function() {}};

    To the top of the page (Or above the “jQuery(document).ready(function() {” in the example) it will allow this to work with IE7/8 of a special kind (Like my company’s).

    Hope this may help someone.

    Thank you Wes, I will use this!

    Reply
  38. THank you for you wonderful tool. Did you have a way of adding and/removing a node? I think I can add the html to add the ul or li tags, but I cannot get the org chart to re-draw.

    Thanks!!

    Reply
  39. The component is Great!!! but not even the demo works on the Internet Explorer IE.. any version, is there a tweak to meke it work, it does not display te tree

    Reply
  40. The component is Great!!! but not even the demo works on the Internet Explorer IE.. any version, is there a tweak to meke it work, it does not display te tree

    Reply
  41. I had problems with it loading in IE9. The fix was to comment out the //console.log($node); on line 213 of the jquery.jOrgChart.js file. Hope this help someone else.

    Reply
    • Thanks man! This helped me a bunch! You should submit a pull request with this fix. I might try and do it and give you credit.

      Reply
  42. Is there anyway to construct the node containers (line 98) to be vertical and not collect side by side horizontal? I thought I could simply just add multiple itemitem2 but it only shows the first and it ignores the rest.

    Reply
    • It parsed out my html code. I had UL LI LI UL, UL LI LI UL, etc but it only shows the first set of UL’s in the list.

      Reply
  43. Is there anyway to construct the node containers (line 98) to be vertical and not collect side by side horizontal? I thought I could simply just add multiple itemitem2 but it only shows the first and it ignores the rest.

    Reply

Leave a Comment