C#如何实现画矩形图,饼状图

时间:2021-12-29 13:31:26

      在用C#做报表类的网站的时候,很多时候需要一些图形来直观的表达数据所蕴含的信息,这些图形包括曲线图,矩形图,饼状图,  最近作了个项目,内容涉及到画CPK曲线,画某个时间段各个工站的产能图,良率图,以及每月的产能图。现贴出来跟大家一起分享。

画出的效果如下:

C#如何实现画矩形图,饼状图

 

      画图常需要的命名空间是:using System.Drawing;

     画图主函数:

 private void DrawGraph()

  {
   // 在此处放置用户代码以初始化页面
   Graphics objGraphis;
   int width=600,height=300;
   Bitmap objBitMap=new Bitmap(width,height);//图片的大小
   objGraphis=Graphics.FromImage(objBitMap);
   objGraphis.Clear(Color.White);//设置图片背景颜色
   System.Drawing.Pen pen=new System.Drawing.Pen(Brushes.Black,2);//设置画笔的颜色粗细
   System.Drawing.Pen pen1=new System.Drawing.Pen(Brushes.Black,1);//设置画笔的颜色粗细
   //画图形的外框,这样会好看些
   objGraphis.DrawLine(pen,0,0,width,0);
   objGraphis.DrawLine(pen,0,0,0,height);
   objGraphis.DrawLine(pen,0,height,width,height);
   objGraphis.DrawLine(pen,width,height,width,0);

   int [] arrValues={0,0};
   arrValues[0]=90;//存放Pass的数量
   arrValues[1]=10;//存放Fail的数量
   
   //文字标识
   string[] arrValueNames={"0","0"};
   arrValueNames[0]="Pass";
   arrValueNames[1]="Fail";

   objGraphis.DrawString("产线生产情况统计",new Font("宋体",10),Brushes.Black,new PointF(10,10));

   PointF symbolLeg=new PointF(300,20);//标识图的起初位置
   //画标识图
   objGraphis.FillRectangle(Brushes.Green,symbolLeg.X,symbolLeg.Y,40,20);
   objGraphis.DrawRectangle(Pens.Black,symbolLeg.X,symbolLeg.Y,40,20);
   objGraphis.DrawString(arrValueNames[0],new Font("宋体",10),Brushes.Black,new PointF(350,20));

   objGraphis.FillRectangle(Brushes.Red,symbolLeg.X+100,symbolLeg.Y,40,20);
   objGraphis.DrawRectangle(Pens.Black,symbolLeg.X+100,symbolLeg.Y,40,20);
   objGraphis.DrawString(arrValueNames[1],new Font("宋体",10),Brushes.Black,new PointF(450,20));
   //画坐标
   objGraphis.DrawLine(pen1,60,50,60,270);
   objGraphis.DrawLine(pen1,60,270,540,270);
   //画Y轴坐标
   for(int i=0;i<11;i++)
   {
    float ykedu=10*i;
    objGraphis.DrawLine(pen1,60,70+20*i,55,70+20*i);
    objGraphis.DrawString(ykedu.ToString(),new Font("宋体",10),Brushes.Black,30,270-20*i-5);
  

   }
   //画矩形图
   PointF RectangStart=new PointF(100,270);
   for (int i=0;i<2;i++)
   {
    objGraphis.FillRectangle(new SolidBrush(GetColor(i)),RectangStart.X,270-2*arrValues[i],100,arrValues[i]*2);//矩形的宽度为50和相应数值的高度
    objGraphis.DrawRectangle(Pens.Black,RectangStart.X,270-arrValues[i]*2,100,arrValues[i]*2);
    objGraphis.DrawString(arrValues[i].ToString(),new Font("宋体",10),Brushes.Black,RectangStart.X+10,270-arrValues[i]*2-15);
                RectangStart.X+=110;
   }

 


   //画饼形图
   float sglCurrentAngle;
   float sglTotalAngle=0;
   for(int a=0;a<arrValues.Length-1;a++)
   {
    sglTotalAngle+=arrValues[a];

   }
   for (int b=0;b<arrValues.Length;b++)
   {
    sglCurrentAngle=arrValues[b]/sglTotalAngle*360;
    objGraphis.FillPie(new SolidBrush(GetColor(b)),420,95,100,100,sglTotalAngle,sglCurrentAngle);
    sglTotalAngle+=sglCurrentAngle;
   }
   objBitMap.Save(Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);

 //将图形输出到屏幕,也可以将图形输出到Image控件上.
  }

颜色获取函数

private Color GetColor(int itemIndex)
  {
   Color objColor=new Color();
   switch(itemIndex)
   {
    case 0:
     objColor=Color.Green;
     break;
    case 1:
     objColor=Color.Red;
     break;
    default:
     objColor=Color.Blue;
     break;


   }
   return objColor;
  }