|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThere is, in organizations, a constant need to display tree structures. This can be done for file systems, Organization charts, Bill of Material, etc. This article discusses a component which can generate an image of the required tree, based on the data in a This first version of this DLL was called
So, it became necessary to rewrite the whole application and give it a new name. Hence, this article. Using the CodeThe download ZIP file contains the C# solution, which has two projects:
ConstructorThe
MethodsThere is just one main method, Another method exists mainly as a helper method. //First - create the DataTable. In real life it would be extracted from a DB
//or created some other way.
private void Form1_Load(object sender, EventArgs e) {
TreeGenerator.TreeData.TreeDataTableDataTable dtTree =
new TreeData.TreeDataTableDataTable();
dtTree.AddTreeDataTableRow("1000", "","1000","");
dtTree.AddTreeDataTableRow("2000", "1000","2000","");
dtTree.AddTreeDataTableRow("2001", "1000","2001","");
dtTree.AddTreeDataTableRow("3000","2000", "3000","");
dtTree.AddTreeDataTableRow("3001", "2000", "3001","");
dtTree.AddTreeDataTableRow("3000", "2001", "3000","");
dtTree.AddTreeDataTableRow("4000", "3000", "4000","");
dtTree.AddTreeDataTableRow("4001", "3000", "4001", "");
//instantiate the object
myTree = new TreeBuilder(dtTree);
}
//Now - Generate the tree itself
picTree.Image = Image.FromStream(
myTree.GenerateTree("1000",
System.Drawing.Imaging.ImageFormat.Bmp));
PropertiesThe image that is returned has default properties. In order to control the sizes and colors of the Tree, these properties can be changed (all sizes are in pixels):
Changing these properties could generate weird results, so care should be taken by the user. For example, it would not look good if both the Trapping MouseClick EventsUsing the private void picOrgChart_MouseClick(object sender, MouseEventArgs e)
{
Rectangle currentRect;
//determine if the mouse clicked on a box,
//if so, show the node description.
string SelectedNode = "No Node";
//find the node
foreach (XmlNode oNode in myTree.xmlTree.SelectNodes("//Node"))
{
//iterate through all employees until found.
currentRect = myTree.getRectangleFromNode(oNode);
if (e.X >= currentRect.Left &&
e.X <= currentRect.Right &&
e.Y >= currentRect.Top &&
e.Y <= currentRect.Bottom)
{
SelectedNode =
oNode.Attributes["nodeDescription"].InnerText;
break;
}
}
MessageBox.Show(SelectedNode);
}
ASP.NET UsageUsing this code in ASP.NET requires two pages (see attached ZIP file):
ShowTree.aspxThe page contains a Clicking on any of the tree nodes will show the child nodes of that node, in a new tree. It is possible, instead of showing a new tree each time, to enlarge the tree all the time, by keeping the tree The main function in this page is the one that generates the image. The image can be shown as an private void GenerateChart(string mItem)
{
//create 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);
}
}
OrgChartImage.aspxThis page loads the cache item based on the passed parameter, converts it into an image and writes it into the output stream. There is only one function here: protected void Page_Load(object sender, EventArgs e)
{
// Change the response headers to output a JPEG image.
this.Response.Clear();
this.Response.ContentType = "image/jpeg";
System.Drawing.Image OC = null;
//Build the image
string imageName = Request.QueryString["ID"];
if (Cache[imageName] != null)
{
OC = (System.Drawing.Image)Cache[imageName];
// Write the image to the response stream in JPEG format.
OC.Save(this.Response.OutputStream, ImageFormat.Jpeg);
OC.Dispose();
}
}
History
|
||||||||||||||||||||||