Click here to Skip to main content
15,886,608 members
Articles / Web Development / ASP.NET

Tree Chart Generator

Rate me:
Please Sign up or sign in to vote.
4.66/5 (42 votes)
18 Nov 2007CPOL5 min read 356.7K   16.4K   119  
An article on the development of a Tree Chart Generator
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TreeGenerator;
using System.Drawing;
using System.Web.Caching;
using System.Xml;

public partial class ShowTree : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     
    }
    /// <summary>
    /// Generates tree data randomely on the fly.
    /// In real life this function would be used to get the data from the DB.
    /// </summary>
    /// <param name="topID"></param>
    /// <returns></returns>
    private TreeGenerator.TreeData.TreeDataTableDataTable GetDT(string topID)
    {
        TreeGenerator.TreeData.TreeDataTableDataTable Result = new TreeData.TreeDataTableDataTable();
        //add the parent
        Result.AddTreeDataTableRow(topID, "", topID, "");
       
        Random r = new Random();
        int childCount = r.Next(4);
        string childName;
        for(int i=0;i<=childCount;i++)
        {
            //randomely add some children
            childName = topID + "-" + i.ToString();
            Result.AddTreeDataTableRow(childName, topID, childName, "");
        }
        return Result;
    }
    
    /// <summary>
    /// Show the Tree based on what the user clicked
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnShowBOM_Click(object sender, EventArgs e)
    {
        if (txtItem.Text.Trim().Length > 0)
        {
            GenerateChart(txtItem.Text);
        }
    }
    /// <summary>
    /// This is called based on where the user clicked on the imagemap
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void impBOM_Click(object sender, ImageMapEventArgs e)
    {
        GenerateChart(e.PostBackValue);
    }
    /// <summary>
    /// Generate the chart image and save it in the cache, to be used by the image.aspx
    /// </summary>
    private void GenerateChart(string mItem)
    {
        //crate the chart object
         TreeGenerator.TreeBuilder currentBOM = new TreeGenerator.TreeBuilder(GetDT(mItem));
        
        //specify visual properties
        currentBOM.HorizontalSpace = 15;
        currentBOM.VerticalSpace = 15;
        currentBOM.FontSize = 7;
        currentBOM.BoxHeight = 80;
        currentBOM.BoxWidth = 80;
        currentBOM.LineColor = System.Drawing.ColorTranslator.FromHtml("#BFBFC9");
        currentBOM.FontColor = System.Drawing.ColorTranslator.FromHtml("#3B4164");
        System.Drawing.Image OC =
           System.Drawing.Image.FromStream(currentBOM.GenerateTree(mItem,
              System.Drawing.Imaging.ImageFormat.Bmp));

        string nameOfImage = Session.SessionID + "CurrentImage";
        //save in the cache, to be used by the page creating the image
        Cache.Insert(nameOfImage, OC, null, DateTime.MaxValue, TimeSpan.FromMinutes(1), CacheItemPriority.NotRemovable, null);
        //update the item image map
        //supply the image name by querystring
        impBOM.ImageUrl = "~/OrgChartImage.aspx?ID=" + nameOfImage;
        //remove all hotspots
        impBOM.HotSpots.Clear();
        //regenerate the hotspots.
        foreach (XmlNode oNode in currentBOM.xmlTree.SelectNodes("//Node"))
        {
            RectangleHotSpot RectHS = new RectangleHotSpot();
            RectHS.HotSpotMode = HotSpotMode.PostBack;
            Rectangle currentRect = currentBOM.getRectangleFromNode(oNode);
            RectHS.Top = currentRect.Top;
            RectHS.Bottom = currentRect.Bottom;
            RectHS.Left = currentRect.Left;
            RectHS.Right = currentRect.Right;
            RectHS.PostBackValue = oNode.Attributes["nodeID"].InnerText;
            RectHS.AlternateText = oNode.Attributes["nodeDescription"].InnerText;
            impBOM.HotSpots.Add(RectHS);
            //SelectedEmployee = EmpRec.EmployeeData.EmployeeName;

        }
    }

    
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Web Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions