Click here to Skip to main content
15,879,348 members
Articles / Desktop Programming / MFC
Article

A 2D data visualisation class

Rate me:
Please Sign up or sign in to vote.
4.54/5 (34 votes)
20 Mar 2000 315.6K   17.6K   175   51
A comprehensive set of classes for displaying 2 dimensional data
  • Download demo project - 305 Kb
  • Download source files - 80 Kb
  • Sample Image - graph2d.gif

    Introduction

    CGraphWnd is a class that provides multiplot 2D data visualization.

    CGraphWnd class is derived from CWnd MFC class and could be used as base class for different kinds of 2D data plot views. Features supported:

    • Normal and scatter graph modes
    • Autoscale
    • Zooming
    • Fit width, height and page
    • Mouse coordinates tracing
    • Ability to add and display interactively up to 15000 points per second (depends on the speed of the computer)
    • Panning support - just press and hold shift key while moving mouse with left button pressed
    • Graph could be drawn on any device context, including printer's one

    See the sample picture above of 2D plot with 3 graphs:

    Class interface.

    Class interface provides several groups of functions:

    Graphs creation routines:

    CGraphWnd(long maximum_graphs = 32);

    Constructor. Creats graph and initializes storage for plots. maximum_graphs parameter specifies maximum numberof plots.

    BOOL Create(LPCTSTR lpszWindowName, const RECT& rect, CWnd* pParentWnd, UINT nID,
    DWORD dwStyle = WS_CHILD | WS_VISIBLE, BOOL bAutoUpdateTimer = FALSE);

    Functrion creates a graph window. bAutoUpdateTimer parameter specifies whether to start toolbar update timer for situations when Idle functionality is not available (modal dialog boxes). For description on other parameters - see MSDN help on CWnd::Create.

    Graphs manipulation routines:

    virtual int AddGraph(COLORREF newColor = 0, char* title = "", BOOL bRedraw = TRUE, BOOL b_sort_x = TRUE, BOOL b_keep_same_x = FALSE);

    Function adds new graph to the view. Returns graph index if successful or -1 if not. If bInvalidate flag is set then view will be updated.If b_sort_x flag is set, then points will be sorted by X-axis.

    virtual int RemoveGraph(int index, BOOL bRedraw = TRUE);

    Function removes graph from collection.

    virtual int UpdateGraph(int index, BOOL bRedraw = TRUE);

    Function updates graph information and view, if bInvalidate flag is set. Useful after collective operations with hidden graph.

    virtual CGraphProps* GetGraph(int index);

    Function returns properties for the given graph or NULL in case of error.

    virtual CGraphProps* GetFirstGraph(int* index);
    virtual CGraphProps* GetNextGraph(int* index);

    These functions are used to enumerate through all existing graphs in the view.

    virtual void SetGraphFlags(DWORD new_flags, BOOL bRedraw);
    virtual DWORD GetGraphFlags();

    These functions are used to get or set graph view flags. Flag value is an OR'ed combination of:

    enum GRAPH_PANEL_FLAGS
    {
       GRAPH_AUTOSCALE      =    0x00000001,     //turns autoscale feature on/off
       GRAPH_SQUAREPOINTS   =    0x00000002,     //defines whether to draw points as squares
       GRAPH_SHOW_TOOLTIP   =    0x00000004,     //defines whether to show tooltip with mouse coordinates information
       GRAPH_DRAW_AXIS      =    0x00000008,     //defines whether to draw axis
       GRAPH_GRAPH_SCATTER  =    0x00000010      //specifies if graph is shown as "scatter" graph
    };

    Points manipulation routines:

    virtual int AddPoint(int graphnum, double x, double y, BOOL bRedraw, int index = -1);

    Adds (if index == -1) or inserts new point to the graph. If bRedraw set - graph will be invalidated.

    virtual int EditPoint(int graphnum, int index, double x, double y, BOOL bRedraw);

    Sets new point coordinates. If bRedraw set - invalidates graph.

    virtual int RemovePoint(int graphnum, int index, BOOL bRedraw);

    Removes point from graph.

    virtual void ClearGraph(int graphnum, BOOL bRedraw);

    removes all points from specified graph or from all graphs if graphnum == -1.

    Axis manipulation routines.

    virtual void SetAxisProps(char* _title, char* _UOM, int _precision, BOOL bXAxis, BOOL bRedraw);

    Sets properties for axis. Properties include: Axis' title (_title), unit of measurement (_UOM) and precision - number of digits after comma (_precision). bXAxis parameter specify axis to apply attributes to and could be one of the following: GRAPH_X_AXIS or GRAPH_Y_AXIS.

    virtual void FormatAxisOutput(double value, BOOL bXAxis, int format_level, CString& res_str);

    Returns formatted value parameter, regarding current axis' properties and format level. Currently three levels of format information are defined:
    0 - minimum information.
    1 - value and unit of measurement
    2 - axis' title, value and unit of measurement

    Axis World coordinates.

    virtual void SetGraphWorldCoords(double x1, double x2, double y1, double y2, BOOL bRedraw = TRUE);

    Functions sets new world coordinates for the graph and, if bRedraw flag is set, redraws graph.

    virtual void GetGraphWorldCoords(double* x1, double* x2, double* y1, double* y2);

    Function retrieves graph's world coordinates.

    virtual BOOL GetBoundRect(double* minx, double* maxx, double* miny, double* maxy);

    Function retrieves bounding rectangle for all visible graphs.

    Misc operations.

    virtual void UpdateWindows(unsigned long what_to_update);

    Function updates (redraws) specified views and/or windows. what_to_update paraneter could be OR'ed combination of the following values:

    enum GRAPH_WINDOW_UPDATE_VALUES
    {
       GRAPH_WUV_GRAPH  =    0x00000001,
       GRAPH_WUV_PVIEW  =    0x00000002,
       GRAPH_WUV_RULERS =    0x00000004,
       GRAPH_WUV_ALL    =    0xFFFFFFFF
    };

    virtual void OperateWithPointView(unsigned long pview_operations);

    Function does the following operations with point view:

    • Shows view - shows the view in the latest visible position
    • Hides view - hides point view
    • Disables view - point view will not be updated with any kind of point-information (add/remove/edit points)
    • Enables view - enables point view to trace point-changing operations

    pview_operations parameter could be one of the following values:

    enum GRAPH_PVIEW_OPERATIONS
    {
       GRAPH_PO_SHOW    = 0x00000001,
       GRAPH_PO_HIDE    = 0x00000002,
       GRAPH_PO_DISABLE = 0x00000004,
       GRAPH_PO_ENABLE  = 0x00000008
    };

    void DrawGraphToDC(CDC* dest_dc, CRect& rect_to_draw);

    Using this function graph with rulers could be drawn on the device context, specified by dest_dc parameter. rect_to_draw parameter defines a rectangle in pixels where graph should be drawn.

    Menu and property pages operations.

    virtual void AppendMenuItems(CMenu* menu);

    Overwrite this function to change contens of RB-menu.

    virtual void AppendPropertyPage(CPropertySheet* prop_sheet);

    Overwrite this function to add property page to Property Sheet with graph properties.

    virtual void ReleasePropertyPage(UINT dialog_status);

    This function should apply changes to the graph (if any) and delete previously added property pages. dialog_status parameter is a result of DoModal() function of Property Sheet (either IDOK or IDCANCEL).

    Class usage.

    Modification of the project.

    There are several steps that you should do in order to use this class in your application:

    1. Include all graph files (except grres.rc and grres.h) into your project.
    2. Open grres.rc and copy following resources into your project resources:

      IDD_GRAPH_CHANGE_TITLE dialog
      IDD_GRAPH_AXIS_PROP_PAGE dialog
      IDD_GRAPH_GRAPH_PROP_PAGE dialog
      IDD_GRAPH_GRAPHICS_PROPS dialog
    3. Add /GR (Enable RTTI) compiler option to your project
    4. Derive your window class from CGraphWnd or use it directly - and off we go!

    Using the class in the Dialogs.

    There are no specific steps that you should make in order to use this class in dialogs except when using it in modal dialog boxes. In this case you must set bAutoUpdateTimer parameter in CGraphWnd::Create function to TRUE.

    Sources, demos, updates and legal stuff.

    Latest sources, demos an updates for the class could be found <a href="http://www.geocities.com/pbarvinko" target="_blank">here. You can drop an e-mail to author here.

    This class uses the CSizingControlBar class, written by Cristi Posea.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    Questionvisualisation Pin
    sersh1415-Mar-19 8:54
    sersh1415-Mar-19 8:54 
    Questionadding writting to svg, png or other image file format Pin
    serup19-Jun-18 2:44
    serup19-Jun-18 2:44 
    QuestionPorting to x64 Pin
    natalie_estevez15-Jul-14 4:43
    natalie_estevez15-Jul-14 4:43 
    Are you thinking in porting this code to x64 support? I have tried it, but I have had some problems with _asm directives.

    Thank you
    GeneralMy vote of 3 Pin
    Member 1039431815-Nov-13 2:03
    Member 1039431815-Nov-13 2:03 
    QuestionX and Y-axis scale values are not updating in a correct way with the x and y values(input). Pin
    Nilesh Kumar Srivastava13-Mar-13 23:03
    Nilesh Kumar Srivastava13-Mar-13 23:03 
    AnswerRe: X and Y-axis scale values are not updating in a correct way with the x and y values(input). Pin
    RAJU PONNAGANTI8-Feb-14 1:37
    RAJU PONNAGANTI8-Feb-14 1:37 
    GeneralI want to use the class in a VC++ 2005 version but I have this errors Pin
    vacing7-Sep-09 0:35
    vacing7-Sep-09 0:35 
    GeneralRe: I want to use the class in a VC++ 2005 version but I have this errors Pin
    Bernardo_H18-Jan-10 1:27
    Bernardo_H18-Jan-10 1:27 
    GeneralRe: I want to use the class in a VC++ 2005 version but I have this errors Pin
    Yenidai_5-Feb-10 1:13
    Yenidai_5-Feb-10 1:13 
    GeneralRe: I want to use the class in a VC++ 2005 version but I have this errors Pin
    Bernardo_H10-Mar-10 23:16
    Bernardo_H10-Mar-10 23:16 
    GeneralRe: I want to use the class in a VC++ 2005 version but I have this errors Pin
    spiderkarma3-May-10 9:59
    spiderkarma3-May-10 9:59 
    QuestionHow to create graph with Date coordinate ? Pin
    yoramn@cti2.com31-Jul-07 22:36
    yoramn@cti2.com31-Jul-07 22:36 
    GeneralDynamic Graph Pin
    BeerFizz6-Jan-06 12:53
    BeerFizz6-Jan-06 12:53 
    GeneralLegends / Axis doesn't work Pin
    satishmirle2-Jan-06 7:35
    satishmirle2-Jan-06 7:35 
    QuestionGraphs not plotting in color Pin
    satishmirle1-Jan-06 13:56
    satishmirle1-Jan-06 13:56 
    GeneralBug with CPointsCollection class Pin
    paulhodgson035-Jul-05 10:24
    paulhodgson035-Jul-05 10:24 
    GeneralIGNORE Pin
    paulhodgson036-Jul-05 13:48
    paulhodgson036-Jul-05 13:48 
    GeneralRe: IGNORE Pin
    Member 205412616-Aug-05 11:00
    Member 205412616-Aug-05 11:00 
    GeneralRe: IGNORE Pin
    paulhodgson0316-Aug-05 13:18
    paulhodgson0316-Aug-05 13:18 
    Generalresize does not work on a dialog. Pin
    ahgu25-May-05 5:16
    ahgu25-May-05 5:16 
    Questionsave an actual graph, but it is 0kb??? Pin
    zucki27-Dec-04 3:59
    zucki27-Dec-04 3:59 
    Generalmistake for update the graph Pin
    christophecrepin18-Aug-04 1:31
    christophecrepin18-Aug-04 1:31 
    GeneralThank you Pin
    Alexander Kent8-Feb-04 13:12
    Alexander Kent8-Feb-04 13:12 
    GeneralProblem plotting bulky data Pin
    lufutusi28-Jun-03 22:33
    lufutusi28-Jun-03 22:33 
    GeneralChanging the ruler Pin
    Kontaxis19-Jun-03 9:08
    Kontaxis19-Jun-03 9:08 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.