统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的;这里我们用就C# 制作三款最经典的统计图: 柱状图, 折线图和扇形图;既然是统计, 当然需要数据, 这里演示的数据存于Sql Server2000中, 三款统计图形都是动态生成. 其中柱状图我会附上制作步骤, 其他两款统计图直接附源码.

说明: 需求不一样, 统计图形绘制后的显示效果也不一样, 比如这里柱状图的主要需求是为了比较每一期报名人数与通过人数的差, 因此会把两根柱子放在一起会使比较结果一目了然. 因此大家可以根据需要灵活绘制.

一. 柱状图的绘制.

1. 定义绘图用到的类.

1
2
3
4
int height = 500, width = 700;
Bitmap image = new Bitmap(width, height);
Graphics g = Graphics.FromImage(image);
Pen mypen = new Pen(brush, 1);

2. 绘制图框.

1
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);

3. 绘制横向坐标线

1
2
3
4
5
for (int i = 0; i < 14; i++)
{
g.DrawLine(mypen, x, 80, x, 340);
x = x + 40;
}

4. 绘制纵向坐标线

1
2
3
4
5
for (int i = 0; i < 9; i++)
{
g.DrawLine(mypen, 60, y, 620, y);
y = y + 26;
}

5. 绘制横坐标值

1
2
3
4
5
6
String[] n = { "第一期", "第二期", "第三期", "第四期", "全年" };
for (int i = 0; i < 7; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348);
x = x + 78;
}

6. 绘制纵坐标值

1
2
3
4
5
6
String[] m = {"250","225", "200", "175", "150", "125", "100“};
for (int i = 0; i < 10; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y);
y = y + 26;
}

  

7. 定义数组存储数据库中统计的数据

1
2
int[] Count1 = new int[7]; //存储从数据库读取的报名人数
int[] Count2 = new int[7]; //存储从数据库读取的通过人数

8. 从数据库中读取报名人数与通过人数

1
2
3
4
5
6
7
8
SqlConnection Con = new SqlConnection(
"Server=(Local);Database=committeeTraining;");
Con.Open();
string cmdtxt2 = "SELECT * FROM ##Count
where Company='" + ****+ "'";
SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
DataSet ds = new DataSet();
da.Fill(ds);

9. 将读取的数据存储到数组中

1
2
3
4
Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0][“count1”].ToString());
Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0][“count3”].ToString());
Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0][“count2”].ToString());
Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());

10.定义画笔和画刷准备绘图

1
2
3
4
5
x = 80;
Font font2 = new System.Drawing.Font(
"Arial", 10, FontStyle.Bold);
SolidBrush mybrush = new SolidBrush(Color.Red);
SolidBrush mybrush2 = new SolidBrush(Color.Green);

11. 根据数组中的值绘制柱状图

(1)第一期报名人数

1
2
3
g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]);
g.DrawString(Count1[0].ToString(), font2,
Brushes.Red, x, 340 - Count1[0] - 15);

(2) 第一期通过人数

1
2
3
4
x = x + 20;
g.FillRectangle(mybrush2, x, 340 - Count2[0], 20, Count2[0]);
g.DrawString(Count2[0].ToString(), font2,
Brushes.Green, x, 340 - Count2[0] - 15);

12. 将图形输出到页面.

1
2
3
4
5
6
System.IO.MemoryStream ms = new
System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());

最终柱状图的效果图:

柱状图的完整代码:

1
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
private void CreateImage()
{
int height = 500, width = 700;
Bitmap image = new Bitmap(width, height);
//创建Graphics类对象
Graphics g = Graphics.FromImage(image);
 
try
{
//清空图片背景色
g.Clear(Color.White);
 
Font font = new Font("Arial", 10, FontStyle.Regular);
Font font1 = new Font("宋体", 20, FontStyle.Bold);
 
LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(0, 0, image.Width, image.Height),
Color.Blue, Color.BlueViolet, 1.2f, true);
g.FillRectangle(Brushes.WhiteSmoke, 0, 0, width, height);
// Brush brush1 = new SolidBrush(Color.Blue);
 
g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text +
" 成绩统计柱状图", font1, brush, new PointF(70, 30));
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
 
 
Pen mypen = new Pen(brush, 1);
//绘制线条
//绘制横向线条
int x = 100;
for (int i = 0; i < 14; i++)
{
g.DrawLine(mypen, x, 80, x, 340);
x = x + 40;
}
Pen mypen1 = new Pen(Color.Blue, 2);
x = 60;
g.DrawLine(mypen1, x, 80, x, 340);
 
//绘制纵向线条
int y = 106;
for (int i = 0; i < 9; i++)
{
g.DrawLine(mypen, 60, y, 620, y);
y = y + 26;
}
g.DrawLine(mypen1, 60, y, 620, y);
 
//x轴
String[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" };
x = 78;
for (int i = 0; i < 7; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Blue, x, 348); //设置文字内容及输出位置
x = x + 78;
}
 
//y轴
String[] m = {"250","225", "200", "175", "150", "125", "100", " 75",
" 50", " 25", " 0"};
y = 72;
for (int i = 0; i < 10; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Blue, 25, y); //设置文字内容及输出位置
y = y + 26;
}
 
int[] Count1 = new int[7];
int[] Count2 = new int[7];
 
SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=**");
Con.Open();
string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";
SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
DataSet ds = new DataSet();
da.Fill(ds);
 
Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString());
Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString());
Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString());
Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString());
 
Count1[4] = Count1[0] + Count1[1];
Count1[5] = Count1[2] + Count1[3];
 
Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString());
 
 
Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString());
Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString());
Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString());
 
Count2[4] = Count2[0] + Count2[1];
Count2[5] = Count2[2] + Count2[3];
 
Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString());
 
//绘制柱状图.
x = 80;
Font font2 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
SolidBrush mybrush = new SolidBrush(Color.Red);
SolidBrush mybrush2 = new SolidBrush(Color.Green);
 
//第一期
g.FillRectangle(mybrush, x, 340 - Count1[0], 20, Count1[0]);
g.DrawString(Count1[0].ToString(), font2, Brushes.Red, x, 340 - Count1[0] - 15);
 
x = x + 20;
g.FillRectangle(mybrush2, x, 340 - Count2[0], 20, Count2[0]);
g.DrawString(Count2[0].ToString(), font2, Brushes.Green, x, 340 - Count2[0] - 15);
 
 
//第二期
x = x + 60;
g.FillRectangle(mybrush, x, 340 - Count1[1], 20, Count1[1]);
g.DrawString(Count1[1].ToString(), font2, Brushes.Red, x, 340 - Count1[1] - 15);
 
 
x = x + 20;
g.FillRectangle(mybrush2, x, 340 - Count2[1], 20, Count2[1]);
g.DrawString(Count2[1].ToString(), font2, Brushes.Green, x, 340 - Count2[1] - 15);
 
 
//第三期
x = x + 60;
g.FillRectangle(mybrush, x, 340 - Count1[2], 20, Count1[2]);
g.DrawString(Count1[2].ToString(), font2, Brushes.Red, x, 340 - Count1[2] - 15);
 
x = x + 20;
g.FillRectangle(mybrush2, x, 340 - Count2[2], 20, Count2[2]);
g.DrawString(Count2[2].ToString(), font2, Brushes.Green, x, 340 - Count2[2] - 15);
 
//第四期
x = x + 60;
g.FillRectangle(mybrush, x, 340 - Count1[3], 20, Count1[3]);
g.DrawString(Count1[3].ToString(), font2, Brushes.Red, x, 340 - Count1[3] - 15);
 
x = x + 20;
g.FillRectangle(mybrush2, x, 340 - Count2[3], 20, Count2[3]);
g.DrawString(Count2[3].ToString(), font2, Brushes.Green, x, 340 - Count2[3] - 15);
 
//上半年
x = x + 60;
g.FillRectangle(mybrush, x, 340 - Count1[4], 20, Count1[4]);
g.DrawString(Count1[4].ToString(), font2, Brushes.Red, x, 340 - Count1[4] - 15);
 
x = x + 20;
g.FillRectangle(mybrush2, x, 340 - Count2[4], 20, Count2[4]);
g.DrawString(Count2[4].ToString(), font2, Brushes.Green, x, 340 - Count2[4] - 15);
 
//下半年
x = x + 60;
g.FillRectangle(mybrush, x, 340 - Count1[5], 20, Count1[5]);
g.DrawString(Count1[5].ToString(), font2, Brushes.Red, x, 340 - Count1[5] - 15);
 
x = x + 20;
g.FillRectangle(mybrush2, x, 340 - Count2[5], 20, Count2[5]);
g.DrawString(Count2[5].ToString(), font2, Brushes.Green, x, 340 - Count2[5] - 15);
 
//全年
x = x + 60;
g.FillRectangle(mybrush, x, 340 - Count1[6], 20, Count1[6]);
g.DrawString(Count1[6].ToString(), font2, Brushes.Red, x, 340 - Count1[6] - 15);
 
 
x = x + 20;
g.FillRectangle(mybrush2, x, 340 - Count2[6], 20, Count2[6]);
g.DrawString(Count2[6].ToString(), font2, Brushes.Green, x, 340 - Count2[6] - 15);
 
 
//绘制标识
Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Regular);
g.DrawRectangle(new Pen(Brushes.Blue), 170, 400, 250, 50); //绘制范围框
g.FillRectangle(Brushes.Red, 270, 410, 20, 10); //绘制小矩形
g.DrawString("报名人数", font3, Brushes.Red, 292, 408);
 
g.FillRectangle(Brushes.Green, 270, 430, 20, 10);
g.DrawString("通过人数", font3, Brushes.Green, 292, 428);
 
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}

二. 折线统计图的绘制

效果:

折线图的完整代码:

1
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
private void CreateImage()
{
int height = 480, width = 700;
Bitmap image = new Bitmap(width, height);
Graphics g = Graphics.FromImage(image);
 
try
{
//清空图片背景色
g.Clear(Color.White);
 
Font font = new System.Drawing.Font("Arial", 9, FontStyle.Regular);
Font font1 = new System.Drawing.Font("宋体", 20, FontStyle.Regular);
Font font2 = new System.Drawing.Font("Arial", 8, FontStyle.Regular);
LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, image.Width, image.Height), Color.Blue, Color.Blue, 1.2f, true);
g.FillRectangle(Brushes.AliceBlue, 0, 0, width, height);
Brush brush1 = new SolidBrush(Color.Blue);
Brush brush2 = new SolidBrush(Color.SaddleBrown);
 
g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text +
" 成绩统计折线图", font1, brush1, new PointF(85, 30));
//画图片的边框线
g.DrawRectangle(new Pen(Color.Blue), 0, 0, image.Width - 1, image.Height - 1);
 
Pen mypen = new Pen(brush, 1);
Pen mypen2 = new Pen(Color.Red, 2);
//绘制线条
//绘制纵向线条
int x = 60;
for (int i = 0; i < 8; i++)
{
g.DrawLine(mypen, x, 80, x, 340);
x = x + 80;
}
Pen mypen1 = new Pen(Color.Blue, 3);
x = 60;
g.DrawLine(mypen1, x, 82, x, 340);
 
//绘制横向线条
int y = 106;
for (int i = 0; i < 10; i++)
{
g.DrawLine(mypen, 60, y, 620, y);
y = y + 26;
}
// y = 106;
g.DrawLine(mypen1, 60, y - 26, 620, y - 26);
 
