使用donetcharting可以很方便制作图表
引入dll,在页面放入控件
<dotnetCHARTING:Chart ID="Chart1" runat="server"> </dotnetCHARTING:Chart>
先写一个实体类,定义需要操作的变量
public class Charting { private string _phaysicalimagepath;//图片存放路径 private string _title; //图片标题 private string _xtitle;//图片x座标名称 private string _ytitle;//图片y座标名称 private string _seriesname;//图例名称 private int _picwidth;//图片宽度 private int _pichight;//图片高度 private MyChartType _type;//统计图类型(柱形,线形等) private bool _use3d;//是否显示成3维图片 private SeriesCollection _dt;//统计图数据源 private string _filename;//统计图片的名称(不包括后缀名) /**/ /// <summary> /// 图片存放路径 /// </summary> public string PhaysicalImagePath { set { _phaysicalimagepath = value; } get { return _phaysicalimagepath; } } /// <summary> /// 图片标题 /// </summary> public string Title { set { _title = value; } get { return _title; } } /**/ /// <summary> /// 图片x座标名称 /// </summary> public string XTitle { set { _xtitle = value; } get { return _xtitle; } } /**/ /// <summary> /// 图片y座标名称 /// </summary> public string YTitle { set { _ytitle = value; } get { return _ytitle; } } /**/ /// <summary> /// 图例名称 /// </summary> public string SeriesName { set { _seriesname = value; } get { return _seriesname; } } /// <summary> /// 图片宽度 /// </summary> public int PicWidth { set { _picwidth = value; } get { return _picwidth; } } /**/ /// <summary> /// 图片高度 /// </summary> public int PicHight { set { _pichight = value; } get { return _pichight; } } /// <summary> /// 统计图类型(柱形,线形等) /// </summary> public MyChartType Type { set { _type = value; } get { return _type; } } /// <summary> /// 是否将输出的图片显示成三维 /// </summary> public bool Use3D { set { _use3d = value; } get { return _use3d; } } /// <summary> /// 对比图形数据源 /// </summary> public SeriesCollection DataSource { set { _dt = value; } get { return _dt; } } /// <summary> /// 生成统计图片的名称 /// </summary> public string FileName { set { _filename = value; } get { return _filename; } } /// <summary> /// 生成统计图片 /// </summary> /// <param name="chart"></param> /// <param name="type">图形类别,如柱状,折线型</param> public void CreateStatisticPic(dotnetCHARTING.Chart chart) { chart.SeriesCollection.Clear(); chart.Title = this.Title; chart.XAxis.Label.Text = this.XTitle; chart.YAxis.Label.Text = this.YTitle; chart.TempDirectory = this.PhaysicalImagePath; chart.FileManager.FileName = this.FileName; chart.Width = this.PicWidth; chart.Height = this.PicHight; if (this.Type == MyChartType.Pie) { chart.Type = ChartType.Pie; chart.Use3D = true; chart.PieLabelMode = PieLabelMode.Outside; chart.DefaultSeries.DefaultElement.Transparency = 5; } else { chart.Type = ChartType.Combo; SeriesType st = (SeriesType)this.Type; chart.DefaultSeries.Type = st; //统一使用默认的序列图类型属性 chart.Use3D = this.Use3D; } //chart.Series.Type = this.Type;//生成对比的线型图时不适用 chart.Series.Name = this.SeriesName; chart.SeriesCollection.Add(this.DataSource); //chart.Series.Data = this.DataSource; chart.DefaultSeries.DefaultElement.ShowValue = true; chart.ShadingEffect = true; chart.Series.DefaultElement.ShowValue = true; } public SeriesCollection getArrayData(DataTable dt) { SeriesCollection SC = new SeriesCollection(); for (int i = 0; i < dt.Rows.Count; i++) { Series s = new Series(); s.Name = dt.Rows[i][0].ToString(); Element e = new Element(); // 每元素的名称 e.Name = dt.Rows[i][0].ToString(); // 每元素的大小数值 e.YValue = Convert.ToInt32(dt.Rows[i][1].ToString()); s.Elements.Add(e); SC.Add(s); } return SC; } public enum MyChartType { Marker = 1, Spline = 2, Line = 3, AreaLine = 4, Column = 5, Cylinder = 6, Bar = 7, Bubble = 8, AreaSpline = 9, Pyramid = 10, Cone = 11, BubbleShape = 12, BarSegmented = 13, Pie = 14 } }
创建图像
/// <summary> /// 主要功能实例化Charting类创建图像, /// 适合多维数据,依赖外部函数GetDataSource(DataTable dt), /// 传入的数据列需要和GetDataSource(DataTable dt)函数中数据列相照应。 /// </summary> /// <param name="dt"></param> private void CreateImg(DataTable dt) { try { Charting c = new Charting(); if (ReportType.Value == "AreaReport") { c.DataSource = c.getArrayData(dt); } else { c.DataSource = GetDataSource(dt); } c.PicHight = 500; c.PicWidth = 800; c.Title = CreateTitle(); if (ReportType.Value == "AreaReport") { c.XTitle = "地区"; c.YTitle = "人数"; } else { c.XTitle = "日期"; c.YTitle = "人数"; } if (RadioButtonList1.SelectedValue == "1") { c.Type = Charting.MyChartType.Cylinder; } else { c.Type = Charting.MyChartType.Line; } if (ReportType.Value == "AreaReport") { c.Type = Charting.MyChartType.Pie; RadioButtonList1.Visible = false; } c.Use3D = false; //定义图像的存储路径 c.PhaysicalImagePath = "ChartImages"; c.SeriesName = "总计"; //图像的标题 c.FileName = CreateTitle(); //绘制chart1控件 c.CreateStatisticPic(this.Chart1); } catch (Exception ex) { } }
创建图像中的排序数据方法
private SeriesCollection GetDataSource(DataTable dt) { SeriesCollection sc = new SeriesCollection(); Series y = new Series(); Series y1 = new Series(); y.Name = "网络预约人数"; y1.Name = "网络到院人数"; string type = ReportType.Value; if (type == "HourDate") { y.Name = "网络对话预约人数"; y1.Name = "网络到院人数"; } #region 年龄统计中筛选年龄段 if (ReportType.Value == "AgeData") { int temp1 = 0; int temp2 = 0; for (int i = 0; i < dt.Rows.Count; i++) { Element e = new Element(); Element e1 = new Element(); if (Convert.ToInt32( dt.Rows[i]["date"])%5==0) { e.Name = dt.Rows[i]["date"].ToString(); e1.Name = dt.Rows[i]["date"].ToString(); if (!DBNull.Value.Equals(dt.Rows[i]["yuyuecount"])) { e.YValue = Convert.ToInt32(dt.Rows[i]["yuyuecount"]) + temp1; } else { e.YValue = 0; } if (!DBNull.Value.Equals(dt.Rows[i]["daoyuancount"])) { e1.YValue = Convert.ToInt32(dt.Rows[i]["daoyuancount"]) + temp2; } else { e1.YValue = 0; } y.Elements.Add(e); y1.Elements.Add(e1); temp1 = 0; temp2 = 0; } else { if (!DBNull.Value.Equals(dt.Rows[i]["yuyuecount"])) { temp1 += Convert.ToInt32(dt.Rows[i]["yuyuecount"]); } else { e.YValue = 0; } if (!DBNull.Value.Equals(dt.Rows[i]["daoyuancount"])) { temp2 += Convert.ToInt32(dt.Rows[i]["daoyuancount"]) ; } else { e1.YValue = 0; } } } } #endregion //普通统计 else { for (int i = 0; i < dt.Rows.Count; i++) { Element e = new Element(); Element e1 = new Element(); e.Name = dt.Rows[i]["date"].ToString(); e1.Name = dt.Rows[i]["date"].ToString(); if (!DBNull.Value.Equals(dt.Rows[i]["yuyuecount"])) { e.YValue = Convert.ToInt32(dt.Rows[i]["yuyuecount"]); } else { e.YValue = 0; } if (!DBNull.Value.Equals(dt.Rows[i]["daoyuancount"])) { e1.YValue = Convert.ToInt32(dt.Rows[i]["daoyuancount"]); } else { e1.YValue = 0; } y.Elements.Add(e); y1.Elements.Add(e1); } } sc.Add(y); sc.Add(y1); return sc; }
获取数据
private DataTable GetTable() { DataTable dt = new DataTable(); string type = ReportType.Value; string sql = ""; StringBuilder condition = new StringBuilder(); StringBuilder conditionDaoyuan = new StringBuilder(); switch (type) { case "OneWeek": condition.Append(" and datediff(dd,yuyuedate,getdate())<7 "); conditionDaoyuan.Append(" and datediff(dd,jizhendate,getdate())<7 "); break; case "AnyWeek": int num = Convert.ToInt32(DropAnyWeek.SelectedItem.Text); condition.Append(" and datediff(day,yuyuedate,getdate())<" + num * 7); conditionDaoyuan.Append(" and datediff(day,jizhendate,getdate())<" + num * 7); break; case "TatolWeek": condition.Append(" and datediff(year,yuyuedate,getdate())=0 "); conditionDaoyuan.Append(" and datediff(year,jizhendate,getdate())=0 "); break; case "MonthDate": #region MyRegion string selectT = DropDate.SelectedItem.Text; //添加时间限定,添加日期号限定 if (startTimeTxt.Text != "" && endTimeTxt.Text != "") { DateTime sT = Convert.ToDateTime(startTimeTxt.Text + "-01 00:00:00.000"); DateTime endT = Convert.ToDateTime(endTimeTxt.Text + "-01 00:00:00.000").AddMonths(1); condition.Append(" and yuyuedate>'" + sT.ToString() + "' and yuyuedate <'" + endT.ToString() + "'"); conditionDaoyuan.Append(" and jizhendate >'" + sT.ToString() + "' and jizhendate <'" + endT.ToString() + "'"); } condition.Append(" and datepart(dd,yuyuedate)= " + selectT); conditionDaoyuan.Append(" and datepart(dd,jizhendate)= " + selectT); break; case "TotalMonthDate": condition.Append(" and datepart(dd,yuyuedate)= " + DropDate.SelectedItem.Text); conditionDaoyuan.Append(" and datepart(dd,jizhendate)= " + DropDate.SelectedItem.Text); condition.Append(" and datediff(year,yuyuedate,getdate())=0 "); conditionDaoyuan.Append(" and datediff(year,jizhendate,getdate())=0 "); break; #endregion case "HourDate": break; case "AgeData": if (StartAge.Text != "" && EndAge.Text != "") { if (Convert.ToDateTime(StartAge.Text) > Convert.ToDateTime(EndAge.Text)) { Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('日期选择错误!')", true); return null; } DateTime sT = Convert.ToDateTime(StartAge.Text + "-01 00:00:00.000"); DateTime endT = Convert.ToDateTime(EndAge.Text + "-01 00:00:00.000").AddDays(1); condition.Append(" and ( dialogTime between '" + sT.ToString() + "' and '" + endT.ToString() + "')"); conditionDaoyuan.Append(" and (jizhendate between'" + sT.ToString() + "' and '" + endT.ToString() + "')"); } break; } if (ddlZixun.SelectedValue != "-1") { condition.Append(" and expert_zixun=" + ddlZixun.SelectedValue); conditionDaoyuan.Append(" and expert_zixun=" + ddlZixun.SelectedValue); } if (ddlKeshi.SelectedValue != "-1") { condition.Append(" and Keshi =" + ddlKeshi.SelectedValue); conditionDaoyuan.Append(" and Keshi =" + ddlKeshi.SelectedValue); } //sql语句选取 switch (type) { case "OneWeek": case "AnyWeek": case "TatolWeek": sql = "select yuyue.date,yuyue.yuyuecount,daoyuan.daoyuancount from (select count(*) as yuyuecount,datepart(w,yuyuedate) as date " + "from yy_detail where 1=1 and tujing=(select id from yy_tujingtype where name='网络')" + condition.ToString() + " group by datepart(w,yuyuedate)" + ")as yuyue,(select count(*) as daoyuancount,datepart(w,jizhendate) as date from yy_detail where 1=1 and tujing=(select id from yy_tujingtype where name='网络')" + conditionDaoyuan.ToString() + " and laiyuanqingkuang=1 group by datepart(w,jizhendate)) as daoyuan where yuyue.date=daoyuan.date order by yuyue.date asc"; break; case "MonthDate": case "TotalMonthDate": sql = " select yuyue.date,yuyue.yuyuecount,daoyuan.daoyuancount from (select count(*) as yuyuecount,datepart(mm,yuyuedate) as date" + " from yy_detail where 1=1 and tujing=(select id from yy_tujingtype where name='网络') " + condition.ToString() + " and datediff(yy,yuyuedate,getdate())=0 group by datepart(mm,yuyuedate)" + ")as yuyue,(select count(*) as daoyuancount,datepart(mm,jizhendate) as date from yy_detail where 1=1 and tujing=(select id from yy_tujingtype where name='网络') " + conditionDaoyuan.ToString() + "group by datepart(mm,jizhendate) ) as daoyuan where yuyue.date=daoyuan.date order by yuyue.date asc"; break; case "HourDate": //添加时间限定,添加日期号限定 if (HourStart.Text != "" && HourEnd.Text != "") { DateTime sHour = Convert.ToDateTime(HourStart.Text + " 00:00:00.000"); DateTime endHour = Convert.ToDateTime(HourEnd.Text + " 00:00:00.000").AddDays(1); SqlParameter[] p = new SqlParameter[]{ new SqlParameter("@conditionStart",SqlDbType.VarChar,60), new SqlParameter("@conditionEnd",SqlDbType.VarChar,60) }; p[0].Value = sHour; p[1].Value = endHour; try { dt = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.StoredProcedure, "pro_hourshot", p).Tables[0]; } catch { } } break; case "AgeData": sql = "select yuyue.date,yuyue.yuyuecount,daoyuan.daoyuancount from (select cast(Patientage as int) as date, count(*) as yuyuecount from yy_detail where 1=1 and( laiyuanqingkuang=0 or laiyuanqingkuang is null) and tujing=(select id from yy_tujingtype where name='网络') " + condition.ToString() + " group by cast(Patientage as int) )as yuyue,(select cast(Patientage as int) as date, count(*) as daoyuancount from yy_detail where 1=1 and laiyuanqingkuang=1 and tujing=(select id from yy_tujingtype where name='网络') " + conditionDaoyuan.ToString() + " group by cast(Patientage as int)) as daoyuan where yuyue.date=daoyuan.date order by yuyue.date asc"; break; } try { if (dt.Columns.Count == 0) { dt = SqlHelper.ExecuteDataset(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, sql).Tables[0]; } } catch (Exception ex) { Response.Write(ex.ToString()); } return dt; }
其中sql的编写方式在另一篇中。