Draw a line chart/column chart/bar/pie chart with code

Source: Internet
Author: User
Tags define tostring
Pie chart | Line Chart | Column chart

This article will introduce you to the. NET how to use code drawing tables, just as you would with a chart generated in MS Excel. You can also sketch a DataGrid-like table.
In NET, Microsoft has provided us with the drawing Class (System.Drawing.Imaging), in this class the basic function of drawing. For example: Straight line, line, rectangle, polygon, oval, fan, curve and so on, so the general graphics can be directly drawn through the code. Next, we'll introduce some drawing functions:
Bitmap bmap=new Bitmap (500,500)//define image size;
Bmap.save (Stream,imagecodecinfo)//Saves the image to the specified output stream;
Graphics GPH//define or create GDI drawings for image;
PointF cPt//definition of x,y coordinates in two-dimensional plane;
DrawString (STRING,FONT,BRUSH,PONITF)///To draw the specified string with the specified Brush and Font pairs, as in the specified rectangle or point;
DrawLine (Pen,ponit,ponit)//(pen) to draw a straight line between two points;
DrawPolygon (pen,ponit[])///with the specified pen (pen) to draw the specified polygon, such as triangles, quadrilateral and so on;
FillPolygon (brush,ponit[])//fill the specified polygon with the specified brush (Brush);
DrawEllipse (Pen,x,y,width,height)//Draw a border-defined ellipse with the specified pen;
FillEllipse (brush,x,y,width,height)//Fill a border-defined ellipse with the specified brush;
DrawRectangle (Pen,x,y,width,height)//Draw a rectangle with specified coordinates, width, and height with the pen you specify;
Drawpie (Pen,x,y,width,height,startangle,sweepangle)//-Draws a sector of a specified coordinate point, width, height, and two rays with the specified pen;
OK, so much for the introduction, the parameters are abbreviated. I believe that in the actual use of the process, we will have a more profound experience. Finally, take a simple example to see how to use these drawing functions (line charts).
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};
Paint 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 ("A product monthly production chart for a Factory", New Font ("XXFarEastFont-Arial"), 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 ("XXFarEastFont-Arial,", 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 (million)", New Font ("XXFarEastFont-Arial",), 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 ("Arial", one), Brushes.black, New PointF (cpt.x-30, cpt.y-i*30-6));
Gph. DrawLine (Pens.black, cpt.x-3,cpt.y-i*30, cpt.x,cpt.y-i*30);
}
Draw X Axis Item
Gph. DrawString (Month[i-1]. Substring (0,1), new Font ("Arial", one), Brushes.black, New PointF (cpt.x+i*30-5, cpt.y+5));
Gph. DrawString (Month[i-1]. Substring (1,1), New Font ("Arial", one), Brushes.black, New PointF (cpt.x+i*30-5, cpt.y+20));
if (month[i-1). LENGTH&GT;2) gph. DrawString (Month[i-1]. Substring (2,1), New Font ("Arial", one), Brushes.black, New PointF (cpt.x+i*30-5, cpt.y+35));
Draw a Point
Gph. DrawEllipse (pens.black,cpt.x+i*30-1.5f,cpt.y-d[i-1]*3-1.5f,3,3);
Gph. FillEllipse (New SolidBrush (Color.Black), cpt.x+i*30-1.5f,cpt.y-d[i-1]*3-1.5f,3,3);
Draw a value
Gph. DrawString (D[i-1]. ToString (), New Font ("XXFarEastFont-Arial", one), Brushes.black, New PointF (cpt.x+i*30,cpt.y-d[i-1]*3));
Draw a polyline
if (i>1) gph. DrawLine (pens.red,cpt.x+ (i-1) *30,cpt.y-d[i-2]*3,cpt.x+i*30,cpt.y-d[i-1]*3);
}
Save output picture
Bmap.save (Response.outputstream, imageformat.gif);



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.