Click here to Skip to main content
6,594,432 members and growing! (16,599 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Charting Controls     Advanced

A 2D data visualisation class

By Paul Barvinko

A comprehensive set of classes for displaying 2 dimensional data
VC6, MFC, Dev
Posted:20 Mar 2000
Views:174,157
Bookmarked:97 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
51 votes for this article.
Popularity: 7.67 Rating: 4.49 out of 5
1 vote, 4.2%
1

2
2 votes, 8.3%
3
6 votes, 25.0%
4
15 votes, 62.5%
5

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

About the Author

Paul Barvinko


Member

Location: United States United States

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 41 (Total in Forum: 41) (Refresh)FirstPrevNext
GeneralI want to use the class in a VC++ 2005 version but I have this errors Pinmembervacing1:35 7 Sep '09  
QuestionHow to create graph with Date coordinate ? Pinmemberyoramn@cti2.com23:36 31 Jul '07  
GeneralDynamic Graph PinmemberBeerFizz13:53 6 Jan '06  
GeneralLegends / Axis doesn't work Pinmembersatishmirle8:35 2 Jan '06  
QuestionGraphs not plotting in color Pinmembersatishmirle14:56 1 Jan '06  
GeneralBug with CPointsCollection class Pinmemberpaulhodgson0311:24 5 Jul '05  
GeneralIGNORE Pinmemberpaulhodgson0314:48 6 Jul '05  
GeneralRe: IGNORE PinmemberChaseWRX12:00 16 Aug '05  
GeneralRe: IGNORE Pinmemberpaulhodgson0314:18 16 Aug '05  
Generalresize does not work on a dialog. Pinmemberahgu6:16 25 May '05  
Generalsave an actual graph, but it is 0kb??? Pinmemberzucki4:59 27 Dec '04  
Generalmistake for update the graph Pinmemberchristophecrepin2:31 18 Aug '04  
GeneralThank you PinmemberAlexander Kent14:12 8 Feb '04  
GeneralProblem plotting bulky data Pinmemberlufutusi23:33 28 Jun '03  
GeneralChanging the ruler PinmemberKontaxis10:08 19 Jun '03  
Generalgood! but how to active the curve and copy the curve to word? Pinmemberdxhdxh21:50 11 Jun '03  
AnswerRe: good! but how to active the curve and copy the curve to word? Pinmemberjfcantin0:40 19 Jan '06  
GeneralProgrammatically re/moving table data? PinsussTim Thieler11:55 12 May '03  
GeneralRe: Programmatically re/moving table data? PinmemberMichaT6:34 2 Feb '04  
GeneralIs it suitable for plotting bulky data? Pinmemberlamppost_ko22:42 27 Jan '03  
Generalcannot add 2nd point at same x Pinmemberweiwei28:49 4 Dec '02  
Generalmouse input Pinmembertoon22:25 22 Jul '02  
GeneralDid you have following changes with the new version? Pinmemberchen21:07 10 Jul '02  
GeneralDialog based appication ? Pinmemberchen6:45 4 Jul '02  
Generalneed help implementing your code Pinmemberassaf14:22 24 Feb '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Mar 2000
Editor: Chris Maunder
Copyright 2000 by Paul Barvinko
Everything else Copyright © CodeProject, 1999-2009
Web19 | Advertise on the Code Project