Click here to Skip to main content
15,886,518 members
Articles / Desktop Programming / MFC

2D Graph ActiveX Control

Rate me:
Please Sign up or sign in to vote.
4.89/5 (159 votes)
5 Aug 2003MIT8 min read 1.5M   70.2K   339  
An ActiveX control for 2D data visualisation
#if !defined(AFX_NTGRAPHCTL_H__4E772943_A5F2_4917_B53F_DE7BE01A6E36__INCLUDED_)
#define AFX_NTGRAPHCTL_H__4E772943_A5F2_4917_B53F_DE7BE01A6E36__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#define STATIC

#ifndef MAX
#define MAX 1
#endif

#ifndef MIN
#define MIN 0
#endif

#define MAX_ELEMENTS 100

class CGraphElement {

public :
	int m_nCount; // Added by A. Hofmann
	BOOL m_bShow; // Added by A. Hofmann
	CGraphElement() ;
	BOOL bIsPlotAvailable ;
	double *dValueX ;
	double *dValueY ;
	COLORREF m_Color ;
	BOOL NewElement(UINT nPoint);
	BOOL DeleteElement();
	// TODO: to add m_lineWidth, m_lineType

protected:
	BOOL bBufferAllocated;
} ;

// NTGraphCtl.h : Declaration of the CNTGraphCtrl ActiveX Control class.

/////////////////////////////////////////////////////////////////////////////
// CNTGraphCtrl : See NTGraphCtl.cpp for implementation.

class CNTGraphCtrl : public COleControl
{
	DECLARE_DYNCREATE(CNTGraphCtrl)

// Constructor
public:
	CNTGraphCtrl();

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CNTGraphCtrl)
	public:
	virtual void OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid);
	virtual void DoPropExchange(CPropExchange* pPX);
	virtual void OnResetState();
	virtual DWORD GetControlFlags();
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	virtual void OnFontChanged();
	//}}AFX_VIRTUAL



// Implementation
protected:
	~CNTGraphCtrl();

	DECLARE_OLECREATE_EX(CNTGraphCtrl)    // Class factory and guid
	DECLARE_OLETYPELIB(CNTGraphCtrl)      // GetTypeInfo
	DECLARE_PROPPAGEIDS(CNTGraphCtrl)     // Property page IDs
	DECLARE_OLECTLTYPE(CNTGraphCtrl)	  // Type name and misc status

// Subclassed control support
	BOOL IsSubclassedControl();
	LRESULT OnOcmCommand(WPARAM wParam, LPARAM lParam);

// Message maps
	//{{AFX_MSG(CNTGraphCtrl)
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()

// Dispatch maps
	//{{AFX_DISPATCH(CNTGraphCtrl)
	BOOL m_showGrid;
	afx_msg void OnShowGridChanged();
	CString m_strLabelX;
	afx_msg void OnXLabelChanged();
	CString m_strLabelY;
	afx_msg void OnYLabelChanged();
	OLE_COLOR m_gridColor;
	afx_msg void OnGridColorChanged();
	OLE_COLOR m_labelColor;
	afx_msg void OnLabelColorChanged();
	OLE_COLOR m_axisColor;
	afx_msg void OnAxisColorChanged();
	short m_nGridX;
	afx_msg void OnNGridXChanged();
	short m_nGridY;
	afx_msg void OnNGridYChanged();
	double m_minX;
	afx_msg void OnMinXChanged();
	double m_maxX;
	afx_msg void OnMaxXChanged();
	double m_minY;
	afx_msg void OnMinYChanged();
	double m_maxY;
	afx_msg void OnMaxYChanged();
	CString m_strGraphTitle;
	afx_msg void OnGraphTitleChanged();
	OLE_COLOR m_cursorColor;
	afx_msg void OnCursorColorChanged();
	OLE_COLOR m_elementColor;
	afx_msg void OnElementColorChanged();
	BOOL m_zoomMode;
	afx_msg void OnZoomModeChanged();
	BOOL m_cursorMode;
	afx_msg void OnCursorModeChanged();
	afx_msg short GetElement();
	afx_msg void SetElement(short nNewValue);
	afx_msg short GetElementCount();
	afx_msg void SetElementCount(short nNewValue);
	afx_msg LPDISPATCH GetTickFont();
	afx_msg void SetTickFont(LPDISPATCH newValue);
	afx_msg LPDISPATCH GetTitleFont();
	afx_msg void SetTitleFont(LPDISPATCH newValue);
	afx_msg LPDISPATCH GetLabelFont();
	afx_msg void SetLabelFont(LPDISPATCH newValue);
	afx_msg void ClearGraph();
	afx_msg BOOL CreateElement(long nElement);
	afx_msg BOOL SetRange(double MinX, double MaxX,double MinY,double MaxY);
	afx_msg BOOL SetXYValue(double x, double y, long index, short iElementId);
	afx_msg void SetShowElement(BOOL bShow);
	afx_msg void Copy2Clipboard();
	//}}AFX_DISPATCH
	DECLARE_DISPATCH_MAP()


	afx_msg void AboutBox();

