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

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

概述统计图形种类繁多,有柱状图,折线图,扇形图等等,而统计图形的绘制方法也有很多,有Flash制作的统计图形,有水晶报表生成统计图形,有专门制图软件制作,也有编程语言自己制作的;这里我们用就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,wIDth,height);

3. 绘制横向坐标线

for (int i = 0; i < 14; i++) {g.Drawline(mypen,x,80,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,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(),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,340 - Count1[0],20,Count1[0]);g.DrawString(Count1[0].ToString(),Font2,Brushes.Red,340 - Count1[0] - 15);

(2) 第一期通过人数

x = x + 20;g.FillRectangle(mybrush2,340 - Count2[0],Count2[0]);g.DrawString(Count2[0].ToString(),Brushes.Green,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,height);//创建Graphics类对象Graphics g = Graphics.FromImage(image);try{//清空图片背景色g.Clear(color.White);Font Font = new Font("Arial",FontStyle.Regular);Font Font1 = new Font("宋体",FontStyle.Bold);linearGradIEntBrush brush = new linearGradIEntBrush(new Rectangle(0,image.WIDth,image.Height),color.Blue,color.BlueViolet,1.2f,true);g.FillRectangle(Brushes.WhiteSmoke,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),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,340);x = x + 40;}Pen mypen1 = new Pen(color.Blue,2);x = 60;g.Drawline(mypen1,340);//绘制纵向线条int y = 106;for (int i = 0; i < 9; i++){g.Drawline(mypen,y);y = y + 26;}g.Drawline(mypen1,y);//x轴String[] n = { "第一期","上半年","下半年","全年统计" };x = 78;for (int i = 0; i < 7; i++){g.DrawString(n[i].ToString(),348); //设置文字内容及输出位置x = x + 78;}//y轴String[] m = {"250","100"," 75"," 50"," 25"," 0"};y = 72;for (int i = 0; i < 10; i++){g.DrawString(m[i].ToString(),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",FontStyle.Bold);SolIDBrush mybrush = new SolIDBrush(color.Red);SolIDBrush mybrush2 = new SolIDBrush(color.Green);//第一期g.FillRectangle(mybrush,340 - Count1[0] - 15);x = x + 20;g.FillRectangle(mybrush2,340 - Count2[0] - 15);//第二期x = x + 60;g.FillRectangle(mybrush,340 - Count1[1],Count1[1]);g.DrawString(Count1[1].ToString(),340 - Count1[1] - 15);x = x + 20;g.FillRectangle(mybrush2,340 - Count2[1],Count2[1]);g.DrawString(Count2[1].ToString(),340 - Count2[1] - 15);//第三期x = x + 60;g.FillRectangle(mybrush,340 - Count1[2],Count1[2]);g.DrawString(Count1[2].ToString(),340 - Count1[2] - 15);x = x + 20;g.FillRectangle(mybrush2,340 - Count2[2],Count2[2]);g.DrawString(Count2[2].ToString(),340 - Count2[2] - 15);//第四期x = x + 60;g.FillRectangle(mybrush,340 - Count1[3],Count1[3]);g.DrawString(Count1[3].ToString(),340 - Count1[3] - 15);x = x + 20;g.FillRectangle(mybrush2,340 - Count2[3],Count2[3]);g.DrawString(Count2[3].ToString(),340 - Count2[3] - 15);//上半年x = x + 60;g.FillRectangle(mybrush,340 - Count1[4],Count1[4]);g.DrawString(Count1[4].ToString(),340 - Count1[4] - 15);x = x + 20;g.FillRectangle(mybrush2,340 - Count2[4],Count2[4]);g.DrawString(Count2[4].ToString(),340 - Count2[4] - 15);//下半年x = x + 60;g.FillRectangle(mybrush,340 - Count1[5],Count1[5]);g.DrawString(Count1[5].ToString(),340 - Count1[5] - 15);x = x + 20;g.FillRectangle(mybrush2,340 - Count2[5],Count2[5]);g.DrawString(Count2[5].ToString(),340 - Count2[5] - 15);//全年x = x + 60;g.FillRectangle(mybrush,340 - Count1[6],Count1[6]);g.DrawString(Count1[6].ToString(),340 - Count1[6] - 15);x = x + 20;g.FillRectangle(mybrush2,340 - Count2[6],Count2[6]);g.DrawString(Count2[6].ToString(),340 - Count2[6] - 15);//绘制标识Font Font3 = new System.Drawing.Font("Arial",FontStyle.Regular);g.DrawRectangle(new Pen(Brushes.Blue),170,400,250,50); //绘制范围框g.FillRectangle(Brushes.Red,270,410,10); //绘制小矩形g.DrawString("报名人数",Font3,292,408);g.FillRectangle(Brushes.Green,430,10);g.DrawString("通过人数",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,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("宋体",FontStyle.Regular);Font Font2 = new System.Drawing.Font("Arial",8,FontStyle.Regular);linearGradIEntBrush brush = new linearGradIEntBrush(new Rectangle(0,true);g.FillRectangle(Brushes.AliceBlue,height);Brush brush1 = new SolIDBrush(color.Blue);Brush brush2 = new SolIDBrush(color.Saddlebrown);g.DrawString(this.ddlTaget.SelectedItem.Text + " " + this.ddlYear.SelectedItem.Text + " 成绩统计折线图",brush1,new PointF(85,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,340);x = x + 80;}Pen mypen1 = new Pen(color.Blue,3);x = 60;g.Drawline(mypen1,82,340);//绘制横向线条int y = 106;for (int i = 0; i < 10; i++){g.Drawline(mypen,y);y = y + 26;}// y = 106;g.Drawline(mypen1,y - 26,y - 26);//x轴String[] n = { "第一期","全年统计" };x = 45;for (int i = 0; i < 7; i++){g.DrawString(n[i].ToString(),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(),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",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(),58,points1[0].Y - 20);g.DrawString(Count1[1].ToString(),138,points1[1].Y - 20);g.DrawString(Count1[2].ToString(),218,points1[2].Y - 20);g.DrawString(Count1[3].ToString(),298,points1[3].Y - 20);g.DrawString(Count1[4].ToString(),378,points1[4].Y - 20);g.DrawString(Count1[5].ToString(),458,points1[5].Y - 20);g.DrawString(Count1[6].ToString(),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(),61,points2[0].Y - 15);g.DrawString(Count2[1].ToString(),131,points2[1].Y - 15);g.DrawString(Count2[2].ToString(),221,points2[2].Y - 15);g.DrawString(Count2[3].ToString(),301,points2[3].Y - 15);g.DrawString(Count2[4].ToString(),381,points2[4].Y - 15);g.DrawString(Count2[5].ToString(),461,points2[5].Y - 15);g.DrawString(Count2[6].ToString(),541,points2[6].Y - 15);//绘制标识g.DrawRectangle(new Pen(Brushes.Red),180,390,402,400);g.FillRectangle(Brushes.Green,422,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.ToInt32Total = 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",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,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),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],((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,Titleheight),stringFormat);//列出各字段与得数目objgraphics.DrawRectangle(new Pen(color.Red,2),height + 10 - legendheight,legendheight + 50);objgraphics.DrawString("----------------统计信息------------------",Fontlegend,bluebrush,height - legendheight + Fontlegend.Height * 1 + 1);objgraphics.DrawString("统计单位: " + this.ddlTaget.SelectedItem.Text,height - legendheight + Fontlegend.Height * 3 + 1);objgraphics.DrawString("统计年份: " + this.ddlYear.SelectedItem.Text,height - legendheight + Fontlegend.Height * 4 + 1);objgraphics.DrawString("统计期数: " + this.ddlSpan.SelectedItem.Text,height - legendheight + Fontlegend.Height * 5 + 1);objgraphics.FillRectangle((SolIDBrush)colors[1],5,height - legendheight + Fontlegend.Height * 8 + 1,10);objgraphics.DrawString("报名总人数: " + Convert.ToString(Convert.ToSingle(ds.tables[0].Rows[0][this.count[0]])),height - legendheight + Fontlegend.Height * 7 + 1);objgraphics.FillRectangle((SolIDBrush)colors[0],height - legendheight + Fontlegend.Height * 9 + 1,10);objgraphics.DrawString("通过总人数: " + Convert.ToString(Convert.ToSingle(ds.tables[0].Rows[0][this.count[1]])),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]]))),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)+ " %",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# 绘制统计图大全(柱状图, 折线图, 扇形图)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1257074.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存