Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A 2D data visualisation class

0.00/5 (No votes)
20 Mar 2000 1  
A comprehensive set of classes for displaying 2 dimensional data

Introduction

CGraphWnd is a class that provides multiplot 2D data visualization. CGraphWnd class is derived from the MFC class CWnd and could be used as a base class for different kinds of 2D data plot views. The following features are supported:

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

See the sample picture above of a 2D plot with three graphs.

Class interface

The class interface provides several groups of functions.

Graph creation routines

CGraphWnd(long maximum_graphs = 32);

The constructor that creates a graph and initializes the storage for plots. The maximum_graphs parameter specifies the maximum number of plots.

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

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

Graph manipulation routines

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

The function adds a new graph to the view. It returns the graph index if successful and -1 if not. If the bInvalidate flag is set then the view will be updated. If b_sort_x flag is set, then the points will be sorted by the X-axis.

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

The function removes the graph from the collection.

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

The function updates the graph information and the view, if the bInvalidate flag is set. It is useful after collective operations on a hidden graph.

virtual CGraphProps* GetGraph(int index);

The function returns the properties of a given graph or NULL in case of an error.

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

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

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

These functions are used to get or set the graph view flags. The 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);

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

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

It sets the new point coordinates. If bRedraw is set - it invalidates the graph.

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

It removes the point from the graph.

virtual void ClearGraph(int graphnum, BOOL bRedraw);

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

Axis manipulation routines

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

It sets the properties for the axis. The properties include: Axis' title (_title), unit of measurement (_UOM) and precision - number of digits after comma (_precision). The bXAxis parameter specifies 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);

It returns a formatted value parameter, regarding the 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);

The function sets a new world coordinates for the graph and, if bRedraw flag is set, it redraws the graph.

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

The function retrieves the graph's world coordinates.

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

The function retrieves the bounding rectangle for all the visible graphs.

Misc. operations

virtual void UpdateWindows(unsigned long what_to_update);

The function updates (redraws) the specified views and/or windows. what_to_update parameter could be an 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);

The function does the following operations with the point view:

  • Shows view - shows the view in the latest visible position.
  • Hides view - hides the point view.
  • Disables view - the 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 can take 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 the function graph with rulers this can be drawn on the device context, specified by the dest_dc parameter. rect_to_draw parameter defines a rectangle in pixels where the graph should be drawn.

Menu and property pages operations

virtual void AppendMenuItems(CMenu* menu);

Overwrite this function to change the contents of RB-menu.

virtual void AppendPropertyPage(CPropertySheet* prop_sheet);

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

virtual void ReleasePropertyPage(UINT dialog_status);

This function should apply changes to the graph (if any) and delete the previously added property pages. The dialog_status parameter is a result of the DoModal() function of the 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 the graph files (except grres.rc and grres.h) into your project.
  2. Open grres.rc and copy the 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 the bAutoUpdateTimer parameter in the CGraphWnd::Create function to TRUE.

Sources, demos, updates and legal stuff

The latest sources, demos and the updates for the class can be found here. You can drop an e-mail to the 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