Click here to Skip to main content
11,639,554 members (69,333 online)
Click here to Skip to main content

Oscilloscope/StripChart Control

, 17 Jan 2000 253.8K 11.1K 154
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

Mark C. Malburg
Software Developer (Senior) Digital Metrology Solutions, Inc.
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionAdd additional lines Pin
Member 86059969-Mar-15 8:38
memberMember 86059969-Mar-15 8:38 
QuestionLicense ? Pin
WernerSlabon29-Jan-14 5:46
memberWernerSlabon29-Jan-14 5:46 
AnswerRe: License ? Pin
Mark C. Malburg30-Jan-14 1:21
memberMark C. Malburg30-Jan-14 1:21 
QuestionI'm confused Pin
Member 809408518-Jul-11 19:47
memberMember 809408518-Jul-11 19:47 
AnswerRe: I'm confused Pin
Mark C. Malburg19-Jul-11 1:20
memberMark C. Malburg19-Jul-11 1:20 
GeneralRe: I'm confused Pin
Member 809408523-Jul-11 17:29
memberMember 809408523-Jul-11 17:29 
Generalcompile error Pin
Member 46267788-Feb-11 4:04
memberMember 46267788-Feb-11 4:04 
GeneralRe: compile error Pin
Mark C. Malburg10-Feb-11 5:21
memberMark C. Malburg10-Feb-11 5:21 
GeneralCompile Error [modified] Pin
ringram207714-Feb-07 2:28
memberringram207714-Feb-07 2:28 
GeneralLeft to Right Pin
muratmaman14-Jan-07 3:47
membermuratmaman14-Jan-07 3:47 
GeneralRe: Left to Right Pin
kezhu27-Mar-08 21:49
memberkezhu27-Mar-08 21:49 
GeneralBug Report [Trifle Bug] Pin
featured22-Dec-05 3:02
memberfeatured22-Dec-05 3:02 
QuestionVery Important Question(Help me) Pin
VirtualMemory14-Dec-05 10:36
memberVirtualMemory14-Dec-05 10:36 
GeneralMultiple Scopes Pin
cfry7725-Jul-05 10:22
membercfry7725-Jul-05 10:22 
GeneralRe: Multiple Scopes Pin
yeol23812-Dec-05 13:59
memberyeol23812-Dec-05 13:59 
GeneralkweHow to add 3 data in one chart Pin
rahmon3-Jul-05 23:49
memberrahmon3-Jul-05 23:49 
QuestionPerformance? Pin
Astrona16-Jun-05 7:15
memberAstrona16-Jun-05 7:15 
QuestionHow to use this control in C# Pin
Anonymous30-May-05 19:08
sussAnonymous30-May-05 19:08 
GeneralHelp Pin
g92769-Sep-04 22:15
memberg92769-Sep-04 22:15 
GeneralOn a frame window Pin
Larry Dobson4-Jun-04 10:45
memberLarry Dobson4-Jun-04 10:45 
GeneralAdded Y-Axis Autoranging Pin
Gilrock13-Jul-03 17:04
memberGilrock13-Jul-03 17:04 
GeneralRe: Added Y-Axis Autoranging Pin
Gilrock16-Jul-03 19:47
memberGilrock16-Jul-03 19:47 
I was asked for my changes so I figured I'd also post what I did. Feel free to make any suggestions or let me know about any problems with this.

Added the following variables to the header file:
class COScopeCtrl : public CWnd
{
  ...
protected:
  ...
  <code>int m_nBufferSize;
  int m_nDataIndex;
  bool m_bWrapped;
  double *m_pdblDataPoints;</code>
}
Added the following to the end of the constructor code:
COScopeCtrl::COScopeCtrl()
{
  ...
<code>  m_pdblDataPoints = NULL;
  m_nDataIndex = 0;
  m_bWrapped = false;</code>
 
}  // COScopeCtrl
Update the SetRange() method:
void COScopeCtrl::SetRange(double dLower, double dUpper, int nDecimalPlaces)
{
  ASSERT(dUpper > dLower) ;
 
  m_dLowerLimit     = dLower ;
  m_dUpperLimit     = dUpper ;
  m_nYDecimals      = nDecimalPlaces ;
  m_dRange          = m_dUpperLimit - m_dLowerLimit ;
  m_dVerticalFactor = (double)m_nPlotHeight / m_dRange ; 
  
 
  // clear out the existing garbage, re-start with a clean plot
  InvalidateCtrl() ;
  
 
  <code>if(m_bWrapped )  // see if data points have wrapped
  {
    m_dPreviousPosition = m_pdblDataPoints[(m_nDataIndex+1)%m_nBufferSize];
    for( int i = ((m_nDataIndex+1)%m_nBufferSize); i != m_nDataIndex; i = (i + 1)%m_nBufferSize)
    {
      m_dCurrentPosition = m_pdblDataPoints[i];
      DrawPoint() ;
    }
  }
  else if( m_nDataIndex != 0 )  // see if any data points have been stored
  {
    m_dPreviousPosition = m_pdblDataPoints[0];
    for( int i = 0; i < m_nDataIndex; i++)
    {
      m_dCurrentPosition = m_pdblDataPoints[i];
      DrawPoint() ;
    }
  }
  Invalidate() ;</code>
Added the following to the end of InvalidateCtrl:
void COScopeCtrl::InvalidateCtrl()
{
  ...
 
<code>  if(m_nPlotWidth/m_nShiftPixels != m_nBufferSize )
  {
    if( m_pdblDataPoints != NULL )
      delete m_pdblDataPoints;
    m_nBufferSize = m_nPlotWidth/m_nShiftPixels;     // autosize the buffer to cover
    m_pdblDataPoints = new double[m_nBufferSize];    // the width of the strip chart
    m_nDataIndex = 0;
    m_bWrapped = false;
  }</code>
And finally update AppendPoint:
double COScopeCtrl::AppendPoint(double dNewPoint)
{
  // append a data point to the plot
  // return the previous point

  double dPrevious ;
 
<code>  m_pdblDataPoints[m_nDataIndex++] = dNewPoint;
  m_nDataIndex %= m_nBufferSize;
  if(m_nDataIndex == 0)
    m_bWrapped = true;</code>
 
  dPrevious = m_dCurrentPosition ;
  m_dCurrentPosition = dNewPoint ;
  DrawPoint() ;
 
  Invalidate() ;
 
  return dPrevious ;
 
} // AppendPoint

Enjoy,
Gil Jones
GeneralRe: Added Y-Axis Autoranging Pin
ronpeters4-Dec-03 6:31
memberronpeters4-Dec-03 6:31 
GeneralRe: Added Y-Axis Autoranging Pin
Gilrock4-Dec-03 7:04
memberGilrock4-Dec-03 7:04 
GeneralRe: Added Y-Axis Autoranging Pin
ronpeters4-Dec-03 15:24
memberronpeters4-Dec-03 15:24 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150731.1 | Last Updated 18 Jan 2000
Article Copyright 2000 by Mark C. Malburg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid