服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# 绘制统计图大全(柱状图, 折线图, 扇形图)

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

2021-12-09 13:29jack_Meng C#

本篇文章介绍了C# 绘制统计图大全,其中包括状图, 折线图, 扇形图,有需要的同学可以了解一下。

统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有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());

最终柱状图的效果图:C# 绘制统计图大全(柱状图, 折线图, 扇形图)
柱状图的完整代码:

?
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();
}
}

二. 折线统计图的绘制

效果:
C# 绘制统计图大全(柱状图, 折线图, 扇形图)
折线图的完整代码:

?
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();
 
}

 这里的统计图直接输出到网页,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐
  • C#利用C#实现网络爬虫

    利用C#实现网络爬虫

    这篇文章主要介绍了利用C#实现网络爬虫,完整的介绍了C#实现网络爬虫详细过程,感兴趣的小伙伴们可以参考一下...

    C#教程网11852021-11-16
  • C#SQLite在C#中的安装与操作技巧

    SQLite在C#中的安装与操作技巧

    SQLite,是一款轻型的数据库,用于本地的数据储存。其优点有很多,下面通过本文给大家介绍SQLite在C#中的安装与操作技巧,感兴趣的的朋友参考下吧...

    蓝曈魅11162022-01-20
  • C#如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

    这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴...

    bbird201811792022-03-05
  • C#三十分钟快速掌握C# 6.0知识点

    三十分钟快速掌握C# 6.0知识点

    这篇文章主要介绍了C# 6.0的相关知识点,文中介绍的非常详细,通过这篇文字可以让大家在三十分钟内快速的掌握C# 6.0,需要的朋友可以参考借鉴,下面来...

    雨夜潇湘8272021-12-28
  • C#VS2012 程序打包部署图文详解

    VS2012 程序打包部署图文详解

    VS2012虽然没有集成打包工具,但它为我们提供了下载的端口,需要我们手动安装一个插件InstallShield。网上有很多第三方的打包工具,但为什么偏要使用微软...

    张信秀7712021-12-15
  • C#深入理解C#的数组

    深入理解C#的数组

    本篇文章主要介绍了C#的数组,数组是一种数据结构,详细的介绍了数组的声明和访问等,有兴趣的可以了解一下。...

    佳园9492021-12-10
  • C#C#微信公众号与订阅号接口开发示例代码

    C#微信公众号与订阅号接口开发示例代码

    这篇文章主要介绍了C#微信公众号与订阅号接口开发示例代码,结合实例形式简单分析了C#针对微信接口的调用与处理技巧,需要的朋友可以参考下...

    smartsmile20127762021-11-25
  • C#C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    C#设计模式之Strategy策略模式解决007大破密码危机问题示例

    这篇文章主要介绍了C#设计模式之Strategy策略模式解决007大破密码危机问题,简单描述了策略模式的定义并结合加密解密算法实例分析了C#策略模式的具体使用...

    GhostRider10972022-01-21