Click here to Skip to main content
Click here to Skip to main content

Bar Graph Control

By , 5 Feb 2007
 

Sample Image - BarGraph.jpg

Introduction

Here is a simple bar graph control class derived from CStatic. It supports displaying real-time data, using any number of bars with different colors, using a scale, both in centimeter and inch, etc. The code uses optimized plotting without flickering. We can change the background color, the text color, the bar color etc., change the scale, and change the display area.

Using the code

  1. Copy the files GraphCtrl.h and GraphCtrl.cpp to your project directory, and add to your project.
  2. Include GraphCtrl.h in files where you need to use this class.
  3. Change CStatic definitions to CGraphCtrl.
  4. Use the functions described below.

Methods

  • void SetUnit(CString pUnit); - Sets the unit type string to be displayed near the Y axis.
  • void SetScale(int); - Sets the value of one unit in Y axis.
  • int GetScale(); - Returns the current scale value.
  • int AddBar(int iVal, COLORREF bClr, const char* sLabel); - Adds a new bar to the right end. bClr is the bar color and sLabel is the string to be displayed under the bar.
  • int InsertBar(int index,int iVal, COLORREF bClr, const char* sLabel); - Inserts a new bar at a position.
  • int DeleteBar(int index); - Deletes a bar.
  • void SetBarValue(int index, int iVal, BOOL bDraw=TRUE); - Sets the value of a bar.
  • void SetBarColor(int, COLORREF, BOOL bDraw=TRUE); - Sets the color of a bar.
  • int GetBarValue(int iIndex); - Returns the value of the bar at a position.
  • COLORREF GetBarColor(int iIndex); - Returns the color value of a bar at a position.
  • void SetBGColor(COLORREF clr); - Sets the background color of the graph.
  • COLORREF GetBGColor(); - Return the background color of the graph.
  • void SetAxisColor(COLORREF); - Sets the color of the axis lines in the graph.
  • COLORREF GetAxisColor(); - Returns the axis color.
  • void SetTextColor(COLORREF); - Sets the text color.
  • COLORREF GetTextColor(); - Returns the text color
  • int GetNumberOfBars(); - Returns the number of bars.
  • int DrawGraph(void); - Draws the graph.
  • int SetDisplayRange(int iRangeStart, int iRangeEnd); - Sets the display range of the Y axis.
  • void GetDisplayRange(int& iRangeStart, int& iRangeEnd); - Gets the display range of the Y axis.

Example of use

m_Graph.SetUnit("centi-meters"); //Sets Unit Text
m_Graph.SetScale(10); // Scale value per 1 unit
m_Graph.GetDisplayRange(0, 100); //Display from 0 to 100
for(UINT i=0;i<10;i++) 
{ 
char tmp[16]; 
sprintf(tmp, &quot;bar%d&quot;, i); 
m_Graph.AddBar(rand()%100, RGB(rand()%256, rand()%256, rand()%256), tmp); 
//Sets Random values
sprintf(tmp, &quot;%d&quot;, i); 
m_BarCombo.AddString(tmp); 
} 
m_Graph.SetBGColor(RGB(0,0,0)); 
m_Graph.SetAxisColor(RGB(255,255,255)); 
m_Graph.SetTextColor(RGB(0,0,255));

License

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

About the Author

Shaibujan
Team Leader
India India
Member
Mr. Shaibujan has got 9+ yrs of programming experience in C/C++, on various domains like Cryptography, Biometric Security, Network Security, Pattern Recognition, Image Processing, MPEG2, MPEG4, H.264, AAC etc.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberVinod Viswanath14 Sep '12 - 21:05 
Nice Work!!
QuestionExport imagememberanand m s7 Aug '11 - 19:23 
Hi........
 

I like your project(bar graph), i need one more feature how to export(save) the image. pls if you know replay it
 
Regards
Anand
AnswerRe: Export imagemembermerano21 Sep '11 - 11:51 
Dont know if its useful, just found older article
 
Bar Graph Control (Version2004)
 
->Save to Bitmap File varandas79 15:00 18 Jun '06
 
Let us know if its usefull ..
Generalhave some problemmemberbetatest8926 Apr '11 - 16:43 
Dear Sir,
 
Thank for you code that help me complete a demo quickly.
But I have some problem that application will crash on
 

pFont->GetLogFont(&lf);

 
I thought that object was used many timer and didn't be released
So I add some release code at the DrawGraph's bottom.
 
//Release
pFont->DeleteObject();
pDC->DeleteDC();

 
And it looks like more stable, but I don't sure this modification is ok?
 
Thanks.
Generalgreat code!!membergenliu77720 Sep '09 - 16:09 
great code!! Smile | :)
GeneralThanks,membershajeer1222 Jul '07 - 23:07 
Very useful for simple applications

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 6 Feb 2007
Article Copyright 2007 by Shaibujan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid