CodeGuru
Earthweb Search
Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

Home


Simple Graph Control
Rating: none

Larry Leonard (view profile)
February 29, 2000

at Definitive Solutions, Inc.


(continued)




Click here to enlarge image

Environment: Windows NT4 SP5, Visual C++ 5

This is a very simple (but easily enhanced) CStatic-derived class to do bar, line, and pie graphs. Why create one of these instead of just using the Microsoft-supplied control? Mostly because I want better control over the appearance.

The class has the following features:

  • Supports any number of series and groups.
  • Tooltips to tell you what the values are.
  • The colors are picked automatically.
  • The font sizes scale to the size of the control.
  • Trivia: const correct, Hungarian, no leaks, lots of ASSERTS and VERIFYs, commented!
To use the class, take the following really easy steps:
  1. Insert the MyGraph.cpp and MyGraph.h files into your project.
  2. Create a "static" control called IDC_STATIC_MYGRAPH (for example) in a dialog box. Give it a border so it looks nice, and make sure to check the "Notify" checkbox (or you won't get the tooltips).
  3. Use ClassWizard to add a member variable called "m_graph" of category "Control" and variable type "CStatic" for your "static" control.
  4. Edit the dialog's header file to the header file:
  5. #include "MyGraph.h"
    
  6. Edit the dialog's header file to change your static control's class from "CStatic" to "MyGraph".
    // Dialog Data
    //{{AFX_DATA(CMyGraphDemoDlg)
    enum { IDD = IDD_MYGRAPHDEMO_DIALOG };
    /* CStatic */ MyGraph	m_graph;
    //}}AFX_DATA
    
  7. Edit the dialog's header file to include member variables for each series you want to graph:
    // MyGraph data.
    private:
     MyGraphSeries	m_gsBoys;
     MyGraphSeries	m_gsGirls;
    
  8. In your dialog's OnInitDialog(), add code to initialize the graph; for example:
  9. // Create the series.
    m_gsBoys.SetLabel("Males");
    m_gsGirls.SetLabel("Females");
    
    // Add series to graph.
    m_graph.AddSeries(m_gsBoys);
    m_graph.AddSeries(m_gsGirls);
    
    // Set up the graph.
    m_graph.SetGraphType(MyGraph::Pie);
    m_graph.SetYAxisLabel("Births");
    m_graph.SetXAxisLabel("Day of Week");
    m_graph.SetGraphTitle("Babies Born by Gender, Day of Week");
    
    // Add some data for each weekday.
    int nGroup(-1);
    
    nGroup = m_graph.AppendGroup("Sunday");
    m_gsBoys.SetData(nGroup, 25);
    m_gsGirls.SetData(nGroup, 35);
    
    nGroup = m_graph.AppendGroup("Monday");
    m_gsBoys.SetData(nGroup, 15);
    m_gsGirls.SetData(nGroup, 12);
    
    nGroup = m_graph.AppendGroup("Tuesday");
    m_gsBoys.SetData(nGroup, 5);
    m_gsGirls.SetData(nGroup, 21);
    
    nGroup = m_graph.AppendGroup("Wednesday");
    m_gsBoys.SetData(nGroup, 17);
    m_gsGirls.SetData(nGroup, 6);
    
    nGroup = m_graph.AppendGroup("Thursday");
    m_gsBoys.SetData(nGroup, 20);
    m_gsGirls.SetData(nGroup, 18);
    
    nGroup = m_graph.AppendGroup("Friday");
    m_gsBoys.SetData(nGroup, 12);
    m_gsGirls.SetData(nGroup, 16);
    
    nGroup = m_graph.AppendGroup("Saturday");
    m_gsBoys.SetData(nGroup, 8);
    m_gsGirls.SetData(nGroup, 13);
    
    // Paint the graph now that we're through.
    m_graph.Invalidate();
    

The code is as clean as I can make it, but is by no means complete or bulletproof; it should give you a good starting point, though. There are several assumptions and shortcuts made, so beware. Of course, no warranty is implied, so use at you own risk. This is code is based on un-copyrighted code posted here by Brian Convery at CGraph - Graph Class for Plotting Groups of Data. I pretty much rewrote the entire thing, but much of the basic design is his. I pass it along enhanced and still un-copyrighted. As far as I'm concerned, you can use it for any purpose whatsoever.

Downloads

Download demo project - 32 Kb
Download source - 11 Kb

Tools:
Add www.codeguru.com to your favorites
Add www.codeguru.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed







RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

(You must be signed in to rank an article. Not a member? Click here to register)

Latest Comments:
it's said "Supports any number of series",but impossible - Legacy CodeGuru (08/11/2003)
Why I can not use demo? - Legacy CodeGuru (06/22/2003)
How we can add MyGraph.h and MyGraph.cpp to our project? - Legacy CodeGuru (01/02/2003)
Good for Basic Graphing - Legacy CodeGuru (12/09/2002)
Flickerfree Drawing.. - Legacy CodeGuru (01/03/2002)

View All Comments
Add a Comment:
Title:
Comment:
Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



(You must be signed in to comment on an article. Not a member? Click here to register)


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES