C#编写OWC11组件(转贴)

  using  System;
using  System.Data;
using  System.Text;

namespace  FishPro
{
    
/// 
    
/// 使用 OWCChart11 生成各种图表
    
/// 
    
/// 
    
/// 

    public class OWCChart11
    
{
        
#region 属性
        
private string m_SavePath;
        
private string m_Category;
        
private string m_Value;
        
private DataTable m_DataSource;
        
private string m_SeriesName;
        
private string m_Title;
        
private string m_AxesXTitle;
        
private string m_AxesYTitle;
        
private int m_PicWidth;
        
private int m_PicHeight;
        
private int m_Type;
        
private string m_FileName;

        
/// 
        
/// 保存图片的路径和名称,物理路径
        
/// 

        public string SavePath
        
{
            
get{return m_SavePath;}
            
set{m_SavePath=value;}
        }

        
        
/// 
        
/// 直接获得类型
        
/// 

        public string OCategory
        
{
            
get{return m_Category;}
            
set{m_Category=value;}
        }

        
        
/// 
        
/// 直接获得值
        
/// 

        public string OValue
        
{
            
get{return m_Value;}
            
set{m_Value=value;}
        }



        
/// 
        
/// 以表格DataTable的形式获取原始数据
        
/// 

        public DataTable DataSource
        
{
            
get{return m_DataSource;}
            
set
            
{
                m_DataSource
=value;
                m_Category
=GetColumnsStr(m_DataSource);
                m_Value
=GetValueStr(m_DataSource);
            }

        }

        
        
/// 
        
/// 简要说明
        
/// 

        public string SeriesName
        
{
            
get{return m_SeriesName;}
            
set{m_SeriesName=value;}
        }

        
        
/// 
        
/// 图表的总标题,说明图表的简单意思
        
/// 

        public string Title
        
{
            
get{return m_Title;}
            
set{m_Title=value;}
        }


        
/// 
        
/// 图表横坐标标题,说明横坐标的意义
        
/// 

        public string AxesXTitle
        
{
            
get{return m_AxesXTitle;}
            
set{m_AxesXTitle=value;}
        }


        
/// 
        
/// 图表纵坐标标题,说明纵坐标的意义
        
/// 

        public string AxesYTitle
        
{
            
get{return m_AxesYTitle;}
            
set{m_AxesYTitle=value;}
        }

        
        
/// 
        
///  生成的图片宽度
        
/// 

        public int PicWidth
        
{
            
get{return m_PicWidth;}
            
set{m_PicWidth=value;}
        }


        
/// 
        
///  生成的图片高度
        
/// 

        public int PicHeight
        
{
            
get{return m_PicHeight;}
            
set{m_PicHeight=value;}
        }


        
/// 
        
/// 类型
        
/// chChartTypeColumnStacked100 =2 
        
///chChartTypeColumnStacked1003D = 49 
        
///chChartTypeColumnStacked3D = 48 
        
///chChartTypeCombo = -1 
        
///chChartTypeCombo3D = -2 
        
///chChartTypeDoughnut = 32 
        
///chChartTypeDoughnutExploded = 33 
        
///chChartTypeLine = 6 
        
///chChartTypeLine3D = 54 
        
///chChartTypeLineMarkers=  7 
        
///chChartTypeLineOverlapped3D=  55 
        
///chChartTypeLineStacked = 8 
        
///chChartTypeLineStacked100  =10 
        
///chChartTypeLineStacked1003D=  57 
        
///chChartTypeLineStacked100Markers = 11 
        
///chChartTypeLineStacked3D = 56 
        
///chChartTypeLineStackedMarkers = 9 
        
///chChartTypePie = 18 
        
///chChartTypePie3D =58 
        
///chChartTypePieExploded = 19 
        
///chChartTypePieExploded3D = 59 
        
///chChartTypePieStacked = 20 
        
///chChartTypePolarLine = 42 
        
///chChartTypePolarLineMarkers = 43 
        
///chChartTypePolarMarkers = 41 
        
///chChartTypePolarSmoothLine = 44 
        
///chChartTypePolarSmoothLineMarkers = 45 
        
///chChartTypeRadarLine=  34 
        
///chChartTypeRadarLineFilled = 36 
        
///chChartTypeRadarLineMarkers=  35 
        
///chChartTypeRadarSmoothLine = 37 
        
///chChartTypeRadarSmoothLineMarkers = 38 
        
///chChartTypeScatterLine = 25 
        
///chChartTypeScatterLineFilled = 26 
        
///chChartTypeScatterLineMarkers = 24 
        
///chChartTypeScatterMarkers = 21 
        
///chChartTypeScatterSmoothLine = 23 
        
///chChartTypeScatterSmoothLineMarkers = 22 
        
///chChartTypeSmoothLine = 12 
        
///chChartTypeSmoothLineMarkers = 13 
        
///chChartTypeSmoothLineStacked = 14 
        
///chChartTypeSmoothLineStacked100 = 16 
        
///chChartTypeSmoothLineStacked100Markers = 17 
        
///chChartTypeSmoothLineStackedMarkers = 15 
        
///chChartTypeStockHLC = 39 
        
///chChartTypeStockOHLC = 40 
        
/// 

        public int Type
        
{
            
get{return m_Type;}
            
set{m_Type=value;}
        }


        
public string FileName
        
{
            
get{return m_FileName;}
            
set
            
{
                m_FileName
=value;
                
if(m_FileName==null||m_FileName=="")
                
{
                    m_FileName
=DateTime.Now.ToString("yyyyMMddHHmmssff")+".gif";//生成图片名称
                }

            }

        }


        
/// 
        
/// 是DataTable的转换
        
/// 

        
/// 
        
/// 

        private string GetColumnsStr(DataTable dt)
        
{
            StringBuilder strList
=new StringBuilder();
            
foreach(DataRow r in dt.Rows)
            
{
                strList.Append(r[
0].ToString()+'/t');
            }

            
return strList.ToString();
        }


        
/// 
        
/// 是DataTable的转换
        
/// 

        
/// 
        
/// 

        private string GetValueStr(DataTable dt)
        
{
            StringBuilder strList
=new StringBuilder();
            
foreach(DataRow r in dt.Rows)
            
{
                strList.Append(r[
1].ToString()+'/t');
            }

            
return strList.ToString();
        }


        
#endregion


        
#region 枚举类型 
        
/// 
        
/// 枚举类型 对应于OWC的图表类型
        
/// 

        public  enum ChartType:int
        
{
            
            
/// 
            
/// 面积图
            
/// 

            chChartTypeArea = 29 ,
            
/// 
            
/// 面积图3D
            
/// 

            chChartTypeArea3D = 60 ,
            
/// 
            
/// 面积图重复
            
/// 

            chChartTypeAreaOverlapped3D = 61 ,
            
/// 
            
/// 堆积面积图
            
/// 

            chChartTypeAreaStacked  =30 ,
            
/// 
            
/// 堆积面积图百分比图
            
/// 

            chChartTypeAreaStacked100 = 31 ,
            
/// 
            
/// 堆积面积图百分比图3D
            
/// 

            chChartTypeAreaStacked1003D  =63 ,
            
/// 
            
/// 堆积面积图3D
            
/// 

            chChartTypeAreaStacked3D = 62 ,
            
/// 
            
/// 横道图3D
            
/// 

            chChartTypeBar3D = 50 ,
            
/// 
            
/// 横道图串风格
            
/// 

            chChartTypeBarClustered=  3 ,
            
/// 
            
/// 横道图串风格3D
            
/// 

            chChartTypeBarClustered3D=  51 ,
            
/// 
            
/// 堆横道图
            
/// 

            chChartTypeBarStacked = 4 ,
            
/// 
            
/// 堆横道图百分比图
            
/// 

            chChartTypeBarStacked100  =5 ,
            
/// 
            
/// 堆横道图百分比图3D
            
/// 

            chChartTypeBarStacked1003D = 53 ,
            
/// 
            
/// 堆横道图3D
            
/// 

            chChartTypeBarStacked3D  =52 ,
            
/// 
            
/// 气泡图
            
/// 

            chChartTypeBubble = 27 ,
            
/// 
            
/// 线形气泡图
            
/// 

            chChartTypeBubbleLine =28 ,
            
/// 
            
/// 柱形图
            
/// 

            chChartTypeColumn3D  =46 ,
            
/// 
            
/// 3D柱形图
            
/// 

            chChartTypeColumnClustered = 0 ,
            
/// 
            
/// 3D串柱形图
            
/// 

            chChartTypeColumnClustered3D = 47 ,
            
/// 
            
/// 重叠柱形图
            
/// 

            chChartTypeColumnStacked =1 ,
            
/// 
            
/// 100%重叠柱形图
            
/// 

            chChartTypeColumnStacked100 =2 ,
            
/// 
            
/// 100%3D重叠柱形图
            
/// 

            chChartTypeColumnStacked1003D = 49 ,
            
/// 
            
/// 3D柱形图
            
/// 

            chChartTypeColumnStacked3D = 48 ,
            
/// 
            
/// 组合图
            
/// 

            chChartTypeCombo = -1 ,
            
/// 
            
/// 3D组合图
            
/// 

            chChartTypeCombo3D = -2 ,
            
/// 
            
/// 环形图
            
/// 

            chChartTypeDoughnut = 32 ,
            
/// 
            
/// 破式环形图
            
/// 

            chChartTypeDoughnutExploded = 33 ,
            
/// 
            
/// 折线图
            
/// 

            chChartTypeLine = 6,
            
/// 
            
/// 3D折线图
            
/// 

            chChartTypeLine3D = 54 ,
            
/// 
            
/// 制造折线图
            
/// 

            chChartTypeLineMarkers=  7
            
/// 
            
/// 重复折线图
            
/// 

            chChartTypeLineOverlapped3D=  55
            
/// 
            
/// 重叠折线图
            
/// 

            chChartTypeLineStacked = 8 ,
            
/// 
            
/// 100%重叠折线图
            
/// 

            chChartTypeLineStacked100  =10 ,
            
/// 
            
/// 100%3D重叠折线图
            
/// 

            chChartTypeLineStacked1003D=  57
            
/// 
            
/// 制造100%重叠折线图
            
/// 

            chChartTypeLineStacked100Markers = 11 ,
            
/// 
            
/// 3D重叠折线图
            
/// 

            chChartTypeLineStacked3D = 56 ,
            
/// 
            
/// 制造重叠折线图
            
/// 

            chChartTypeLineStackedMarkers = 9 ,
            
/// 
            
/// 饼图
            
/// 

            chChartTypePie = 18 ,
            
/// 
            
/// 3D饼图
            
/// 

            chChartTypePie3D =58 ,
            
/// 
            
/// 破式饼图
            
/// 

            chChartTypePieExploded = 19 ,
            
/// 
            
/// 3D破式饼图
            
/// 

            chChartTypePieExploded3D = 59 ,
            
/// 
            
/// 重叠饼图
            
/// 

            chChartTypePieStacked = 20 ,
            
/// 
            
/// 极坐标图
            
/// 

            chChartTypePolarLine = 42 ,
            
/// 
            
/// 制造线形极坐标图
            
/// 

            chChartTypePolarLineMarkers = 43
            
/// 
            
/// 制造极坐标图
            
/// 

            chChartTypePolarMarkers = 41 ,
            
/// 
            
/// 平滑线形极坐标图
            
/// 

            chChartTypePolarSmoothLine = 44 ,
            
/// 
            
/// 制造平滑线形极坐标图
            
/// 

            chChartTypePolarSmoothLineMarkers = 45 ,
            
/// 
            
/// 雷达图
            
/// 

            chChartTypeRadarLine=  34 ,
            
/// 
            
/// 填充雷达图
            
/// 

            chChartTypeRadarLineFilled = 36 ,
            
/// 
            
/// 制造雷达图
            
/// 

            chChartTypeRadarLineMarkers=  35 ,
            
/// 
            
/// 平滑雷达图
            
/// 

            chChartTypeRadarSmoothLine = 37 ,
            
/// 
            
/// 制造平滑雷达图
            
/// 

            chChartTypeRadarSmoothLineMarkers = 38 ,
            
/// 
            
/// 线形散点图
            
/// 

            chChartTypeScatterLine = 25 ,
            
/// 
            
/// 填充线形散点图
            
/// 

            chChartTypeScatterLineFilled = 26 ,
            
/// 
            
/// 制造线形散点图
            
/// 

            chChartTypeScatterLineMarkers = 24 ,
            
/// 
            
/// 制造散点图
            
/// 

            chChartTypeScatterMarkers = 21 ,
            
/// 
            
/// 平滑散点图
            
/// 

            chChartTypeScatterSmoothLine = 23 ,
            
/// 
            
/// 制造平滑散点图
            
/// 

            chChartTypeScatterSmoothLineMarkers = 22 ,
            
/// 
            
/// 平滑线图
            
/// 

            chChartTypeSmoothLine = 12 ,
            
/// 
            
/// 制造平滑线图
            
/// 

            chChartTypeSmoothLineMarkers = 13 ,
            
/// 
            
/// 重叠平滑线图
            
/// 

            chChartTypeSmoothLineStacked = 14 ,
            
/// 
            
/// 100%重叠平滑线图
            
/// 

            chChartTypeSmoothLineStacked100 = 16 ,
            
/// 
            
/// 制造100%重叠平滑线图
            
/// 

            chChartTypeSmoothLineStacked100Markers = 17 ,
            
/// 
            
/// 制造重叠平滑线图
            
/// 

