溶入海洋中的雨滴
将自己溶入大海,化作一阵清风
简介:
owc控件是microsoft office 图表控件(owc10为ofiiceXP的组件、owc11为office2003的组件,组件的路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\owc11,帮助文件的路径为C:\Program Files\Common Files\Microsoft Shared\Web Components\11\2052),它可以生成三维图、柱状图、饼状图、趋势图和误差图
--------------------------------------------------------------------------------
相关资料:
--------------------------------------------------------------------------------
Office 2003 加载项:Office Web Components 官方下载地址(OWC11)

http://www.microsoft.com/downloads/details.aspx?FamilyID=7287252c-402e-4f72-97a5-e0fd290d4b76&DisplayLang=zh-cn

如果您机器里完全安装OFFICE2003则不需要下载

获取并安装 Office 2003 主 interop 程序集 (PIA) 和如何对其进行引用以及疑难解答

http://www.microsoft.com/china/msdn/library/office/office/OfficePrIntopAssFAQ.mspx?mfr=true

--------------------------------------------------------------------------------
相关代码(本代码主要功能类来自博客园)
--------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Office.Interop.Owc11;
using System.IO;
using Microsoft.Office.Interop;
using OWC11 = Microsoft.Office.Interop.Owc11;
using System.Text;





public class ChartFactory
    {
        public ChartFactory()
        {
            InitTypeMap();
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        private string[] chartCategoriesArr;
        private string[] chartValuesArr;
        private OWC11.ChartChartTypeEnum chartType = OWC11.ChartChartTypeEnum.chChartTypeColumn3D;//默认值
        private static Hashtable chartMap = new Hashtable();
        private static string chartTypeCh = "垂直柱状统计图";
        private static string chartTitle = "aaa";

        private void InitTypeMap()
        {
            chartMap.Clear();
            OWC11.ChartChartTypeEnum[] chartTypes = new 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]);
            }
        }
        public ChartSpaceClass BuildCharts()
        {
            string chartCategoriesStr = String.Join("\t", chartCategoriesArr);
            string chartValuesStr = String.Join("\t", chartValuesArr);

            OWC11.ChartSpaceClass oChartSpace = new OWC11.ChartSpaceClass();

            // ------------------------------------------------------------------------
            // Give pie and doughnut charts a legend on the bottom. For the rest of
            // them let the control figure it out on its own.
            // ------------------------------------------------------------------------

            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].PlotArea.Interior.Color = "red";
            //绘图区
            // oChartSpace.Charts[0].PlotArea.Floor.Interior.Color = "green";

            oChartSpace.Charts[0].SeriesCollection.Add(0);
            oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection.Add();
            //柱状图颜色
            //oChartSpace.Charts[0].SeriesCollection[0].Interior.Color = "red";

            // ------------------------------------------------------------------------
            // If you're charting a pie or a variation thereof percentages make a lot
            // more sense than values...
            // ------------------------------------------------------------------------
            if (chartType == ChartChartTypeEnum.chChartTypePie ||
                chartType == ChartChartTypeEnum.chChartTypePie3D ||
                chartType == ChartChartTypeEnum.chChartTypeDoughnut)
            {
                oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasPercentage = true;
                oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasValue = false;
            }
                // ------------------------------------------------------------------------
                // Not so for other chart types where values have more meaning than
                // percentages.
                // ------------------------------------------------------------------------
            else
            {
                oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasPercentage = false;
                oChartSpace.Charts[0].SeriesCollection[0].DataLabelsCollection[0].HasValue = true;
            }

            // ------------------------------------------------------------------------
            // Plug your own visual bells and whistles here
            // ------------------------------------------------------------------------
            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 = true;
            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(OWC11.ChartDimensionsEnum.chDimCategories,
                Convert.ToInt32(OWC11.ChartSpecialDataSourcesEnum.chDataLiteral), chartCategoriesStr);

            oChartSpace.Charts[0].SeriesCollection[0].SetData(OWC11.ChartDimensionsEnum.chDimValues,
                Convert.ToInt32(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
    }





调用方法部分代码:
private void Form1_Load(object sender, System.EventArgs e)
        {
            try
            {
                                System.Threading.Thread thread=new System.Threading.Thread(new System.Threading.ThreadStart(aa));
                thread.Start();
                //ShowChart();
            }
            catch
            {}
            
        
        }

        void aa()
        {

            while(true)
            {
                ShowChart();
                System.Threading.Thread.Sleep(1000);
            }
        }

                
        //调用   首先需要在页面上放置一个pictureBox1来显示产生的统计图

        public void ShowChart()
        {
            try
            {
                string ConnString = "Data Source=SQL服务器地址;Initial Catalog=数据库名;Persist Security Info=True;User ID=用户名;Password=密码";
                DB.sqlDB db = new DB.sqlDB(ConnString);
                string sql = "查询字符串";
                DataTable table = db.getTable(sql);
                if (table != null && table.Rows.Count > 0)
                {

                    StringBuilder sb=new StringBuilder();
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        if (i == 0)
                        {
                            sb.Append(table.Rows[i]["content"].ToString());
                        }
                        else
                        {
                            sb.Append("," + table.Rows[i]["content"].ToString());
                        }

                    }
                    string[] CategoriesArr ={sb.ToString()};

                    sb=new StringBuilder();


                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        if (i == 0)
                        {
                            sb.Append(table.Rows[i]["cnt"].ToString());
                        }
                        else
                        {
                            sb.Append("," + table.Rows[i]["cnt"].ToString());
                        }

                    }
                    string[] ValuesArr = { sb.ToString() };
              
                    ChartFactory chartFactory = new ChartFactory();
                    //初始化赋值
                    chartFactory.chartCategoriesArrValue = CategoriesArr;
                    chartFactory.chartValuesArrValue = ValuesArr;
                    chartFactory.chartTitleValue = "“龙源夜、网通情”欢送毕生晚会节目投票";//柱形图标注名称
                    chartFactory.chartTypeValue = "垂直柱状统计图";//图类型,在类中定义过

                    OWC11.ChartSpaceClass oChartSpace = chartFactory.BuildCharts();
                    string path = FileName;  //产生图片并保存 页可以是png gif图片

                    oChartSpace.ExportPicture(path, "jpeg", this.Width-100, this.Height-100);//后面是图片的宽和高
                
//下面使用    FileStream 对象的原因是本例使用线程.每秒重新生成一个图,并将原图覆盖.
//如果不使用 FileStream 将会出现共享冲突.        
                    FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
                    byte[] bytes = new byte[fs.Length];
                    fs.Read(bytes, 0, bytes.Length);
                    fs.Close();
                    MemoryStream ms = new MemoryStream(bytes);
                    
                
                    System.Drawing.Image img=System.Drawing.Image.FromStream(ms);
//本例使用pictureBox1显示图片
                    this.pictureBox1.Image=img;
                    this.pictureBox1.Refresh();
                

                }
            }
            catch(Exception e)
            { //MessageBox.Show(e.ToString()); 
            }
        
        }

转自:http://blog.csdn.net/killer000777/archive/2006/12/04/1429837.aspx
posted on 2008-06-22 21:22  溶入海洋中的雨滴  阅读(4278)  评论(1编辑  收藏  举报