C # line chart example

Source: Internet
Author: User

in. net, Microsoft provides us with the system. Drawing. Imaging class, which provides basic functions for drawing. For example, straight line, line, rectangle, polygon, elliptical shape, slice, curve, etc, therefore, common images can be drawn directly using Code . Next we will introduce some plotting functions:
bitmap bmap = new Bitmap (500,500)/defines the image size;
bmap. save (stream, imagecodecinfo)/Save the image to the specified output stream;
graphics gph/define or create a GDI drawing image;
pointf CPT/defines the X and Y coordinates in a two-dimensional plane.
drawstring (string, Font, brush, ponitf) /use the specified brush and font pairs to draw the specified string in the specified rectangle or point;
drawline (pen, ponit, ponit)/use the specified pen) draw a straight line between two points for the image.
drawpolygon (pen, ponit [])/use the specified pen to draw a specified polygon, such as a triangle or a quadrilateral;
fillpolygon (brush, ponit [])/fill the specified polygon with the specified brush (Brush);
drawellipse (pen, X, Y, width, height)/draw an ellipse defined by a border with the specified pen;
fillellipse (brush, X, Y, width, height) /fill the border-defined ellipse with the specified brush;
drawrectangle (pen, X, Y, width, height) /draw a rectangle with the specified coordinate point, width, and height with the specified pen.
drawpie (pen, X, Y, width, height, startangle, sweepangle) /draw a sector composed of the specified coordinate point, width, height, and two rays with the specified pen.

The following example shows a line chart. The Code is as follows:
Private void form1_load (Object sender, eventargs E)
{
String [] month = new string [12] {"May January", "May February", "May March", "May April", "May", "May June", "May July ", "August", "August "};
Float [] d = new float [12] {4155f, 60, 10.8f, 15.6f, 30, 70.9f, 50.3f, 30.7f, 70, 50.4f, 30.8f, 20 };
// Painting 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 output chart of a product in a factory", new font ("", 14), brushes. black, new pointf (CPT. X + 60, CPT. x); // chart title
// Draw the 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 the 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 thousand)", new font ("", 12), brushes. Black, new pointf (0, 7 ));
For (INT I = 1; I <= 12; I ++)
{
// Draw the Y axis Scale
If (I <11)
{
Gph. drawstring (I * 10 ). tostring (), new font ("", 11), 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 the X axis Project
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 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 ("", 11), brushes. black, new pointf (CPT. X + I * 30, CPT. y-d [I-1] * 3 ));
// Draw line
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 the output image
// Bmap. Save (response. outputstream, imageformat. GIF );
Picturebox1.image = bmap;
}

As follows:

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.