Click here to Skip to main content
Click here to Skip to main content

Tagged as

A 2D data visualisation class

, 20 Mar 2000
Rate this:
Please Sign up or sign in to vote.
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 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

    Share

    About the Author

    Paul Barvinko

    United States United States
    No Biography provided

    Comments and Discussions

     
    QuestionPorting to x64 Pinmembernatalie_estevez15-Jul-14 4:43 
    GeneralMy vote of 3 PinmemberMember 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). [modified] PinmemberNilesh 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). PinmemberRAJU PONNAGANTI8-Feb-14 1:37 
    GeneralI want to use the class in a VC++ 2005 version but I have this errors Pinmembervacing7-Sep-09 0:35 
    GeneralRe: I want to use the class in a VC++ 2005 version but I have this errors PinmemberBernardo_H18-Jan-10 1:27 
    GeneralRe: I want to use the class in a VC++ 2005 version but I have this errors PinmemberYenidai_5-Feb-10 1:13 
    GeneralRe: I want to use the class in a VC++ 2005 version but I have this errors PinmemberBernardo_H10-Mar-10 23:16 
    GeneralRe: I want to use the class in a VC++ 2005 version but I have this errors Pinmemberspiderkarma3-May-10 9:59 
    QuestionHow to create graph with Date coordinate ? Pinmemberyoramn@cti2.com31-Jul-07 22:36 
    GeneralDynamic Graph PinmemberBeerFizz6-Jan-06 12:53 
    GeneralLegends / Axis doesn't work Pinmembersatishmirle2-Jan-06 7:35 
    QuestionGraphs not plotting in color Pinmembersatishmirle1-Jan-06 13:56 
    GeneralBug with CPointsCollection class Pinmemberpaulhodgson035-Jul-05 10:24 
    GeneralIGNORE Pinmemberpaulhodgson036-Jul-05 13:48 
    GeneralRe: IGNORE PinmemberChaseWRX16-Aug-05 11:00 
    GeneralRe: IGNORE Pinmemberpaulhodgson0316-Aug-05 13:18 
    Generalresize does not work on a dialog. Pinmemberahgu25-May-05 5:16 
    Questionsave an actual graph, but it is 0kb??? Pinmemberzucki27-Dec-04 3:59 
    Generalmistake for update the graph Pinmemberchristophecrepin18-Aug-04 1:31 
    GeneralThank you PinmemberAlexander Kent8-Feb-04 13:12 
    GeneralProblem plotting bulky data Pinmemberlufutusi28-Jun-03 22:33 
    GeneralChanging the ruler PinmemberKontaxis19-Jun-03 9:08 
    Questiongood! but how to active the curve and copy the curve to word? Pinmemberdxhdxh11-Jun-03 20:50 
    AnswerRe: good! but how to active the curve and copy the curve to word? Pinmemberjfcantin18-Jan-06 23:40 

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

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

    | Advertise | Privacy | Mobile
    Web04 | 2.8.141022.2 | Last Updated 21 Mar 2000
    Article Copyright 2000 by Paul Barvinko
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid