惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - 溶入海洋中的雨滴

ADO Entities Framework不对多表查询进行优化? case when遇上null值 【转】C#正则表达式整理备忘 【摘】UI设计中对比色颜色的选取 JavaScript中的正则表达式解析 JavaScript正则表达式exec和test方法实例! - 溶入海洋中的雨滴 - 博客园 用js实现用回车键、ctrl键在文本框导航 [原]无刷新三级连动用户控件 c#批量插入数据到数据库【支持事务操作】 SQL:定时作业的设置方法 【原】css设置布局时,尽量天上背景色 【原】JS控制控件的显示,你选择display还是visibilty 【原】js实现2个listbox的乾坤大挪移 [转]数据库设计14个技巧 【转】sql查询性能调试,用SET STATISTICS IO和SET STATISTICS TIME 【原】winform定制datagrid模板 【原】winform高效导出Excel带格式设置 【原】让3层下的objectDatasource,支持griedview不写代码具备编辑功能 推荐一个可以下.net电子书的论坛
【转】c#用柱形图、折线图和饼形图展示数据
溶入海洋中的雨滴 · 2008-06-22 · via 博客园 - 溶入海洋中的雨滴

简介:
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