开发中经常需要显示组织架构图,例如:组织(或者子组织 ,例如科室,部门,团队等等)。如下是一些解决方案:
1、使用一张静态图,可以来自PowerPoint或其他来源,但是每次改变都需要重新生成图片
2、使用商业产品
3、设计你自己的产品,这就是本文的目的。
本文讲述一个Dll ,为其传入一个DataTable,返回一个组织架构图Image。此Dll可用于 Windows Forms 或者 ASP.NET.
大部分的参数(大小、颜色)已经以属性的方式公开,可以由开发者自己控制
注:本文翻译自codeproject,只是个人使用后分享给大家,感谢您提出意见。
点此下载:http://www.codeproject.com/KB/cs/Org_Chart_Generator/OrgChartGenerator.zip
下载文件中是C#解决方案,包含两个项目:
1、OrgChartGenerator ,生成组织架构图的Dll源码
2、TestOrgchart,这是个Windows Forms应用程序,演示OrgChartGenerator的使用方式。
下面为部分代码,更多代码请到上面提供的链接下载。
//First - create the object
private void TestForm_Load(object sender, EventArgs e)
{
//Build the data for the org chart
//This will be done differently in real life.
//The DataTable will be filled from a database, probably.
OrgChartGenerator.OrgData.OrgDetailsDataTable myOrgData =
new OrgChartGenerator.OrgData.OrgDetailsDataTable();
myOrgData.AddOrgDetailsRow("1111", "Alon", "", "Manager");
myOrgData.AddOrgDetailsRow("1112", "Yoram", "1111", "Team Leader");
myOrgData.AddOrgDetailsRow("1113", "Dana", "1111", "Team Leader");
myOrgData.AddOrgDetailsRow("1114", "Moshe", "1113", "SW Engineer");
myOrgData.AddOrgDetailsRow("1115", "Oren", "1113", "SW Engineer");
myOrgData.AddOrgDetailsRow("1116", "Noa", "1113", "SW Engineer");
myOrgData.AddOrgDetailsRow("1117", "Mor", "1112", "Consultant");
myOrgData.AddOrgDetailsRow("1118", "Omer", "1112", "Consultant");
//instantiate the object
myOrgChart = new OrgChartGenerator.OrgChart(myOrgData);
}
//Then - Generate the Org Chart
/// <span class="code-SummaryComment"><summary></span>
/// Generate the chart and link it to the picturebox control
/// <span class="code-SummaryComment"></summary></span>
private void ShowChart()
{
picOrgChart.Image =
Image.FromStream(
myOrgChart.GenerateOrgChart("1111",
System.Drawing.Imaging.ImageFormat.Bmp));
}
private void picOrgChart_MouseClick(object sender, MouseEventArgs e)
{
//determine if the mouse clicked on a box, if so, show the employee name.
string SelectedEmployee = "No employee";
foreach (OrgChartGenerator.OrgChartRecord EmpRec in
myOrgChart.EmployeeData.Values)
{
if (e.X >= EmpRec.EmployeePos.Left &&
e.X <= EmpRec.EmployeePos.Right &&
e.Y >= EmpRec.EmployeePos.Top &&
e.Y <= EmpRec.EmployeePos.Bottom)
{
SelectedEmployee = EmpRec.EmployeeData.EmployeeName;
break;
}
}
MessageBox.Show(SelectedEmployee);
}