Click here to Skip to main content
11,488,212 members (80,970 online)
Click here to Skip to main content

Hierarchical Tree

, 24 Dec 2008 CPOL 32.5K 2K 39
Rate this:
Please Sign up or sign in to vote.
Updated version of the wonderful and sleek "Tree Chart Generator" written by Rotem Sapir

Introduction

This is an extension to the wonderful and simple tree generator that Rotem Sapir wrote. (TreeGenerator.aspx)

Using the Code

The code base is developed on top of the code that Rotem Sapir had developed. However, there have been updates to the way the node is rendered. This one gives a professional touch with a subtle shadow effect to each of the node.

This also has a provision of specifying the background color to each of the nodes. You could have your own logic of coloring the nodes, either by levels in the organization tree or based on any other geographic or data specific colors (for example, if you are rendering a hierarchy chart to show the % of sales, # of people working under a person etc., etc.,)

This code now supports a selection color for the node, ie., when the user selects a node in the tree the selected color of the node can now be changed by setting the _SelectedBoxFillColor

Here is the sample client code that uses the TreeGenerator:

  public partial class MainForm : Form
  {
    private Color[] hierarchyNodeColor = { Color.FromArgb(26, 80, 160),
        Color.FromArgb(35, 107, 215), Color.FromArgb(100, 152, 230),
        Color.FromArgb(169, 198, 241), Color.FromArgb(178, 207, 250) };
    private TreeBuilder myTree = null;

    public MainForm()
    {
      InitializeComponent();
      InitializeHierarchyTree();
    }

    public void InitializeHierarchyTree()
    {
      picHierarchyTree.Location = containerPanel.Location;
      picHierarchyTree.Dock = DockStyle.Left;
      picHierarchyTree.SizeMode = PictureBoxSizeMode.AutoSize;
      containerPanel.AutoScroll = true;

      LoadHierarchyControl();
    }

    public void LoadHierarchyControl()
    {
      myTree = new TreeBuilder(GetTreeData());
      PaintHierarchyTree();
    }

    private void PaintHierarchyTree()
    {
      picHierarchyTree.Image = Image.FromStream(myTree.GenerateTree(-1, -1, "1",
        System.Drawing.Imaging.ImageFormat.Bmp));
    }
    private TreeData.TreeDataTableDataTable GetTreeData()
    {
      TreeData.TreeDataTableDataTable dt = new TreeData.TreeDataTableDataTable();
      dt.AddTreeDataTableRow("1", "", "Localhost", "This is your Local Server",
          hierarchyNodeColor[0].ToArgb());
      dt.AddTreeDataTableRow("2", "1", "Child 1", "This is the first child.",
          hierarchyNodeColor[1].ToArgb());
      dt.AddTreeDataTableRow("3", "1", "Child 2", "This is the second child.",
          hierarchyNodeColor[2].ToArgb());
      dt.AddTreeDataTableRow("4", "1", "Child 3", "This is the third child.",
          hierarchyNodeColor[3].ToArgb());
      dt.AddTreeDataTableRow("5", "2", "GrandChild 1", "This is the only Grandchild.",
          hierarchyNodeColor[4].ToArgb());
      for (int i = 6; i < 10; i++)
      {
        Random rand = new Random();
        dt.AddTreeDataTableRow(i.ToString(), rand.Next(1, i).ToString(),
            "GrandChild " + i.ToString(), "This is the only Grandchild.",
            hierarchyNodeColor[4].ToArgb());
      }
      return dt;
    }
    private void picHierarchyTree_MouseClick(object sender, MouseEventArgs e)
    {
      // returns the XML node
      XmlNode selectedNode = myTree.OnClick(e.X, e.Y);
      PaintHierarchyTree();
    }

  }
}

Points of Interest

Though the original code base was nice, there were few redundant files and bringing in a shadow effect brought a lot of professional look to the whole code, in addition to removing unncessary code files that were not in use.

History

First version which is based on the TreeGenerator written by Rotem Sapir.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
GeneralExcellent Work and Help Request Pin
rosunbasha29-Nov-12 1:03
memberrosunbasha29-Nov-12 1:03 
GeneralMy vote of 1 Pin
Аslam Iqbal27-Aug-12 0:03
memberАslam Iqbal27-Aug-12 0:03 
GeneralNot working in vs2008 Pin
rdselway24-Feb-11 12:24
memberrdselway24-Feb-11 12:24 
GeneralGr8 Control, but modifications needed Pin
a codeproject fan29-Nov-10 3:36
membera codeproject fan29-Nov-10 3:36 
GeneralMy vote of 1 Pin
sudheendra96-Jul-10 1:21
membersudheendra96-Jul-10 1:21 
GeneralBuild Error Pin
ginu29-Jun-09 21:58
memberginu29-Jun-09 21:58 
GeneralRe: Build Error Pin
Nagarajan Rajesh30-Jun-09 7:59
memberNagarajan Rajesh30-Jun-09 7:59 
GeneralRe: Build Error Pin
ginu30-Jun-09 19:29
memberginu30-Jun-09 19:29 
GeneralHelp Pin
ginu30-Jun-09 19:34
memberginu30-Jun-09 19:34 
GeneralRe: Build Error Pin
Bartosz Bien29-Nov-09 22:24
memberBartosz Bien29-Nov-09 22:24 
GeneralExcellent Pin
byk2k616-Apr-09 23:11
memberbyk2k616-Apr-09 23:11 
GeneralRe: Excellent Pin
Nagarajan Rajesh18-Apr-09 17:33
memberNagarajan Rajesh18-Apr-09 17:33 
GeneralRe: Excellent Pin
Rotem Sapir9-Dec-09 1:09
memberRotem Sapir9-Dec-09 1:09 
GeneralDear Nagarajan Pin
Member 350268513-Mar-09 6:56
memberMember 350268513-Mar-09 6:56 
GeneralRe: Dear Nagarajan Pin
Nagarajan Rajesh18-Mar-09 11:42
memberNagarajan Rajesh18-Mar-09 11:42 
GeneralRe: Dear Nagarajan Pin
Member 350268524-Mar-09 2:25
memberMember 350268524-Mar-09 2:25 
GeneralRe: Dear Nagarajan Pin
Nagarajan Rajesh30-Jun-09 8:02
memberNagarajan Rajesh30-Jun-09 8:02 
GeneralLink to original article Pin
tonyt28-Dec-08 1:56
membertonyt28-Dec-08 1:56 
GeneralRe: Link to original article Pin
Nagarajan Rajesh1-Jan-09 20:27
memberNagarajan Rajesh1-Jan-09 20:27 
GeneralGood Pin
prasad0224-Dec-08 5:31
memberprasad0224-Dec-08 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 24 Dec 2008
Article Copyright 2008 by Nagarajan Rajesh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid