• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2221
  • Last Modified:

Real-time C# plotting

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
jfburke1
Asked:
jfburke1
  • 5
  • 4
1 Solution
 
jfburke1Author Commented:
Sorry about the MFC zone reference.  As the question indicates, I am a beginner...
0
 
Bob LearnedCommented:
If you are using the Paint event to draw, then you can use the Invalidate method to force a paint to redraw.

Bob
0
 
jfburke1Author Commented:


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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Bob LearnedCommented:
How intensive is the plotting algorithm?

Bob
0
 
jfburke1Author Commented:
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
 
Bob LearnedCommented:
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
 
jfburke1Author Commented:
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
 
Bob LearnedCommented:
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
 
jfburke1Author Commented:
Thanks Bob for the solution!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now