前一阵的一个项目用到了开源项目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
2
using System;
3
using System.Collections;
4
using System.Collections.Generic;
5
using System.Configuration;
6
using System.Data;
7
using System.Linq;
8
using System.Web;
9
using System.Web.Security;
10
using System.Web.UI;
11
using System.Web.UI.HtmlControls;
12
using System.Web.UI.WebControls;
13
using System.Web.UI.WebControls.WebParts;
14
using System.Xml.Linq;
15
16
using DHEECommoditySellSystem.ClassFiles;
17
using ZedGraph;
18
using ZedGraph.Web;
19
using System.Drawing;
20
using System.Drawing.Imaging;
21
using DHEECommoditySellSystem.DataAccess;
22
using System.Data.Common;
23
using System.Data.SqlClient;
24
25
26
namespace 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

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

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步