This article will introduce in. How to use code to draw charts in Net, just like charts generated with MS Excel. You can also draw a table like DataGrid.
exist. Net, Microsoft provides us with the drawing class (System.Drawing.Imaging), which has the basic functions of drawing. For example: straight lines, polylines, rectangles, polygons, ovals, sectors, curves, etc., so general graphics can be drawn directly through code. Next, we introduce some drawing functions:
Bitmap bMap=new Bitmap(500,500) //Define image size;
bMap.Save(Stream,ImageCodecInfo) //Save the image to the specified output stream;
Graphics gph //Define or create a GDI drawing object;
PointF cPt //Define the x, y coordinates in the two-dimensional plane;
DrawString(string,Font,Brush,PonitF) //Use the specified Brush and Font objects to draw the specified string at the specified rectangle or point;
DrawLine(Pen,Ponit,Ponit) //Use the specified pen (Pen) object to draw a straight line between the specified two points;
DrawPolygon(Pen,Ponit[]) //Use the specified pen (Pen) object to draw the specified polygon, such as triangle, quadrilateral, etc.;
FillPolygon(Brush,Ponit[]) //Fill the specified polygon with the specified brush (Brush) object;
DrawEllipse(Pen,x,y,Width,Height) //Use the specified pen to draw an ellipse defined by the border;
FillEllipse(Brush,x,y,Width,Height) //Use the specified brush to fill an ellipse defined by the border;
DrawRectangle(Pen,x,y,Width,Height) //Use the specified pen to draw a rectangle with specified coordinate points, width, and height;
DrawPie(Pen,x,y,Width,Height,startAngle,sweepAngle) //Use the specified pen to draw a sector composed of the specified coordinate point, width, height and two rays;
OK, that’s all. The parameters have been abbreviated. I believe that in the actual use process, everyone will have a deeper experience. Finally, let's look at how to use these drawing functions (line chart) through a simple example.
The code to draw the above line chart is as follows:
//data initialization
string[] month=new string[12]{"January","February","March","April","May","June","July","August" ,"September","October","November","December"};
float[] d=new float[12]{20.5F,60,10.8F,15.6F,30,70.9F,50.3F,30.7F,70,50.4F,30.8F,20};
//Drawing initialization
Bitmap bMap=new Bitmap(500,500);
Graphics gph=Graphics.FromImage(bMap);
gph.Clear(Color.White);
PointF cPt=new PointF(40,420);//center point
PointF[] xPt=new PointF[3]{new PointF(cPt.Y+15,cPt.Y),new PointF(cPt.Y,cPt.Y-8),new PointF(cPt.Y,cPt.Y+ 8)};//X-axis triangle
PointF[] yPt=new PointF[3]{new PointF(cPt.X,cPt.X-15),new PointF(cPt.X-8,cPt.X),new PointF(cPt.X+8,cPt. X)};//Y axis triangle
gph.DrawString("Monthly production volume chart of a certain product in a factory", new Font("宋体", 14), Brushes.Black, new PointF(cPt.X+60, cPt.X));//Chart title
//Draw X axis
gph.DrawLine(Pens.Black, cPt.X,cPt.Y ,cPt.Y,cPt.Y);
gph.DrawPolygon(Pens.Black,xPt);
gph.FillPolygon(new SolidBrush(Color.Black),xPt);
gph.DrawString("Month", new Font("宋体", 12), Brushes.Black, new PointF(cPt.Y+10, cPt.Y+10));
//Draw Y axis
gph.DrawLine(Pens.Black, cPt.X,cPt.Y,cPt.X,cPt.X);
gph.DrawPolygon(Pens.Black,yPt);
gph.FillPolygon(new SolidBrush(Color.Black),yPt);
gph.DrawString("Unit (10,000)", new Font("宋体", 12), Brushes.Black, new PointF(0, 7));
for(int i=1;i<=12;i++)
{
//Draw Y-axis scale
if (i<11)
{
gph.DrawString((i*10).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X-30, cPt.Yi*30-6));
gph.DrawLine(Pens.Black, cPt.X-3,cPt.Yi*30,cPt.X,cPt.Yi*30);
}
//Draw X-axis items
gph.DrawString(month[i-1].Substring(0,1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X+i*30-5, cPt.Y+5 ));
gph.DrawString(month[i-1].Substring(1,1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X+i*30-5, cPt.Y+20 ));
if(month[i-1].Length>2) gph.DrawString(month[i-1].Substring(2,1), new Font("宋体", 11), Brushes.Black, new PointF(cPt. X+i*30-5, cPt.Y+35));
//draw dots
gph.DrawEllipse(Pens.Black,cPt.X+i*30-1.5F,cPt.Yd[i-1]*3-1.5F,3,3);
gph.FillEllipse(new SolidBrush(Color.Black),cPt.X+i*30-1.5F,cPt.Yd[i-1]*3-1.5F,3,3);
//draw numerical values
gph.DrawString(d[i-1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X+i*30,cPt.Yd[i-1]*3 ));
//Draw a polyline
if(i>1) gph.DrawLine(Pens.Red,cPt.X+(i-1)*30,cPt.Yd[i-2]*3,cPt.X+i*30,cPt.Yd[i- 1]*3);
}
//Save the output image
bMap.Save(Response.OutputStream, ImageFormat.Gif);
http://www.cnblogs.com/172838427/archive/2006/09/19/508688.html