owc组件使用(统计分析图)

<% @ Page Language = " C# "  AutoEventWireup = " true "  CodeFile = " _ReportOwc.aspx.cs "  Inherits = " _ReportOwc "   %>

<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

< html  xmlns ="http://www.w3.org/1999/xhtml"   >
< head  runat ="server" >
    
< title > 无标题页 </ title >
< link  href ="../images/xj_style.css"  _fcksavedurl ="" ../images/xj_style.css"" _fcksavedurl ="" ../images/xj_style.css"" rel ="stylesheet"  type ="text/css"   />  
     
< script  language ="javascript"  src ="../System_Web_Resources.js"  type ="text/css" ></ script >    
</ head >
< body  onload ="parent.window.document.all.myiframe.style.height=document.body.scrollHeight+10;" >
    
< form  id ="form1"  runat ="server" >
        
< table  style ="width: 100%"  id ="TABLE2"  runat ="server" >
            
< tr >
                
< td  align ="center"  colspan ="1"  rowspan ="1"  style ="height: 23px; width: 949px;"  valign ="top" >
        
< table  id ="Table4"  border ="0"  cellpadding ="0"  cellspacing ="0"  width ="98%" >
            
< tr >
                
< td  align ="left"  style ="height: 43px"  valign ="bottom"  width ="40%" >
                    
< table  id ="Table5"  border ="0"  cellpadding ="0"  cellspacing ="0"  style ="border-bottom: #ffffff 1px solid" >
                        
< tr >
                            
< td  class ="lmstyle" >
                                
< img  align ="Middle"  height ="20"  src ="../images/blacksjbiao_big.gif"  width ="15"   /> &nbsp;
                                
< asp:Label  ID ="LabelMailTitle"  runat ="server"  Font-Size ="14px"  Text ="统计图表" ></ asp:Label ></ td >
                            
< td  align ="left" >
                                
< img  height ="27"  src ="../images/lmtu_right.gif"  width ="27"   /></ td >
                        
</ tr >
                    
</ table >
                
</ td >
                
< td  style ="height: 43px"  width ="60%" >
                    
< div  align ="right" >
                        
< img  height ="41"  src ="../images/index2.gif"  width ="306"   /></ div >
                
</ td >
            
</ tr >
        
</ table >
                
</ td >
            
</ tr >
            
< tr >
                
< td  align ="left"  rowspan ="2"  valign ="top"  style ="width: 949px" >
                
< table  id ="Table1"  border ="0"  cellpadding ="1"  cellspacing ="1"  width ="100%" >
                    
< tr >
                        
< td  style ="height: 24px" >
                            选择时间段:
< asp:DropDownList  ID ="DropDownList2"  runat ="server" >
                            
</ asp:DropDownList >
                            
< asp:DropDownList  ID ="DropDownList3"  runat ="server" >
                            
</ asp:DropDownList >
                            
< asp:DropDownList  ID ="DropDownList4"  runat ="server" >
                            
</ asp:DropDownList >
                            
< asp:DropDownList  ID ="DropDownList5"  runat ="server" >
                            
</ asp:DropDownList > &nbsp;  
                            
< asp:Button  ID ="btnSelect"  runat ="server"  OnClick ="btnSelect_Click"  Text ="检索"  CssClass ="xjbutton1"   /> &nbsp;&nbsp;
                            选择图表类型:
                            
< asp:DropDownList  ID ="_DropDownListtype"  runat ="server"  AutoPostBack ="True"  OnSelectedIndexChanged ="DropDownList_SelectedIndexChanged" >
                            
</ asp:DropDownList > &nbsp;   &nbsp;
                            
< asp:Button  ID ="Button3"  runat ="server"  Text ="绘制饼图"  OnClick ="Button3_Click"  Visible ="False"  CssClass ="xjbutton1"   />
                            
< asp:Button  ID ="Button2"  runat ="server"  Text ="绘制条状图"  OnClick ="Button2_Click"  Visible ="False"  CssClass ="xjbutton1"   />
                            
< asp:Button  ID ="Button1"  runat ="server"  OnClick ="Button1_Click"  Text ="绘制柱状图"  Visible ="False"  CssClass ="xjbutton1"   /></ td >
                    
</ tr >
                
</ table >
                
</ td >
            
