Simple Graph Control
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!
- Insert the MyGraph.cpp and MyGraph.h files into your project.
- 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).
- Use ClassWizard to add a member variable called "m_graph" of category "Control" and variable type "CStatic" for your "static" control.
- Edit the dialog's header file to the header file:
- 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 - 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;
- In your dialog's OnInitDialog(), add code to initialize the graph; for example:
#include "MyGraph.h"
// 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 KbDownload source - 11 Kb


Comments
it's said "Supports any number of series",but impossible
Posted by Legacy on 08/11/2003 12:00amOriginally 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???
ReplyWhy I can not use demo?
Posted by Legacy on 06/22/2003 12:00amOriginally 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.
ReplyHow we can add MyGraph.h and MyGraph.cpp to our project?
Posted by Legacy on 01/02/2003 12:00amOriginally posted by: ashkan
Dear Sir,
ReplyHow 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 12:00amOriginally 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).
ReplyFlickerfree Drawing..
Posted by Legacy on 01/03/2002 12:00amOriginally 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:00amOriginally posted by: Drew
Hey, this is a very nice straightforward tool. But is there
Replyany 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 12:00amOriginally posted by: Jeff
Can you use this in Visual Basic and if so...how?
Reply