Click here to Skip to main content
15,308,404 members
Articles / Programming Languages / C++
Posted 12 Apr 2001


82 bookmarked

CHistogramCtrl, A Windows 2000 Like Histogram Control

Rate me:
Please Sign up or sign in to vote.
4.94/5 (22 votes)
12 Apr 2001CPOL5 min read
A histogram control that is used to plot specified values within the defined range (limits)

Initial Idea

The initial idea for doing this code was put into my mind when my love asked me to go to a clinic in order to check out my heart. You know, doctors have got some sort of plotters that plot the information obtained from the heart on a piece of paper as a histogram. The idea came into my mind from there! This control is sincerely dedicated to my love, Cindy.

How to Use

To use the control, you import the header file as well as the .cpp file attached to this article. Then you simply open the dialog editor and place a rectangle on your dialog. We call this static control IDC_STATIC_HISTOGRAM. Thereafter, you instantiate an object of the CHistogramCtrl class, say m_ctrlHistogram, as a data member of your dialog class.

In the message handler of the WM_INITDIALOG message, you get the area of the static rectangle you already placed on your dialog box and you simply create the Histogram Control as follows:

// Declaring a local variable
CRect rect;
// Get the rectangular coordinates of the rectangle 
// we already placed on dialog
// Convert to the client coordinates
// Create and place the histogram control on screen
m_ctrlHistogram.Create(WS_VISIBLE | WS_CHILD
      | WS_TABSTOP, rect, this, IDC_STATIC_HISTOGRAM);

After doing this, the histogram's window is shown on screen in the default colors assigned to the control within the constructor of the class. For example, the background color of the control is held in the m_crBackGround member variable of the class and is assigned to the color black in the constructor of the CHistogramCtrl. The other member variable, m_crGrids holds the grid color and is assigned to RGB(0, 130, 66) in the constructor of the class.

What if we would like to change the grid color to red? Shall we alter the mentioned variables within the constructor? Of course, not! To do so, I've created a method, SetGridsColor, prototyped as follows:

BOOL CHistogramCtrl::SetGridsColor(COLORREF cr);

Where cr is the grids' color reference. For example, you can change the color of grids to red this way:

//Change the color of grids to red
m_ctrlHistogram.SetGridsColor(RGB(255, 0, 0));

Here are the other methods of the class that can be used to change the default behavior of the control:

BOOL CHistogramCtrl::SetBkColor(COLORREFcr);
void CHistogramCtrl::SetPen(int nWidth, COLORREF crColor);
void CHistogramCtrl::SetRange(UINT uLower, UINT uUpper);
CHistogramCtrl::SPEED SetSpeed(CHistogramCtrl::SPEED uSpeed);
void CHistogramCtrl::SetPos(UINT uPos);</td>

Although the method names clarifies their purposes, it makes sense if we have a quick look at what each one is useful for.

The first above-mentioned method, SetBkColor, is used to set the background color of the control. The default value is black, i.e., RGB(0, 0, 0), declared in the constructor of the class.

The second method is used to set the control's pen, the one that's used to draw the histogram. The first parameter, nWidth, is used to set the width of the line. The second parameter, crColor, is a COLORREF that sets the color of the pen. If you don't use this function, the default values are used, 1 as the pen width, and RGB(0, 255, 0) as the pen's color.

The third method, SetRange, is used to set the upper and lower limits (range) of the control. The valid range for these arguments is as follows:

  • uLower > 0
  • uUpper > 0
  • uLower < uUpper

Please note that the range will be set to 1-100 if you don't specify a range - i.e., you can plot numbers between 1 to 100.

Now let's move from SetRange to the next method, SetSpeed. This is used to control the refresh speed of CHistogramCtrl. Let me say that the window contents are shifted 3 pixels to the left within the specified intervals. There are 4 predefined intervals declared as an enum as follows:

  1. LOW_SPEED (3000 ms)
  2. NORMAL_SPEED (1000 ms; default value)
  3. HIGH_SPEED (500 ms)
  4. IDLE (0 ms)

To use this function, all you've to do is as follows:

// Change the speed of scrolling

This way, the contents of the control is shifted 3 pixels to the left at 500ms intervals.

Now it is time to turn our attention to the last public method of the class, SetPos, declared as follows:

void CHistogramCtrl::SetPos(UINT uPos);

This member function does the actual plotting. For example, to set the current position of the histogram to 26, you can easily write the following statement:

//Set the current position of the histogram to 26

Now imagine what happens if you set the position of the histogram to 87 ten seconds later. You'll see a histogram as follows:

In other words, there will be no change in the line if we don't feed the control with new values. But what happens if we feed the control more than it can show? For example, imagine that we've set the speed of the control to HIGH_SPEED (500ms) and we feed the control 20 times within the 500ms. Which value is shown at the end of the first 500ms?

