ASP.NET中使用OWC制作统计图

        最近老大要求对公司员工网络访问情况进行统计分析,作出柱状图、饼状图等统计图。想想用.net的GDI+画这些图形很麻烦,就在网上查了这方面的资料,看有没有什么工具可以取代GDI+,发现微软的OWC都是网友的首选。下面就把本人使用OWC制作统计图的一些经验和大家分享分享。
        OWC的全称是Microsoft Office Web Components,它的最新版本是OWC11,只要你的电脑安装了Office2003就会自动安装这个组件,在.net工程中添加新的引用,它位于COM组件中。以下是一段示例代码
None.gif      using  System;
None.gif    
using  System.Collections;
None.gif    
using  System.ComponentModel;
None.gif    
using  System.Data;
None.gif    
using  System.Data.SqlClient;
None.gif    
using  System.Drawing;
None.gif    
using  System.Web;
None.gif    
using  System.Web.SessionState;
None.gif    
using  System.Web.UI.WebControls;
None.gif    
using  System.Web.UI.HtmlControls;
None.gif    
using  System.IO;
None.gif    
using  Microsoft.Office.Interop.Owc11;
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**/ /// <summary>
InBlock.gif    
///        top10url 的摘要描述。
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  top10url : System.Web.UI.UserControl
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
protected System.Web.UI.WebControls.Image Image1;
InBlock.gif
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在這裡放置使用者程式碼以初始化網頁
InBlock.gif
            SqlConnection conn=new SqlConnection("server=;uid=;pwd=;database=");
InBlock.gif            conn.Open();
InBlock.gif            SqlCommand cmd
=new SqlCommand("",conn);
InBlock.gif            SqlDataAdapter da
=new SqlDataAdapter();
InBlock.gif            da.SelectCommand
=cmd;
InBlock.gif            DataSet ds
=new DataSet();
InBlock.gif            da.Fill(ds);
InBlock.gif            conn.Close();
InBlock.gif
InBlock.gif            
//定義兩個數組
InBlock.gif
            string[] urlname=new string[10];
InBlock.gif            
string[] bytes=new string[10];
InBlock.gif
InBlock.gif            
for(int i=0;i<10;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                urlname[i]
=ds.Tables[0].Rows[i]["DestHost"].ToString();
InBlock.gif                bytes[i]
=ds.Tables[0].Rows[i]["temp"].ToString();
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            ChartSpace objCSpace
=new ChartSpaceClass();
InBlock.gif            ChChart objChart
=objCSpace.Charts.Add(0);
InBlock.gif            objChart.Type
=ChartChartTypeEnum.chChartTypeBarClustered;
InBlock.gif            objChart.HasLegend
=true;
InBlock.gif            objChart.HasTitle
=true;
InBlock.gif            objChart.Title.Caption
="一周内用戶最常要求的前十名網站";
InBlock.gif            objChart.Axes[
0].HasTitle=true;
InBlock.gif            objChart.Axes[
0].Title.Caption="網站";
InBlock.gif            objChart.Axes[
1].HasTitle=true;
InBlock.gif            objChart.Axes[
1].Title.Caption="流量(M)";
InBlock.gif
InBlock.gif            
string strSeriesName = "圖例1";
InBlock.gif            
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];
InBlock.gif            
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];
InBlock.gif
InBlock.gif            
//添加一個series
InBlock.gif
            objChart.SeriesCollection.Add(0);
InBlock.gif
InBlock.gif            
//給定series名字
InBlock.gif
            objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames,(int)ChartSpecialDataSourcesEnum.chDataLiteral,strSeriesName);
InBlock.gif
InBlock.gif            
//給定分類
InBlock.gif
            objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories,(int)ChartSpecialDataSourcesEnum.chDataLiteral,strCategory);
InBlock.gif
InBlock.gif            
//給定值
InBlock.gif
            objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimValues,(int)ChartSpecialDataSourcesEnum.chDataLiteral,strValue);
InBlock.gif
InBlock.gif            ChDataLabels dls
=objChart.SeriesCollection[0].DataLabelsCollection.Add();
InBlock.gif            dls.HasValue
=true;
InBlock.gif            dls.Font.Name
="Verdana";
InBlock.gif
InBlock.gif            objCSpace.ExportPicture(MapPath(
@"images/urlbytes.gif"),"gif",500,300);
InBlock.gif            
this.Image1.ImageUrl=@"images/urlbytes.gif";
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web Form 設計工具產生的程式碼#region Web Form 設計工具產生的程式碼
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 此為 ASP.NET Web Form 設計工具所需的呼叫。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
///        此為設計工具支援所必須的方法 - 請勿使用程式碼編輯器修改
InBlock.gif        
///        這個方法的內容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

ExpandedBlockEnd.gif    }

        需要注意以下几点:
        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
 

转载于:https://www.cnblogs.com/ioleon13/archive/2006/05/15/400586.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值