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

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

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

一. 柱状图的绘制.

绘制步骤如下:

1. 定义绘图用到的类.

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

2. 绘制图框.

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

3. 绘制横向坐标线

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

4. 绘制纵向坐标线

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

5. 绘制横坐标值

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

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. 定义数组存储数据库中统计的数据

int[] Count1 = new int[7]; //存储从数据库读取的报名人数
int[] Count2 = new int[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. 将读取的数据存储到数组中

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.定义画笔和画刷准备绘图

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)第一期报名人数

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

(2) 第一期通过人数

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. 将图形输出到页面.

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

最终柱状图的效果图:
柱状图的完整代码:

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

二. 折线统计图的绘制

效果:

折线图的完整代码:

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

三. 扇形统计图的绘制

扇形图完整代码:

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

}

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

(0)

相关推荐

  • 适合初学者开发的C#在线英汉词典小程序

    今天写了一个英汉词典小程序,我加了好多注释,适合初学者一起参考,哪里写的不好请帮忙指出,一起学习进步. 这里用到了,泛型,泛型字典,一些控件的操作,split的应用,数组的应用,时间间隔,linkLabel的使用. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using

  • C#实现win10 uwp 右击浮出窗在点击位置

    本文主要让MenuFlyout出现在我们右击位置. 我们一般使用的MenuFlyout写在前台,写在Button里面,但是可能我们的MenuFlyout显示的位置和我们想要的不一样. 通过使用后台写ShowAt的方法,我们可以通过e.GetPosition获得鼠标点击位置,需要对函数传入相对的元素,这个元素一般可以用我们点击使用的元素,也可以使用我们的最外层Grid,这样我们就可以获得了鼠标位置,也就可以显示我们的MenuFlyout在点击位置. 我们建一个ListView,然后绑定后台,在我们

  • c#图片上传和显示的实现方法

    由于需要图片上传的功能,所以花了一些时间网上找相关资料终于搞定,效果图如下: 下面的是解决方案截图和上传的图片截图: 具体实现代码如下: 1.界面代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadPic.aspx.cs" Inherits="Pic_Try.UploadPic" %> <!DOCTYPE html PUBLIC

  • C#完成word文档打印的方法

    在日常工作中,我们可能常常需要打印各种文件资料,比如word文档.对于编程员,应用程序中文档的打印是一项非常重要的功能,也一直是一个非常复杂的工作.特别是提到Web打印,这的确会很棘手.一般如果要想选择非默认打印机或者说想显示打印设置对话框时,我们也需要对代码进行一定的设置. 针对这样的问题,今天这篇文章我就来分享一下如何利用免费的第三方组件轻松打印word文档.免费组件简化了代码,提高我们的工作效率,何乐而不为呢.所以,在下面的示例中我使用了其中一个Free Spire.Doc组件来实现这一功

  • C#开发微信 二维码鼠标滑动 图像显示隐藏效果(推荐)

    客户端微信在二维码状态下,鼠标滑过,会有一张手机的图片滑动滑出,从隐藏到显示,从显示到隐藏. 思路很简单:1.设置透明度:2.给个移动的位移 先看下做的效果 整体代码也不难,就是给Image控件设置动画效果. <Grid x:Name="grid_content" Background="WhiteSmoke" Grid.Row="1"> <Grid.Triggers> <EventTrigger RoutedEve

  • 基于c#用Socket做一个局域网聊天工具

    程序设计成为简单的服务端和客户端之间的通信, 但通过一些方法可以将这两者进行统一起来, 让服务端也成为客户端, 让客户端也成为服务端, 使它们之间可以互相随时不间断的通信. 考虑到实现最原始的服务端和客户端之间的通信所需要的步骤对于写这样的程序是很有帮助的. 作为服务端, 要声明一个Socket A并绑定(Bind)某一个IP+这个IP指定的通信端口, 比如这个是127.0.0.1:9050, 然后开始监听(Listen), Listen可以监听来自多个IP传过来的连接请求, 具体可以同时连接几

  • C# ComboBox控件“设置 DataSource 属性后无法修改项集合”的完美解决方法

    由于毕业后工作没有对接到专业问题,导致四五年没有碰过Winform程序了.突然由于工作问题,为了方便自己,所以想自己写写小winform小软件,用于自己使用.在使用ComboBox控件时,遇到了重新绑定赋值出问题的情况. 错误代码如下: if (CustomerBLL.select().Rows.Count > 0) { cbTcid.Items.Clear(); cbTcid.DataSource = CustomerBLL.select(); cbTcid.ValueMember = "

  • C#实现较为实用的SQLhelper

    第一次写博客,想不到写什么好b( ̄▽ ̄)d ,考虑的半天决定从sqlhelper开始,sqlhelper对程序员来说就像helloworld一样,很简单却又很重要,helloworld代表着程序员萌新第一次写代码,而sqlhelper则是初次接触数据库(不知道这种说法对不对). 好了不废话了,下面直接上代码(无话可说了): public class SQLHelper { // 超时时间 private static int Timeout = 1000; // 数据库名称 public con

  • C#实现组合排列的方法

         C#实现组合排列的方法 最近在做数据分析系统,里面涉及到组合排列的问题,查找了很多的资料,但是感觉很多资料都是比较零散的,达不到项目需求. 后来经过一段的时间的探索,终于实现了组合排列的功能.下面我就来简单说说吧.      需求描述:   要实现的功能就是字符或数字的组合排列.例如:ab 的所有组合为:ab,ba :  ab的所有不重复排列为:ab. 其实这也是彩票中常说的直选和组选.效果图如下:     功能实现 这里就不多说了,直接贴上实现代码吧.       1.窗体界面 窗体

  • C#登入实例

    想成为一名优秀的C#程序设计师吗?首先我们要对一个程序有所了解,了解程序设计的流程.一个程序的登陆是如何实现的呢,大家跟着小编来看下,希望对你们今后的学习有所帮助 C#登入例子--第一个程序 第一步:在数据库创建一个存放账号密码的表单 第二步:创建一个登入项目 拆分成三层: CS层: BLL层: DAL层: Common层: Web.config: 以上所述是本文的全部内容,希望对大家有所帮助!

  • C#操作XML通用方法汇总

    在.net的项目开发中,经常会对XML文件进行操作,由于XML文件可以实现跨平台传输,较多的应用在数据传输中,特总结以下几种常用的XML操作方法: 1.创建XML文档: /// <summary> /// 创建XML文档 /// </summary> /// <param name="name">根节点名称</param> /// <param name="type">根节点的一个属性值</param

  • PC蓝牙通信C#代码实现

    本文实例为大家分享了C#实现PC蓝牙通信代码,供大家参考,具体内容如下 添加引用InTheHand.Net.Personal.dll 首先创建一个蓝牙类 class LanYa { public string blueName { get; set; } //l蓝牙名字 public BluetoothAddress blueAddress { get; set; } //蓝牙的唯一标识符 public ClassOfDevice blueClassOfDevice { get; set; }

  • .NET的深复制方法(以C#语言为例)

    很多时候我们复制一个对象实例A到实例B,在用实例B去做其他事情的时候,会对实例B进行修改,为保证对B的修改不会影响到A的正常使用,就需要使用到深复制. 我在网上搜到一些深复制的方法,同时写了几组例子对这些方法进行测试. 我的操作系统版本为Win7旗舰版,.NET Framework版本是4.5 测试程序 我建了一个C#窗体应用程序(Winform),其主窗口FormMain的Load函数内容如下: private void FormMain_Load(object sender, EventAr

随机推荐