A 2D data visualisation class






4.54/5 (33 votes)
Mar 21, 2000

321656

17681
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 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:
- Include all graph files (except grres.rc and grres.h) into your project.
- 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
- Add /GR (Enable RTTI) compiler option to your project
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.