Use C # To draw real-time Graphs

Source: Internet
Author: User

In actual projects, we often need to draw some real-time data images, such as the current water usage, power consumption, and real-time display of the current audio frequency when playing audio videos, the most familiar Task Manager also has such a function to indicate the current CPU usage frequency. Recently, I just gave my friends a similar function diagram, using graphs to express some actual data in real time is very popular with customers because of its intuitive image.
However, for some reason, I cannot share the code in the actual project with you. I can only simulate a simple implementation, and the code is not optimized too much, so there are still many optimizations, hope you can improve your skills.

In order to operate and cope with changes, the function of drawing a graph is encapsulated into a class separately, and the data in it is completely simulated, in horizontal coordinates, each pixel interval is controlled by a point (this distance may be increased in reality ), the horizontal direction is a random number (in actual development, this should come from our real-time data rate calculation), the display form uses a thread to regularly draw real-time curves.

The actual code is as follows:

Using system; using system. Collections. Generic; using system. Text; using system. Drawing; using system. Drawing. imaging; namespace realtimecurve {////// Description: real-time image generation class. In this example, each pixel has a control point in horizontal coordinates. // the control points can be reduced in actual development, for example, every 5 pixels with a control point // such effect may be more realistic // Author: Zhou Gong // Date: 2008-07-21 // starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/07/21/2682027.aspx ///Public class realtimeimagemaker {private int width; // The width of the curve to be generated private int height; // The height of the curve to be generated private point [] pointlist; // It is used to draw the key points of the curve, and connect these points in sequence to obtain the graph private random = new random (); // It is used to generate a random number private bitmap currentimage; // The Private color backcolor of the image to be drawn; // the background color of the image is private color forecolor; // The foreground color of the image ////// Image Height ///Public int height {get {return height;} set {Height = value ;}}////// Image Width ///Public int width {get {return width;} set {width = value ;}}////// Constructor, specify the width and height of the generated graph //////Width of the graph to be generated ///Public realtimeimagemaker (INT width, int height): This (width, height, color. Gray, color. Blue ){}////// Constructor, which specifies the width, height, background color, and foreground color of the generated graph //////Width of the graph to be generated ///Height of the graph to be generated ///Graph background color ///Public realtimeimagemaker (INT width, int height, color backcolor, color forecolor) {This. width = width; this. height = height; this. backcolor = backcolor; this. forecolor = forecolor; pointlist = new point [width]; point temppoint; // all the coordinate points on the initialization curve for (INT I = 0; I <width; I ++) {temppoint = new point (); // the x-axis of the curve increments sequentially. Each pixel has a point temppoint at the horizontal position. X = I; // The ordinate values of each point on the curve are randomly generated, but the temppoint must be within the display area. y = random. next () % height; pointlist [I] = temppoint ;}}////// Obtain the curve drawn from each point on the current sequentially connected curve //////
 Public Image getcurrentcurve () {// currentimage = historyimage. clone (New rectangle (1, 0, width-1, height), pixelformat. format24bpprgb); currentimage = new Bitmap (width, height); point P; // advance the coordinate point of the currently located graph and reduce the X coordinate by 1, // The effect is equivalent to removing the current first vertex for (INT I = 0; I <width-1; I ++) {P = pointlist [I + 1]; pointlist [I] = new point (P. x-1, P. y);} Point temppoint = new point (); // generate the coordinate temppoint of the last vertex of the curve point. X = width; // The ordinate values of each point on the curve are randomly generated, but the temppoint must be within the display area. y = random. next (datetime. now. millisecond) % height; // Add a new coordinate point at the end: pointlist [width-1] = temppoint; graphics G = graphics. fromimage (currentimage); G. clear (backcolor); // draw the graph G. drawlines (new pen (forecolor), pointlist); G. dispose (); Return currentimage ;}}}

Key code of the form:

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. text; using system. windows. forms; using system. threading; namespace realtimecurve {////// Description: Display real-time graph form // Author: Zhou Gong // Date: 2008-07-21 // starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/07/21/2682027.aspx ///Public partial class formrealtime: FORM {thread; realtimeimagemaker RRTI; color backcolor = color. black; // specifies the background color public formrealtime () {initializecomponent (); RRTI = new realtimeimagemaker (width, height, backcolor, color. green); thread = new thread (New threadstart (run); thread. start ();} private void run () {While (true) {image = Ti. getcurrentcurve (); graphics G = creategraphics (); // use the specified background color to clear the image G on the current form. clear (backcolor); G. drawimage (image, 0, 0); G. dispose (); // refresh the thread every second. sleep (1000) ;}} private void formrealtime_formclosing (Object sender, formclosingeventargs e) {// stop the thread before the form is about to close. abort ();}}}

The final running result of the program:

Download all program code at http://download.csdn.net/zhoufoxcn.

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.