//x轴
String[] n = { "第一期", "第二期", "第三期", "第四期", "上半年", "下半年", "全年统计" };
x = 45;
for (int i = 0; i < 7; i++)
{
g.DrawString(n[i].ToString(), font, Brushes.Red, x, 348); //设置文字内容及输出位置
x = x + 77;
}
 
//y轴
String[] m = { "220人", " 200人", " 175人", "150人", " 125人", " 100人", " 75人", " 50人",
" 25人"};
y = 100;
for (int i = 0; i < 9; i++)
{
g.DrawString(m[i].ToString(), font, Brushes.Red, 10, y); //设置文字内容及输出位置
y = y + 26;
}
 
int[] Count1 = new int[7];
int[] Count2 = new int[7];
 
SqlConnection Con = new SqlConnection("Server=(Local);Database=committeeTraining;Uid=sa;Pwd=eesoft");
Con.Open();
string cmdtxt2 = "SELECT * FROM ##Count where Company='" + this.ddlTaget.SelectedItem.Text.Trim() + "'";
SqlDataAdapter da = new SqlDataAdapter(cmdtxt2, Con);
DataSet ds = new DataSet();
da.Fill(ds);
 
//报名人数
Count1[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count1"].ToString());
Count1[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count3"].ToString());
Count1[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count5"].ToString());
Count1[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count7"].ToString());
 
Count1[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count9"].ToString()); //全年
 
Count1[4] = Count1[0] + Count1[1];
Count1[5] = Count1[2] + Count1[3];
 
 
Count2[0] = Convert.ToInt32(ds.Tables[0].Rows[0]["count2"].ToString());
Count2[1] = Convert.ToInt32(ds.Tables[0].Rows[0]["count4"].ToString());
Count2[2] = Convert.ToInt32(ds.Tables[0].Rows[0]["count6"].ToString());
Count2[3] = Convert.ToInt32(ds.Tables[0].Rows[0]["count8"].ToString());
 
Count2[6] = Convert.ToInt32(ds.Tables[0].Rows[0]["count10"].ToString()); //全年
 
Count2[4] = Count2[0] + Count2[1];
Count2[5] = Count2[2] + Count2[3];
 
 
//显示折线效果
Font font3 = new System.Drawing.Font("Arial", 10, FontStyle.Bold);
SolidBrush mybrush = new SolidBrush(Color.Red);
Point[] points1 = new Point[7];
points1[0].X = 60; points1[0].Y = 340 - Count1[0]; //从106纵坐标开始, 到(0, 0)坐标时
points1[1].X = 140; points1[1].Y = 340 - Count1[1];
points1[2].X = 220; points1[2].Y = 340 - Count1[2];
points1[3].X = 300; points1[3].Y = 340 - Count1[3];
 
points1[4].X = 380; points1[4].Y = 340 - Count1[4];
points1[5].X = 460; points1[5].Y = 340 - Count1[5];
 
points1[6].X = 540; points1[6].Y = 340 - Count1[6];
g.DrawLines(mypen2, points1); //绘制折线
 
//绘制数字
g.DrawString(Count1[0].ToString(), font3, Brushes.Red, 58, points1[0].Y - 20);
g.DrawString(Count1[1].ToString(), font3, Brushes.Red, 138, points1[1].Y - 20);
g.DrawString(Count1[2].ToString(), font3, Brushes.Red, 218, points1[2].Y - 20);
g.DrawString(Count1[3].ToString(), font3, Brushes.Red, 298, points1[3].Y - 20);
 
g.DrawString(Count1[4].ToString(), font3, Brushes.Red, 378, points1[4].Y - 20);
g.DrawString(Count1[5].ToString(), font3, Brushes.Red, 458, points1[5].Y - 20);
 
g.DrawString(Count1[6].ToString(), font3, Brushes.Red, 538, points1[6].Y - 20);
 
Pen mypen3 = new Pen(Color.Green, 2);
Point[] points2 = new Point[7];
points2[0].X = 60; points2[0].Y = 340 - Count2[0];
points2[1].X = 140; points2[1].Y = 340 - Count2[1];
points2[2].X = 220; points2[2].Y = 340 - Count2[2];
points2[3].X = 300; points2[3].Y = 340 - Count2[3];
 
points2[4].X = 380; points2[4].Y = 340 - Count2[4];
points2[5].X = 460; points2[5].Y = 340 - Count2[5];
 
points2[6].X = 540; points2[6].Y = 340 - Count2[6];
g.DrawLines(mypen3, points2); //绘制折线
 
//绘制通过人数
g.DrawString(Count2[0].ToString(), font3, Brushes.Green, 61, points2[0].Y - 15);
g.DrawString(Count2[1].ToString(), font3, Brushes.Green, 131, points2[1].Y - 15);
g.DrawString(Count2[2].ToString(), font3, Brushes.Green, 221, points2[2].Y - 15);
g.DrawString(Count2[3].ToString(), font3, Brushes.Green, 301, points2[3].Y - 15);
 
g.DrawString(Count2[4].ToString(), font3, Brushes.Green, 381, points2[4].Y - 15);
g.DrawString(Count2[5].ToString(), font3, Brushes.Green, 461, points2[5].Y - 15);
 
g.DrawString(Count2[6].ToString(), font3, Brushes.Green, 541, points2[6].Y - 15);
 
//绘制标识
g.DrawRectangle(new Pen(Brushes.Red), 180, 390, 250, 50); //绘制范围框
g.FillRectangle(Brushes.Red, 270, 402, 20, 10); //绘制小矩形
g.DrawString("报名人数", font2, Brushes.Red, 292, 400);
 
g.FillRectangle(Brushes.Green, 270, 422, 20, 10);
g.DrawString("通过人数", font2, Brushes.Green, 292, 420);
 
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.ClearContent();
Response.ContentType = "image/Jpeg";
Response.BinaryWrite(ms.ToArray());
}
finally
{
g.Dispose();
image.Dispose();
}
}

三. 扇形统计图的绘制

效果图:

完整代码:

1
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
private void CreateImage()
{
//把连接字串指定为一个常量
SqlConnection Con = new SqlConnection("Server=(Local);
Database=committeeTraining;Uid=sa;Pwd=**");
Con.Open();
string cmdtxt = selectString; // "select * from ##Count"; //
//SqlCommand Com = new SqlCommand(cmdtxt, Con);
DataSet ds = new DataSet();
SqlDataAdapter Da = new SqlDataAdapter(cmdtxt, Con);
Da.Fill(ds);
Con.Close();
float Total = 0.0f, Tmp;
 
//转换成单精度。也可写成Convert.ToInt32
Total = Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);
 
// Total=Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]);
//设置字体,fonttitle为主标题的字体
Font fontlegend = new Font("verdana", 9);
Font fonttitle = new Font("verdana", 10, FontStyle.Bold);
 
//背景宽
int width = 350;
int bufferspace = 15;
int legendheight = fontlegend.Height * 10 + bufferspace; //高度
int titleheight = fonttitle.Height + bufferspace;
int height = width + legendheight + titleheight + bufferspace;//白色背景高
int pieheight = width;
Rectangle pierect = new Rectangle(0, titleheight, width, pieheight);
 
//加上各种随机色
ArrayList colors = new ArrayList();
Random rnd = new Random();
for (int i = 0; i < 2; i++)
colors.Add(new SolidBrush(Color.FromArgb(rnd.Next(255), rnd.Next(255), rnd.Next(255))));
 
//创建一个bitmap实例
Bitmap objbitmap = new Bitmap(width, height);
Graphics objgraphics = Graphics.FromImage(objbitmap);
 
//画一个白色背景
objgraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
 
//画一个亮黄色背景
objgraphics.FillRectangle(new SolidBrush(Color.Beige), pierect);
 
//以下为画饼图(有几行row画几个)
float currentdegree = 0.0f;
 
//画通过人数
objgraphics.FillPie((SolidBrush)colors[1], pierect, currentdegree,
Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360);
currentdegree += Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) / Total * 360;
 
//未通过人数饼状图
objgraphics.FillPie((SolidBrush)colors[0], pierect, currentdegree,
((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]]))-(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360);
currentdegree += ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) -
(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))) / Total * 360;
 
 
//以下为生成主标题
SolidBrush blackbrush = new SolidBrush(Color.Black);
SolidBrush bluebrush = new SolidBrush(Color.Blue);
string title = " 机关单位成绩统计饼状图: "
+ "\n \n\n";
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.LineAlignment = StringAlignment.Center;
 
