项目中对统计分析结果生成简单的柱状图及饼图等时,以前是使用OWC来进行处理.后来发现不知为何代码从windows2000server服务器迁移至window2003server服务器时,调用OWC时不能正常工作.
简单地寻找方案未果,同时想到 使用OWC依赖性太强,索性找一个开源控件来代替它.于是找到ZedGraph.在学习使用的过程中发现示例代码中几乎都是在OnRenderGraph中生成图的,非常不方便利用代码.费了些许力气,在一个方法中调用ZedGraph类,根据输入的数据生成的饼图,并在保存后返回路径.下面为代码,希望对大家有所帮助:
use ZedGraph
1 GraphPane gpPane = new GraphPane();
2 做一些初始设置#region 做一些初始设置
3 gpPane.XAxis.IsVisible = false;
4 gpPane.YAxis.IsVisible = false;
5 gpPane.Border.IsVisible = false;
6 gpPane.IsBoundedRanges = false;
7 gpPane.Legend.Position = LegendPos.BottomFlushLeft;
8 gpPane.XAxis.MajorGrid.IsVisible = false;
9 gpPane.XAxis.MinorGrid.IsVisible = false;
10 #endregion
11
12 gpPane.Title.Text = sTitle;
13 gpPane.Chart.Border.IsVisible = false;
14
15 // 添加饼图选项
16 for (int i = 0; i < saYAllData.Length; i++)
17 {
18 gpPane.AddPieSlice(Convert.ToDouble(saYAllData[i]),
19 aColors[i % 7], 0, saXAllData[i].ToString());
20 }
21
22 string sFileName = GetExclusiveFileName("jpg");
23 string strAbsolutePath = (HttpContext.Current.Server.MapPath(".")) + "\\"
24 + sChartImage + "\\" + sFileName;
25 gpPane.GetImage().Save(strAbsolutePath, ImageFormat.Jpeg);
26
27 strAbsolutePath = "./" + sChartImage + "/" + sFileName;
28
29 return strAbsolutePath;
30 }
1 GraphPane gpPane = new GraphPane();
2 做一些初始设置#region 做一些初始设置
3 gpPane.XAxis.IsVisible = false;
4 gpPane.YAxis.IsVisible = false;
5 gpPane.Border.IsVisible = false;
6 gpPane.IsBoundedRanges = false;
7 gpPane.Legend.Position = LegendPos.BottomFlushLeft;
8 gpPane.XAxis.MajorGrid.IsVisible = false;
9 gpPane.XAxis.MinorGrid.IsVisible = false;
10 #endregion
11
12 gpPane.Title.Text = sTitle;
13 gpPane.Chart.Border.IsVisible = false;
14
15 // 添加饼图选项
16 for (int i = 0; i < saYAllData.Length; i++)
17 {
18 gpPane.AddPieSlice(Convert.ToDouble(saYAllData[i]),
19 aColors[i % 7], 0, saXAllData[i].ToString());
20 }
21
22 string sFileName = GetExclusiveFileName("jpg");
23 string strAbsolutePath = (HttpContext.Current.Server.MapPath(".")) + "\\"
24 + sChartImage + "\\" + sFileName;
25 gpPane.GetImage().Save(strAbsolutePath, ImageFormat.Jpeg);
26
27 strAbsolutePath = "./" + sChartImage + "/" + sFileName;
28
29 return strAbsolutePath;
30 }