Modifying the View Class

The view class provided by the AppWizard can be easily modified to include Objective Chart's capabilities. Objective Chart provides an alternative view class with some advanced features including data dragging, data feedback with chart tips, editing, printing, and print previewing. We recommend that you use all these features to their fullest advantage.

The view class for the standard MDI child window is based on CView. By changing the base class of the ready-made view (in this case CChartAppView) from CView to SRGraphView, you are inserting the capabilities of the Objective Chart system into the class ancestor list just before your newly created view. In this manner, you can still add your own custom handlers and routines without worrying about altering the Objective Chart sources.

Here's how to change your view class step-by-step:

  1. Include the Objective Chart header file.

    Find the stdafx.h file for the project and add the following line to the end of the file:

    #include "chart\ochart.h"

    Our stdafx.h file should look like this...

    // stdafx.h : includes standard system include files,

    // or project specific include files that are used

    // frequently, but are changed infrequently

    //

     

    #define VC_EXTRALEAN // Exclude rarely-used stuff

    // from Windows headers

     

     

    #include <afxwin.h> // MFC core and standard components

    #include <afxext.h> // MFC extensions

    #ifndef _AFX_NO_AFXCMN_SUPPORT

    // MFC support for Windows Common Controls

    #include <afxcmn.h>

    #endif // _AFX_NO_AFXCMN_SUPPORT

     

    #include "chart\ochart.h"

    By doing this, all of the files that we have created using the AppWizard now know about Objective Chart.

  2. Change the base class of the view from CView to SRGraphView.

    In both the header file (CChartAppView.h) and the implementation file (CChartAppView.cpp), perform a global search for “CView” and replace each reference with “SRGraphView.”

  3. Modify the OnDraw() routine to perform chart drawing.

    Note that the code below refers to a variable named m_Graph. This variable is the default graph object that will be stored in the CChartAppDoc document after suitable modifications to that class have been made.

    ////////////////////////////////////////////////////

    // CChartAppView drawing

     

    void CChartAppView::OnDraw(CDC* pDC)

    {

    CChartAppDoc* pDoc = GetDocument();

    ASSERT_VALID(pDoc);

     

    // Simply add a draw command for the Objective Chart

    // object which is stored in the document

    pDoc->m_Graph.DrawComponentList(pDC,this);

    }

    A second way of performing this task would be to simply remove the OnDraw() function from the CChartAppView.cpp file and CChartAppView.h files allowing the built-in handler in the base class (SRGraphView) to manage the drawing of the graph.

    Now we can go on to add printing capabilities to this class.

  4. Modify the OnBeginPrinting() member function.

    The OnBeginPrinting() function is called when the document is printed or print-previewed. The base class (SRGraphView) provides a handler that must be called from our own handler. Again, an alternative is to simply remove all reference to the OnBeginPrinting() function from CChartAppView.cpp and CChartAppView.h and allow the base class to manage the call.

    Note: Remove the comments from the function declaration.

    Here is the original code:

    void CChartAppView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)

    {

    // TODO: add extra initialization before printing

    }

    And here, the edited version:

    void CChartAppView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)

    {

    SRGraphView::OnBeginPrinting(pDC,pInfo);

    }

    This step completes the modifications to the CChartAppView code. Before we go on to modify the document, you may want to read some of the information regarding the SRGraphView class from which our CChartAppView is derived.

    There are other standard command handlers provided by the SRGraphView:

    • OnLButtonDown(), OnLButtonUp(), and OnMouseMove() handle data dragging, data zooming, and the initial phase of chart tip generation in concert.

    • OnTimer() handles chart tip generation proper.

    • OnEditCopy() copies the bitmap from your SRGraphView-based view onto the clipboard for pasting into other documents.

    If your application overrides these handlers and you want to keep the SRGraphView capabilities, you must make sure that you either call the base class handlers or perform similar tasks in your own routines.

    There are also several non-standard (i.e. unique to Objective Chart) command handlers that are available to you. These are built into SRGraphView in the same way that the standard OnFile??? commands are, and they have their own special command IDs.

    The special handlers are:

    • OnGraphZoomout() which returns zoomed displays to their normal state. The command ID, ID_GRAPH_ZOOMOUT, is defined in the Objective Chart resources.

    • OnUpdateGraphZoomout() which enables or disables the graph zoom-out buttons and menu items according to the current state.

    • OnGraphEdit() which invokes the built-in editor, Chart Explorer. The command ID, ID_GRAPH_EDIT, is defined in the Objective Chart resources.

    • OnGraphWizard() which invokes the Chart Wizard allowing the user to interactively experiment with various chart types and add or modify components.

    • OnGraphPagesetup() which invokes a dialog box that allows you to paginate your graph before printing. The command ID, ID_GRAPH_PAGESETUP, is defined in the Objective Chart resources.

    • OnSaveAsDib() which stores the chart image in a disk file as a Device-Independent Bitmap. The command ID, ID_FILE_SAVE_AS_DIB, is defined in the Objective Chart resources.

    • OnSaveAsJpeg() which stores the chart image in a disk file as a Device-Independent Bitmap. The command ID, ID_FILE_SAVE_AS_JPEG, is defined in the Objective Chart resources.

    Later in this tutorial, you will see how to modify your resources for use with Objective Chart. Read on to see how to modify your document class to create a simple but useful graph object.