Showing posts with label Genealogy. Show all posts
Showing posts with label Genealogy. Show all posts

Thursday, January 15, 2015

Generate Genealogy view in ASP.NET C# using Google Organizational Chart

Previously I  have show you how to create a genealogy view of a family or a tree view in ASP.NET using C#. You can go through that link here.  But that process was some kind of orthodox concept to show the data, because we have used a primary structure of HTML table and put a huge code of all the buttons, labels and images present in the HTML table.

But in this article I will show you how to create a genealogy view of data coming from database with coding of just 20-25 lines. We will use Google organizer chart to create the tree view. Our only motive is to generate the parent and position (left & right) wise data presentation by fetching it from database.

So lets come to the database part first. I cut all other thing except name and parent no. If only one child is present it will show direct under the parent(neither in left nor in right) and two children will show to left and right according to their position in database. So you need to be careful to handle these kinds of small but very effective things when you will do yours.

I will first discuss about the Google organizational chart. Lets check how it works first. Here I am just pasting the  code which will generate the tree view. For more visit this link.

<script>
google.setOnLoadCallback(drawChart);
function drawChart() {
     var data = new google.visualization.DataTable();
     data.addColumn('string', 'Name');
     data.addColumn('string', 'Manager');
         
     data.addRows([['Mike',''], ['Jim', 'Mike'], ['Alice', 'Mike'],['Bob','Jim'],['Carol', 'Jim']]);

     var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));
     chart.draw(data, { allowHtml: true });
}
</script>


This code is not similar to the code present in the website. I took only the things that will help me to do the primary things. Others can be added later.

All these codes are basically based on the data. Data containing of Mike, Jim, Alice, Bob etc. So if we can replace it with our value, which is coming from database then our work will be done. So first lets fetch the data from database and form a string like does here.

string s = "";
DataTable table = new DataTable();
table.Columns.Add("name", typeof(string));
table.Columns.Add("parent", typeof(string));
table.Rows.Add("Mike", "");
table.Rows.Add("Jim", "Mike");
table.Rows.Add("Alice", "Mike");
table.Rows.Add("Carol", "Jim");
for (int i = 0; i < table.Rows.Count; i++)
{
     s = s + "['"+table.Rows[i][0].ToString()+"','"+table.Rows[i][1].ToString()+"'],";
}
s = s.TrimEnd(',');

I only took a DataTable and bind it some dummy data. You need to bind it with SQL to fetch it from DB.

Now our string "s" is ready to launch as a bunch of data to Google organizational chart. Now how to add this data to javascript fucntion to call it or pass it.

The way I did it here, call a javascript fucntion which is created within code behind. Lets see how I did this.

String csname1 = "PopupScript";
Type cstype = this.GetType();
ClientScriptManager cs = Page.ClientScript;
if (!cs.IsStartupScriptRegistered(cstype, csname1))
{
     StringBuilder cstext1 = new StringBuilder();
     cstext1.Append("<script>");
     cstext1.Append("google.setOnLoadCallback(drawChart);");
     cstext1.Append("function drawChart() {");
     cstext1.Append("var data = new google.visualization.DataTable();");
     cstext1.Append("data.addColumn('string', 'Name'); data.addColumn('string', 'Manager');");
     cstext1.Append("data.addRows(["+s+"]);");
     cstext1.Append("var chart = new google.visualization.OrgChart(document.getElementById('chart_div'));");
     cstext1.Append("chart.draw(data, { allowHtml: true });");
     cstext1.Append("}");
     cstext1.Append("</script>");

      cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
 }

And one more thing don't forget to add a "chart_div" in the ASPX page. Its the thing which is holding all the tree view.

Output :


Download the whole source code here. 


Thursday, July 31, 2014

Genealogy View(family tree) in ASP.NET C# with database

In my previous post I shown how to find no of nodes in left and right of a binary tree. Now in this article I will show you how to design the binary tree in ASP.NET  C#. We had retrieve the data from database in the previous post. I remain the database structure same in this example as previous.

Database structure :

-------------------------------------------------------
Id(int) | Parent_Id (int) | Placement (NVarChar(10))
-------------------------------------------------------
1          0                        -
2          1                        Left
3          1                        Right
4          2                        Left
5          2                        Right
-------------------------------------------------------


CREATE TABLE [dbo].[tblBinaryUsers](
    [tid_no] [nvarchar](50) NULL,
    [placement] [nchar](10) NULL,
    [parent_no] [nvarchar](50) NULL,
    [First_name] [nvarchar](50) NULL,
    [enroll_time] [nvarchar](50) NULL
)  


And insert into the table 

INSERT [dbo].[tblBinaryUsers] ([tid_no], [placement], [parent_no], [First_name], [enroll_time]) VALUES (N'100001', N'', NULL, N'Arkadeep De', N'6/7/2014')

INSERT [dbo].[tblBinaryUsers] ([tid_no], [placement], [parent_no], [First_name], [enroll_time]) VALUES (N'100002', N'Left', N'100001', N'Pradeep Das', N'8/7/2014')


INSERT [dbo].[tblBinaryUsers] ([tid_no], [placement], [parent_no], [First_name], [enroll_time]) VALUES (N'100003', N'Right', N'100001', N'Mekesh Nayak', N'9/7/2014')


I added two more fields with this structure. First_Name (nvarchar(255)) and Enroll_Time  (nvarchar(20)). 

First decide how many levels you want to show in the binary tree. Then design according to that Laval. Here I have done with 4 levels. 

So design the html first and then write the coding. I also added a search facility in this project to find the ID with a DropDownList. 
 Your design will be some thing like this.

 
As the code is little bit long I am not giving these here. But don't worry here is the link where you can download the full source code.

Your output will be like this.
Download the full source code here.



Popular Posts

Pageviews