Use zedgraph to create dynamic update statistical charts

Source: Internet
Author: User

Zedgraph is good. as mentioned in a previous article about the open-source project of statistical charts under. net, zedgraph directly draws images on the canvas rather than generating image Displays, compared with some other Statistical Chart controls, therefore, the performance is better. In Real-Time trend charts such as the stock market, it shows that CPU usage and other real-time applications have a good performance, and the method is not difficult, but few people write this information.ArticleIn other forums, I wrote the following example.

When drawing a line chart, zedgraph saves all the coordinate points in the pointpairlist. This is the X and Y coordinates when drawing the line. To make a dynamic line chart, we need to add coordinates to the pointpairlist and refresh it.

CodeSimple:

Random ran = new random ();

Pointpairlist list = new pointpairlist ();

Lineitem mycurve;

Random is used to generate sample data, that is, Y coordinate, and pointpairlist is used to store point sets. Mycarve is the line to be drawn. Of course, you cannot forget to add the zedgraph control on the form.

To highlight the effect, we add the following code to the form load event:

This. zedgraphcontrol1.graphpane. Title. Text = "dynamic line chart ";

This. zedgraphcontrol1.graphpane. xaxis. Title. Text = "time ";

This. zedgraphcontrol1.graphpane. yaxis. Title. Text = "quantity ";

This. zedgraphcontrol1.graphpane. xaxis. type = zedgraph. axistype. dateasordinal;

For (INT I = 0; I <= 100; I ++)

{

Double X = (double) New xdate (datetime. Now. addsecondds (-(100-I )));

Double Y = ran. nextdouble ();

List. Add (x, y );

}

Datetime dt = datetime. now;

Mycurve = zedgraphcontrol1.graphpane. addcurve ("My curve ",

List, color. darkgreen, symboltype. None );

This. zedgraphcontrol1.axischange ();

This. zedgraphcontrol1.refresh ();

In this way, a line chart has been drawn after the form is loaded. It may look like the following:

But now, this line is still not moving. To make it dynamic, You need to regularly add coordinates to pointpairlist.

Add a timer control, set the interval attribute to 1000, and then add code to the tick event of Timer:

Zedgraphcontrol1.graphpane. xaxis. Scale. maxauto = true;

Double X = (double) New xdate (datetime. Now );

Double Y = ran. nextdouble ();

List. Add (x, y );

This. zedgraphcontrol1.axischange ();

This. zedgraphcontrol1.refresh ();

Run the command to see the line moving.

To display a specified number of points in a line chart, you only need to remove the first coordinate point before adding the coordinates:

If (list. Count> = 100)

{

List. removeat (0 );

}

If the CPU usage in Windows Task Manager is the same as that in Windows Task Manager, It is blank at the beginning and will gradually become full over time, you can fill in a few points whose Y coordinates are 0 during initialization:

For (INT I = 0; I <= 100; I ++)

{

Double X = (double) New xdate (datetime. Now. addsecondds (-(100-I )));

Double Y = 0;

List. Add (x, y );

}

In fact, the code is relatively simple, and the key lies in the performance. In the above Code, symboltype is used when generating a line. none. If you use other types of images, the discount points can be represented as square or star images, and the performance will be much lower. For example, according to the above Code, in my Windows2000 Professional Edition, sai Yang 1.7g, 512 points can be displayed in 10000 memory, and there is no obvious pause, But if you set the graph of the discount point to symboltype. diamond, for example:


In the case of 10000 points, the pause phenomenon is very serious. In fact, less than 2000 points already have a clear feeling. At the same time, the anti-aliasing feature is not used in Line tracing, which can improve the performance. However, the performance improvement is still very limited.

Take the real-time stock market trend chart as an example. If the chart is updated every 10 seconds for four hours every day, it can be seen that zedgraph is fully applicable.

Original article: http://www.cnblogs.com/dahuzizyd/archive/2007/01/16/zedgraph_dynamic_line_chart.html

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.