⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clplot.h

📁 实时曲线类的改进增加功能: 1)采样数据存盘和加载 2)演示程序中可以实现图形的打印预览和打印功能,曲线从原点开始
💻 H
字号:

#include "mmsystem.h"
#include "afxwin.h"
//*******************************************************************************************************/
//* FileName        : clPlot.h
//*
//* Description	    : Real Time Plot for MFC
//*
//* Contents:		: axis		y (x) axis info.
//*					  timeaxis	time axis info
//*					  legend	legend info.
//*					  serie		data serie info & array
//*					  clPlot	The plot itself.
//*
//* Author          : Jan Vidar Berger
//*******************************************************************************************************/
#if !defined(AFX_DQPLOT_H__0D536D37_5CF1_11D1_AED1_0060973A08A4__INCLUDED_)
#define AFX_DQPLOT_H__0D536D37_5CF1_11D1_AED1_0060973A08A4__INCLUDED_


#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// clPlot.h : header file
//
#define MAXLEGENDS	10   //图例中最多允许显示的图例数
#define MAXSERIES	50   //最多允许同时显示的曲线条数
#define MAXMILESTONES	20   //ASM曲线上最多允许的转折点数(包括起始点)(ASM线是折线形式的)

//*******************************************************************************************************/
//* simple data value struct. used in dynamic array
//*******************************************************************************************************/
typedef struct _value{
	long	ValueTime;  //X轴值。由于本类是一个事实监控图,所以X轴总是为时间。
	double	dValue;     //Y轴值
}value;

//*******************************************************************************************************/
//* non-time axis. used for left and right y axis. might be used for x as well.
//*******************************************************************************************************/
class  AFX_EXT_CLASS axis
{
public:
	CString szTitle;
	double	minrange;
	double	maxrange;

	double	m_dValuePrPixel;//屏幕上每象素代表的实际值

	axis()
	{
		szTitle	= "标题:单位";
		minrange	= 0.0;
		maxrange	= 2000.0;

		m_dValuePrPixel = 1;
	};
};

//*******************************************************************************************************/
//* time axis
//*******************************************************************************************************/
class  AFX_EXT_CLASS timeaxis //本程序为实时曲线显示,X轴为时间(s)
{
public:
	CString		m_szTitle;		// time axis title;
	long		m_mintime;		// min time(ms)
	long		m_maxtime;		// max time(ms)
	//double   	m_mintime;		// min time(s)
	//double		m_maxtime;		// max time(s)
	int			m_iTimeMode;	// axis grid and legend interval index

	double		m_dMilliSecondsPrPixel;//屏幕上每象素表示的时间(ms)
	//long		m_dMilliSecondsPrPixel;//屏幕上每象素表示的时间(ms)

	timeaxis()
	{
		m_szTitle = "时间:秒";
		m_mintime =0;
		m_maxtime = 10000;//ms
		m_iTimeMode=0;
		m_dMilliSecondsPrPixel=1;
	}
};

//*******************************************************************************************************/
//* legend 
//*******************************************************************************************************/
class  AFX_EXT_CLASS legend
{
public:
	BOOL		m_bIAmInUse;    //标识本图例是否标出   true:在左上方标出
	COLORREF	m_color;		// legend color code
	int			m_istyle;
	CString		m_szTitle;		// legend title

	legend(){
		m_bIAmInUse = FALSE;
		m_color		= 0;
		m_istyle	= PS_SOLID;
		m_szTitle	= "";
	}
};

//*******************************************************************************************************/
//* data serie
//*******************************************************************************************************/
class  AFX_EXT_CLASS serie
{
public:
	BOOL		m_bIAmInUse;    //表示本serie(序号)曲线是否要显示
	COLORREF	m_color;		// serie line color  曲线的颜色
	int			m_iLineStyle;	// line style
	BOOL		m_bRightAxisAlign; // align to right axis 右Y轴
	value	*	m_pvalues;		// value array
	
	//当前曲线(单条)采样的数据总个数
	long		m_lNoValues;	// number of values (used for array size) 数据的总个数
	
	//采样数据循环存放,m_lbegin为数组起点,m_lend为数组终点,
	long		m_lbegin;		// list begin   
	long		m_lend;			// list end
	CPen		m_pen;			// pre-calculated pen (for speed)

	serie();
	~serie();
	
	void Serialize(CArchive &ar);
	void AddPoint(long &valuetime, double &y);
	void Reset();
};