</ tr >
            
< tr >
            
</ tr >
            
< tr >
                
< td  align ="center"  valign ="top"  style ="width: 949px; height: 32px" >
                
< asp:Image  ID ="Image1"  runat ="server"   /></ td >
            
</ tr >
        
</ table >
      
            
< font  face ="宋体" >
                
< asp:TreeView  ID ="TreeView1"  runat ="server"  Visible ="False" >
                    
< Nodes >
                        
< asp:TreeNode  Text ="简单检索"  Value ="0" ></ asp:TreeNode >
                    
</ Nodes >
                
</ asp:TreeView >
                
</ font >
    
</ form >
</ body >
</ html >


using  System;
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;
using  System.Web.UI.WebControls;
using  System.Web.UI.HtmlControls;
using  Microsoft.Office.Interop.Owc11;
using  System.Text; 

public   partial   class  _ReportOwc : System.Web.UI.Page
{
    MyCRM.DB db 
=   new  MyCRM.DB();  
 ChartFactory chartFactory
= new  ChartFactory();  
 DataTable dt
= new  DataTable();
    DataTable treetable 
=   new  DataTable(); 
 OWCChart11  chart 
=   new  OWCChart11();
 
string  filepath  = ""
 
public   static  Hashtable chartMap  =   new  Hashtable();

    
string  type  =   " dept " ;
    
string  sql  =   " select d.name,count(d.name) from person p join dept d on d.id=p.dept where person_state='正常' group by d.name " ;
 
public   void  Page_Load( object  sender, System.EventArgs e)
 {       
        dt 
=  db.ececSelectSql( " select top 50 person.truename,wage.薪资 from person left join wage on person.id=wage.员工姓名 " ); 
  filepath
=  Server.MapPath( " . " +   " \\ChartImages " ;
  Image1.ImageUrl
= "" ;
  InitTypeMap();

        treetable 
=  db.ececSelectSql( " select col_name,show_name from tablestruct where table_name='person' order by [id] " );
       
  
//  在此处放置用户代码以初始化页面
   #region  按钮绘制图形
  chart.Title 
=   " 标题 " ;
  chart.SeriesName 
=   " 图例 " ;
  
  chart.PhaysicalImagePath 
=  filepath;
  chart.PicHight 
=   320 ;
  chart.PicWidth 
=   500 ;
  chart.DataSource 
=  dt; // 这是你的数据源
#endregion
        
if  ( ! IsPostBack)
        {
            deleteFile();           
            
// 默认绘制            
             for  ( int  i  =   1 ; i  <   13 ; i ++ )
            {
                ListItem item 
=   new  ListItem();
                item.Text 
=  Convert.ToString(DateTime.Now.Year - i) + " " ;
                item.Value 
=  Convert.ToString(DateTime.Now.Year - i);
                DropDownList2.Items.Add(item);
                DropDownList4.Items.Add(item);

                item 
=   new  ListItem();
                item.Text 
=  Convert.ToString(i)  +   " " ;
                item.Value 
=  Convert.ToString(i);
                DropDownList3.Items.Add(item);
                DropDownList5.Items.Add(item);
            }           
        }

        
if  (Request.QueryString[ " type " !=   null )
        {
            type 
=  Request.QueryString[ " type " ].ToString().Trim();
            
// Response.Write(type);
        }
        
else  
        {
            type 
=   " dept " ;
        }

        
switch  (type)
        {
            
case   " post " : // 职务
                sql  =   " select d.post,count(d.post) from person p join post d on d.id=p.post where person_state='正常' group by d.post " ;
                
break ;
            
case   " dept " : // 部门
                sql  =   " select d.name,count(d.name) from person p join dept d on d.id=p.dept where person_state='正常' group by d.name " ;
                
break ;
            
case   " sex " : // 性别
                sql  =   " select sex,count(sex) from person where person_state='正常' group by sex " ;
                
break ;
            
case   " PoliticsVisage " : // 政治面貌
                sql  =   " select PoliticsVisage,count(PoliticsVisage) from person where person_state='正常' group by PoliticsVisage " ;
                
break ;
            
case   " person_state " : // 员工状态
                sql  =   " select [person_state],count([person_state]) from person  group by [person_state] " ;
                
break ;
            
default :
                
break ;
        }
        DropDownList_SelectedIndexChanged(sender, e);
 }

    
public   void  deleteFile()
 {
        
string [] strFileName = System.IO.Directory.GetFiles(Server.MapPath( " . " ) + @" \ChartImages\ " );
  
for ( int  i = 0 ;i < strFileName.Length;i ++ )
  {     
   System.IO.FileInfo file
= new  System.IO.FileInfo(strFileName[i]);
   
string  strtemp = file.Name.Split( ' . ' )[ 0 ];
            
if  (strtemp.Length  >   8 )
            {               
                strtemp 
=  strtemp.Substring( 0 4 +   " - "   +  strtemp.Substring( 4 2 +   " - "   +  strtemp.Substring( 6 2 );
                
if  (Convert.ToDateTime(strtemp)  <  DateTime.Now.AddDays( - 1 ))
                {
                    
if  (file.Exists)
                    {
                        file.Delete();
                    }
                }
            }
  }  
 }
    
public   void  InitTypeMap()
    {
        chartMap.Clear();
        
string  str  =   "" ;
        
if  (_DropDownListtype.Items.Count  >   0 )
        {
            str 
=  _DropDownListtype.SelectedItem.Text;
        }

        _DropDownListtype.Items.Clear();
        Microsoft.Office.Interop.Owc11.ChartChartTypeEnum[] chartTypes 
=   new  Microsoft.Office.Interop.Owc11.ChartChartTypeEnum[]{ ChartChartTypeEnum.chChartTypeColumnClustered,
                      ChartChartTypeEnum.chChartTypeColumn3D,
                      ChartChartTypeEnum.chChartTypeBarClustered,
                      ChartChartTypeEnum.chChartTypeBar3D,
                      ChartChartTypeEnum.chChartTypeArea,
                      ChartChartTypeEnum.chChartTypeArea3D,
                      ChartChartTypeEnum.chChartTypeDoughnut,
                      ChartChartTypeEnum.chChartTypeLineStacked,
                      ChartChartTypeEnum.chChartTypeLine3D,
                      ChartChartTypeEnum.chChartTypeLineMarkers,
                      ChartChartTypeEnum.chChartTypePie,
                      ChartChartTypeEnum.chChartTypePie3D,
                      ChartChartTypeEnum.chChartTypeRadarSmoothLine,
                      ChartChartTypeEnum.chChartTypeSmoothLine};
        
string [] chartTypesCh  =   new   string [] {  " 垂直柱状统计图 " " 3D垂直柱状统计图 " " 水平柱状统计图 " " 3D水平柱状统计图 " " 区域统计图 " " 3D区域统计图 " " 中空饼图 " " 折线统计图 " " 3D折线统计图 " " 折线带点统计图 " " 饼图 " " 3D饼图 " " 网状统计图 " " 弧线统计图 "  };

        
for  ( int  i  =   0 ; i  <  chartTypes.Length; i ++ )
        {
            chartMap.Add(chartTypesCh[i], chartTypes[i]);
        }

        
for  ( int  i  =   0 ; i  <  chartTypes.Length; i ++ )
        {
            ListItem ii 
=   new  ListItem(chartTypesCh[i]);
            _DropDownListtype.Items.Add(ii);
        }
        
foreach  (ListItem ii  in  _DropDownListtype.Items)
        {
            
if  (ii.Text  ==  str)
            {
                ii.Selected 
=   true ;
            }
        }
    }

    
#region  按钮事件
    
public   void  Button1_Click( object  sender, System.EventArgs e)
    {
        
this .Image1.ImageUrl  =   @" .\ChartImages\ "   +  chart.CreateColumn(); // 显示给图像控件。
    }

    
public   void  Button2_Click( object  sender, System.EventArgs e)
    {
        
this .Image1.ImageUrl  =   @" .\ChartImages\ "   +  chart.CreateBar(); // 显示给图像控件。
    }

    
public   void  Button3_Click( object  sender, System.EventArgs e)
    {
        
this .Image1.ImageUrl  =   @" .\ChartImages\ "   +  chart.CreatePie(); // 显示给图像控件。
    }
    
// 按钮绘制类
     public   class  OWCChart11
    {

        
#region  属性
        
public   string  _phaysicalimagepath;
        
public   string  _title;
        
public   string  _seriesname;
        
public   int  _picwidth;
        
public   int  _pichight;
        
public  DataTable _datasource;
        
public   string  strCategory;
        
public   string  strValue;

        
///   <summary>
        
///  图片存放路径
        
///   </summary>
         public   string  PhaysicalImagePath
        {
            
set  { _phaysicalimagepath  =  value; }
            
get  {  return  _phaysicalimagepath; }
        }
        
///   <summary>
        
///  图片标题
        
///   </summary>
         public   string  Title
        {
            
set  { _title  =  value; }
            
get  {  return  _title; }
        }
        
///   <summary>
        
///  图片说明
        
///   </summary>
         public   string  SeriesName
        {
            
set  { _seriesname  =  value; }
            
get  {  return  _seriesname; }
        }
        
///   <summary>
        
///  图片宽度
        
///   </summary>
         public   int  PicWidth
        {
            
set  { _picwidth  =  value; }
            
get  {  return  _picwidth; }
        }
        
///   <summary>
        
///  图片高度
        
///   </summary>
         public   int  PicHight
        {
            
set  { _pichight  =  value; }
            
get  {  return  _pichight; }
        }
        
///   <summary>
        
///  图片数据源
        
///   </summary>
         public  DataTable DataSource
        {
            
set
            {
                _datasource 
=  value;
                strCategory 
=  GetColumnsStr(_datasource);
                strValue 
=  GetValueStr(_datasource);
            }
            
get  {  return  _datasource; }
        }
        
///   <summary>
        
///  列类型
        
///   </summary>
         public   string  GetColumnsStr(DataTable dt)
        {
            StringBuilder strList 
=   new  StringBuilder();
            
foreach  (DataRow r  in  dt.Rows)
            {
                strList.Append(r[
0 ].ToString()  +   ' \t ' );
            }
            
return  strList.ToString();
        }
        
///   <summary>
        
///  列值
        
///   </summary>
         public   string  GetValueStr(DataTable dt)
        {
            StringBuilder strList 
=   new  StringBuilder();
            
foreach  (DataRow r  in  dt.Rows)
            {
                strList.Append(r[
1 ].ToString()  +   ' \t ' );
            }
            
return  strList.ToString();
        }

        
#endregion


        
public  OWCChart11()
        {
        }
        
public  OWCChart11( string  PhaysicalImagePath,  string  Title,  string  SeriesName)
        {
            _phaysicalimagepath 
=  PhaysicalImagePath;
            _title 
=  Title;
            _seriesname 
=  SeriesName;
        }


        
///   <summary>
        
///  柱形图
        
///   </summary>
        
///   <returns></returns>
         public   string  CreateColumn()
        {
            Microsoft.Office.Interop.Owc11.ChartSpace objCSpace 
=   new  Microsoft.Office.Interop.Owc11.ChartSpaceClass(); // 创建ChartSpace对象来放置图表   
            Microsoft.Office.Interop.Owc11.ChChart objChart  =  objCSpace.Charts.Add( 0 ); // 在ChartSpace对象中添加图表,Add方法返回chart对象

            
// 指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到 // Microsoft.Office.Interop.OWC.ChartChartTypeEnum
            
// objChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumnClustered;

            objChart.Type 
=  Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumnClustered;
            
// 指定图表是否需要图例
            objChart.HasLegend  =   true ;

            
// 标题
            objChart.HasTitle  =   true ;
            objChart.Title.Caption 
=  _title;
            objChart.Title.Font.Bold 
=   true ;
            objChart.Title.Font.Color 
=   " blue " ;

            
#region  样式设置

            
// 旋转
            objChart.Rotation  =   360 ; // 表示指定三维图表的旋转角度
            objChart.Inclination  =   10 ; // 表示指定三维图表的视图斜率。有效范围为 -90 到 90

            
// 背景颜色
            objChart.PlotArea.Interior.Color  =   " yellow " ;

            
// 底座颜色
            objChart.PlotArea.Floor.Interior.Color  =   " green " ;

            objChart.Overlap 
=   50 ; // 单个类别中标志之间的重叠量

            
#endregion

            
// x,y轴的图示说明
            objChart.Axes[ 0 ].HasTitle  =   true ;
            objChart.Axes[
0 ].Title.Caption  =   " X : 类别 " ;
            objChart.Axes[
1 ].HasTitle  =   true ;
            objChart.Axes[
1 ].Title.Caption  =   " Y : 数量 " ;


            
// 添加一个series
            Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries  =  objChart.SeriesCollection.Add( 0 );

            
// 给定series的名字
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,
                Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), SeriesName);
            
// 给定分类
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
                Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory);
            
// 给定值
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
                Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);

            Microsoft.Office.Interop.Owc11.ChDataLabels dl 
=  objChart.SeriesCollection[ 0 ].DataLabelsCollection.Add();
            dl.HasValue 
=   true ;
            
//    dl.Position=Microsoft.Office.Interop.Owc11.ChartDataLabelPositionEnum.chLabelPositionOutsideEnd;


            
string  filename  =  DateTime.Now.ToString( " yyyyMMddHHmmssff " +   " .gif " ;
            
string  strAbsolutePath  =  _phaysicalimagepath  +   " \\ "   +  filename;
            objCSpace.ExportPicture(strAbsolutePath, 
" GIF " , _picwidth, _pichight); // 输出成GIF文件.

            
return  filename;

        }


        
///   <summary>
        
///  饼图
        
///   </summary>
        
///   <returns></returns>
         public   string  CreatePie()
        {
            Microsoft.Office.Interop.Owc11.ChartSpace objCSpace 
=   new  Microsoft.Office.Interop.Owc11.ChartSpaceClass(); // 创建ChartSpace对象来放置图表   
            Microsoft.Office.Interop.Owc11.ChChart objChart  =  objCSpace.Charts.Add( 0 ); // 在ChartSpace对象中添加图表,Add方法返回chart对象


            
// 指定图表的类型
            objChart.Type  =  Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypePie;

            
// 指定图表是否需要图例
            objChart.HasLegend  =   true ;

            
// 标题
            objChart.HasTitle  =   true ;
            objChart.Title.Caption 
=  _title;


            
// 添加一个series
            Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries  =  objChart.SeriesCollection.Add( 0 );

            
// 给定series的名字
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,
                Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), SeriesName);
            
// 给定分类
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
                Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory);
            
// 给定值
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
                Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);


            
