Draw graphs.

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.
Who is Participating?
Answers2000Connect With a Mentor Commented:
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

bi7206327Author Commented:
Edited text of question
bi7206327Author Commented:
Edited text of question
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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.
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.
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.

bi7206327Author Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.