添加引用
using ZedGraph.dll
using ZedGraph.Web.dll
导命名空间
using ZedGraph;
using ZedGraph.Web;
在目录下添加个ZedGraphImages 放图片的文件夹
<%@ Register Assembly="ZedGraph.Web" Namespace="ZedGraph.Web" TagPrefix="cc1" %>
在Form_load事件中写以下代码
DirectoryInfo da = neaw DirectoryInfo(Server.MapPath(Request.ApplicationPath.ToString()) + "/ZedGraphImages");
FileInfo[] file = da.GetFiles();
for (int i = 0; i < file.Length; i++)
{
File.Delete(file[i].FullName);
}
ConnStr = ConfigurationManager.AppSettings["Connectstr"];
this.ZedGraphWeb1.RenderGraph += new ZedGraphWebControlEventHandler(OnRenderGraph);
private void OnRenderGraph(ZedGraphWeb zgw, Graphics g, MasterPane masterPane)
{
string sql = " select a.dept_no || ' ' || p.dept_cname dept , count(*) sum, round( count(*)/tot*100 , 2 ) percent,p.dept_cname,a.dept_no";
sql += " from advice a, prm06 p,";
sql += " (select count(*) tot";
sql += " from advice a, prm06 p ";
sql += " where a.dept_no = p.dept_no";
sql += " and advice_date between '" + beginTime + "' and '" + endTime + "'";
sql += " ) c";
sql += " where a.dept_no = p.dept_no";
sql += " and advice_date between '" + beginTime + "' and '" + endTime + "' ";
sql += " group by a.dept_no || ' ' || p.dept_cname ,tot,p.dept_cname,a.dept_no ";
sql += " order by count(*) desc";
DataTable dtsql = ExeSQLdt(sql);
GraphPane myPane = masterPane[0];
myPane.Title.Text = "各部门意见数量统计";
ArrayList alcount = new ArrayList();
ArrayList alname = new ArrayList();
if (dtsql.Rows.Count > 0)
{
for (int i = 0; i < dtsql.Rows.Count; i++)
{
alcount.Add(dtsql.Rows[i][1].ToString());
alname.Add(dtsql.Rows[i][0].ToString());
}
}
double[] values = cvtALtoDouble(alcount);
double[] displacement = { .0, .0, .0, .0 };
string[] labels = cvtALtoString(alname);
myPane.Fill = new Fill(Color.Cornsilk);
myPane.Chart.Fill = new Fill(Color.Cornsilk);
myPane.Legend.Position = LegendPos.Right;
PieItem[] slices = new PieItem[values.Length];
slices = myPane.AddPieSlices(values, labels);
for (int i = 0; i < dtsql.Rows.Count; i++)
{
((PieItem)slices[i]).LabelType = PieLabelType.Name_Value_Percent;
}
((PieItem)slices[1]).Displacement = .2;
CurveList curves = myPane.CurveList;
double total = 0;
for (int x = 0; x < curves.Count; x++)
total += ((PieItem)curves[x]).Value;
BoxObj box = new BoxObj(0, 0, 1, 1, Color.Empty, Color.PeachPuff);
box.Location.CoordinateFrame = CoordType.ChartFraction;
box.Border.IsVisible = false;
box.Location.AlignH = AlignH.Left;
box.Location.AlignV = AlignV.Top;
box.ZOrder = ZOrder.E_BehindCurves;
myPane.GraphObjList.Add(box);
masterPane.AxisChange(g);
}
//把ArrayList转换为double的数组
private double[] cvtALtoDouble(ArrayList al)
{
double[] strs = new double[14];
for (int i = 0; i < al.Count; i++)
{
strs[i] = double.Parse(al[i].ToString());
}
return strs;
}
//把ArrayList转换为string的数组
private string[] cvtALtoString(ArrayList al)
{
string[] strs = new string[14];
for (int i = 0; i < al.Count; i++)
{
strs[i] = al[i].ToString();
}
return strs;
}
网上原来的
System.IO.DirectoryNotFoundException: 未能找到路径“C:/Documents and Settings/xiaojiao/桌面/storeAdviceSystem/ZedGraphImages/ZedGraphWeb10c13f13c-d5e5-4449-86f7-b2242936aa04.png”的一部分
在运行网页之前现在根目录下建一个ZedGraphImages文件夹,不然就有这个错误。
<%@ Page Language="C#" CodeFile="Default2.aspx.cs" Inherits="Default2" Theme="" %>
<%@ Register TagPrefix="zgw" Namespace="ZedGraph.Web" Assembly="ZedGraph.Web" %>
<ZGW:ZEDGRAPHWEB id="ZedGraphWeb1" runat="server" width="500" Height="375" RenderMode="RawImage" />
ZedGraph.aspx页面代码
ZedGraph.aspx.cs代码
1 using System;
2 using System.Collections;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Web;
7 using System.Web.SessionState;
8 using System.Web.UI;
9 using System.Web.UI.WebControls;
10 using System.Web.UI.HtmlControls;
11 using ZedGraph;
12 using ZedGraph.Web;
13
14 namespace ZedGraph
15 {
16 /// <summary>
17 /// ZedGraph 的摘要说明。
18 /// </summary>
19 public class ZedGraph : System.Web.UI.Page
20 {
21 protected ZedGraphWeb ZedGraphWeb1;
22 private void Page_Load(object sender, System.EventArgs e)
23 {
24 }
25
26 #region Web 窗体设计器生成的代码
27 override protected void OnInit(EventArgs e)
28 {
29 //
30 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
31 //
32 InitializeComponent();
33 base.OnInit(e);
34 }
35
36 /// <summary>
37 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
38 /// 此方法的内容。
39 /// </summary>
40 private void InitializeComponent()
41 {
42 this.Load += new System.EventHandler(this.Page_Load);
43 this.ZedGraphWeb1.RenderGraph +=new ZedGraphWebControlEventHandler(this.OnRenderGraph);
44
45 }
46 #endregion
47 private void OnRenderGraph(ZedGraphWeb zgw, Graphics g, MasterPane masterPane)
48 {
49 // Get the GraphPane so we can work with it
50 GraphPane myPane = masterPane[0];
51
52 myPane.Title.Text = "销售统计";
53 myPane.XAxis.Title.Text = "区域";
54 myPane.YAxis.Title.Text = "销售总额: 元";
55
56 PointPairList list = new PointPairList();
57 PointPairList list2 = new PointPairList();
58 PointPairList list3 = new PointPairList();
59 Random rand = new Random();
60
61 for (double x = 0; x < 5; x += 1.0)
62 {
63 double y = rand.NextDouble() * 100;
64 double y2 = rand.NextDouble() * 100;
65 double y3 = rand.NextDouble() * 100;
66 list.Add(x, y);
67 list2.Add(x, y2);
68 list3.Add(x, y3);
69 }
70
71 BarItem myCurve = myPane.AddBar("购买", list, Color.Blue);
72 myCurve.Bar.Fill = new Fill(Color.Blue, Color.White, Color.Blue);
73 BarItem myCurve2 = myPane.AddBar("续费", list2, Color.Red);
74 myCurve2.Bar.Fill = new Fill(Color.Red, Color.White, Color.Red);
75 BarItem myCurve3 = myPane.AddBar("升级", list3, Color.Green);
76 myCurve3.Bar.Fill = new Fill(Color.Green, Color.White, Color.Green);
77
78 myPane.XAxis.MajorTic.IsBetweenLabels = true;
79 string[] labels = { "域名", "主机", "数据库", "邮局", "套餐" };
80 myPane.XAxis.Scale.TextLabels = labels;
81 myPane.XAxis.Type = AxisType.Text;
82 myPane.Fill = new Fill(Color.White, Color.FromArgb(200, 200, 255), 45.0f);
83 myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f);
84
85 masterPane.AxisChange(g);
86 }
87 }
88 }