日期:2014-05-20  浏览次数:20806 次

水晶报表 ASP.NET 2.0中OWC生成柱状图和饼状图的源代码 [共享]
1、首先下载owc11:owc11

2、引用OWC:打开"解决方案资源管理器"面板,鼠标右键单击"引用",选择"添加引用"菜单,在弹出的"添加引用"对话框中单击"COM"卡片,找到"Microsoft Office Web Components 10.0(office 2003是11.0)",单击"选择"和"确定"按钮,OWC就被添加到了引用中。

3、建立一个pic.aspx文件,aspx代码如下:

<script language="javascript" type="text/javascript" src="/js/Calendar30.js"></script>
选择开始日期和结束日期。如果时间范围超过365则显示年表,如果时间范围超过30则显示月份表,如果超过1则显示日期表,否则显示小时表。<br />
开始日期:<input id="s" type="text" value="<%= Request.QueryString["s"]%>" onclick="fillDay(this)" >结束日期:<input id="e" value="<%= Request.QueryString["e"]%>" type="text" onclick="fillDay(this)" /><input type="button" value="查看" onclick="location.href='?s='+$F('s')+'&e='+$F('e')" /><br />
<asp:placeholder id="ChartHolder" runat="server"></asp:placeholder>

后台代码:
C# code

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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;


public partial class pic : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack && Request.QueryString["s"] != null && Request.QueryString["e"] != null)
        {
            string file;

            DateTime s = DateTime.Parse(Request.QueryString["s"]);
            DateTime et = DateTime.Parse(Request.QueryString["e"]);

            string x = "";
            string y = "";
            string danwei = "时";
            string sql = "";
            int r = ((TimeSpan)(et - s)).Days;
            //Response.Write(r);
            //Response.Write("<br />");
            if (r >= 365)
            {
                sql = "select datepart(yyyy,regtime) d,count(*) c from t_user where regtime between '{0}' and '{1}' group by datepart(yyyy,regtime) order by d";
                danwei = "年";
            }
            else if (r >= 30)
            {
                sql = "select datepart(mm,regtime) d,count(*) c from t_user where regtime between '{0}' and '{1}' group by datepart(mm,regtime) order by d";
                danwei = "月";
            }
            else if (r >= 1)
            {
                sql = "select datepart(dd,regtime) d,count(*) c from t_user where regtime between '{0}' and '{1}' group by datepart(dd,regtime) order by d";
                danwei = "日";
            }
            else
                sql = "select datepart(HH,regtime) d,count(*) c from t_user where regtime between '{0}' and '{1}' group by datepart(HH,regtime) order by d";

            DateTime s1 = new DateTime(s.Year, s.Month, s.Day);
            DateTime e1 = new DateTime(et.Year, et.Month, et.Day, 23, 59, 59);

            sql = string.Format(sql, s1.ToString(), e1.ToString());
            //Response.Write(sql);
            //return;

            DataSet ds = DAL.dbHelper.ExecuteDataset(sql);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                x = ds.Tables[0].Rows[0][0].ToString();
                y = ds.Tables[0].Rows[0][1].ToString();
                for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
                {
                    x += '\t' + ds.Tables[0].Rows[i][0].ToString();
                    y += '\t' + ds.Tables[0].Rows[i][1].ToString();
                }

                //创建ChartSpace对象来放置图表
                ChartSpace objCSpace = new ChartSpaceClass();

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

                //指定图表的类型。类型由ChartChartTypeEnum枚举值得到
                objChart.Type = ChartChartTypeEnum.chChartTypeColumnClustered;

                //指定图表是否需要图例
                objChart.HasLegend = true;

                //给定标题
                objChart.HasTitle = true;
                objChart.Title.Caption = "注册用户/日期分布图";

                //给定x,y轴的图示说明
                objChart.Axes[0].HasTitle = true;
                objChart.Axes[0].Title.Caption = "X : 日期/单位: " + danwei;
                objChart.Axes[1].HasTitle = true;
                objChart.Axes[1].Title.Caption = "Y : 数量";

                //计算数据
                /*categories 和 values 可以用tab分割的字符串来表示*/
                //string strSeriesName = "图例 1";

                //添加一个series
                objChart.SeriesCollection.Add(0);

                //给定series的名字
                //objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimSeriesNames, +(int)ChartSpecialDataSourcesEnum.chDataLiteral, strSeriesName);

                //给定分类
                objChart.SeriesCollection[0].SetData(ChartDimensionsEnum.chDimCategories,
                +(int)ChartSpecialDataSourcesEnum.chDataLiteral, x);

                //给定值
                objChart.SeriesCollection[0].SetData
                  (ChartDimensionsEnum.chDimValues,
                  (int)ChartSpecialDataSourcesEnum.chDataLiteral, y);
                //输出成GIF文件.
                file = "/tmp/" + Guid.NewGuid().ToString() + ".gif";

                objCSpace.ExportPicture(Server.MapPath("~" + file), "GIF", 800, 400);

            }
            else
            {
                file = "/images/0.gif";
            }
            //把图片添加到placeholder.
            string strImageTag = "<IMG SRC='" + file + "'/>";
            ChartHolder.Controls.Add(new LiteralControl(strImageTag));
        }

    }
}