OWC的全称是Microsoft Office Web Components,它的最新版本是OWC11,只要你的电脑安装了Office2003就会自动安装这个组件,在.net工程中添加新的引用,它位于COM组件中。以下是一段示例代码
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Office.Interop.Owc11;
/**/ /// <summary>
/// top10url 的摘要描述。
/// </summary>
public class top10url : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.Image Image1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在這裡放置使用者程式碼以初始化網頁
SqlConnection conn=new SqlConnection("server=;uid=;pwd=;database=");
conn.Open();
SqlCommand cmd=new SqlCommand("",conn);
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=cmd;
DataSet ds=new DataSet();
da.Fill(ds);
conn.Close();
//定義兩個數組
string[] urlname=new string[10];
string[] bytes=new string[10];
for(int i=0;i<10;i++)
{
urlname[i]=ds.Tables[0].Rows[i]["DestHost"].ToString();
bytes[i]=ds.Tables[0].Rows[i]["temp"].ToString();
}
ChartSpace objCSpace=new ChartSpaceClass();
ChChart objChart=objCSpace.Charts.Add(0);
objChart.Type=ChartChartTypeEnum.chChartTypeBarClustered;
objChart.HasLegend=true;
objChart.HasTitle=true;
objChart.Title.Caption="一周内用戶最常要求的前十名網站";
objChart.Axes[0].HasTitle=true;
objChart.Axes[0].Title.Caption="網站";
objChart.Axes[1].HasTitle=true;
objChart.Axes[1].Title.Caption="流量(M)";
string strSeriesName = "圖例1";
string strCategory=urlname[9]+'\t'+urlname[8]+'\t'+urlname[7]+'\t'+urlname[6]+'\t'+urlname[5]+'\t'+urlname[4]+'\t'+urlname[3]+'\t'+urlname[2]+'\t'+urlname[1]+'\t'+urlname[0];
string strValue=bytes[9]+'\t'+bytes[8]+'\t'+bytes[7]+'\t'+bytes[6]+'\t'+bytes[5]+'\t'+bytes[4]+'\t'+bytes[3]+'\t'+bytes[2]+'\t'+bytes[1]+'\t'+bytes[0];
//添加一個series
objChart.SeriesCollection.Add(0);
//給定series名字
objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames,(int)ChartSpecialDataSourcesEnum.chDataLiteral,strSeriesName);
//給定分類
objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories,(int)ChartSpecialDataSourcesEnum.chDataLiteral,strCategory);
//給定值
objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues,(int)ChartSpecialDataSourcesEnum.chDataLiteral,strValue);
ChDataLabels dls=objChart.SeriesCollection[0].DataLabelsCollection.Add();
dls.HasValue=true;
dls.Font.Name="Verdana";
objCSpace.ExportPicture(MapPath(@"images/urlbytes.gif"),"gif",500,300);
this.Image1.ImageUrl=@"images/urlbytes.gif";
}
Web Form 設計工具產生的程式碼#region Web Form 設計工具產生的程式碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 此為 ASP.NET Web Form 設計工具所需的呼叫。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
/// 這個方法的內容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
需要注意以下几点:
1、将一个image控件拖放到页面上以呈现所画出的图片,这里image控件相当于一个存放图片的容器。
2、添加引用:using System.IO;将生成的图片存放到一个指定的物理路径下面,上面的示例中要在工程的根目录下建立一个名为images的文件夹,生成的图片就会存放到该文件夹下。
3、以上代码生成的是一个水平柱状图,统计图类型通过枚举得到,即下面这段代码:objChart.Type=ChartChartTypeEnum.chChartTypeBarClustered;OWC能生成各种各样的统计图,下面列举出来:
chChartTypeColumnClustered----------垂直柱状统计图
chChartTypeColumn3D-----------------3D垂直柱状统计图
chChartTypeBarClustered---------------水平柱状统计图
chChartTypeBar3D--------------------- 3D水平柱状统计图
chChartTypeArea----------------------- 区域统计图
chChartTypeArea3D--------------------3D区域统计图
chChartTypeDoughnut------------------ 中空饼图
chChartTypeLineStacked--------------- 折线统计图
chChartTypeLine3D---------------------3D折线统计图
chChartTypeLineMarkers--------------- 折线带点统计图
chChartTypePie------------------------- 饼图
chChartTypePie3D----------------------3D饼图
chChartTypeRadarSmoothLine----------网状统计图
chChartTypeSmoothLine----------------弧线统计图
最后贴出几个很有参考价值的连接:
http://echo.cnblogs.com/archive/2005/05/18/158136.html
http://www.pconline.com.cn/pcedu/empolder/net/0409/455562.html
http://oooo.upc.edu.cn/bbs/read.php?tid=27199
http://coolbo.blogbus.com/logs/2004/09/409166.html
http://phenix3.cnblogs.com/articles/105265.html
http://ltp.cnblogs.com/archive/2005/12/03/289690.html
http://coolbo.blogbus.com/logs/2004/09/409166.html