1using System;
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11//下面程序中使用的ImageFormat类所在的命名空间
12using System . Drawing . Imaging ;
13//下面程序中使用到关于数据库方面的类所在的命名空间
14using System . Data . OleDb ;
15
16
17namespace WebPieDemo
18{
19 /** <summary>
20 /// WebForm1 的摘要说明。
21 /// </summary>
22 public class WebForm1 : System.Web.UI.Page
23 {
24 private void Page_Load(object sender, System.EventArgs e)
25 {
26 // 在此处放置用户代码以初始化页面
27 string sRouter = "c://db2.mdb" ;
28
29 //获得当前Access数据库在服务器端的绝对路径
30 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + sRouter ;
31
32 //创建一个数据库连接
33 OleDbConnection myConn = new OleDbConnection ( strCon ) ;
34 string strCom = " SELECT YF ,SL FROM MonthSale ORDER BY YF" ;
35 myConn.Open ( ) ;
36 OleDbCommand myCommand = new OleDbCommand ( strCom , myConn ) ;
37 OleDbDataReader myOleDbDataReader = myCommand.ExecuteReader ( ) ;
38 //创建OleDbDataReader实例,并以此实例来获取数据库中各条记录数据
39
40 int [ ] iXiaoSH = new int [ 12 ] ;
41 //定义一个数组,用以存放从数据库中读取的销售数据
42
43 string [ ] sMoth = new string [ 12 ] ;
44 //定义一个数组,用以存放从数据库中读取的销售月份
45
46 int iIndex = 0 ;
47 while ( myOleDbDataReader.Read ( ) )
48 {
49 iXiaoSH [ iIndex ] = myOleDbDataReader.GetInt32 ( 1 ) ;
50 sMoth [ iIndex ] = myOleDbDataReader.GetInt32 ( 0 ) . ToString() + "月" ;
51 iIndex++ ;
52 }
53 //读取Table01数据表中的各条数据,并存放在先前定义的二个数组中
54
55 myConn . Close ( ) ;
56 myOleDbDataReader . Close ( ) ;
57
58 Bitmap bm = new Bitmap ( 600 , 300 ) ;
59 //创建一个长度为600,宽带为300的Bitmap实例
60
61 Graphics g ;
62 g = Graphics.FromImage ( bm ) ;
63 g . Clear ( Color . Snow ) ;
64 g . DrawString ( " ××公司××××年度销售情况一览表" , new Font ( "宋体" , 16 ) , Brushes . Black , new Point ( 5 , 5 ) ) ;
65 //在绘画图面的指定位置,以指定的字体、指定的颜色绘制指定的字符串。即为图表标题
66
67 //以下代码是是实现图01中的右上部区域
68 //以上是在图01中为下面绘制定位
69 Point myRec = new Point ( 515 , 30 ) ;
70 Point myDec = new Point ( 540 , 30 ) ;
71 Point myTxt = new Point ( 565 , 30 ) ;
72 g . DrawString ( "单位:万套" , new Font ( "宋体" , 9 ) , Brushes . Black , new Point ( 515 , 12 ) ) ;
73 for ( int i = 0 ; i < sMoth.Length ; i++ )
74 {
75 g . FillRectangle ( new SolidBrush ( GetColor ( i ) ) , myRec . X , myRec . Y , 20 , 10 ) ;
76 //填充小方块
77
78 g . DrawRectangle ( Pens.Black , myRec . X , myRec . Y , 20 , 10 ) ;
79 //绘制小方块
80
81 g . DrawString ( sMoth [ i ] . ToString ( ) , new Font ( "宋体", 9 ) , Brushes . Black , myDec ) ;
82 //绘制小方块右边的文字
83
84 g . DrawString ( iXiaoSH[i].ToString (), new Font ( "宋体", 9 ) , Brushes . Black , myTxt ) ;
85 myRec . Y += 15 ;
86 myDec . Y += 15 ;
87 myTxt . Y += 15 ;
88 }
89
90 //以下代码是根据从数据库中得到的数值大小,绘制扇型,并以相应色彩填充扇型,//从而构成图01中的Pie图
91 int iTatal = 0 ;
92 float fCurrentAngle = 0 ;
93 float fStartAngle = 0;
94 for ( int i = 0 ; i < iXiaoSH . Length ; i++ )
95 {
96 iTatal = iTatal + iXiaoSH [ i ] ;
97 }
98 for ( int i = 0 ; i < iXiaoSH . Length ; i++ )
99 {
100 //以下代码是获得要绘制扇型的开始角度
101 if ( i == iXiaoSH . Length - 1 )
102 {
103 fCurrentAngle = 360- fStartAngle ;
104 }
105 else
106 {
107 int iTemp = iXiaoSH [ i ] ;
108 fCurrentAngle = ( iTemp * 360 ) / iTatal ;
109 }
110 //根据参数绘制扇型
111
112 g.DrawPie ( Pens.Black , 100 , 40 , 250 , 250 , fStartAngle , fCurrentAngle ) ;
113 //以指定色彩填充绘制的扇型
114
115 g.FillPie ( new SolidBrush ( GetColor ( i ) ) , 100 , 40 , 250 , 250 , fStartAngle , fCurrentAngle ) ;
116 fStartAngle += fCurrentAngle ;
117 }
118
119 //画出图片的边框
120 Pen p = new Pen ( Color.Black , 2 ) ;
121 g . DrawRectangle ( p , 1 , 1 , 598 , 298 ) ;
122
123 //向客户端输出数据流,并以此数据流形成Jpeg图片
124 bm.Save ( Response . OutputStream , ImageFormat . Jpeg ) ;
125 }
126
127 Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
128 override protected void OnInit(EventArgs e)
129 {
130 //
131 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
132 //
133 InitializeComponent();
134 base.OnInit(e);
135 }
136
137 /** <summary>
138 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
139 /// 此方法的内容。
140 /// </summary>
141 private void InitializeComponent()
142 {
143 this.Load += new System.EventHandler(this.Page_Load);
144 }
145 #endregion
146
147 /** <summary>
148 /// 获取颜色
149 /// </summary>
150 /// <param name="itemIndex">数组的索引</param>
151 /// <returns></returns>
152 private Color GetColor ( int itemIndex )
153 {
154 Color MyColor ;
155 int i = itemIndex ;
156 switch (i)
157 {
158 case 0 :
159 MyColor = Color.Green;
160 return MyColor;
161 case 1 :
162 MyColor = Color.Red;
163 return MyColor;
164 case 2:
165 MyColor = Color.Yellow;
166 return MyColor;
167 case 3 :
168 MyColor = Color.Blue;
169 return MyColor;
170 case 4 :
171 MyColor = Color.Orange;
172 return MyColor;
173 case 5 :
174 MyColor = Color.Aqua;
175 return MyColor;
176 case 6:
177 MyColor = Color.SkyBlue;
178 return MyColor;
179 case 7:
180 MyColor = Color.DeepPink;
181 return MyColor;
182 case 8:
183 MyColor = Color.Azure;
184 return MyColor;
185 case 9:
186 MyColor = Color.Brown;
187 return MyColor;
188 case 10:
189 MyColor = Color.Pink;
190 return MyColor;
191 case 11:
192 MyColor = Color.BurlyWood;
193 return MyColor;
194 case 12:
195 MyColor = Color.Chartreuse;
196 return MyColor;
197 default:
198 MyColor = Color.Pink;
199 return MyColor;
200 }
201 }
202 }
203}
204
2using System.Collections;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Web;
7using System.Web.SessionState;
8using System.Web.UI;
9using System.Web.UI.WebControls;
10using System.Web.UI.HtmlControls;
11//下面程序中使用的ImageFormat类所在的命名空间
12using System . Drawing . Imaging ;
13//下面程序中使用到关于数据库方面的类所在的命名空间
14using System . Data . OleDb ;
15
16
17namespace WebPieDemo
18{
19 /** <summary>
20 /// WebForm1 的摘要说明。
21 /// </summary>
22 public class WebForm1 : System.Web.UI.Page
23 {
24 private void Page_Load(object sender, System.EventArgs e)
25 {
26 // 在此处放置用户代码以初始化页面
27 string sRouter = "c://db2.mdb" ;
28
29 //获得当前Access数据库在服务器端的绝对路径
30 string strCon = " Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " + sRouter ;
31
32 //创建一个数据库连接
33 OleDbConnection myConn = new OleDbConnection ( strCon ) ;
34 string strCom = " SELECT YF ,SL FROM MonthSale ORDER BY YF" ;
35 myConn.Open ( ) ;
36 OleDbCommand myCommand = new OleDbCommand ( strCom , myConn ) ;
37 OleDbDataReader myOleDbDataReader = myCommand.ExecuteReader ( ) ;
38 //创建OleDbDataReader实例,并以此实例来获取数据库中各条记录数据
39
40 int [ ] iXiaoSH = new int [ 12 ] ;
41 //定义一个数组,用以存放从数据库中读取的销售数据
42
43 string [ ] sMoth = new string [ 12 ] ;
44 //定义一个数组,用以存放从数据库中读取的销售月份
45
46 int iIndex = 0 ;
47 while ( myOleDbDataReader.Read ( ) )
48 {
49 iXiaoSH [ iIndex ] = myOleDbDataReader.GetInt32 ( 1 ) ;
50 sMoth [ iIndex ] = myOleDbDataReader.GetInt32 ( 0 ) . ToString() + "月" ;
51 iIndex++ ;
52 }
53 //读取Table01数据表中的各条数据,并存放在先前定义的二个数组中
54
55 myConn . Close ( ) ;
56 myOleDbDataReader . Close ( ) ;
57
58 Bitmap bm = new Bitmap ( 600 , 300 ) ;
59 //创建一个长度为600,宽带为300的Bitmap实例
60
61 Graphics g ;
62 g = Graphics.FromImage ( bm ) ;
63 g . Clear ( Color . Snow ) ;
64 g . DrawString ( " ××公司××××年度销售情况一览表" , new Font ( "宋体" , 16 ) , Brushes . Black , new Point ( 5 , 5 ) ) ;
65 //在绘画图面的指定位置,以指定的字体、指定的颜色绘制指定的字符串。即为图表标题
66
67 //以下代码是是实现图01中的右上部区域
68 //以上是在图01中为下面绘制定位
69 Point myRec = new Point ( 515 , 30 ) ;
70 Point myDec = new Point ( 540 , 30 ) ;
71 Point myTxt = new Point ( 565 , 30 ) ;
72 g . DrawString ( "单位:万套" , new Font ( "宋体" , 9 ) , Brushes . Black , new Point ( 515 , 12 ) ) ;
73 for ( int i = 0 ; i < sMoth.Length ; i++ )
74 {
75 g . FillRectangle ( new SolidBrush ( GetColor ( i ) ) , myRec . X , myRec . Y , 20 , 10 ) ;
76 //填充小方块
77
78 g . DrawRectangle ( Pens.Black , myRec . X , myRec . Y , 20 , 10 ) ;
79 //绘制小方块
80
81 g . DrawString ( sMoth [ i ] . ToString ( ) , new Font ( "宋体", 9 ) , Brushes . Black , myDec ) ;
82 //绘制小方块右边的文字
83
84 g . DrawString ( iXiaoSH[i].ToString (), new Font ( "宋体", 9 ) , Brushes . Black , myTxt ) ;
85 myRec . Y += 15 ;
86 myDec . Y += 15 ;
87 myTxt . Y += 15 ;
88 }
89
90 //以下代码是根据从数据库中得到的数值大小,绘制扇型,并以相应色彩填充扇型,//从而构成图01中的Pie图
91 int iTatal = 0 ;
92 float fCurrentAngle = 0 ;
93 float fStartAngle = 0;
94 for ( int i = 0 ; i < iXiaoSH . Length ; i++ )
95 {
96 iTatal = iTatal + iXiaoSH [ i ] ;
97 }
98 for ( int i = 0 ; i < iXiaoSH . Length ; i++ )
99 {
100 //以下代码是获得要绘制扇型的开始角度
101 if ( i == iXiaoSH . Length - 1 )
102 {
103 fCurrentAngle = 360- fStartAngle ;
104 }
105 else
106 {
107 int iTemp = iXiaoSH [ i ] ;
108 fCurrentAngle = ( iTemp * 360 ) / iTatal ;
109 }
110 //根据参数绘制扇型
111
112 g.DrawPie ( Pens.Black , 100 , 40 , 250 , 250 , fStartAngle , fCurrentAngle ) ;
113 //以指定色彩填充绘制的扇型
114
115 g.FillPie ( new SolidBrush ( GetColor ( i ) ) , 100 , 40 , 250 , 250 , fStartAngle , fCurrentAngle ) ;
116 fStartAngle += fCurrentAngle ;
117 }
118
119 //画出图片的边框
120 Pen p = new Pen ( Color.Black , 2 ) ;
121 g . DrawRectangle ( p , 1 , 1 , 598 , 298 ) ;
122
123 //向客户端输出数据流,并以此数据流形成Jpeg图片
124 bm.Save ( Response . OutputStream , ImageFormat . Jpeg ) ;
125 }
126
127 Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
128 override protected void OnInit(EventArgs e)
129 {
130 //
131 // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
132 //
133 InitializeComponent();
134 base.OnInit(e);
135 }
136
137 /** <summary>
138 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
139 /// 此方法的内容。
140 /// </summary>
141 private void InitializeComponent()
142 {
143 this.Load += new System.EventHandler(this.Page_Load);
144 }
145 #endregion
146
147 /** <summary>
148 /// 获取颜色
149 /// </summary>
150 /// <param name="itemIndex">数组的索引</param>
151 /// <returns></returns>
152 private Color GetColor ( int itemIndex )
153 {
154 Color MyColor ;
155 int i = itemIndex ;
156 switch (i)
157 {
158 case 0 :
159 MyColor = Color.Green;
160 return MyColor;
161 case 1 :
162 MyColor = Color.Red;
163 return MyColor;
164 case 2:
165 MyColor = Color.Yellow;
166 return MyColor;
167 case 3 :
168 MyColor = Color.Blue;
169 return MyColor;
170 case 4 :
171 MyColor = Color.Orange;
172 return MyColor;
173 case 5 :
174 MyColor = Color.Aqua;
175 return MyColor;
176 case 6:
177 MyColor = Color.SkyBlue;
178 return MyColor;
179 case 7:
180 MyColor = Color.DeepPink;
181 return MyColor;
182 case 8:
183 MyColor = Color.Azure;
184 return MyColor;
185 case 9:
186 MyColor = Color.Brown;
187 return MyColor;
188 case 10:
189 MyColor = Color.Pink;
190 return MyColor;
191 case 11:
192 MyColor = Color.BurlyWood;
193 return MyColor;
194 case 12:
195 MyColor = Color.Chartreuse;
196 return MyColor;
197 default:
198 MyColor = Color.Pink;
199 return MyColor;
200 }
201 }
202 }
203}
204