用owc11绘制曲线图

来源:互联网 发布:小卖家怎么找淘宝客 编辑:程序博客网 时间:2024/04/29 16:07

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using Microsoft.Office.Interop;
namespace Ok4sOWCChart
{
    /// <summary>
    /// 利用OWC进行作图统计封装类动态图片
    /// by zuofanghui
    /// </summary>
    public class OWCChart11:System.Web.UI.Page
    {
        #region 基本参数
        private string strCategory, strvalue;
        //图表标题
        private string _chatTitle;

        public string chatTitle
        {
            get { return _chatTitle; }
            set { _chatTitle = value; }
        }
        //图表背景
        private string _BgColor="";

        public string BgColor
        {
            get { return _BgColor; }
            set { _BgColor = value; }
        }
 
        //是否显示标题
        private bool _ShowTitle=false;

        public bool ShowTitle
        {
            get { return _ShowTitle; }
            set { _ShowTitle = value; }
        }
    
        //是否显示XY轴
        private bool _BoolXY=false;

        public bool BoolXY
        {
            get { return _BoolXY; }
            set { _BoolXY = value; }
        }
 
        //X Y轴名称
        private string _Xname;

        public string Xname
        {
            get { return _Xname; }
            set { _Xname = value; }
        }
        private string _Yname;

        public string Yname
        {
            get { return _Yname; }
            set { _Yname = value; }
        }
 
        //给定series的名字
        private string _SeriesName;

        public string SeriesName
        {
            get { return _SeriesName; }
            set { _SeriesName = value; }
        }
        private bool _IsSeries=false;

        public bool IsSeries
        {
            get { return _IsSeries; }
            set { _IsSeries = value; }
        }
 
        //图片宽度
        private int _ImgWidth;

        public int ImgWidth
        {
            get { return _ImgWidth; }
            set { _ImgWidth = value; }
        }
     //图片高度
        private int _ImgHeight;

        public int ImgHeight
        {
            get { return _ImgHeight; }
            set { _ImgHeight = value; }
        }
     //数据源
        private DataTable _DataSource;

        public DataTable DataSource
        {
            get { return _DataSource; }
            set {
                _DataSource = value;
                strCategory = GetColumnsStr(_DataSource);
                strvalue = GetvalueStr(_DataSource);
            }
        }

        //取数据
        private string GetColumnsStr(DataTable dt)
        {
            StringBuilder strList = new StringBuilder();
            foreach (DataRow r in dt.Rows)
            {
                strList.Append(r[0].ToString() + /t);
            }
            return strList.ToString();
        }
        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

        public OWCChart11()
        { }

        public byte[] CreateLine()
        {
            //声明对象
            Microsoft.Office.Interop.Owc11.ChartSpace ThisChart = new Microsoft.Office.Interop.Owc11.ChartSpaceClass(); //创建ChartSpace对象来放置图表 
            Microsoft.Office.Interop.Owc11.ChChart ThisChChart = ThisChart.Charts.Add(0); //在ChartSpace对象中添加图表,Add方法返回chart对象
            Microsoft.Office.Interop.Owc11.ChSeries ThisChSeries = ThisChChart.SeriesCollection.Add(0); ////添加一个series

            //去除容器边框
            ThisChart.Border.Color = Microsoft.Office.Interop.Owc11.ChartColorIndexEnum.chColorNone;
           
            //显示图例
            ThisChChart.HasLegend = _IsSeries;
           
            //标题
            if (_ShowTitle)
            {
                ThisChChart.HasTitle = true;
                ThisChChart.Title.Caption = _chatTitle;
                ThisChChart.Title.Font.Size = 9;
            }
            //给定x,y轴图示说明
            if (_BoolXY)
            {
                ThisChChart.Axes[0].HasTitle = true;
                ThisChChart.Axes[1].HasTitle = true;
                ThisChChart.Axes[0].Title.Caption = _Xname;
                ThisChChart.Axes[0].Title.Font.Size = 9;
                ThisChChart.Axes[1].Title.Caption = _Yname;
                ThisChChart.Axes[1].Title.Font.Size = 9;
                //ThisChChart.Axes[0].Position = Microsoft.Office.Interop.Owc11.ChartAxisPositionEnum.chAxisPositionBottom;
            }

            //图表类型
            ThisChChart.Type = Microsoft.Office.Interop.Owc11.ChartChartTypeEnum.chChartTypeSmoothLine;
            //背景颜色
            ThisChChart.PlotArea.Interior.Color = _BgColor;
           
           
            /**/
            ////给定series的名字
            ThisChSeries.SetData(Microsoft.Office.Interop.Owc11.ChartDimensionsEnum.chDimSeriesNames, Microsoft.Office.Interop.Owc11.ChartSpecialDataSourcesEnum.chDataLiteral.GetHashCode(), "日期");
            //给定分类
            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);
            //ThisChSeries.Line.Color = "black";
            ThisChSeries.Line.DashStyle = Microsoft.Office.Interop.Owc11.ChartLineDashStyleEnum.chLineSolid;
           
           
            //顶部显示数据
            Microsoft.Office.Interop.Owc11.ChDataLabels dl = ThisChChart.SeriesCollection[0].DataLabelsCollection.Add();
            dl.Hasvalue = true;
            dl.Font.Color = "red";

            return (byte[])ThisChart.GetPicture("gif", _ImgWidth, _ImgHeight);

        }
    }
}

原创粉丝点击