Simple Graph Control

at Definitive Solutions, Inc.


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


Comments

  • it's said "Supports any number of series",but impossible

    Posted by Legacy on 08/11/2003 12:00am

    Originally posted by: sw son

    The Line graph doesn't be drawn, if number of series is

    become increase(about range of 10,000)

    how can i do for shoot this trouble???

    Reply
  • Why I can not use demo?

    Posted by Legacy on 06/22/2003 12:00am

    Originally posted by: limzhu

    Hi Larry Leonard:

    I download your demo , and complie it, the error said you missed rc2 file, then I designed same dialog box as yours. But the program still can not run, so could your give me your whole demo with rc2? Thank you.

    Reply
  • How we can add MyGraph.h and MyGraph.cpp to our project?

    Posted by Legacy on 01/02/2003 12:00am

    Originally posted by: ashkan

    Dear Sir,
    How we can add MyGraph.h and MyGraph.cpp to our project?
    When I add MyGraph.h and MyGraph.cpp to my project
    I cant make an object from it because visualc++6 take an error from command #include "MyGraph.h"
    I think that must make a class from that files
    but I dont khnow how?how can help me?
    Thanks in advance

    Reply
  • Good for Basic Graphing

    Posted by Legacy on 12/09/2002 12:00am

    Originally posted by: Heath

    This would be great for basic graphing (i.e. unsigned ints), but unfortunately lacks the power for plotting negatives and decimal point numbers (i.e floats).

    Reply
  • Flickerfree Drawing..

    Posted by Legacy on 01/03/2002 12:00am

    Originally posted by: Bernhard

    Pretty easy if you use Keith Rule's MemDC - Class..

    Just change

    void MyGraph::OnPaint()
    {
    VALIDATE;

    CPaintDC dc(this);
    // DrawGraph(dc);
    CMemDC pDC (&dc);
    DrawGraph(pDC);
    }

    Have fun

    Bernhard

    Reply
  • how to use ?

    Posted by Legacy on 12/28/2001 12:00am

    Originally posted by: Drew

    Hey, this is a very nice straightforward tool. But is there
    any documentation as to how to make a line graph? Sorry if
    I'm dense.....I couldn't figure it out from the source! :)
    Thanks again for the work.

    Reply
  • Can this be used in VB?

    Posted by Legacy on 12/19/2001 12:00am

    Originally posted by: Jeff

    Can you use this in Visual Basic and if so...how?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Adaptation and evolution are fundamental requirements of survival -- not only in nature, but also in business. Our world has changed dramatically in a short amount of time. Many businesses are fueling and capitalizing on this change, while others are desperately clinging to a bygone era. Who is left standing in the years and decades ahead should come as no surprise. This edition of Unleashing IT highlights the companies that are embracing new circumstances, new methods, and new opportunities. By downloading …

  • A majority of organizations are operating under the assumption that their network has already been compromised, or will be, according to a survey conducted by the SANS Institute. With many high profile breaches in 2013 occurring on endpoints, interest in improving endpoint security is top-of-mind for many information security professionals. The full results of the inaugural SANS Endpoint Security Survey are summarized in this white paper to help information security professionals track trends in endpoint …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds