前一阵的一个项目用到了开源项目ZedGraph制作了一个饼图,现把源代码贴出来供大家交流,也方便自己日后查看。
这个项目用的是VS2008,C#代码,zedgraph的版本是:zedgraph_dll_v5.1.4,另外要在项目根目录下面建立一个以ZedGraphImages命名的文件夹,供生成的图片临时存放处。
注:我在制作的过程中baidu搜索了不少相关信息,谢谢大家的无私奉献精神。特别感谢王旭博客上的文章http://www.cnblogs.com/wxukie/archive/2007/05/16/748922.html给我的启示。
1//yearTop10CommoditySell.aspx.cs
2using System;
3using System.Collections;
4using System.Collections.Generic;
5using System.Configuration;
6using System.Data;
7using System.Linq;
8using System.Web;
9using System.Web.Security;
10using System.Web.UI;
11using System.Web.UI.HtmlControls;
12using System.Web.UI.WebControls;
13using System.Web.UI.WebControls.WebParts;
14using System.Xml.Linq;
15
16using DHEECommoditySellSystem.ClassFiles;
17using ZedGraph;
18using ZedGraph.Web;
19using System.Drawing;
20using System.Drawing.Imaging;
21using DHEECommoditySellSystem.DataAccess;
22using System.Data.Common;
23using System.Data.SqlClient;
24
25
26namespace DHEECommoditySellSystem.SearchManagement
27{
28 public partial class Taxis : System.Web.UI.Page
29 {
30 /// 默认颜色种类
31 private List<COLOR></COLOR> defaultColors = new List<COLOR></COLOR>();
32 <SUMMARY></SUMMARY>
33 /// 初始化颜色数组
34
35 private void InitDefaultColors()
36 {
37 defaultColors.Add(Color.Red);
38 defaultColors.Add(Color.Green);
39 defaultColors.Add(Color.Blue);
40 defaultColors.Add(Color.Yellow);
41 defaultColors.Add(Color.YellowGreen);
42 defaultColors.Add(Color.Brown);
43 defaultColors.Add(Color.Aqua);
44 defaultColors.Add(Color.Cyan);
45 defaultColors.Add(Color.DarkSeaGreen);
46 defaultColors.Add(Color.Indigo);
47 }
48
49
50 /// PieItem.LabelType数组
51 PieLabelType[] lt = new PieLabelType[]{
52 PieLabelType.Name_Percent,
53 PieLabelType.Name_Value,
54 PieLabelType.Percent,
55 PieLabelType.Value,
56 PieLabelType.Name_Value
57 };
58
59
60 protected void Page_Load(object sender, EventArgs e)
61 {
62 InitializeComponent();
63 }
64
65 private void InitializeComponent()
66 {
67 this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraph);//注册事件
68 }
69
70 <SUMMARY></SUMMARY>
71 /// This method is where you generate your graph.
72 /// <PARAM name="masterPane" />You are provided with a MasterPane instance that
73 /// contains one GraphPane by default (accessible via masterPane[0]).
74 /// <PARAM name="g" />A graphics instance so you can easily make the call to AxisChange()
75 private void OnRenderGraph(ZedGraphWeb cc1, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane)
76 {
77 InitDefaultColors();
78 // Get the GraphPane so we can work with it
79 GraphPane myPane = masterPane[0];
80
81 // Set the pane title
82 myPane.Title.Text = "本年商品销售Top10";
83
84 // Fill the pane and axis background with solid color
85 myPane.Fill = new Fill(Color.Cornsilk);
86 myPane.Chart.Fill = new Fill(Color.Cornsilk);
87 myPane.Legend.Position = LegendPos.Right;
88
89 DatabaseHelper db=new DatabaseHelper();
90 DataSet ds = db.ExecuteDataSet("GetTop10CommoditySelledInThisWeek", CommandType.StoredProcedure);
91 int i = 0;
92 foreach (DataRowView view in ds.Tables[0].DefaultView)
93 {
94 double salesCount = double.Parse(view[0].ToString());
95 string commodityName = view["商品名称"].ToString();
96
97 PieItem segment = myPane.AddPieSlice(salesCount, defaultColors[i], Color.White, 45f, 0, commodityName);
98 i++;
99 segment.LabelType = PieLabelType.Value;
100 }
101
102 // Sum up the values
103 CurveList curves = myPane.CurveList;
104 double total = 0;
105 for (int x = 0; x < curves.Count; x++)
106 total += ((PieItem)curves[x]).Value;
107
108 // Add a text item to highlight total sales
109 TextObj text = new TextObj("Total Week Sales - " + "___FCKpd___0quot; + total.ToString() + "M", 0.85F, 0.80F, CoordType.PaneFraction);
110 text.Location.AlignH = AlignH.Center;
111 text.Location.AlignV = AlignV.Bottom;
112 text.FontSpec.Border.IsVisible = false;
113 text.FontSpec.Fill = new Fill(Color.White, Color.PowderBlue, 45F);
114 text.FontSpec.StringAlignment = StringAlignment.Center;
115 myPane.GraphObjList.Add(text);
116
117 // Add a colored background behind the pie
118 BoxObj box = new BoxObj(0, 0, 1, 1, Color.Empty, Color.PeachPuff);
119 box.Location.CoordinateFrame = CoordType.ChartFraction;
120 box.Border.IsVisible = false;
121 box.Location.AlignH = AlignH.Left;
122 box.Location.AlignV = AlignV.Top;
123 box.ZOrder = ZOrder.E_BehindCurves;
124 myPane.GraphObjList.Add(box);
125
126 masterPane.AxisChange(g);
127 }
128 }
129}
130
2using System;
3using System.Collections;
4using System.Collections.Generic;
5using System.Configuration;
6using System.Data;
7using System.Linq;
8using System.Web;
9using System.Web.Security;
10using System.Web.UI;
11using System.Web.UI.HtmlControls;
12using System.Web.UI.WebControls;
13using System.Web.UI.WebControls.WebParts;
14using System.Xml.Linq;
15
16using DHEECommoditySellSystem.ClassFiles;
17using ZedGraph;
18using ZedGraph.Web;
19using System.Drawing;
20using System.Drawing.Imaging;
21using DHEECommoditySellSystem.DataAccess;
22using System.Data.Common;
23using System.Data.SqlClient;
24
25
26namespace DHEECommoditySellSystem.SearchManagement
27{
28 public partial class Taxis : System.Web.UI.Page
29 {
30 /// 默认颜色种类
31 private List<COLOR></COLOR> defaultColors = new List<COLOR></COLOR>();
32 <SUMMARY></SUMMARY>
33 /// 初始化颜色数组
34
35 private void InitDefaultColors()
36 {
37 defaultColors.Add(Color.Red);
38 defaultColors.Add(Color.Green);
39 defaultColors.Add(Color.Blue);
40 defaultColors.Add(Color.Yellow);
41 defaultColors.Add(Color.YellowGreen);
42 defaultColors.Add(Color.Brown);
43 defaultColors.Add(Color.Aqua);
44 defaultColors.Add(Color.Cyan);
45 defaultColors.Add(Color.DarkSeaGreen);
46 defaultColors.Add(Color.Indigo);
47 }
48
49
50 /// PieItem.LabelType数组
51 PieLabelType[] lt = new PieLabelType[]{
52 PieLabelType.Name_Percent,
53 PieLabelType.Name_Value,
54 PieLabelType.Percent,
55 PieLabelType.Value,
56 PieLabelType.Name_Value
57 };
58
59
60 protected void Page_Load(object sender, EventArgs e)
61 {
62 InitializeComponent();
63 }
64
65 private void InitializeComponent()
66 {
67 this.ZedGraphWeb1.RenderGraph += new ZedGraph.Web.ZedGraphWebControlEventHandler(this.OnRenderGraph);//注册事件
68 }
69
70 <SUMMARY></SUMMARY>
71 /// This method is where you generate your graph.
72 /// <PARAM name="masterPane" />You are provided with a MasterPane instance that
73 /// contains one GraphPane by default (accessible via masterPane[0]).
74 /// <PARAM name="g" />A graphics instance so you can easily make the call to AxisChange()
75 private void OnRenderGraph(ZedGraphWeb cc1, System.Drawing.Graphics g, ZedGraph.MasterPane masterPane)
76 {
77 InitDefaultColors();
78 // Get the GraphPane so we can work with it
79 GraphPane myPane = masterPane[0];
80
81 // Set the pane title
82 myPane.Title.Text = "本年商品销售Top10";
83
84 // Fill the pane and axis background with solid color
85 myPane.Fill = new Fill(Color.Cornsilk);
86 myPane.Chart.Fill = new Fill(Color.Cornsilk);
87 myPane.Legend.Position = LegendPos.Right;
88
89 DatabaseHelper db=new DatabaseHelper();
90 DataSet ds = db.ExecuteDataSet("GetTop10CommoditySelledInThisWeek", CommandType.StoredProcedure);
91 int i = 0;
92 foreach (DataRowView view in ds.Tables[0].DefaultView)
93 {
94 double salesCount = double.Parse(view[0].ToString());
95 string commodityName = view["商品名称"].ToString();
96
97 PieItem segment = myPane.AddPieSlice(salesCount, defaultColors[i], Color.White, 45f, 0, commodityName);
98 i++;
99 segment.LabelType = PieLabelType.Value;
100 }
101
102 // Sum up the values
103 CurveList curves = myPane.CurveList;
104 double total = 0;
105 for (int x = 0; x < curves.Count; x++)
106 total += ((PieItem)curves[x]).Value;
107
108 // Add a text item to highlight total sales
109 TextObj text = new TextObj("Total Week Sales - " + "___FCKpd___0quot; + total.ToString() + "M", 0.85F, 0.80F, CoordType.PaneFraction);
110 text.Location.AlignH = AlignH.Center;
111 text.Location.AlignV = AlignV.Bottom;
112 text.FontSpec.Border.IsVisible = false;
113 text.FontSpec.Fill = new Fill(Color.White, Color.PowderBlue, 45F);
114 text.FontSpec.StringAlignment = StringAlignment.Center;
115 myPane.GraphObjList.Add(text);
116
117 // Add a colored background behind the pie
118 BoxObj box = new BoxObj(0, 0, 1, 1, Color.Empty, Color.PeachPuff);
119 box.Location.CoordinateFrame = CoordType.ChartFraction;
120 box.Border.IsVisible = false;
121 box.Location.AlignH = AlignH.Left;
122 box.Location.AlignV = AlignV.Top;
123 box.ZOrder = ZOrder.E_BehindCurves;
124 myPane.GraphObjList.Add(box);
125
126 masterPane.AxisChange(g);
127 }
128 }
129}
130