// 表示系列或趋势线上的单个数据标志
            Microsoft.Office.Interop.Owc11.ChDataLabels dl  =  objChart.SeriesCollection[ 0 ].DataLabelsCollection.Add();
            dl.HasValue 
=   true ;
            dl.HasPercentage 
=   true ;
            
// 图表绘图区的图例放置在右侧。
            
//    dl.Position=Microsoft.Office.Interop.Owc11.ChartDataLabelPositionEnum.chLabelPositionRight;

            
string  filename  =  DateTime.Now.ToString( " yyyyMMddHHmmssff " +   " .gif " ;
            
string  strAbsolutePath  =  _phaysicalimagepath  +   " \\ "   +  filename;
            objCSpace.ExportPicture(strAbsolutePath, 
" GIF " , _picwidth, _pichight); // 输出成GIF文件.

            
return  filename;
        }

        
///   <summary>
        
///  条形图
        
///   </summary>
        
///   <returns></returns>
         public   string  CreateBar()
        {
            Microsoft.Office.Interop.Owc11.ChartSpace objCSpace 
=   new  Microsoft.Office.Interop.Owc11.ChartSpaceClass(); // 创建ChartSpace对象来放置图表   
            Microsoft.Office.Interop.Owc11.ChChart objChart  =  objCSpace.Charts.Add( 0 ); // 在ChartSpace对象中添加图表,Add方法返回chart对象

            
// 指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到 // Microsoft.Office.Interop.OWC.ChartChartTypeEnum
            objChart.Type  =  Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBarClustered;

            
// 指定图表是否需要图例
            objChart.HasLegend  =   true ;

            
// 标题
            objChart.HasTitle  =   true ;
            objChart.Title.Caption 
=  _title;
            objChart.Title.Font.Bold 
=   true ;
            objChart.Title.Font.Color 
=   " blue " ;


            
#region  样式设置

            
//     // 旋转
            objChart.Rotation  =   360 ; // 表示指定三维图表的旋转角度
            objChart.Inclination  =   10 ; // 表示指定三维图表的视图斜率。有效范围为 -90 到 90

            
// 背景颜色
            objChart.PlotArea.Interior.Color  =   " red " ;

            
// 底座颜色
            objChart.PlotArea.Floor.Interior.Color  =   " green " ;

            objChart.Overlap 
=   50 ; // 单个类别中标志之间的重叠量

            
#endregion

            
// x,y轴的图示说明
            objChart.Axes[ 0 ].HasTitle  =   true ;
            objChart.Axes[
0 ].Title.Caption  =   " X : 类别 " ;
            objChart.Axes[
1 ].HasTitle  =   true ;
            objChart.Axes[
1 ].Title.Caption  =   " Y : 数量 " ;


            
// 添加一个series
            Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries  =  objChart.SeriesCollection.Add( 0 );


            
// 给定series的名字
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames,
                Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), SeriesName);
            
