[Last Call] Learn how to a build a cloud-first strategyRegister Now


Draw graphs.

Posted on 1998-09-01
Medium Priority
Last Modified: 2008-02-20
i have currently developed a sdi program for my sch's project. the window has two panes which i have derived from the CFormView and CView.

the users will enter some parameters in the CFormView, they will be processed by some equations. the question is... how do i draw a graph wif x and y axis on the CView so that when the user enter the parameters (from the CFormView), the results can be shown on the CView with a graph. the graph is not unlike a sin/cos curve.
Question by:bi7206327
  • 3
  • 3

Author Comment

ID: 1171856
Edited text of question

Author Comment

ID: 1171857
Edited text of question

Accepted Solution

Answers2000 earned 200 total points
ID: 1171858
1. You need to share the data between the 2 views.  The best place to do this in your CDocument derived class.

i. Add data members to the document
ii. In CFormView set the data at appropriate points into the document using

((CMyDoc *)GetDocument())->m_nValue = /* value from CFormView */

If you change the GetDocument() definition to return your doc class in CFormView you can avoid the nasty cast.

2. When the graph needs to be repaint (say the user hit the Refresh button in the CFormView) you can force a repaint using GetDocument()->UpdateAllViews()

3. In CView you add the drawing code.  Override OnDraw and adding painting code here.  You can get the data out the document using the reverse of the code I gave in step #1.  Use functions like LineTo, MoveToEx, TextOut (members of CDC and you get a pointer to one of these in OnDraw) to draw the stuff.

4. Drawing graphs with all the correct axes, labels, and so forth is a lot of work.  You may wish to consider using a commercial toolkit (which will save you writing most of the graph drawing code).

The two best known (to me <g>) are

i. Graphics Server by Bits Per Second

ii. Olectra Chart

You can find a comparison at

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1171859
You're editted text came thru while I was typing, so commercial toolkits may be out the question ?  

If you need more help on #3 and #4 then let me know what you need.  

The coordinate system by default has 0,0 at the top left and is in pixels.

I've got to go offline for a bit, but I'll be back later, so post any comments on what else you might need.

Expert Comment

ID: 1171860
In the form view catch when the user makes any changes to the children controls. One good technique for this is to create handler for notification messages of the children controls. For example you might catch EN_CHANGE or EN_KILLFOCUS for an edit control, or BN_CLICKED for a check box and so on. Then in these handlers call the UpdateData(TRUE) method of the CFormView. Link your controls to corresponding data members of your document class. You can do that by using DDX mechanism in your CFormView's DoDataExchange(). The easiest way to link them is to create member variables for the controls in the class wizard, and then in the DoDataExchange() replace those variables with your document variables. Be sure however to move the changed code outside of the class wizard comments. Otherwise the class wizord will not work the next time you start it.
Finally after calling UpdateData(TRUE) in your handlers call UpdateAllViews() method of your document. This will ensure that the document (assuming it is the document of the CView) will call the CView's OnUpdate() and will repaint your other view.
Basically that's the MFC way of one view to react to changes of another view.

Expert Comment

ID: 1171861
Milenvk makes a good point.  The only caveat is that if regenerating the graph takes a long time you may prefer to either
(i) have a manual update/refresh button
(ii) Setup a timer (SetTimer funciton) in the FormView, add a boolean member to it to (initially set to FALSE), and whenever one of milenvk's changes happen set the flag to TRUE.  In the TimerProc look for a true flag, and only in this case redraw the graph (and then reset the flag back to false).  This way you don't have to have a long pause immediately after the user types in each digit into say a numeric field.


Author Comment

ID: 1171862
commercial toolkits are out of the question bcos my school project does not have a budget for it (such toolkits cost money rite??)

pls forgive me for i have just started learning VC++.... so can you kindly explain in more details the following:

1) Drawing of grids using the LineTo, TextOut etc etc functions.

2) For the program to generate the graph... let say i got two variables, theta and displacement, i will calculate the displacement at certain interval of theta. eg, at theta equal to 5, 10, 15, 20.... displacements are d1, d2, d3, d4. as such i would have the various pts. question is, how do i map this points into the grid i have drawn?? also, how to i join this various pts up after i mapped them into the grid i drawn??

the project gonna due very soon and i just can't seem to solve the problem. your help is very much appreciated. thanz in advance.


Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question