            chChartTypeSmoothLineStackedMarkers = 15 ,
            
/// 
            
/// 股价图
            
/// 

            chChartTypeStockHLC = 39 ,
            
/// 
            
/// 股价图O型
            
/// 

            chChartTypeStockOHLC = 40 

        }

        
#endregion


        
#region 构造函数
        
public OWCChart11()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }


        
public OWCChart11(string SavePath,string SeriesName,string Title,int ChartType)
        
{
            m_SavePath
=SavePath;
            m_SeriesName
=SeriesName;
            m_Title
=Title;
            m_Type
=ChartType;
        }


        
public OWCChart11(string SavePath,string SeriesName,string Title,int ChartType,string AxesXTitle,string AxesYTitle)
        
{
            m_SavePath
=SavePath;
            m_SeriesName
=SeriesName;
            m_Title
=Title;
            m_AxesXTitle
=AxesXTitle;
            m_AxesYTitle
=AxesYTitle;
            m_Type
=ChartType;
        }


        
public OWCChart11(string SavePath,string SeriesName,string Title,int ChartType,string AxesXTitle,string AxesYTitle,int PicWidth,int PicHeight)
        
{
            m_SavePath
=SavePath;
            m_SeriesName
=SeriesName;
            m_Title
=Title;
            m_AxesXTitle
=AxesXTitle;
            m_AxesYTitle
=AxesYTitle;
            m_PicWidth
=PicWidth;
            m_PicHeight
=PicHeight;
            m_Type
=ChartType;
        }

        
#endregion


        
public bool Create()
        
{
            
//声明对象
            Microsoft.Office.Interop.Owc11.ChartSpace ThisChart = new  Microsoft.Office.Interop.Owc11.ChartSpaceClass();
            Microsoft.Office.Interop.Owc11.ChChart ThisChChart  
= ThisChart.Charts.Add(0);
            Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries 
= ThisChChart.SeriesCollection.Add(0);
            
            
//显示图例
            ThisChChart.HasLegend = true;

            
//显示标题选项
            ThisChChart.HasTitle = true;
            ThisChChart.Title.Font.Name
="黑体";
            ThisChChart.Title.Font.Size
=14;
            ThisChChart.Title.Caption 
= m_Title;//from

            
//x,y轴说明
            
//x
            ThisChChart.Axes[0].HasTitle=true;
            ThisChChart.Axes[
0].Title.Font.Name="黑体";
            ThisChChart.Axes[
0].Title.Font.Size=12;
            ThisChChart.Axes[
0].Title.Caption=m_AxesXTitle;

            ThisChChart.Axes[
1].HasTitle=true;
            ThisChChart.Axes[
1].Title.Font.Name="黑体";
            ThisChChart.Axes[
1].Title.Font.Size=12;
            ThisChChart.Axes[
1].Title.Caption=m_AxesYTitle;

            
//图表类型
            ThisChChart.Type=(Microsoft.Office.Interop.Owc11.ChartChartTypeEnum) m_Type;
//            switch(m_Type)
//            {
//                case 0:    
//                    ThisChChart.Type =(Microsoft.Office.Interop.Owc11.ChartChartTypeEnum) m_Type;// Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeColumn3D;//柱状图3D 
//                    break;
//                case 1:
//                    ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeBar3D;//横道图3D
//                    break;
//                case 2:
//                    ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeSmoothLine;//平滑曲线图
//                    break;
//                case 3:
//                    ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypePie;//圆饼图
//                    
//
//                    break;
//            }
            
//旋转
            ThisChChart.Rotation  = 360;
            ThisChChart.Inclination 
= 10;
            
//背景颜色
            ThisChChart.PlotArea.Interior.Color = "red";
            
//底座颜色
            ThisChChart.PlotArea.Floor.Interior.Color = "green";
            
            
//ThisChChart.Overlap = 50;

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

            Microsoft.Office.Interop.Owc11.ChDataLabels dl
=ThisChChart.SeriesCollection[0].DataLabelsCollection.Add();   
            dl.HasValue
=true;
            
//dl.HasPercentage=true;   
            
//导出图像文件
            try
            
{       
                
if(m_FileName==null||m_FileName=="")
                
{
                    m_FileName
=DateTime.Now.ToString("yyyyMMddHHmmssff")+".gif";//生成图片名称
                }

                ThisChart.ExportPicture(m_SavePath
+"//"+m_FileName,"gif",m_PicWidth,m_PicHeight);
                
return true;
            }
            
            
catch(Exception ee)                   
            
{
                
return false;                              
            }

            
return false;
        }

        
    }

}

你可能感兴趣的:(C#)