objgraphics.DrawString(title, fonttitle, blackbrush,
new Rectangle(0, 0, width, titleheight), stringFormat);
 
//列出各字段与得数目
objgraphics.DrawRectangle(new Pen(Color.Red, 2), 0, height + 10 - legendheight, width, legendheight + 50);
 
objgraphics.DrawString("----------------统计信息------------------",
fontlegend, bluebrush, 20, height - legendheight + fontlegend.Height * 1 + 1);
objgraphics.DrawString("统计单位: " + this.ddlTaget.SelectedItem.Text,
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 3 + 1);
objgraphics.DrawString("统计年份: " + this.ddlYear.SelectedItem.Text,
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 4 + 1);
objgraphics.DrawString("统计期数: " + this.ddlSpan.SelectedItem.Text,
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 5 + 1);
 
objgraphics.FillRectangle((SolidBrush)colors[1], 5,height - legendheight + fontlegend.Height * 8 + 1, 10, 10);
objgraphics.DrawString("报名总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])),
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 7 + 1);
objgraphics.FillRectangle((SolidBrush)colors[0], 5, height - legendheight + fontlegend.Height * 9 + 1, 10, 10);
objgraphics.DrawString("通过总人数: " + Convert.ToString(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]])),
fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 8 + 1);
objgraphics.DrawString("未通过人数: " + ((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) -
(Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]))), fontlegend, blackbrush, 20, height - legendheight + fontlegend.Height * 9 + 1);
 