// Event maps
	//{{AFX_EVENT(CNTGraphCtrl)
	void FireCursorEvent(double x, double y)
		{FireEvent(eventidCursorEvent,EVENT_PARAM(VTS_R8  VTS_R8), x, y);}
	//}}AFX_EVENT
	DECLARE_EVENT_MAP()


// Dispatch and event IDs
public:
	enum {
	//{{AFX_DISP_ID(CNTGraphCtrl)
	dispidShowGrid = 1L,
	dispidXLabel = 2L,
	dispidYLabel = 3L,
	dispidGridColor = 4L,
	dispidLabelColor = 5L,
	dispidAxisColor = 6L,
	dispidNGridX = 7L,
	dispidNGridY = 8L,
	dispidMinX = 9L,
	dispidMaxX = 10L,
	dispidMinY = 11L,
	dispidMaxY = 12L,
	dispidGraphTitle = 13L,
	dispidCursorColor = 14L,
	dispidElement = 18L,
	dispidElementCount = 19L,
	dispidTickFont = 20L,
	dispidTitleFont = 21L,
	dispidLabelFont = 22L,
	dispidElementColor = 15L,
	dispidZoomMode = 16L,
	dispidCursorMode = 17L,
	dispidClearGraph = 23L,
	dispidCreateElement = 24L,
	dispidSetRange = 25L,
	dispidSetXYValue = 26L,
	dispidSetShowElement = 27L,
	dispidCopy2Clipboard = 28L,
	eventidCursorEvent = 1L,
	//}}AFX_DISP_ID
	};

protected:
	void CreateFont();
	void ResCalc();
	void CalcRect();
	void DrawGraphTitle();
	void DrawGrid();
	void DrawAxis();
	void DrawTicks();
	void DrawAxisLabel();
	void DrawCursor();
	void PlotElement();
	void PrepareForDrawing(CRect rect);
	void CursorPosition(CPoint point);
	CPoint Corrdinate(double x, double y);
	CGraphElement mpElement[MAX_ELEMENTS] ;

	int    nPointCount, nPlotIndex, nElementCount ;
	short  m_ElementId;
	double dResY, dResX ;
	double dRangeY[2], dRangeX[2];
	double m_dXCur;
	double m_dYCur;
	COLORREF m_BGColor ;

    BOOL bIsCursorInRange;
	BOOL bIsElementAllocated ;
	BOOL bBufferAllocated;
	BOOL bIsPlotAvailable ;
	BOOL bFontIsCreated ;

	CDC*  m_pDC;
    CRect m_ctlRect;
	CRect m_axisRect;

	CFont *pYLabelFont;
	CFont *pXLabelFont;
	CFont *pTickFont;
	CFont *pTitleFont;
    CFontHolder m_fontTick;
	CFontHolder m_fontTitle;
    CFontHolder m_fontLabel;


public :
	double *dValueX ;
	double *dValueY ;
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_NTGRAPHCTL_H__4E772943_A5F2_4917_B53F_DE7BE01A6E36__INCLUDED)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


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

Comments and Discussions