//*******************************************************************************************************/
//* Class           : clPlot
//*
//* Base Class      : public CWnd
//*
//* Description     : Plot Component.
//*
//*					  This is a standard plot and can be used for any application.
//*
//*						1. A special 'autoscroll'mode exist for real time plots.
//*						2. Only a minimum of features are implemented.
//*						3. Series and legends are separated and must be set up individually.
//*						4. A set of defines (see top of file) are used to set the max array sizes.
//*						5. Only time are supported as x-axis.
//*						6. A large range of pre-calculated values are used for maximum speed.
//*
//* Author          : Jan Vidar Berger
//*******************************************************************************************************/
class AFX_EXT_CLASS clPlot : public CWnd
{
// Construction
public:
	clPlot();
	virtual ~clPlot();

	DECLARE_SERIAL(clPlot)
// Attributes
public:

	CRect		m_ctlRect;			// control rect
	CRect		m_clientRect;		// ctlRect - borderspace
	CRect		m_plotRect;			// clientRect - margins  (logic)
//	CRect		m_plotBorderRect;			// clientRect +border (logic)
	CRect		m_legendRect;		// any rect within clientRect
	CRect		m_axisLYRect;		// Left axisi rect
	CRect		m_axisRYRect;		// right y axis
	CRect		m_axisBXRect;		// bottom x axis

	int			m_iMleft;			// left margin   左缩进
	int			m_iMright;			// right margin  右缩进
	int			m_iMtop;			// top margin    图眉
	int			m_iMbottom;			// bottom margin 图脚

	COLORREF	m_ctlBkColor;		// control background color
	COLORREF	m_plotBkColor;		// plot bacground color
	COLORREF	m_legendBkColor;	// legend background color
	COLORREF	m_gridColor;		// grid line color

	BOOL		m_bctlBorder;		// control border
	BOOL		m_bplotBorder;		// plot border
	BOOL		m_blegendBorder;	// legend border
	BOOL		m_bPrimaryLegend;	// primary legend
	BOOL		m_bSecondaryLegend;	// secondary legend
	BOOL		m_bAxisLY;			// left axis
	BOOL		m_bAxisRY;			// right axis
	BOOL		m_bAxisBX;			// bottom axis
	BOOL		m_bAutoScrollX;		// automatic x range scrolling
	BOOL		m_bSimMode;			// simulate data input

	//单条曲线数组最多容纳的数据个数
	static long	m_lMaxDataPrSerie;	// max allowed data pr. serie.(数组大小超过此值,数组不再扩大,而是从0索引循环使用。
	
	//单条曲线最多允许的数据个数(目前该变量没用上)
	static long	m_lMaxDataTotal;	// max allowed data total.
	
	//作为曲线的分界(修改后该变量不起作用)
	double		m_dNoData;			// No Data Value (used for gaps)

	legend		m_primarylegends[MAXLEGENDS];//主图例
	legend		m_secondarylegends[MAXLEGENDS];//辅图例

	serie		m_series[MAXSERIES]; //多条曲线

	axis		m_leftaxis;			// left axis
	axis		m_rightaxis;		// right axis
	timeaxis	m_timeaxis;			// bottom axis


	//采样数据的坐标(x,y),即(时间,监控项的采样值)
	CPoint		*pLineArray;		// pre-calculated when new data are entered into the system

	long		lArraySize;			// current size of pLineArray

	CFont		m_font;
	HFONT       m_hOldFont;//handle of m_font

	LOGFONT		m_logFont;
	LOGFONT		m_zoomFont;
	double		m_dzoom;
	int			m_TextHeight;


	//自己修改加入的变量
	BOOL m_bUseRightYAxis;//是否标右Y轴
	BOOL m_bRightYMargin; //右Y轴留空格

	//long m_lStartTime;//计时起始时刻(ms)
	//double m_lStartTime;//计时起始时刻(s)
	BOOL m_bXThickGrid; //是否画X轴粗网格
	BOOL m_bXThinGrid;  //是否画X轴细网格
	BOOL m_bYThickGrid; //是否画Y轴粗网格
	BOOL m_bYThinGrid;  //是否画Y轴细网格
	BOOL m_bDrawLegend;     //是否显示图例(默认不显示)
	BOOL m_bDispXTitle;     //是否显示X轴名称(默认显示)
	BOOL m_bDispYTitle;     //是否显示Y轴名称(默认显示)
	BOOL m_bLeftYAutoAdjust;//Y轴坐标是否自动调整

	int  m_iXBigGrids;   //X轴分为m_iXBigGrid个大网格(模认为6)
	int  m_iYBigGrids;   //Y轴分为m_iYBigGrid个大网格(模认为5)

	//由于坐标轴是动态调整的,所以下面的两个值保存用户设定的值。实际显示的刻度是经过调整的值
	double m_dLeftYSetMinRange;//左Y轴设定的最小值(实际值)
	double m_dLeftYSetMaxRange;//左Y轴设定的最大值(实际值)
	
