Click here to Skip to main content
15,884,176 members
Articles / Desktop Programming / MFC
Article

C2DPushGraph: A Push Graph Control

Rate me:
Please Sign up or sign in to vote.
4.64/5 (36 votes)
24 Apr 20054 min read 134.3K   3.9K   99   22
A push graph control similiar to the graph control located in Microsoft's Task Manager.

Introduction

In past programming experiences, I've often had a desire to display and monitor the rate or performance of some operation on a graph. Whether it be the rate of write operations or the number of content results extracted each second, I always thought a graph similar to the graphs found in Windows XP’s Task Manager (CTRL-Alt-Delete) performance and networking tabs would do the trick well. After a quick search on Google for the graph control clone yielded no relevant results, I decided cloning it myself would be an interesting and beneficial experience.

As time progressed and cloning the control proved itself easy, I decided to add more features and make it completely customizable in nearly every aspect. In the end, as the smoke cleared, I was left face to face with the control this article revolves around. It looked and behaved astonishingly beautiful for being developed with such haste. But who cares about its pastime, let's move on and see how easy it is to implement...

Using the code

If any of you read my last article (CGoogle), you’ll be informed of the fact I’m approaching my articles with a new style. Instead of rambling on and on about a certain feature, I'll attempt to hand you the vital information in a much quicker fashion.

Adding the Control to your Dialog

There are two ways to add the control to your dialog, one being the politically correct way of doing it, and the other being the easy way.

If you're looking for the easy way, first add a static control to your dialog (I typically use the picture control for a frame). Then, give the newly created control the desirable styles and a unique ID. You should then proceed to position and stretch the control to where you want the graph. Now, add a new variable to your dialog class using C2DPushGraph as the type. Finally, call CreateFromStatic from OnInitDialog and pass the control ID as the first parameter, and the parent window as the second (you may use the 'this' pointer if you’re calling CreateFromStatic from the parent dialog class).

The politically correct way involves using the Custom Control item in the resource editor and specifying C2DPushGraph as the class name. From there, you proceed just as you would with any custom control.

Setting the Graph Range

Before continuing, you should set the range of your graph. The range being the minimum and maximum possible magnitudes (for example, a CPU usage graph might have a minimum of 0 and a maximum of 100). To set the range, you can use one or more of the following: SetMaxPeek, SetMinPeek, or SetPeekRange.

Adding a New Line

The two most important parts of C2DPushGraph are obviously being able to add a new line, and updating that line with new data. So we'll start off with adding a line. To add a new line, you call AddLine, which has the following prototype: AddLine( UINT uiLineID, COLORREF crColor ). The first parameter is the unique ID of the line, this is what you'll use later to indicate you're changing a specific line. The second parameter is the color of the line. I strongly suggest using a preprocessor definition or named constant for your line ID, it makes things much easier.

Pushing a New Point to a Line

Unlike a typical graph, to add a new point to a line, you only need to provide the point's magnitude or Y coordinate. Each magnitude should be a number within your graph’s range, though it will be adjusted to the nearest valid number if it is greater or less than the current maximum and minimum range values. To push a new point to a line, you call Push, which has the following prototype: Push( int nMagnitude, UINT uiLineID ).

You'll notice that after pushing a new magnitude to a line, the graph will remain unchanged. This is because you must first push the desired magnitudes to however many lines you wish, then call Update().

Customizing the Graph’s Visual Appearance

Before listing the functions available for setting and retrieving your graph's visual appearance, here is a quick pictorial showing the most basic attributes of the graph control:

Functions Available for Setting Attributes

  • C2DPushGraph::SetBGColor
    void SetBGColor( COLORREF crColor )
    • crColor: The new background color.
  • C2DPushGraph::SetGridColor
    void SetGridColor( COLORREF crColor )
    • crColor: The new grid color.
  • C2DPushGraph::SetGridSize
    void SetGridSize( unsigned short usWidthAndHeight )
    • usWidthAndHeight: The width/height of each grid unit in pixels.
  • C2DPushGraph::SetInterval
    void SetInterval( unsigned short usInterval )
    • usInterval: The distance in pixels between each magnitude point.
  • C2DPushGraph::SetLabelForMax
    void SetLabelForMax( LPCTSTR lpszLabel )
    • lpszLabel: The C-string used to label the graph's maximum magnitude.
  • C2DPushGraph::SetLabelForMin
    void SetLabelForMin( LPCTSTR lpszLabel )
    • lpszLabel: The C-string used to label the graph's minimum magnitude.
  • C2DPushGraph::SetLineColor
    bool SetLineColor( COLORREF crColor, UINT uiLineID )
    • crColor: The line's new color.
    • uiLineID: The line's ID.
  • C2DPushGraph::SetTextColor
    void SetTextColor( COLORREF crColor )
    • crColor: The new color for the minimum and maximum labels.
  • C2DPushGraph::ShowAsBar
    bool ShowAsBar( UINT uiLineID, bool bAsBar )
    • UiLineID: The line’s ID.
    • bAsBar: true if you want the line displayed in bar graph format, false if not.
  • C2DPushGraph::ShowGrid
    void ShowGrid( bool bShow = true)
    • bShow: true if the grid is to be shown, false if not.
  • C2DPushGraph::ShowLabels
    void ShowLabels( bool bShow = true)
    • bShow: true to show the minimum and maximum labels, false to hide them.

