C#处理chart

C#处理owc chart

---------------------------------------------ASP.NET版-----------------------------
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Office.Interop.Owc11;
using System.Data.OleDb;
using System.Collections;

public partial class _Default : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.Items.Add("chChartTypeColumnClustered");
        DropDownList1.Items.Add("chChartTypeColumn3D");
        DropDownList1.Items.Add("chChartTypeBarClustered");
        DropDownList1.Items.Add("chChartTypeBar3D");
        DropDownList1.Items.Add("chChartTypeArea");
        DropDownList1.Items.Add("chChartTypeArea3D");
        DropDownList1.Items.Add("chChartTypeDoughnut");
        DropDownList1.Items.Add("chChartTypeLineStacked");
        DropDownList1.Items.Add("chChartTypeLine3D");
        DropDownList1.Items.Add("chChartTypeLineMarkers");
        DropDownList1.Items.Add("chChartTypePie");
        DropDownList1.Items.Add("chChartTypePie3D");
        DropDownList1.Items.Add("chChartTypeRadarSmoothLine");
        DropDownList1.Items.Add("chChartTypeSmoothLine");
        //DropDownList1.SelectedIndex = 0;
        int num = DropDownList1.SelectedIndex;
        OleDbDataReader dr = GetList("select TOP 10 OrderDate,ShipVia from orders order by orderid");
        data_Chart(dr, 0, Server.MapPath("test.gif"), 800, 600);
        ImageMap1.ImageUrl = "~/test.gif";
    }
    /// <summary>
    /// 生成图表
    /// </summary>
    /// <param name="sdr">数据源(sqlDataReader)</param>
    /// <param name="intChartTypes">图片类型</param>
    /// <param name="filePath">图片路径</param>
    /// <param name="chartWidth">图片宽度</param>
    /// <param name="chartHeight">图片高度</param>
    /// <returns>图片存放的相对路径</returns>
    public string data_Chart(OleDbDataReader sdr, int intChartTypes, string filePath, int chartWidth, int chartHeight)
    {
        ArrayList[] data_al = new ArrayList[sdr.FieldCount];

        for (int i = 0; i < sdr.FieldCount; i++)
        {
            data_al[i] = new ArrayList();
        }

        while (sdr.Read())
        {
            for (int i = 0; i < sdr.FieldCount; i++)
            {
                data_al[i].Add(sdr.GetValue(i).ToString());
            }
        }
        sdr.Close();
        ChartSpace objCSpace = new ChartSpaceClass();

        //在ChartSpace对象中添加图表,Add方法返回chart对象
        ChChart objChart = objCSpace.Charts.Add(0);

        //指定图表是否需要图例
        objChart.HasLegend = true;
        objChart.HasTitle = true;
        objChart.Title.Caption = "分析图";

        //指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到
        ChartChartTypeEnum[] chartTypes = new 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};

        objChart.Type = chartTypes[intChartTypes];
        //string[] chartTypesCh = new string [] {"垂直柱状统计图","3D垂直柱状统计图","水平柱状统计图","3D水平柱状统计图","区域统计图","3D区域统计图","中空饼图","折线统计图","3D折线统计图","折线带点统计图","饼图","3D饼图","网状统计图","弧线统计图"};

        for (int i = 0; i < data_al.Length - 1; i++)
        {
            //在ChartSpace对象中添加图表,Add方法返回chart对象
            objChart.SeriesCollection.Add(i);
            objChart.SeriesCollection[i].DataLabelsCollection.Add();
            string strSeriesName = "" + (i + 1);
            //给定series的名字
            objChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimSeriesNames,
             (int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
            //给定分类
            objChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimCategories,
             (int)ChartSpecialDataSourcesEnum.chDataLiteral, data_al[0].ToArray());

            //给定值
            objChart.SeriesCollection[i].SetData
             (ChartDimensionsEnum.chDimValues,
             (int)ChartSpecialDataSourcesEnum.chDataLiteral, data_al[i + 1].ToArray());

            objChart.SeriesCollection[i].Caption = "Example";

        }
        //输出成GIF文件.
        //string strAbsolutePath = filePath + "\\temp\\test.gif";

        objCSpace.ExportPicture(filePath, "GIF", chartWidth, chartHeight);

        //创建GIF文件的相对路径.
        string strRelativePath;
        //strRelativePath = @"..\Analyse\temp\test.gif";
        strRelativePath = filePath;
        return strRelativePath;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        int num = DropDownList1.SelectedIndex;
        OleDbDataReader dr = GetList("select TOP 10 OrderDate,ShipVia from orders order by orderid");
        data_Chart(dr, num, Server.MapPath("test.gif"), 800, 600);
        ImageMap1.ImageUrl = "~/test.gif";
    }
    public OleDbDataReader GetList(string Sql)
    {
        OleDbConnection myConnection = new OleDbConnection("Provider=SQLOLEDB;server=192.168.0.15;uid=sa;pwd=sa; database=Northwind;");
        OleDbCommand myCommand = new OleDbCommand(Sql, myConnection);
        myConnection.Open();
        OleDbDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
        return result;
    }
}


