?
Solved

Real-time C# plotting

Posted on 2007-10-04
11
Medium Priority
?
2,131 Views
Last Modified: 2013-12-17
I used onPaint in a C# Windows application (a windows FORM) to draw a 2-D line plot.  Now I would like to refresh that plot periodically with new data.  How could I do that?  Any help would be great.
0
Comment
Question by:jfburke1
  • 5
  • 4
9 Comments
 

Author Comment

by:jfburke1
ID: 20017895
Sorry about the MFC zone reference.  As the question indicates, I am a beginner...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20140290
If you are using the Paint event to draw, then you can use the Invalidate method to force a paint to redraw.

Bob
0
 

Author Comment

by:jfburke1
ID: 20140465


I actually went a new route with the C# code...I now am creating an event and then maually calling that event every 50 ms in order to plot the data that I am acquiring through an optical data acquisition card.  

The goal is to plot the data in real-time by updating a plot every 50 ms.  I know this is possible because I have interfaced with the card in Matlab and have made such a plot.  But now, I want lower level control of the card - thus the C# code.  

The problem is that the plotting is taking forever in C# (much slower than Matlab) using my event triggered straegy - the program is currently non-functional.  Does anyone know why the plotting takes so long in C#? Or, what would be the most efficient manner of sequential "real-time" plotting in C#?

jfburke1
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 20140487
How intensive is the plotting algorithm?

Bob
0
 

Author Comment

by:jfburke1
ID: 20140563
I am not quite sure what you mena by intensive...but we are reading in data sampled at 32000 Hz and want to plot ~6 sec of data on the screen.  That would translate  to about 192,000 samples that have to be plotted every 50 ms.  Realistically, we plot the downsampled data (dowsampled to ~100 HZ) so actually we would only be plotting about 6000 samples each 50 ms iteration, but we would not like to limit our downsampling rate (which affects our processing of the data) by the time it takes to display the data: therefore, we would like to be able plot all 192,000 samples.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20140591
If you are not quite sure what intensive means, then let me say that what you described is intensive.  What are you using to plot these samples?  Are you using a GraphicsPath, DirectX, OpenGL, ...?

Bob
0
 

Author Comment

by:jfburke1
ID: 20157340
Bob,

I really appreciate your help (I saw your EE ranking!).  I am an electrical engineer and have difficulty with designing software so thanks for your patience.  Currently, I am drawing the graph by creating a Graphics object in the Form1 class using CreateGraphics();.  Then I pass this object (I call it g) to my plotting class where I execute the following code:

Pen aPen = new Pen(LineStyle.LineColor, LineStyle.Thickness);
aPen.DashStyle = LineStyle.Pattern;

foreach(ArrayList aL in dc.DataSeriesList)
{
    for(int i = 1; i <  aL.Count ; i++)
        {
              g.DrawLine(aPen, [normalized X-Y Data for new Point[1]],  [normalized X-Y Data for new Point[0]])
        }
 }

 aPenn.Dispose()


Notes:  (1) I have defined a class linestyle to hold the information aboiut the line
             (2) The dc object holds an arraylist of arraylists that each point to a vector of short data types
                   representing the data grabbbed every 50 ms.
             (3) I believe the rendering used for this graphics scheme is GDI+.

Thanks again for your help,

John
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 20157448
John,

1) Using CreateGraphics is called non-persistent graphics

2) For persistent graphics you would need to handle the Paint event, and use the Graphics object passed in the event arguments

3) There is Graphics.DrawLines to draw an entire set of lines

4) There is GraphicsPath that is a container for other graphic objects, such as points, lines, ellipses, etc.  It can be configured and drawn in one step.

References:

Demonstrate the Graphics object's drawing methods in VB .NET
http://www.vb-helper.com/howto_net_drawing_methods.html

