Code
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Drawing;
12using System.IO;
13
14public partial class WebForm3: System.Web.UI.Page
15 {
16 private string[,] data = new string[6, 2];
17
18 private void Page_Load(object sender, System.EventArgs e)
19 {
20 DrawingAPic();
21 }
22
23 private void DrawingAPic()
24 {
25 int i;
26 // 实例化Bitmap对象
27 Bitmap objbitmap;
28 objbitmap = new Bitmap(400, 300);
29 Graphics objGraphics;
30
31 // 实例化Graphics类
32 objGraphics = Graphics.FromImage(objbitmap);
33
34 // 填充背景色
35 objGraphics.Clear(Color.White);
36
37 // 画圆
38 objGraphics.DrawRectangle(Pens.Black, 1, 1, 398, 298);
39
40 // 写标题
41 objGraphics.DrawString("本公司上半年营业额统计图", new Font("宋体", 16, FontStyle.Bold), Brushes.Black, new PointF(60, 5));
42
43 // 获取数据,这里模拟出6个月的公司业务数据,实际应用可以从数据库读取
44 getdata();
45
46 PointF monthcolor = new PointF(260, 40);
47 PointF fontinfor = new PointF(285, 36);
48
49 for (i = 0; i <= 5; i++)
50 {
51 // 画出填充矩形
52 objGraphics.FillRectangle(new SolidBrush(getcolor(i)), monthcolor.X, monthcolor.Y, 20, 10);
53
54 //画出矩形边框。
55 objGraphics.DrawRectangle(Pens.Black, monthcolor.X, monthcolor.Y, 20, 10);
56
57 //画出图例说明文字--data(i, 0)
58 objGraphics.DrawString(data[i, 0], new Font("宋体", 10), Brushes.Black, fontinfor);
59
60 //移动坐标位置,只移动Y方向的值即可。
61 monthcolor.Y += 15;
62 fontinfor.Y += 15;
63 }
64
65 // 遍历数据源的每一项数据,并根据数据的大小画出矩形图(即柱形图的柱)。
66 for (i = 0; i <= 5; i++)
67 {
68 //画出填充矩形。
69 objGraphics.FillRectangle(new SolidBrush(getcolor(i)), (i * 25) + 35, 270 - System.Convert.ToInt32(data[i, 1]), 15, System.Convert.ToInt32(data[i, 1]));
70
71 //'画出矩形边框线。
72 objGraphics.DrawRectangle(Pens.Black, (i * 25) + 35, 270 - System.Convert.ToInt32(data[i, 1]), 15, System.Convert.ToInt32(data[i, 1]));
73 }
74
75 //画出示意坐标
76 objGraphics.DrawLine(new Pen(Color.Blue, 1), 10, 0, 10, 320);
77 objGraphics.DrawLine(new Pen(Color.Blue, 1), 10, 270, 200, 270);
78
79 // 在示意坐标上添加数值标志,注意坐标的计算
80 for (i = 0; i <= 5; i++)
81 {
82 objGraphics.DrawLine(new Pen(Color.Blue, 1), 10, i * 50 + 20, 20, i * 50 + 20);
83 objGraphics.DrawString((250 - i * 50).ToString(), new Font("宋体", 10), Brushes.Black, 12, i * 50 + 8);
84 }
85 //统计总销售额
86 float scount = 0;
87 for (i = 0; i <= 5; i++)
88 {
89 scount += float.Parse((data[i, 1]));
90 }
91
92 //定义画出扇形角度变量
93 float scg = 0;
94 float stg = 0;
95 for (i = 0; i <= 5; i++)
96 {
97 //计算当前角度值:当月销售额 / 总销售额 * 360,得到饼图中当月所占的角度大小。
98 float num = float.Parse(data[i, 1]);
99 scg = (num / scount) * 360;
100
101 //画出填充圆弧。
102 objGraphics.FillPie(new SolidBrush(getcolor(i)), 220, 150, 120, 120, stg, scg);
103
104 //画出圆弧线。
105 objGraphics.DrawPie(Pens.Black, 220, 150, 120, 120, stg, scg);
106
107 // 把当前圆弧角度加到总角度上。
108 stg += scg;
109 }
110
111 // 画出说明文字
112 objGraphics.DrawString("柱状图", new Font("宋体", 15, FontStyle.Bold), Brushes.Blue, 50, 272);
113 objGraphics.DrawString("饼状图", new Font("宋体", 15, FontStyle.Bold), Brushes.Blue, 250, 272);
114
115 // 输出到客户端
116 objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
117
118 }
119 // 为数组赋值
120 // 即生成模拟业务数据
121 private void getdata()
122 {
123 data[0, 0] = "一月份";
124 data[1, 0] = "二月份";
125 data[2, 0] = "三月份";
126 data[3, 0] = "四月份";
127 data[4, 0] = "五月份";
128 data[5, 0] = "六月份";
129 data[0, 1] = "85";
130 data[1, 1] = "135";
131 data[2, 1] = "85";
132 data[3, 1] = "110";
133 data[4, 1] = "130";
134 data[5, 1] = "200";
135 }
136
137 // 产生色彩值,便于显示区别
138 private Color getcolor(int i)
139 {
140 Color newcolor;
141 i += 1;
142 if (i == 1)
143 {
144 newcolor = Color.Blue;
145 }
146 else if (i == 2)
147 {
148 newcolor = Color.ForestGreen;
149 }
150 else if (i == 3)
151 {
152 newcolor = Color.Gainsboro;
153 }
154 else if (i == 4)
155 {
156 newcolor = Color.Moccasin;
157 }
158 else if (i == 5)
159 {
160 newcolor = Color.Indigo;
161 }
162 else if (i == 6)
163 {
164 newcolor = Color.BurlyWood;
165 }
166 else
167 newcolor = Color.Goldenrod;
168 return newcolor;
169 }
170 }
171
172
1using System;
2using System.Data;
3using System.Configuration;
4using System.Collections;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.Drawing;
12using System.IO;
13
14public partial class WebForm3: System.Web.UI.Page
15 {
16 private string[,] data = new string[6, 2];
17
18 private void Page_Load(object sender, System.EventArgs e)
19 {
20 DrawingAPic();
21 }
22
23 private void DrawingAPic()
24 {
25 int i;
26 // 实例化Bitmap对象
27 Bitmap objbitmap;
28 objbitmap = new Bitmap(400, 300);
29 Graphics objGraphics;
30
31 // 实例化Graphics类
32 objGraphics = Graphics.FromImage(objbitmap);
33
34 // 填充背景色
35 objGraphics.Clear(Color.White);
36
37 // 画圆
38 objGraphics.DrawRectangle(Pens.Black, 1, 1, 398, 298);
39
40 // 写标题
41 objGraphics.DrawString("本公司上半年营业额统计图", new Font("宋体", 16, FontStyle.Bold), Brushes.Black, new PointF(60, 5));
42
43 // 获取数据,这里模拟出6个月的公司业务数据,实际应用可以从数据库读取
44 getdata();
45
46 PointF monthcolor = new PointF(260, 40);
47 PointF fontinfor = new PointF(285, 36);
48
49 for (i = 0; i <= 5; i++)
50 {
51 // 画出填充矩形
52 objGraphics.FillRectangle(new SolidBrush(getcolor(i)), monthcolor.X, monthcolor.Y, 20, 10);
53
54 //画出矩形边框。
55 objGraphics.DrawRectangle(Pens.Black, monthcolor.X, monthcolor.Y, 20, 10);
56
57 //画出图例说明文字--data(i, 0)
58 objGraphics.DrawString(data[i, 0], new Font("宋体", 10), Brushes.Black, fontinfor);
59
60 //移动坐标位置,只移动Y方向的值即可。
61 monthcolor.Y += 15;
62 fontinfor.Y += 15;
63 }
64
65 // 遍历数据源的每一项数据,并根据数据的大小画出矩形图(即柱形图的柱)。
66 for (i = 0; i <= 5; i++)
67 {
68 //画出填充矩形。
69 objGraphics.FillRectangle(new SolidBrush(getcolor(i)), (i * 25) + 35, 270 - System.Convert.ToInt32(data[i, 1]), 15, System.Convert.ToInt32(data[i, 1]));
70
71 //'画出矩形边框线。
72 objGraphics.DrawRectangle(Pens.Black, (i * 25) + 35, 270 - System.Convert.ToInt32(data[i, 1]), 15, System.Convert.ToInt32(data[i, 1]));
73 }
74
75 //画出示意坐标
76 objGraphics.DrawLine(new Pen(Color.Blue, 1), 10, 0, 10, 320);
77 objGraphics.DrawLine(new Pen(Color.Blue, 1), 10, 270, 200, 270);
78
79 // 在示意坐标上添加数值标志,注意坐标的计算
80 for (i = 0; i <= 5; i++)
81 {
82 objGraphics.DrawLine(new Pen(Color.Blue, 1), 10, i * 50 + 20, 20, i * 50 + 20);
83 objGraphics.DrawString((250 - i * 50).ToString(), new Font("宋体", 10), Brushes.Black, 12, i * 50 + 8);
84 }
85 //统计总销售额
86 float scount = 0;
87 for (i = 0; i <= 5; i++)
88 {
89 scount += float.Parse((data[i, 1]));
90 }
91
92 //定义画出扇形角度变量
93 float scg = 0;
94 float stg = 0;
95 for (i = 0; i <= 5; i++)
96 {
97 //计算当前角度值:当月销售额 / 总销售额 * 360,得到饼图中当月所占的角度大小。
98 float num = float.Parse(data[i, 1]);
99 scg = (num / scount) * 360;
100
101 //画出填充圆弧。
102 objGraphics.FillPie(new SolidBrush(getcolor(i)), 220, 150, 120, 120, stg, scg);
103
104 //画出圆弧线。
105 objGraphics.DrawPie(Pens.Black, 220, 150, 120, 120, stg, scg);
106
107 // 把当前圆弧角度加到总角度上。
108 stg += scg;
109 }
110
111 // 画出说明文字
112 objGraphics.DrawString("柱状图", new Font("宋体", 15, FontStyle.Bold), Brushes.Blue, 50, 272);
113 objGraphics.DrawString("饼状图", new Font("宋体", 15, FontStyle.Bold), Brushes.Blue, 250, 272);
114
115 // 输出到客户端
116 objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);
117
118 }
119 // 为数组赋值
120 // 即生成模拟业务数据
121 private void getdata()
122 {
123 data[0, 0] = "一月份";
124 data[1, 0] = "二月份";
125 data[2, 0] = "三月份";
126 data[3, 0] = "四月份";
127 data[4, 0] = "五月份";
128 data[5, 0] = "六月份";
129 data[0, 1] = "85";
130 data[1, 1] = "135";
131 data[2, 1] = "85";
132 data[3, 1] = "110";
133 data[4, 1] = "130";
134 data[5, 1] = "200";
135 }
136
137 // 产生色彩值,便于显示区别
138 private Color getcolor(int i)
139 {
140 Color newcolor;
141 i += 1;
142 if (i == 1)
143 {
144 newcolor = Color.Blue;
145 }
146 else if (i == 2)
147 {
148 newcolor = Color.ForestGreen;
149 }
150 else if (i == 3)
151 {
152 newcolor = Color.Gainsboro;
153 }
154 else if (i == 4)
155 {
156 newcolor = Color.Moccasin;
157 }
158 else if (i == 5)
159 {
160 newcolor = Color.Indigo;
161 }
162 else if (i == 6)
163 {
164 newcolor = Color.BurlyWood;
165 }
166 else
167 newcolor = Color.Goldenrod;
168 return newcolor;
169 }
170 }
171
172