--------------------------------------c#版----------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Owc11;
using System.Data.OleDb;
using System.Collections;

namespace OWC
{
    public partial class ADOchart : Form
    {
        public ADOchart()
        {
            InitializeComponent();
            //string[] chartTypesCh = new string [] {"垂直柱状统计图","3D垂直柱状统计图","水平柱状统计图","3D水平柱状统计图","区域统计图","3D区域统计图","中空饼图","折线统计图","3D折线统计图","折线带点统计图","饼图","3D饼图","网状统计图","弧线统计图"};
            comboBox1.Items.Add("垂直柱状统计图");
            comboBox1.Items.Add("3D垂直柱状统计图");
            comboBox1.Items.Add("水平柱状统计图");
            comboBox1.Items.Add("3D水平柱状统计图");
            comboBox1.Items.Add("区域统计图");
            comboBox1.Items.Add("3D区域统计图");
            comboBox1.Items.Add("中空饼图");
            comboBox1.Items.Add("折线统计图");
            comboBox1.Items.Add("3D折线统计图");
            comboBox1.Items.Add("折线带点统计图");
            comboBox1.Items.Add("饼图");
            comboBox1.Items.Add("3D饼图");
            comboBox1.Items.Add("网状统计图");
            comboBox1.Items.Add("弧线统计图");
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int num = comboBox1.SelectedIndex;
            OleDbDataReader dr = GetList("select top 10 OrderDate,ShipVia from orders order by orderid");
            data_Chart(dr, num, Application.StartupPath+"test.gif", 800, 600);
            MessageBox.Show("完成");
        }
        public OleDbDataReader GetList(string Sql)
        {
            OleDbConnection myConnection = new OleDbConnection("Provider=SQLOLEDB;server=192.168.0.15;uid=sa;pwd=sa; database=Northwind;");
            OleDbCommand myCommand = new OleDbCommand(Sql, myConnection);
            myConnection.Open();
            OleDbDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            return result;
        }
        public string data_Chart(OleDbDataReader sdr, int intChartTypes, string filePath, int chartWidth, int chartHeight)
        {
            ArrayList[] data_al = new ArrayList[sdr.FieldCount];
            for (int i = 0; i < sdr.FieldCount; i++)
            {
                data_al[i] = new ArrayList();
            }
            while (sdr.Read())
            {
                for (int i = 0; i < sdr.FieldCount; i++)
                {
                    data_al[i].Add(sdr.GetValue(i).ToString());
                }
            }
            sdr.Close();
            //清空上一次存在的图例
            axChartSpace1.Clear();
            ChartSpace objCSpace = new ChartSpaceClass();

            //在ChartSpace对象中添加图表,Add方法返回chart对象
           // ChChart objChart = objCSpace.Charts.Add(0);//保存图片用
            ChChart objChart = axChartSpace1.Charts.Add(0);//界面上用

            //指定图表是否需要图例
            objChart.HasLegend = true;
            objChart.HasTitle = true;
            objChart.Title.Caption = "分析图";
            //xy轴说明
            objChart.Axes[0].HasTitle = true;
            objChart.Axes[0].Title.Caption = "OrderDate";
            objChart.Axes[1].HasTitle = true;
            objChart.Axes[1].Title.Caption = "ShipVia";
            //指定图表的类型。类型由OWC.ChartChartTypeEnum枚举值得到
            ChartChartTypeEnum[] chartTypes = new 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};

            objChart.Type = chartTypes[intChartTypes];//得到图例类型

            for (int i = 0; i < data_al.Length - 1; i++)
            {
                //在ChartSpace对象中添加图表,Add方法返回chart对象
                objChart.SeriesCollection.Add(i);
                objChart.SeriesCollection[i].DataLabelsCollection.Add();
                string strSeriesName = "" + (i + 1);
                //给定series的名字
                objChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimSeriesNames,
                 (int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);
                //给定分类
                objChart.SeriesCollection[i].SetData(ChartDimensionsEnum.chDimCategories,
                 (int)ChartSpecialDataSourcesEnum.chDataLiteral, data_al[0].ToArray());
                //给定值
                objChart.SeriesCollection[i].SetData
                 (ChartDimensionsEnum.chDimValues,
                 (int)ChartSpecialDataSourcesEnum.chDataLiteral, data_al[i + 1].ToArray());
                objChart.SeriesCollection[i].Caption = "Example";
            }
            //输出成GIF文件.
            objCSpace.ExportPicture(filePath, "GIF", chartWidth, chartHeight);

            //创建GIF文件的相对路径.
            string strRelativePath;
            strRelativePath = filePath;
            return strRelativePath;
        }
    }
}

你可能感兴趣的:(chart)