Click here to Skip to main content
15,879,096 members
Articles / Desktop Programming / MFC
Article

Oscilloscope/StripChart Control

Rate me:
Please Sign up or sign in to vote.
4.75/5 (42 votes)
17 Jan 2000 317.4K   14K   164   68
A control that graphically displays numerical information

Sample Image - oscope.gif

Introduction

Despite the rather vast array of tools that are provided with Visual C++ one area that seems to be somewhat ignored is that which is concerned with graphical data analysis. There seem to be millions of ways of showing, sorting and relating text and numbers but no good ways of graphically displaying them. (In my world, one decent plot is worth a thousand CRichEditCtrl's.) Thus I'm always looking for a better way of graphically presenting information in an easily interpreted manner.

Historically, I've shown "live" gauge readings through the constant updating of a numerical value in a read-only edit control. However, apart from making the user seasick, this approach does not easily reveal subtle patterns or trends. This level of interpretation requires an "oscilloscope" or "strip chart" style display of historical data.

This OscopeCtrl is based on the bitmap shifting concept used in Ken C. Len's Histogram Control. It provides significant enhancements through the display of scaling information and plotting of double precision values. The user implementation is described below.

  1. In the control's owner (for example dialog) insert a dummy picture control.
  2. Size the picture border to be the desired size of the COScopeCtrl. Name the control something that sounds technical, like "IDC_OSCOPE".

  3. Insert the control in the owner class.
  4. Add a member variable of type COScopeCtrl.
    class CTestOScopeDlg : public CDialog
    {
      // Construction
      ...
    protected:
      COScopeCtrl m_OScopeCtrl;
      ...
    }

  5. Create the control.
  6. Setup the control, placing it in the picture rectangle from step #1.
    BOOL CTestOScopeDlg::OnInitDialog()
    {
      ...
      // determine the rectangle for the control
      CRect rect;
      GetDlgItem(IDC_OSCOPE)->GetWindowRect(rect) ;
      ScreenToClient(rect) ;
    
      // create the control
      m_OScopeCtrl.Create(WS_VISIBLE | WS_CHILD, rect, this) ; 
      ...
    }

  7. Personalize the Control
  8. Set the vertical range, background color, grid color and plot color.
    BOOL CTestOScopeDlg::OnInitDialog()
    {
      ...
      // determine the rectangle for the control
      CRect rect;
      GetDlgItem(IDC_OSCOPE)->GetWindowRect(rect) ;
      ScreenToClient(rect) ;
    
      // create the control
      m_OScopeCtrl.Create(WS_VISIBLE | WS_CHILD, rect, this) ; 
    
      // customize the control
      m_OScopeCtrl.SetRange(-10.0, 10.0, 1) ;
      m_OScopeCtrl.SetYUnits("Volts") ;
      m_OScopeCtrl.SetXUnits("Samples (Windows Timer: 100 msec)") ;
      m_OScopeCtrl.SetBackgroundColor(RGB(0, 0, 64)) ;
      m_OScopeCtrl.SetGridColor(RGB(192, 192, 255)) ;
      m_OScopeCtrl.SetPlotColor(RGB(255, 255, 255)) ;
      ...
    }

  9. Use the control.
  10. Call the COScopeCtrl::SetPosition function with the the data value to be appended to the plot. Subsequent calls to SetPosition will continue to shift the plot to the left.
    ...
    m_OScopeCtrl.AppendPoint(nRandom);
    ...
    

  11. Other Considerations
    1. Color Selection
    2. The OScopeCtrl optimizes the drawing of the plot for the sake of speed and smoothness of animation. In this approach, memory based device contexts and associated bitmaps are constructed for 1.) the underlying grid and 2.) the plot line segments. These two bitmaps are combined in another memory based display context to avoid flicker. During the painting of the control, the grid is BitBlt'd (as a SRCCOPY), the plot is then added by BitBlt'ing it as a "pattern" via SRCPAINT. The result is then BitBlt'd to the display (as SRCCOPY). (See the COScopeCtrl::OnPaint function for details.)

      This approach is quite clean and fast for light colored plots and light colored grids which are to be displayed on dark colored backgrounds. For some light colored backgrounds, the plot and/or grid may not be displayed do to the OR'ing nature of the SRCPAINT-style BitBlt.

    3. Font Selection
    4. The COScopeCtrl draws the axis information text in a font which is 14 pixels high. The attributes for both the horizontal and vertical fonts can be modified in the COScopeCtrl::InvalidateCtrl() function.

    5. Further Customization
    6. The COScopeCtrl constructor sets a most of the default behavior and is thoroughly documented. These values can be modified to achieve different styles of displays.

      Try this one for fun.

      To give the feel of an old green oscilloscope display, try these settings:
      m_OScopeCtrl.SetRange(-10, 10, 1) ;
      m_OScopeCtrl.SetYUnits("Volts") ;
      m_OScopeCtrl.SetXUnits("Samples (Windows Timer: 100 msec)") ;
      m_OScopeCtrl.SetBackgroundColor(RGB(0, 64, 0)) ;
      m_OScopeCtrl.SetGridColor(RGB(192, 255, 192)) ;
      m_OScopeCtrl.SetPlotColor(RGB(255, 255, 255)) ;
      

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Digital Metrology Solutions, Inc.
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Added Y-Axis Autoranging Pin
ronpeters29-Dec-03 10:34
ronpeters29-Dec-03 10:34 
GeneralRe: Added Y-Axis Autoranging Pin
worndown15-Apr-04 9:33
worndown15-Apr-04 9:33 
GeneralRe: Added Y-Axis Autoranging Pin
worndown15-Apr-04 10:13
worndown15-Apr-04 10:13 
GeneralRe: Added Y-Axis Autoranging Pin
Gilrock16-Apr-04 8:10
Gilrock16-Apr-04 8:10 
GeneralRe: Added Y-Axis Autoranging Pin
worndown16-Apr-04 10:34
worndown16-Apr-04 10:34 
GeneralRe: Added Y-Axis Autoranging Pin
Gilrock17-Apr-04 5:03
Gilrock17-Apr-04 5:03 
GeneralRe: Added Y-Axis Autoranging Pin
Gilrock17-Apr-04 5:24
Gilrock17-Apr-04 5:24 
GeneralRe: Added Y-Axis Autoranging Pin
worndown19-Apr-04 5:07
worndown19-Apr-04 5:07 
There is a HUGE difference. As huge as a difference between different runtimes (especially debug and release versions). Operators new and delete not necessary have to be implemented in terms of free and malloc. Here example of code, which hopefully will convince you to use delete[] with new[]. Build release version and run it from the command line. You will see that while 8 objects were constructed (and each allocated 128 bytes in constructor), only 1 was properly deleted (destructor was called). You have 128 x 7 = 896 bytes memory leak. Now add [] after delete, recomplile and see the difference.

=========== cut here ================

#include <iostream>
using std::cout;
using std::endl;

class CTest
{
public:
CTest() : m_p(new char[128])
{
cout << "Constructor" << endl;
}
~CTest()
{
cout << "Destructor" << endl;
delete[] m_p;
}
private:
char* m_p;
};

int main(int argc, char* argv[])
{
CTest* p = new CTest[8];
delete p; // OOPS!!!
return 0;
}

=========== cut here ================

az0te
GeneralRe: Added Y-Axis Autoranging Pin
Gilrock19-Apr-04 5:44
Gilrock19-Apr-04 5:44 
GeneralRe: Added Y-Axis Autoranging Pin
worndown19-Apr-04 9:20
worndown19-Apr-04 9:20 
GeneralRe: Added Y-Axis Autoranging Pin
kezhu30-Mar-08 21:41
kezhu30-Mar-08 21:41 
GeneralRe: Added Y-Axis Autoranging Pin
chen_ylu21-Feb-08 21:43
chen_ylu21-Feb-08 21:43 
GeneralRe: Added Y-Axis Autoranging Pin
Ikey_Boy18-Jan-07 1:50
Ikey_Boy18-Jan-07 1:50 
QuestionHow to port This MFC code to WTL code ? Pin
Atomic Jo5-May-03 19:07
Atomic Jo5-May-03 19:07 
GeneralRefreshing problems Pin
TaylorLee15-Jul-02 14:28
TaylorLee15-Jul-02 14:28 
GeneralSome colors cannot be shown Pin
kezhu5-Feb-02 19:11
kezhu5-Feb-02 19:11 
GeneralRe: Some colors cannot be shown Pin
Mark C. Malburg6-Feb-02 1:56
Mark C. Malburg6-Feb-02 1:56 
GeneralRe: Some colors cannot be shown Pin
kezhu6-Feb-02 11:10
kezhu6-Feb-02 11:10 
GeneralRe: Some colors cannot be shown Pin
xradish14-Jul-03 19:16
xradish14-Jul-03 19:16 
GeneralRe: Some colors cannot be shown Pin
kezhu29-Sep-03 19:12
kezhu29-Sep-03 19:12 
Questionmemory leak? Pin
13-May-01 4:37
suss13-May-01 4:37 
AnswerRe: memory leak? Pin
18-Feb-02 19:22
suss18-Feb-02 19:22 
GeneralRe: memory leak? Pin
Anonymous1-Oct-03 18:00
Anonymous1-Oct-03 18:00 
GeneralRe: memory leak? Pin
baldo12-Jan-04 23:59
baldo12-Jan-04 23:59 
AnswerRe: memory leak? Pin
kezhu30-Mar-08 21:49
kezhu30-Mar-08 21:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.