	COLORREF	m_txtColor;		// 标识符(刻度,X,Y轴名称及单位)颜色

private:
	



	//坐标图上当前点距离右轴占整个X轴长度的百分比,这样做便于看到后面曲线的期望值
	double m_dMarginPercentOfCurrentPointToRightY;//默认为0.3





	// Operations
public:
	BOOL		Create(DWORD dwstyle, CRect &rect, CWnd *pParent, UINT id);
	void		MoveWindow(CRect &Rect);

	virtual void Draw(CDC * dc);			// Draw the entire plot
	virtual void Print(CDC * dc);			// Print the entire plot to printer

	virtual BOOL AddPoint(int serie, long &valuetime, double &y);
	
   //将cplot一切状态复位回首次进入测试的状态(包括释放分配数据存储区,指针回位,重置即使起点等)
	virtual void Reset();
	
	//SetBXRange()的bMove项。只有为固定显示时,才调整横轴坐标比例。在动态显示时,
	//由于坐标自动调整,会使每次的显示比例不一致,这不是我们想要的。
	virtual void SetBXRange(long &fromtime, long &totime,BOOL bMove=TRUE);

	virtual void SetLYRange(double &minrange, double &maxrange);
	virtual void SetRYRange(double &minrange, double &maxrange);

	virtual void SetSerie(int s, int style, COLORREF color, double minrange, double maxrange, const char *szTitle, BOOL Rightalign=FALSE);
	virtual void SetLegend(int l, int style, COLORREF color, const char *text);
	virtual void SetBXTitle(const char *title);
	virtual void SetLYTitle(const char *title);
	virtual void SetRYTitle(const char *title);


	//自己修改加入的函数
	void SetXThickGridDisp(BOOL set=true);//set=true,draw;otherwise,not draw.
	void SetXThinGridDisp(BOOL set=true);//set=true,draw;otherwise,not draw.
	void SetYThickGridDisp(BOOL set=true);//set=true,draw;otherwise,not draw.
	void SetYThinGridDisp(BOOL set=true);//set=true,draw;otherwise,not draw.
	void SetLegendDraw(BOOL set=true);   //是否显示图例(默认不显示)
	void SetXTitleDisp(BOOL set=true);   //是否显示X轴名称(默认显示)
	void SetYTitleDisp(BOOL set=true);   //是否显示Y轴名称(默认显示)
	void SetLeftYAutoAdjust(BOOL set=true); //Y轴坐标是否自动调整(默认自动)

	void SetPlotBkColor(COLORREF newcolor);//设置曲线区的背景色
	void SetCtlBkColor(COLORREF newcolor);//设置图全部区域的背景色
	void SetGridColor(COLORREF newcolor);//设置主网格线的颜色
	
	void SetDiagramRect(CRect newrect,CDC *printerDC=NULL);//设置图的大小(父窗口中的逻辑坐标)
	void GetDiagramRect(CRect &rect);//得到图的大小(父窗口中的逻辑坐标)
	void GetXFromToTime(long &fromtime,long &totime);//得到当前图上的X轴起始时间

	//调整坐标图上当前点距离右轴占整个X轴长度的百分比,这样做便于看到
	//后面曲线的期望值
	void SetMarginPercentOfCurrentToRightY(double setpercent=0.3);



    virtual BOOL StoreToFile();
    virtual BOOL LoadFromFile();


private:

	virtual void DrawBasic(CDC * dc);		// Draw plot basics
	virtual void DrawPlot(CDC * dc);		// Draw the plot series
	virtual void DrawSerie(CDC *dc, int serie);
	virtual void DrawGrid(CDC * dc);		// Draw grids
	virtual void DrawLegendShadow(CDC * dc);// Draw legend shadows
	virtual void DrawLegend(CDC * dc);		// Draw legends

	virtual void DrawYAxisGrid(CDC * dc);
	virtual void DrawXAxisGrid(CDC * dc);

	//virtual void ComputeRects(BOOL bInitialize);
	virtual void ComputeRects(BOOL bInitialization=true,CDC *printerDCdc=NULL);


   

	

	
	// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(clPlot)
	public:
	virtual void Serialize(CArchive& ar);
	//}}AFX_VIRTUAL

// Implementation
public:

	// Generated message map functions
protected:
	//{{AFX_MSG(clPlot)
	afx_msg void OnPaint();
	//}}AFX_MSG
	BOOL clPlot::OnEraseBkgnd(CDC* pDC) ;
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DQPLOT_H__0D536D37_5CF1_11D1_AED1_0060973A08A4__INCLUDED_)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -