Draw graphs.

Posted on 1998-09-01
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Author Comment

ID: 1171856
Edited text of question

Author Comment

ID: 1171857
Edited text of question

Accepted Solution

Answers2000 earned 100 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

Independent Software Vendors: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Building cUrl in Windows v7.43.0 4 39
FMX StringGrid1->Canvas->FillRect Problem 3 197
Dialogbox API leak? 18 109
How do I get Window Title of all opened process? 4 39
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 clear a vector as well as how to detect empty vectors in C++.

726 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