日期:2014-05-20 浏览次数:20806 次
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)); } } }