随着WWW的发展,愈来愈多的信息系统,专家系统开发采用浏览器/服务器(B/S)模式。自微软(Microsoft)公司在2002年3月推出.net之后,由于快速,高效,方便等特性,ASP.NET成为开发B/S系统的首选。而对各种有用信息进行统计并生成相应的动态统计图是十分重要的和必要,调用组件OWC(Office Web Component)相关类里的方法,属性,就能轻易的实现画各种柱状图,曲线图横条图,雷达图,区域图,饼图等,大大提高了开发效率。OfficeXP对应OWC10,Office2003对应OWC11版本,它实际就是一个COM,本文根据在实际项目开发中总结的经验给出了一个在ASP.NET中调用OWC10实现画折线趋势图的实例方法,并进行了扩展,以抛砖引玉。
2 功能实现
开发系统平台是WindowsXP+VisualStudio.NET2003+SQL Server2000,开发语言是C#.
1) 系统配置
在ASP.NET中调用OWC10绘图之前必须在服务器端进行如下设置:
(1) 从微软的网站免费下载OWC10,地址是http://office.microsoft.com/downloads/2002/owc10.aspx或直接从网上搜索Interop.OWC10.dll,OWC10.dll两个dll文件,也可从OfficeXP安装盘里找到并安装为共享程序集。
(2) 将找到的Interop.OWC10.dll和OWC10.dll文件放到项目目录下的bin文件夹里。
(3) 打开项目文件,在引用里添加Interop.OWC10.dll和OWC10.dll文件。
在webform1.aspx的前台里添加控件Image,并设置其Id=Image1。
(4) 安装IIS(在WindowsXP添加组件里),设置虚拟目录。并配置Web.config文件
(5) 在企业管理器里创建数据库zj_new,并创建你所需要的表,添加相关的数据。
在本项目里数据库表结构如下:
|
数据库名
|
zj_new
| ||
|
表名
|
TestMuti
| ||
|
字段名
|
字段类型
|
字段长度
|
备注
|
|
id
|
Int
|
4
|
关键字
|
|
MetalComponent
|
Char
|
10
|
金属名(如:Fe,Cu,Al)
|
|
TestTime
|
float
|
8
|
测试时间
|
|
NongDu
|
float
|
8
|
所含金属浓度
|
2)后台代码
在webform1.aspx.cs中关键源代码及文字说明如下:
using OWC10; //很关键
using System.Data.SqlClient;//
using System.Configuration;//
using System.Xml; //关键,引用System.XML
SqlConnection myConn=new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
string myCommString="select TestTime,NongDu from TestMuti where MetalComponent='Fe' ";//注意x轴按升序排序
DataSet ds = new DataSet("Chart");
DataTable dt=new DataTable();
SqlDataAdapter da = new SqlDataAdapter(myCommString, myConn);
da.Fill(ds);
OWC10.ChartSpaceClass oChartSpace = new OWC10.ChartSpaceClass(); //创建图空间
System.IO.StringWriter sw = new System.IO.StringWriter(); //用于写入字符串
XmlDocument xDoc = new XmlDocument(); //创建xml文档
ds.WriteXml(sw);
// clean up
myConn.Close();
da.Dispose();
xDoc.LoadXml(sw.ToString()); sw.Close();
System.Xml.XmlNodeList nodes;
nodes = xDoc.ChildNodes.Item(0).ChildNodes;
int nCount = nodes.Count;
string[] aNames = new string[nCount];
string[] aTotals = new string[nCount];
string names=String.Empty;
string totals =String.Empty;
int i = 0;
for(i=0;i<nCount;i++)
{
aNames[i]= nodes.Item(i).ChildNodes.Item(0).InnerText; //千万别写成Item(i-1)否则就少一个点了;
aTotals[i]= nodes.Item(i).ChildNodes.Item(1).InnerText;
}
names= String.Join("/t", aNames); totals= String.Join("/t", aTotals);
oChartSpace.Charts.Add(0);
//设置绘图空间及图表格式
oChartSpace.Charts[0].Border.Color="blue";//边界颜色
oChartSpace.Charts[0].HasTitle=true;//图表标题;
oChartSpace.Charts[0].Title.Caption="油样铁谱分析";//图表标题说明文字
oChartSpace.HasChartSpaceTitle=true; //
oChartSpace.ChartSpaceTitle.Caption="折线趋势图测试"; //绘图空间标题说明文字
oChartSpace.Charts[0].Axes[0].HasTitle=true; //x轴
oChartSpace.Charts[0].Axes[0].Title.Caption="时间(10小时)"; //x轴标题说明
oChartSpace.Charts[0].Axes[1].HasTitle=true; //y轴
oChartSpace.Charts[0].Axes[1].Title.Caption="Is(磨损烈度)";//y轴标题说明
oChartSpace.Charts[0].HasLegend=true; //允许有图例
oChartSpace.Charts[0].Legend.Position=OWC10.ChartLegendPositionEnum.chLegendPositionRight;//设置图例位置, oChartSpace.Charts[0].PlotArea.Interior.Color="Wheat"; //设置图表区域块内填充颜色
oChartSpace.Charts[0].Type=OWC10.ChartChartTypeEnum.chChartTypeSmoothLineMarkers; //设置第一个图表Charts[0]的第一个图形的属性
oChartSpace.Charts[0].SeriesCollection.Add(0);//为图表一添加第一个图形
oChartSpace.Charts[0].SeriesCollection[0].SetData(OWC10.ChartDimensionsEnum.chDimSeriesNames,Convert.ToInt32(OWC10.ChartSpecialDataSourcesEnum.chDataLiteral),"Fe");//设置第一条曲线即SeriesCollection[0]的标题oChartSpace.Charts[0].SeriesCollection[0].Interior.Color="Blue"; //用来设置第一个图形内的节点的颜色为颜色(此处为蓝色);
oChartSpace.Charts[0].SeriesCollection[0].SetData(OWC10.ChartDimensionsEnum.chDimCategories,
Convert.ToInt32(OWC10.ChartSpecialDataSourcesEnum.chDataLiteral),names ); oChartSpace.Charts[0].SeriesCollection[0].SetData(OWC10.ChartDimensionsEnum.chDimValues,
Convert.ToInt32(OWC10.ChartSpecialDataSourcesEnum.chDataLiteral),totals );
oChartSpace.Charts[0].SeriesCollection[0].Line.Color="green";//控制线形图的连线的颜色;
oChartSpace.Charts[0].SeriesCollection[0].Marker.Style=OWC10.ChartMarkerStyleEnum.chMarkerStyleDiamond;//线形图
string strFullPathAndName=Server.MapPath(System.DateTime.Now.Ticks.ToString() +".gif");
oChartSpace.ExportPicture( strFullPathAndName, "gif", 600, 400); //确定图框的长和宽
Image1.ImageUrl=strFullPathAndName; //将图形输到id为Image1的控件里;
Image1.Visible =true;
本文介绍如何使用OWC10组件在ASP.NET应用中绘制动态统计图,重点展示了绘制折线趋势图的过程及代码实现。
5923

被折叠的 条评论
为什么被折叠?



