How to implement data charts in ASP. NET

Source: Internet
Author: User
I read an article today Article The content is how to implement data charts in ASP. NET. It first explains how to create a simple graph, and then explains how to draw lines and write on the created graph. To implement these functions, we must introduce the following namespaces: system. Drawing, system. Drawing. drawing2d, and system. Drawing. imaging.

1. First, let's talk about how to create a simple graph on a page.
First, we need to instantiate a bitmap object.
Bitmap image = new Bitmap (400,400 ); // The two parameters are the width and height of the image.

Then, use the classGraphicsOfFromimage()Method,Pass the previously instantiated bitmap object image as a parameter to it to generateGraphics object g;
Graphics G = graphics. fromimage (image );
Then, you can call the fillellipse, fillrectangle, and fillpie methods of G to set the background color and coordinates of each point for the image.
G. fillellipse (New solidbrush (color. Yellow), 0, 0, 400,400 );
G. fillpie (New solidbrush (color. Red), 200,200 ); // Draw a slice and fill it in red...

Finally, we use the SAVE () method of the image object to output the image to the client browser for display.
// Save the image object in "Jpeg" format and display it on the client
Image. Save (response. outputstream, imageformat. JPEG );

In this way, a single image is generated.

2. How to draw other images (such as line, word, ellipse, and rectangle) on the Image)
1. First, the Custom font F = new font (parameter list) can have many different overload methods, such
Font F = new font ("", 20 );// Define the font as and the font size as 20

2. Define the pen penname = new pen (parameter list), which can be different colors and different widths.
Pen redpen = new pen (color. Red );// Define the red paint brush
Pen bluepen = new pen (New solidbrush (color. Blue), 4 );// Define the blue paint brush. The width of the line drawn by the paint brush is 4.

3. You can call various draw * methods of the graphics object G above, such as drawline and drawstring to implement the functions you need. For example:
G. drawline (redpen, 200,200,250,100 ); // Draw a line with redpen. The starting coordinates are (200,200) and (250,100), respectively)
G. drawrectangle (bluepen, 250,200 ); // Use bluepen to draw a rectangle. The coordinates in the upper left corner are (250). The width and height of the rectangle are 200 and, respectively.
G. drawstring ("how are you? ", F, new solidbrush (color. Yellow), 50, 50 ); // Write "how are you? ", The font is f and starts from (50, 50 ).

4. Finally, use the SAVE () method of the image object to output the image to the client browser for display.
// Save the image object in "Jpeg" format and display it on the client
Image. Save (response. outputstream, imageformat. JPEG );

Iii. aboveCodeThe following figure shows the effect:


This is just aboutSimple Application of the lower classification of system. Drawing namespace. This class can also be used to make a variety of images and intuitive graphics, such as pie charts and various comparative analysis charts. So we need to be further familiar with and practice well before we can better master this function .. . Net encapsulates various types of data. Currently, we only use the interfaces they provide for application development. If you are interested, you can also study some underlying implementations, it should be interesting, too.

I am not a master. I just want to record some things I learned. If I think it is simple, I hope everyone will laugh at it.

Related Article

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.