Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I'm trying to create an oscilloscope type program in c# which can plot data points at very high speeds. Using the standard plotting tool, I created an application that graphs 750 2-digit random numbers as fast as possible, with a traveling waveform. Here is some code I use:

dt = new DataTable();
 dc = new DataColumn();
 dc.ColumnName = "Time"; // x-axis title;
 dt.Columns.Add(dc);
 dc = new DataColumn();
 dc.ColumnName = "Score"; // y-axis title
 dt.Columns.Add(dc);

     for (int i = 0; i < 750; i++)
     {

         Random r = new Random();
         DataRow dr;//add rows
         dr = dt.NewRow();
         dr["Time"] = index;
         dr["Score"] = r.Next(10, 99);
         dt.Rows.Add(dr);

         if (index >= 30)
         {
             DataRow rowRemoved = dt.Rows[0];
             dt.Rows.Remove(rowRemoved);
         }

         dataGridView1.DataSource = dt;
         chart1.DataSource = dt;
         chart1.Series["Series1"].XValueMember = "Time";
         chart1.Series["Series1"].YValueMembers = "Score";
         chart1.DataBind();

         index++;

         Application.DoEvents();
     }

I know that Application.DoEvents() always seems to be frowned upon, but I use it in order to interrupt the thread so that I can see the waveform every point that is generated. It takes almost 30 seconds to graph 750 points. My ultimate goal is to graph 1000 points in 1 second. Any suggestions?

Thanks
GabeA
Posted
Updated 10-Aug-11 4:04am
v2
Comments
[no name] 10-Aug-11 10:05am    
FORMAT your code snippet

I'm not sure if that charts are built to do that kind of tasks very fast. I'll suggest you to try to use another charts, and make some test with performance and evaluate your requirements.
 
Share this answer
 
Comments
Philippe Mori 10-Aug-11 23:53pm    
Effectively for large chart, you would have to select a chart designed to handle large quantities of point and/or real time data. But here the number of point is relatively small and the time are really big... so there must be another problem.
I would think that the main problem is that you do too much long operation inside the loop.

You are calling chart1.DataBind() and Application.DoEvents() on each iteration whichy will make you application deadly slow as it is.

Generally, it make not much sense to use Application.DoEvents(). For an oscilloscope, it would make more sense to use a timer and update the chart each time the timer fire.
 
Share this answer
 
Comments
GabeA 17-Aug-11 11:24am    
When I use a timer instead of DoEvents to update the chart and graph, the results are the same. The fastest I can plot is 750 points in 30 seconds. It seems like that is the fastest that the plotter can plot and update. Any other ideas for making a fast updating oscilloscope?
Philippe Mori 17-Aug-11 11:49am    
I am not sure that I follow you. 750 points total (25 refresh of 30 points) would be very slow while 750 refresh of 30 points would be relatively fast. Be more precise and tell us how many points are displayed on each refresh and how many refresh occurs during that 30 seconds period.
GabeA 17-Aug-11 13:13pm    
The way the code works is that it graphs 30 points, then it takes in the next point (point 31) and plots it, at the same time, it removes the first point, and repeats this, thus creating a "traveling" waveform. I do this for 750 points, so in the end the graph only shows the last 30 points, and the graph never shows more than 30 points. The refresh is after each point, but it removes the point farthest left of the graph while it adds the new point.
Philippe Mori 17-Aug-11 13:28pm    
Thus you are doing 750/30 = 25 refresh per second. I think it is "fast" for a control that is not intended to display real-time data.

You might optimize it a bit by selecting options that help performance like, "fast-line", single pixel thickness, no tooltips, fixed scale...

But for serious real-time chart, you might have to buy a component specifically designed for that and it might be quite expansive.

Performance can vary widely between chart components and even within one component depending on the selected options.
Thus, I would suggest that after having tried "easy" optimizations, that you try to download charts from other vendors and test their performance. You might have a good hint from the sample they have on thier web site.

You might also try some WPF charts (hardware accelerated).

Finally and particulary with the "timer" alternative, you might simply merge a few refresh. Say that you would be satisfied with 15 fps, then you might have a timer that tick every 65 milliseconds or so and add all points received since last update.

The advantage of this method is that it would somehow automatically adjust with the computer speed (or load) when the computer has not enough power. Thus a slow computer migth add 3 new points and remove 3 old points (on average) for each refresh.
 
Share this answer
 
Comments
GabeA 17-Aug-11 14:21pm    
Ok, sounds good, thank you for the help.
GabeA 17-Aug-11 14:49pm    
Also, I was able to speed it up a lot by just setting the data table as hidden, so that only the graph is shown in the GUI. When this was done, I was able to graph the 750 points in 4 seconds.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900