To solve this problem, I created a CList as a private data member of CHistogramCtrl. Whenever you call SetPos, I append the value you provide to the mentioned list. Whenever I want to refresh the control (at the end of each 500ms in our example), I process the list and obtain the average value of the current values within the list. To clarify what I'm talking about, imagine that we set the current position of the control to the following values within the first 500ms:

10, 87, 19, 54, 63, 74

When this time elapses, I process the current values within the list and calculate the average of the list members:

10 + 87 + 19 + 54 +63 + 74 = 307
307 / 6 = 51.1

And I show 51.1 as the current value... Got it?

This is done via one of the private methods of the class called, GetAverage, that returns the average of numbers within the mentioned list. In some cases, you have to change the mechanism of calculating the current position of the control within the refresh intervals with your desired one. For example, I've used the variance of the numbers instead of their average in my latest project. Please note that the function is called within the DrawLine method to obtain a value to show at the end of each period.

Since the source code is straight forward and easy to understand this article is finished now. Please feel free to send your comments, questions and/or suggestions about this control by leaving a comment below. Aloha!


  • 13th April, 2001: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mehdi Mousavi
United States United States
Mehdi Mousavi is a professional computer programmer. He has written many programs using C, C++, MFC, Win32, COM, DCOM, JavaScript, ASP, HTML, DHTML, SQL, C# and MC++.

Comments and Discussions

GeneralThanks a lot Pin
chao88282768-Nov-12 14:48
Memberchao88282768-Nov-12 14:48 
GeneralQuestion Pin
mycareer3-Mar-11 19:30
Membermycareer3-Mar-11 19:30 
GeneralMy vote of 5 Pin
hpghy2-Dec-10 21:12
Memberhpghy2-Dec-10 21:12 
GeneralMy vote of 5 Pin
liushiyao25-Jul-10 23:21
Memberliushiyao25-Jul-10 23:21 
Generalgood Pin
mayasu8-Jan-10 1:26
Membermayasu8-Jan-10 1:26 
QuestionMaking Invisible Pin
franzcatch29-Jul-06 16:32
Memberfranzcatch29-Jul-06 16:32 
Questioncannot find Pin
franzcatch29-Jul-06 5:43
Memberfranzcatch29-Jul-06 5:43 
I am using Microsoft Visual C++ .NET and when I try to compile it tells me it cannot find <chplot.h> ...
Where can I get this #include file???
AnswerRe: cannot find Pin
franzcatch29-Jul-06 16:27
Memberfranzcatch29-Jul-06 16:27 
GeneralClearing the control Pin
Neta77722-Feb-04 0:51
MemberNeta77722-Feb-04 0:51 
GeneralRe: Clearing the control Pin
jinglebell1-Jan-06 4:43
Memberjinglebell1-Jan-06 4:43 
GeneralRe: Clearing the control Pin
flagmaxiden16-Feb-11 17:09
Memberflagmaxiden16-Feb-11 17:09 
GeneralOnSize problem Pin
chito28-Sep-03 14:33
Memberchito28-Sep-03 14:33 
GeneralRe: OnSize problem Pin
jinglebell1-Jan-06 4:56
Memberjinglebell1-Jan-06 4:56 
GeneralGDI Resource Leaks on Win9x Pin
Abin23-Sep-03 17:01
MemberAbin23-Sep-03 17:01 
GeneralRe: GDI Resource Leaks on Win9x Pin
Michael Dunn23-Sep-03 18:23
sitebuilderMichael Dunn23-Sep-03 18:23 
GeneralRe: GDI Resource Leaks on Win9x Pin
Abin24-Sep-03 0:29
MemberAbin24-Sep-03 0:29 
GeneralRe: GDI Resource Leaks on Win9x Pin
Glyn16-Feb-04 4:59
MemberGlyn16-Feb-04 4:59 
Generaltimer Pin
Member 16840529-May-03 3:31
MemberMember 16840529-May-03 3:31 
GeneralRe: timer Pin
bli@copepod.de12-Mar-07 6:00
Memberbli@copepod.de12-Mar-07 6:00 
Generalto .Net platform Pin
pocalypse18-Jan-03 20:08
Memberpocalypse18-Jan-03 20:08 
Generaldear mmehdi Pin
mohammadsh5-Oct-02 18:06
Membermohammadsh5-Oct-02 18:06 
GeneralRe: dear mmehdi Pin
Peter Moonen23-Nov-02 4:08
MemberPeter Moonen23-Nov-02 4:08 
GeneralRe: dear peter Pin
mohammadsh6-Dec-02 6:57
Membermohammadsh6-Dec-02 6:57 
GeneralThis is what I had been looking for! Pin
Anonymous29-Sep-02 16:10
MemberAnonymous29-Sep-02 16:10 
Generalhistogram.h Pin
TaylorLee15-Jul-02 8:47
MemberTaylorLee15-Jul-02 8:47 

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.