如何使用ASP.NET和VISIO的COM控件生成策略关系图- -
有些Web项目中要求显示下面的策略关系图,这种图形利用ASP.NET以及Microsoft VISIO的COM控件比较容易完成
这是我使用一个ASPX页面的代码
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
if (!File.Exists(Server.MapPath(".") + "//Cache//" + Session.SessionID + ".jpg"))
{
Application app = new Visio.ApplicationClass();
StrategyMap sm = new StrategyMap(); //调用VisioClass
sm.GenStrategyMap( //生成策略图
app,
Request.Cookies["zoneno"].ToString(),
Request.Cookies["branchno"].ToString(),
Request.Cookies["processdate"].ToString()
);
app.ActivePage.Export(Server.MapPath(".") + "//Cache//" + Session.SessionID + ".jpg"); //输出后台生成的Visio的图片
app.ActiveDocument.Saved = true; //以免出现确认保存文件的确认窗口
app.ActiveDocument.Close(); //关闭文件
app.Quit();
}
imgStrategyMap.ImageUrl = "./Cache/" + Session.SessionID + ".jpg";
}
}
下面是生成策略图的代码
private const string stencilFuncHNameU = "XFUNCH_M.VSS";
private const string masterFuncBandNameU = "Functional band";
private const string stencilFlowNameU = "BASFLO_M.VSS";
private const string masterProcessNameU = "Process";
private const string masterConnectorNameU = "Line-curve connector";
private const string templateNameU = "XFUNC_M.VST";
public bool GenStrategyMap(
Visio.Application visioApplication,
string strZoneNo,
string strBranchNo,
string strProcessDate
)
{
bool returnValue = false;
Visio.Document addedDocument;
Visio.Page addedPage;
try
{
addedDocument = visioApplication.Documents.Add(templateNameU);
addedPage = visioApplication.ActivePage;
// Drop masters in the stencil in one call.
returnValue = DropMultipleShapes(addedPage);
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine(err.Message);
}
return returnValue;
}
public bool DropMultipleShapes(
Visio.Page targetPage)
{
Visio.Master masterFuncBand;
Visio.Master masterProcess;
Visio.Shape shapePerspective;
Visio.Shape shapeStrategyGoal;
DropMaster masterDrop = new DropMaster();
PerspectiveData dsPerspective;
PerspectiveGoalData dsPersGoal;
int nPtFBX = 0, nPtFBY = 0;
int nPtProX = 350, nPtProY = 0;
int nSep = 10, nProWidth = 70;
int nGoalCount = 0;
try
{
masterProcess = targetPage.Application.Documents[stencilFlowNameU].
Masters.get_ItemU(masterProcessNameU);
masterFuncBand = targetPage.Application.Documents[stencilFuncHNameU].
Masters.get_ItemU(masterFuncBandNameU);
using (PerspectiveGoalDA daPersGoal = new PerspectiveGoalDA())
{
dsPerspective = daPersGoal.SelectPerspectives();
foreach (DataRow drEachPers in dsPerspective.Tables[PerspectiveData.TABLE_PERSPECTIVE].Rows)
{
// Drawing Perspective Functional like Band
nPtFBX = 100;
nPtFBY += 90;
shapePerspective = masterDrop.DropMasterOnPage(targetPage,
masterFuncBand, stencilFuncHNameU,
nPtFBX, 700 - nPtFBY, "pt");
shapePerspective.Characters.Text = drEachPers[PerspectiveData.FIELD_PERSPECTIVENAME].ToString();
dsPersGoal = daPersGoal.SelectPersGoal(drEachPers[PerspectiveData.FIELD_PERSPECTIVEID].ToString());
nGoalCount = dsPersGoal.Tables[PerspectiveGoalData.TABLE_PERSGOALRELATIONSHIP].Rows.Count;
if (nGoalCount % 2 != 0)
{
nPtProX = 350 - nProWidth / 2 - (nGoalCount - 1) * (nSep + nProWidth) / 2;
}
else
{
nPtProX = 350 - (nSep + nProWidth) * nGoalCount / 2 + nSep / 2;
}
foreach (DataRow drEachGoal in dsPersGoal.Tables[PerspectiveGoalData.TABLE_PERSGOALRELATIONSHIP].Rows)
{
// Drawing Strategy Goal like Process
nPtProX += nSep + nProWidth;
nPtProY = nPtFBY + 40;
shapeStrategyGoal = masterDrop.DropMasterOnPage(targetPage,
masterProcess, stencilFlowNameU,
nPtProX, 700 - nPtProY, "pt");
hashStrategyGoal.Add(drEachGoal[PerspectiveGoalData.FIELD_GOALID].ToString(), shapeStrategyGoal);
shapeStrategyGoal.Characters.Text = drEachGoal[9].ToString() + "/n权重:" + Convert.ToString(Convert.ToDecimal(drEachGoal[PerspectiveGoalData.FIELD_WEIGHTAGE].ToString()) * 100) + "%";
shapeStrategyGoal.Characters.set_CharProps((short)Visio.VisCellIndices.visCharacterSize, 10);
shapeStrategyGoal.get_CellsSRC((short)Visio.VisSectionIndices.visSectionObject, (short)Visio.VisRowIndices.visRowFill, (short)Visio.VisCellIndices.visFillForegnd).FormulaU = "RGB(255,153,51)";
}
}
}
connectWithDynamicGlueAndConnector(targetPage, (Shape)hashStrategyGoal["G006"], (Shape)hashStrategyGoal["G001"]);
connectWithDynamicGlueAndConnector(targetPage, (Shape)hashStrategyGoal["G006"], (Shape)hashStrategyGoal["G002"]);
connectWithDynamicGlueAndConnector(targetPage, (Shape)hashStrategyGoal["G006"], (Shape)hashStrategyGoal["G003"]);
connectWithDynamicGlueAndConnector(targetPage, (Shape)hashStrategyGoal["G007"], (Shape)hashStrategyGoal["G004"]);
connectWithDynamicGlueAndConnector(targetPage, (Shape)hashStrategyGoal["G008"], (Shape)hashStrategyGoal["G005"]);
connectWithDynamicGlueAndConnector(targetPage, (Shape)hashStrategyGoal["G010"], (Shape)hashStrategyGoal["G006"]);
connectWithDynamicGlueAndConnector(targetPage, (Shape)hashStrategyGoal["G010"], (Shape)hashStrategyGoal["G007"]);
connectWithDynamicGlueAndConnector(targetPage, (Shape)hashStrategyGoal["G010"], (Shape)hashStrategyGoal["G008"]);
connectWithDynamicGlueAndConnector(targetPage, (Shape)hashStrategyGoal["G009"], (Shape)hashStrategyGoal["G010"]);
targetPage.CenterDrawing ();
}
catch (Exception err)
{
System.Diagnostics.Debug.WriteLine(err.Message);
return false;
}
return true;
}
本文例子的完成需要安装Visio或者Visio的SDK
附件[StrategyMap.rar]:
http://blog.blogchina.com/upload/2004-11-07/20041107032214739866.rar