// 给定分类
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
                Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strCategory);
            
// 给定值
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
                Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), strValue);

            Microsoft.Office.Interop.Owc11.ChDataLabels dl 
=  objChart.SeriesCollection[ 0 ].DataLabelsCollection.Add();
            dl.HasValue 
=   true ;
            
//    dl.Position=Microsoft.Office.Interop.Owc11.ChartDataLabelPositionEnum.chLabelPositionOutsideEnd;


            
string  filename  =  DateTime.Now.ToString( " yyyyMMddHHmmssff " +   " .gif " ;
            
string  strAbsolutePath  =  _phaysicalimagepath  +   " \\ "   +  filename;
            objCSpace.ExportPicture(strAbsolutePath, 
" GIF " , _picwidth, _pichight); // 输出成GIF文件.

            
return  filename;

        }

    }

    
#endregion

    
#region  下拉框事件

    
public   void  DropDownList_SelectedIndexChanged( object  sender, System.EventArgs e)
    {       
        
string [] chartCategories  =   new   string [ 10 ];
        
string [] chartValues  =   new   string [ 10 ];

        
// Response.Write(sql);
        DataTable ddt = db.ececSelectSql(sql);

        
for  ( int  i  =   0 ; i  <  ddt.Rows.Count; i ++ )
        {
            chartCategories[i] 
=  ddt.Rows[i][ 0 ].ToString();
            chartValues[i] 
=  ddt.Rows[i][ 1 ].ToString();
        }

        chartFactory.chartCategoriesArrValue 
=  chartCategories;
        chartFactory.chartValuesArrValue 
=  chartValues;
        chartFactory.chartTitleValue 
=   " 统计图表 " ;
        chartFactory.chartTypeValue 
=  _DropDownListtype.SelectedItem.Text;
        Microsoft.Office.Interop.Owc11.ChartSpaceClass oChartSpace 
=  chartFactory.BuildCharts();
        
string  filename  =  DateTime.Now.ToString( " yyyyMMddHHmmssff " +   " .png " ;
        
string  path  =  Server.MapPath( " . " +   @" \ChartImages\ "   +  filename;   // 产生图片并保存,还可以是png gif图片
         if  (System.IO.File.Exists(path))
        {
            System.IO.File.Delete(path);
        }
        oChartSpace.ExportPicture(path, 
" png " 470 , 330 );
        Image1.ImageUrl 
=   @" .\ChartImages\ "   +  filename;   //  显示统计图
    }

    
#region  下拉框绘制类
    
public   class  ChartFactory
    {
        
public  ChartFactory()
        {

        }
        
public  System.Web.UI.WebControls.Image imgHondaLineup;
        
public   string [] chartCategoriesArr;
        
public   string [] chartValuesArr;
        
public  Microsoft.Office.Interop.Owc11.ChartChartTypeEnum chartType  =  Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumn3D; // 默认值
         public   static   string  chartTypeCh  =   " 垂直柱状图 " ;
        
public   static   string  chartTitle  =   "" ;

        
public  Microsoft.Office.Interop.Owc11.ChartSpaceClass BuildCharts()
        {
            
string  chartCategoriesStr  =  String.Join( " \t " , chartCategoriesArr);
            
string  chartValuesStr  =  String.Join( " \t " , chartValuesArr);

            Microsoft.Office.Interop.Owc11.ChartSpaceClass oChartSpace 
=   new  Microsoft.Office.Interop.Owc11.ChartSpaceClass();

            
int  ii  =  chartMap.Count;
            chartType 
=  (ChartChartTypeEnum)chartMap[chartTypeCh];

            
if  (chartType  ==  ChartChartTypeEnum.chChartTypePie  ||
                chartType 
==  ChartChartTypeEnum.chChartTypePie3D  ||
                chartType 
==  ChartChartTypeEnum.chChartTypeDoughnut)
            {
                oChartSpace.HasChartSpaceLegend 
=   true ;
                oChartSpace.ChartSpaceLegend.Position 
=  ChartLegendPositionEnum.chLegendPositionBottom;
            }

            oChartSpace.Border.Color 
=   " blue " ;
            oChartSpace.Charts.Add(
0 );
            oChartSpace.Charts[
0 ].HasTitle  =   true ;
            oChartSpace.Charts[
0 ].Type  =  chartType;
            oChartSpace.Charts[
0 ].ChartDepth  =   125 ;
            oChartSpace.Charts[
0 ].AspectRatio  =   80 ;
            oChartSpace.Charts[
0 ].Title.Caption  =  chartTitle;
            oChartSpace.Charts[
0 ].Title.Font.Bold  =   true ;

            oChartSpace.Charts[
0 ].SeriesCollection.Add( 0 );
            oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection.Add();

            
if  (chartType  ==  ChartChartTypeEnum.chChartTypePie  ||
                chartType 
==  ChartChartTypeEnum.chChartTypePie3D  ||
                chartType 
==  ChartChartTypeEnum.chChartTypeDoughnut)
            {
                
// oChartSpace.Charts[0].HasLegend = true;
                oChartSpace.Charts[ 0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].HasPercentage  =   true  ;
                oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].HasValue  =   false ;
            }
            
else
            {
                oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].HasPercentage  =   false ;
                oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].HasValue  =   true ;
            }

            oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].Caption  =  String.Empty;
            oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].Font.Name  =   " verdana " ;
            oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].Font.Size  =   10 ;
            oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].Font.Bold  =   false ;
            oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].Font.Color  =   " red " ;
            oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].Position  =  ChartDataLabelPositionEnum.chLabelPositionCenter;

            
