Simple Graph Control


Desktop-as-a-Service Designed for Any Cloud ? Nutanix Frame

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
    /* CStatic */ MyGraph	m_graph;
  7. Edit the dialog's header file to include member variables for each series you want to graph:
    // MyGraph data.
     MyGraphSeries	m_gsBoys;
     MyGraphSeries	m_gsGirls;
  8. In your dialog's OnInitDialog(), add code to initialize the graph; for example:
  9. // Create the series.
    // Add series to graph.
    // Set up the graph.
    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.

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.


Download demo project - 32 Kb
Download source - 11 Kb


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

    Posted by Legacy on 08/11/2003 07: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???

  • Why I can not use demo?

    Posted by Legacy on 06/22/2003 07: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.

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

    Posted by Legacy on 01/02/2003 08: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

  • Good for Basic Graphing

    Posted by Legacy on 12/09/2002 08: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).

  • Flickerfree Drawing..

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

    Originally posted by: Bernhard

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

    Just change

    void MyGraph::OnPaint()

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

    Have fun


  • how to use ?

    Posted by Legacy on 12/28/2001 08: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.

  • Can this be used in VB?

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

    Originally posted by: Jeff

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

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date