Translation:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private PictureBox picHidden = new PictureBox();

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        // Draw a line.
        e.Graphics.DrawLine(Pens.Black, 10, 10, 30, 20);

        // Draw a series of connected lines.
        Point[] pts = { new Point(10, 20), new Point(30, 30), new Point(50, 10), new Point(30, 10), new Point(50, 30) };
        e.Graphics.DrawLines(Pens.Blue, pts);

        // Draw a path.
        // Make the path.
        GraphicsPath graphics_path = new GraphicsPath();
        graphics_path.AddLine(10, 40, 60, 40);
        graphics_path.AddEllipse(60, 30, 30, 20);
        e.Graphics.DrawPath(Pens.Red, graphics_path);

        // Draw an ellipse and a pie slice on it.
        Rectangle rect = new Rectangle(10, 70, 40, 30);
        e.Graphics.DrawEllipse(Pens.White, rect);
        e.Graphics.DrawPie(Pens.Green, rect, 10, 45);

        // Draw an ellipse and an arc on it.
        rect = new Rectangle(60, 70, 40, 30);
        e.Graphics.DrawEllipse(Pens.White, rect);
        e.Graphics.DrawArc(Pens.Green, rect, 10, 45);

        // Draw a polygon.
        pts = new Point[] { new Point(10, 100), new Point(80, 140), new Point(80, 90), new Point(10, 130) };
        e.Graphics.DrawPolygon(Pens.Yellow, pts);

        // Draw a rectangle.
        rect = new Rectangle(10, 150, 50, 20);
        e.Graphics.DrawRectangle(Pens.Brown, rect);

        // Draw some rectangles.
        Rectangle[] rects = { new Rectangle(10, 190, 20, 30), new Rectangle(20, 200, 40, 10), new Rectangle(40, 180, 10, 50) };
        e.Graphics.DrawRectangles(Pens.Aquamarine, rects);

        // Draw some text.
        e.Graphics.DrawString("Hello!", this.Font, Brushes.Chocolate, 10, 250);

        // Draw a Bezier curve.
        PointF[] ptfs = { new PointF(150, 25), new PointF(185, 10), new PointF(200, 50), new PointF(160, 30) };
        e.Graphics.DrawLines(Pens.LightGray, ptfs);
        foreach (PointF ptf in ptfs)
        {
            rect = new Rectangle((int)ptf.X - 2, (int)ptf.Y - 2, 4, 4);
            e.Graphics.DrawRectangle(Pens.LightGray, rect);
        }
        e.Graphics.DrawBezier(Pens.Black, ptfs[0], ptfs[1], ptfs[2], ptfs[3]);

        // Draw a series of connected Bezier curves.
        ptfs = new PointF[] { new PointF(150, 75), new PointF(185, 60), new PointF(200, 100), new PointF(160, 80), new PointF(160, 120), new PointF(200, 140), new PointF(220, 90) };
        e.Graphics.DrawLines(Pens.LightGray, ptfs);
        foreach (PointF ptf in ptfs)
        {
            rect = new Rectangle((int)ptf.X - 2, (int)ptf.Y - 2, 4, 4);
            e.Graphics.DrawRectangle(Pens.LightGray, rect);
        }
        e.Graphics.DrawBeziers(Pens.Orange, ptfs);

        // Draw a closed curve.
        pts = new Point[] { new Point(150, 150), new Point(200, 200), new Point(180, 150), new Point(130, 200) };
        e.Graphics.DrawClosedCurve(Pens.BlueViolet, pts);

        // Draw a curve (open).
        pts = new Point[] { new Point(150, 210), new Point(200, 260), new Point(180, 210), new Point(130, 260) };
        e.Graphics.DrawCurve(Pens.CornflowerBlue, pts);

        // Draw an ellipse.
        rect = new Rectangle(230, 10, 50, 30);
        e.Graphics.DrawEllipse(Pens.DarkMagenta, rect);

        // Draw an image.
        e.Graphics.DrawImage(picHidden.Image, 230, 50);
        e.Graphics.DrawImage(picHidden.Image, 250, 50, 32, 32);
        Rectangle dst_rect = new Rectangle(290, 50, 20, 20);
        Rectangle src_rect = new Rectangle(4, 4, 8, 8);
        e.Graphics.DrawImage(picHidden.Image, dst_rect, src_rect, GraphicsUnit.Pixel);

    }

}

Bob
0
 

Author Comment

by:jfburke1
ID: 20277044
Thanks Bob for the solution!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question