if  (chartType  ==  ChartChartTypeEnum.chChartTypeBarClustered  ||
                chartType 
==  ChartChartTypeEnum.chChartTypeBar3D  ||
                chartType 
==  ChartChartTypeEnum.chChartTypeColumnClustered  ||
                chartType 
==  ChartChartTypeEnum.chChartTypeColumn3D)
            {
                oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].DataLabelsCollection[ 0 ].Position  =  ChartDataLabelPositionEnum.chLabelPositionOutsideEnd;
            }

            oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimCategories,
                Convert.ToInt32(Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral), chartCategoriesStr);

            oChartSpace.Charts[
0 ].SeriesCollection[ 0 ].SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimValues,
                Convert.ToInt32(Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral), chartValuesStr);

            
return  oChartSpace;
        }

        
#region   属性设置
        
public   string [] chartCategoriesArrValue
        {
            
get
            {
                
return  chartCategoriesArr;
            }
            
set
            {
                chartCategoriesArr 
=  value;
            }
        }

        
public   string [] chartValuesArrValue
        {
            
get
            {
                
return  chartValuesArr;
            }
            
set
            {
                chartValuesArr 
=  value;
            }
        }
        
public   string  chartTypeValue
        {
            
get
            {
                
return  chartTypeCh;
            }
            
set
            {
                chartTypeCh 
=  value;
            }
        }
        
public   string  chartTitleValue
        {
            
get
            {
                
return  chartTitle;
            }
            
set
            {
                chartTitle 
=  value;
            }
        }
        
#endregion
    }
#endregion
    
#endregion

    
protected   void  TreeView1_SelectedNodeChanged( object  sender, EventArgs e)
    {
        TreeView1.SelectedNodeStyle.BackColor 
=  System.Drawing.Color.PaleGoldenrod; 
    }
    
protected   void  btnSelect_Click( object  sender, EventArgs e)
    {
        DropDownList_SelectedIndexChanged(sender, e);
    }
}


 參考: http://www.cnblogs.com/pincelee/archive/2007/07/28/834604.html

你可能感兴趣的:(使用)