objgraphics.DrawString("通过率: " + Convert.ToString((Convert.ToSingle(ds.Tables[0].Rows[0][this.count[1]]) /
Convert.ToSingle(ds.Tables[0].Rows[0][this.count[0]])) * 100)+ " %", fontlegend,
blackbrush, 20, height - legendheight + fontlegend.Height * 10 + 1);
 
Response.ContentType = "image/Jpeg";
objbitmap.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
objgraphics.Dispose();
objbitmap.Dispose();
 
}

转载来源:http://www.cnblogs.com/ziyiFly/archive/2008/09/24/1297841.html

C# 绘制统计图(柱状图, 折线图, 扇形图)【转载】的更多相关文章

  1. C# 绘制统计图(柱状图, 折线图, 扇形图)

    统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...

  2. Asp.net 用 Graphics 统计图(柱状图, 折线图, 扇形图)

    统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...

  3. 06. Matplotlib 2 |折线图| 柱状图| 堆叠图| 面积图| 填图| 饼图| 直方图| 散点图| 极坐标| 图箱型图

    1.基本图表绘制 plt.plot() 图表类别:线形图.柱状图.密度图,以横纵坐标两个维度为主同时可延展出多种其他图表样式 plt.plot(kind='line', ax=None, figsiz ...

  4. Qt数据可视化(散点图、折线图、柱状图、盒须图、饼状图、雷达图)开发实例

    ​  目录 散点图 折线图 柱状图 水平柱状图 水平堆叠图 水平百分比柱状图 盒须图 饼状图 雷达图 Qt散点图.折线图.柱状图.盒须图.饼状图.雷达图开发实例. 在开发过程中我们会使用多各种各样的图 ...

  5. 前端数据统计用做Bootstrap的一些柱状图、饼状图和折线图案例

    Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加快捷. Bootstrap ...

  6. WPF、Silverlight项目中使用柱状图、饼状图、折线图

    在开发的过程中,可能会遇到柱状图.饼状图.折线图来更好的显示数据,最近整理了一下,遂放出来望需要的朋友可以参考.本文仅仅是简单显示,如需复杂显示效果请参考官网代码示例.----本文代码使用WPF,Si ...

  7. High-speed Charting Control--MFC绘制图表(折线图、饼图、柱形图)控件

    原文地址:https://www.codeproject.com/articles/14075/high-speed-charting-control 本文翻译在CodeProject上的介绍(主要还 ...

  8. 数据输入——生成你需要的echart图(堆积柱状图、扇形图、嵌套环形图)

    最近论文需要一些比较直观的图表, 发现echart做出来的图还是比较美观的,这里介绍如何修改数据生成你需要的echart图. 1.堆积柱状图: http://echarts.baidu.com/exa ...

  9. Chart图表整合——面积对比图、扇形图、柱状图

    一. chart图表demo网址 网址:http://antv.alipay.com/zh-cn/f2/3.x/demo/index.html 二. 本文主要对面积对比图,扇形图,柱状图三大常见图进行 ...

随机推荐

  1. PHP解释器引擎执行流程 - [ PHP内核学习 ]

    catalogue . SAPI接口 . PHP CLI模式解释执行脚本流程 . PHP Zend Complile/Execute函数接口化(Hook Call架构基础) 1. SAPI接口 PHP ...

  2. 过滤器(Filter)的应用

    过滤器的概念 Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应. 主要用于对HttpServletRequest 进行预处理,也可以对HttpSe ...

  3. Centos提示-bash: make: command not found的解决办法

    一般出现这个-bash: make: command not found提示,是因为安装系统的时候使用的是最小化mini安装,系统没有安装make.vim等常用命令,直接yum安装下即可: yum - ...

  4. Google的Java开发规范

    长期以来,Google一直有针对各种语言的规范,例如C++, Python等等.惟独对于Java语言,Google一直没有给出相应的规范(传说中是因为Google Java首席构架师Joshua Bl ...

  5. CF 370B Berland Bingo

    题目链接: 传送门 Berland Bingo time limit per test:1 second     memory limit per test:256 megabytes Descrip ...

  6. Java内存回收机制

    在Java中,它的内存管理包括两方面:内存分配(创建Java对象的时候)和内存回收,这两方面工作都是由JVM自动完成的,降低了Java程序员的学习难度,避免了像C/C++直接操作内存的危险.但是,也正 ...

  7. 同样有缓冲区,为什么bufferedReader输入流不需要清空缓冲区?而bufferedWriter需要清空缓冲区呢?

    当BufferedReader在读取文本文件时,会先尽量从文件中读入字符数据并置入缓冲区,而之后若使用read()方法,会先从缓冲区中进行读取, 如果缓冲区数据不足,才会再从文件中读取.清不清空Buf ...

  8. 《JavaScript权威指南》学习笔记 第四天 数组

    昨天学习了js的对象,了解了js的原型链.在js里面万事万物皆对象,只不过一些原始类型要经过包装对象的包装才能暂时变为对象.数组的本质是什么呢?数组其实就是一组数,也就是链表.每个数只是这个链表上的一 ...

  9. CDN网络(二)之配置和优化CDN核心缓存软件--squid

    前言 squid是众多CDN厂商使用的核心缓存软件,都在已有的基础上进行二次开发.在部署squid的时候,建议遵循下面的规范. 1. 使用大内存服务器 对于热点文件,我们让squid用内存缓存,这样大 ...

  10. oneM2M

    http://www.ccsa.org.cn/organization/intro.php?org=oneM2M 一.成立背景 随着物联网技术研发及市场推广的不断深入,全球各通信标准化组织都在加强物联 ...