Simple Real-time Class for Advanced Charts and Plotting


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

Click here for larger image

Environment: VC6, NT4 SP5, Windows 95b/98

In real time applications it is necessary to have some graphics functions which can make plotting using dynamic engineering data. The CRealTime class provide such functions which have been implemented using SDK rather than MFC.

1. Features:

(1). Use permanent circular buffers to stored data rather than resize data storage or memory copy 
(2). Scroll through time automatically
(3). Multi-line display with most property features at different time levels
(4). Multiple legends and data annotations
(5). Scroll and zooming
(6). Memory Device Context drawing
(7). Printing

2. History

From Nov. 1999 to May 2000, I involved to write a multithread real-time simulation application using MFC on Windows NT. It communicates with several processes running on different machines through TCP/IP. One of its features is real-time plotting, logging, and periodic background printing with data received from other processes.

However, the provided class, which has been rewritten using Win32 API completely rather than MFC, only implements part of the original MFC CRealTime class. Due to time constraints, logging, periodic background printing, read and write locking, and multithread features have not been rewritten. Therefore, those features are not included in the above class.

You are free to use or modify this class and to use it in any of your applications. But I make no guarantees about this class suitability for use. If there is any problem with anything I wrote, I will certainly try to help solve it.

3. Source Code

(1). ClassArr.h: template based class to implement dynamic array. It is similar to CArray in MFC but independent on MFC.

(2). Graphics.h and Graphics.cpp: property definition and drawing framework description.

(3). Random.h and Random.cpp: a portable subtractive pseudorandom number generator, which is faster than rand() in C Run time. It generates and simulates new data coming in.

(4). RealTime.h and RealTime.cpp: CGraphics derived component and contains most of the real-time plot source.

(4). Str.h and Str.cpp: Simple string class.

4. Demo Project

The RTDemo, which is an MFC MDI application, demonstrates how to use CRealTime. It uses a splitter window, which includes two views (one for plot and the other for legends and data annotations). Please note that running RTDemo needs MSFlex grid 6.0 control, which comes with VC6.0.

From the Edit menu, you can test open Insert, Delete, and Modify a line at run time. The Property menu includes grid, boundary, title, memory drawing, and back color properties. Kill and restore timer can be found in the View menu. Right click in the top view, the Timer Speed menu will popup, and you can select one of 10 timer speed levels. Also you can drag a vertical line on the slider bar by clicking the left mouse button.

About the Author

I am a senior software engineer for TechComm Simulation Pty Ltd., Sydney, Australia, where I specialise in Client/Server and TCP/IP programming on Unix and Windows NT. My e-mail address is yuantuh@techsim.com.au.


Download demo project - 63 Kb
Download source - 18 Kb


  • Question about CRealTime class. From Eugene Buzin; Russia.

    Posted by Eugene on 02/10/2019 02:17pm

    Hello, Yuantu. I'm Eugene Buzin. I'm C#, C++ programmer from Russia. Yesterday (February 9, 2019) I've downloaded source code of your CRealTimeClass from https://www.codeguru.com/cpp/controls/controls/chartingandanaloguecontrols/article.php/c2223/Simple-Realtime-Class-for-Advanced-Charts-and-Plotting.htm. I have not fully understood it yet and, therefore, I have some questions. 1) Can I use this code as a simple library for drawing real-time signal graphs in the Win 32 API С++ application? For example, to output the graph of the original signal, and then to output the result of the discrete Fourier transform of the signal? I do not need a rich palette of colors and thickness of lines, and the like. I just need to display a signal graph and put scale grid on it for setting the scale. And that's all. 2) Are the CRealTime and CGraphics classes enough for me to use your code as a library to draw signal graphs in real time? Or do I still need the other classes, such as: CClassArray, CStr and RndGen? Thank you very much for the code posted on the Codeguru site. I hope that I can use it for my purposes, mentioned above. Yours sincerely Eugene Buzin.

  • Bug

    Posted by Enzo69 on 12/14/2008 04:24pm

    In your demo, SetData comes before m_Graph.UpdateTimeRange and in the latter you handle the roll out of m_nBegin; so there is the danger that SetData use an index out of range. A possible solution is to move the check in AddYValue in the class CRealTime ...

  • Another open-source real-time plotter can be found at http://www.live-graph.org

    Posted by gragus on 05/24/2007 01:11pm

    Another tool for real-time plotting is LiveGraph. It comes with a well commented open source and a tutorial, so it is a good demo on how to code this sort of thing yourself. See http://www.live-graph.org.

  • Please help me~

    Posted by drumist on 03/26/2007 01:30pm

    Hi~ I downloaded your application and source code. It is exactly what i want. But this code is difficult to understand... Could you give some help? (some kind of manual or document) I'll wait for.. Thanks

  • ActiveX Control

    Posted by Legacy on 08/14/2003 07:00am

    Originally posted by: Ken T

    You've got a great program, but I was wondering whether it would be possible for you to make an activeX control out of it. I think that would be awesome!!!

  • Good code... minor bug

    Posted by Legacy on 02/24/2003 08:00am

    Originally posted by: Paul

    I liked your code and after some digging around and some tweaks, I was able to understand it pretty well. I successfully converted it to Unicode for my application.

    I did run across a minor glitch that was tough to get to the bottom of. In the CRealTime class definition, you dimensioned m_sTempStr to 32. If you set the CyclesPerSec to some number, say 2, the ShowTime() will run over this limit when cTime needs 2 digits to convey hours, minutes, and seconds. I dimensioned it to 64 for my purposes because I altered PrintTime to send a longer string and used %02d instead of %2d when printing hh:mm:ss.

    Again, great code. Thanks for the help!

  • Bug report

    Posted by Legacy on 01/28/2003 08:00am

    Originally posted by: Hans Olav Nybo


    I think I have found a small bug in the code:

    In CALine::AddValue:

    New data values are added to the end of the data buffers until 'end == m_nSize'. Then 'begin' is used as index into the buffers. The first time the buffer is full, everything works fine. The second time, however, it goes wrong:

    'end'and 'begin' are both updated in CRealTime::UpdateTimeRange:

    When 'begin == m_nDataPerLine', 'begin' is set to zero, and m_pValueTime is updated like this: m_pValueTime[begin++]. Thus, begin is incremented to 1.

    Because of that 'begin' = 1 the next time CALine::AddValue is called and m_pValues[0] are never updated.

    I solved the problem by adding this test to AddValue:

    if (begin == m_nSize)
    m_pValues[0].YValue = value;

  • Plotting Missed points

    Posted by Legacy on 01/15/2003 08:00am

    Originally posted by: Kala Bazari

    First let me say that this is a very well written application.

    In my opinion, one thing that is missing is that this one does not plot X-Axis accurately. If you happen to get a point that is delayed by a fraction (say received at 1.1 seconds instead of 1 second), this graph still plots the point at 1 second. Similarly, if we miss a point and get the next point at 2 seconds, this graph will still plot the point at 1 second. Basically, it is still a one dimentional plot and not a true 2-D plot.

    Thanks for great code


  • visual worker

    Posted by Legacy on 12/04/2002 08:00am

    Originally posted by: xfxty

    I had downloaded your source code ,very good,Thanks.

  • RT.shtml

    Posted by Legacy on 05/29/2002 07:00am

    Originally posted by: Konstantin

    Thanks! Very useful thing!

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date