Functions Available for Retrieving Attributes

  • C2DPushGraph::GetBGColor
    COLORREF GetBGColor() const
  • C2DPushGraph::GetGridColor
    COLORREF GetGridColor() const
  • C2DPushGraph::GetGridSize
    int GetGridSize() const
  • C2DPushGraph::GetInterval
    unsigned short GetInterval() const
  • C2DPushGraph::GetLabelForMax
    LPCTSTR GetLabelForMax() const
  • C2DPushGraph::GetLabelForMin
    LPCTSTR GetLabelForMin() const
  • C2DPushGraph::GetLineColor
    COLORREF GetLineColor( UINT uiLineID )
    • UiLineID: The line’s ID.
  • C2DPushGraph::GetMaxPeek
    int GetMaxPeek() const
  • C2DPushGraph::GetMinPeek
    int GetMinPeek() const
  • C2DPushGraph::GetTextColor
    COLORREF GetTextColor() const

Contacting the Author

Stuart Konen E-mail address: skonen@gmail.com

Quick shout out: I'm currently looking for employment and am open to contracts.

History

  • April 14, 2005 - Began work on C2DPushGraph.
  • April 20, 2005 - Submitted C2DPushGraph to The Code Project.

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
United States United States
Well first of all, it is fairly obvious my name is Stuart Konen (I'm sure 50% of you just took notice), all of my life I've lived on a farm located in Northern Idaho. What shatters the stereotype of rural residence however, is the fact that I'm very active in the technology and programming worlds. I took up the hobby of programming at age 9, at that point it was little language known as Quick Basic *sigh*. Fast forward another 9 years... (Woah... I just realized that's half of my existence. But that's something I'll have to contemplate later, as I have an autobiography to tell).

Now my experience in programming has improved vastly, I've released various technologies and programs and I'm continuing to pump out concepts and systems that are getting glances from all over the world. My weapon of choice is C++, the core language of the majority of freshly released software, it's sleak, mean, and incrediably powerful. On the side I venture into web application development and site design, where my interest lies in PHP. Over the years my project have included everything from Artificial Intelligence to Web Statistic Tracking systems, but that's the past. What matters is the future... Remember that question we were always asked in grade school? Where did we see ourselves in 10 years. Well that question was asked about 8 years ago in my life, so it looks as though I only have two more for planning stages. In two years I see myself plunging into the world of research, creating my own Research and Development firm, aiming to meet the never-ending need for new and superior software and technology. Soon after becoming a multi-billionare I'll pursue my dream of world domination. Nobody is perfect...

Actually when it comes down to things, the money has no meaning. But there you have it, a 5 minute slice of my thoughts and time... If you have any job opportunities or have the slight urge to initiate a conversation with me, it can be done via email: skonen [at] gmail [dot] com

Comments and Discussions

 
QuestionHow to print ? Pin
Surej9-Mar-11 19:58
Surej9-Mar-11 19:58 
Generalkudos to the author Pin
cupcake.2714-Jul-10 5:19
cupcake.2714-Jul-10 5:19 
GeneralCopyright Pin
JazzMaTazz6-Sep-07 11:09
JazzMaTazz6-Sep-07 11:09 
QuestionSave to image ...? Pin
eldran24-Jul-06 19:06
eldran24-Jul-06 19:06 
GeneralSome error in debug mode on my side.What is wrong??? :( :( Pin
SAMZC6-Sep-05 15:55
SAMZC6-Sep-05 15:55 
GeneralRe: Some error in debug mode on my side.What is wrong??? :( :( Pin
ichiru12-Aug-06 14:15
ichiru12-Aug-06 14:15 
GeneralRe: Several Errors fixed Pin
Nuray9-Aug-05 4:23
Nuray9-Aug-05 4:23 
GeneralRe: Several Errors fixed Pin
Harald Krause9-Aug-05 4:50
Harald Krause9-Aug-05 4:50 
GeneralSeveral Errors fixed Pin
Harald Krause24-Jul-05 8:51
Harald Krause24-Jul-05 8:51 
GeneralRe: Several Errors fixed Pin
Moak4-Aug-05 22:07
Moak4-Aug-05 22:07 
GeneralShowLine Pin
varandas7914-Jul-05 0:58
varandas7914-Jul-05 0:58 
Generalmemory problem on windows ce Pin
marte00115-Jun-05 23:01
marte00115-Jun-05 23:01 
GeneralRe: memory problem on windows ce Pin
Member 210003812-Jul-05 4:32
Member 210003812-Jul-05 4:32 
GeneralRe: memory problem on windows ce Pin
markhaarindewar11-Jan-07 3:19
markhaarindewar11-Jan-07 3:19 
GeneralComes very handy Pin
Moak1-Jun-05 22:00
Moak1-Jun-05 22:00 
GeneralNo grid lines on Release Build Pin
June Hee Lee12-May-05 14:22
June Hee Lee12-May-05 14:22 
GeneralRelease build is fine. Your code. Pin
Stuart Konen13-May-05 12:31
Stuart Konen13-May-05 12:31 
GeneralRe: Release build is fine. Your code. Pin
varandas7917-Jun-05 0:09
varandas7917-Jun-05 0:09 
GeneralRe: Release build is fine. Your code. Pin
phil3502-Jul-05 21:32
phil3502-Jul-05 21:32 
GeneralRe: Release build is fine. Your code. Pin
Michael Pearce18-Oct-05 18:45
Michael Pearce18-Oct-05 18:45 
GeneralLine Thickness Pin
Joseph Dempsey25-Apr-05 10:49
Joseph Dempsey25-Apr-05 10:49 
GeneralRe: Line Thickness Pin
Stuart Konen26-Apr-05 10:01
Stuart Konen26-Apr-05 10:01 

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.