1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| namespace ZedGraphDemo { public partial class Year : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { this.ZgwYear.RenderGraph += new ZedGraphWebControlEventHandler(ZgwYear_RenderGraph); }
void ZgwYear_RenderGraph(ZedGraphWeb webObject, System.Drawing.Graphics g, MasterPane pane) { GraphPane myPane = pane[0];
// 标题 myPane.Title.Text = "每年分析(月为单位)"; myPane.XAxis.Title.Text = "时间"; myPane.YAxis.Title.Text = "访问量";
// 坐标对集 PointPairList list = new PointPairList(); //PointPairList list1 = new PointPairList(); //PointPairList list2 = new PointPairList();
//从数据库中获取数据集 DataTable dtYear = GetData(); if (dtYear == null) return; for (int i = 0; i < dtYear.Rows.Count; i++) { list.Add(Convert.ToDouble(i), Convert.ToDouble(dtYear.Rows[i]["Count"])); //如果有多种类型,可以添加多个 //list1.Add(Convert.ToDouble(i), Convert.ToDouble(dtYear.Rows[i]["Count1"])); //list2.Add(Convert.ToDouble(i), Convert.ToDouble(dtYear.Rows[i]["Count2"])); }
BarItem myBar = myPane.AddBar("页面访问", list, Color.Blue); //BarItem myBar1 = myPane.AddBar("", list1, Color.Red); //BarItem myBar2 = myPane.AddBar("", list2, Color.Yellow);
myBar.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue); //myBar1.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red); //myBar2.Bar.Fill = new Fill(Color.Yellow, Color.White, Color.Yellow);
myPane.XAxis.MajorTic.IsBetweenLabels = true; // X轴Label string[] labels = new string[] { "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月" };
//设置x轴刻度 myPane.XAxis.Scale.TextLabels = labels; myPane.XAxis.Type = AxisType.Text;
// 颜色填充 myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f); myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
pane.AxisChange(g);
myPane.YAxis.Scale.Max += myPane.YAxis.Scale.MajorStep; BarItem.CreateBarLabels(myPane, false, "f0"); //在柱状图上方显示统计数 }
protected void btnQuerys_Click(object sender, EventArgs e) { GetData(); }
private DataTable GetData() { SqlHelper.ConnectionString = ConfigurationManager.AppSettings["ConnectionString"].ToString(); SqlParameter[] parameters = new SqlParameter[1];
parameters[0] = new SqlParameter("@Year", SqlDbType.Int, 4); parameters[0].Value = this.txtYear.Value.Trim().Length == 0 ? DateTime.Now.Year : Convert.ToInt32(txtYear.Value.Trim()); DataSet ds = SqlHelper.ExecuteDataset("Counter_CounterYear", parameters); if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) { return ds.Tables[0]; } else { return null; } } } }
|