标题 以曲线或柱状图实时显示数据的控件
这是一款可以以曲线或柱状图的形式实时动态显示数据的控件。
主要特点是:支持显示浮点数、负数,显示数据范围大、可以自定义显示范围也可自动选择范围,还可锁定显示范围。
使用方法:
1.首先放一个CButton,并对这个按钮生成一个对象。例如 CButton m_btn ;
2.更改按钮的类名 改为 CMulticolorPlotBtn m_btn
3.初始化: 初始化由自己绝对初始化那些(当然也可省略这步)
4。显示数据。m_btn.SetData( ( float ) rand () ) ;
//////////////////////////////////// MulticolorPlotBtn.h ////////////////////////////////////
/*
模块名称:MulticolorPlotBtn.h
模块功能:动态图表显示数据
备 注:支持 UNICODE 和 ANSI 编码
:测试平台 Windows 2000、IE6、Direcx9、 AMD Xp 2000 + CPU
:不兼容 Windows 98
功能简介:动态实时显示数据,显示方式有BAR和LINE(其它方式以后添加)
,可动态实时改变曲线属性。支持任意范围数据,可设定范围,
也可不设定显示范围(自动寻找最符合的范围),还可任意锁定
(自由)范围。支持浮点数。
*/
#pragma once
#include "afxwin.h"
#include <math.h>
#define BAR 0
#define LINE 1
class CMulticolorPlotBtn :
public CButton
{
public:
CMulticolorPlotBtn(void) ;
virtual ~CMulticolorPlotBtn(void);
private: // 公共属性
int nPlotType ; // 曲线类型 BAR 或 LINE // BAR
bool bfInit ; // 是否初始化 // false
// 网格X方向上的间距
int nGridResolutionX ; // 10
// 网格Y方向上的间距
int nGridResolutionY ; // 10
// 网格滚动的速度和方向(正值为从左向右滚动和从上到下,否则反之。0不动)
int nGridScrollSpeedX ; // -1
int nGridScrollSpeedY ; // 0
// 数据点分辨率大小(即:一个数据点占据的像素数)
int nPlotGranulatrity ; // 2
// 网格线宽度
int nGridLineWidth ; // 1
// 背景色
COLORREF m_clrDarkBack ; // RGB(0,0,75)
// 前景色
COLORREF m_clrDarkLine ; // RGB(32,64,32)
// 控件矩形
RECT m_rect ;
// 控件的尺寸
CSize m_Size ;
// 控件可容纳可见的数据点数
int nPlotData ; // 0
// 实际数据
float * pfData ; // 0
// 数据范围
float fLow , fHigh ; // 0,0
// 数据比例
float fScaleY ; // 1.0
// 数据点处的颜色
COLORREF m_clrCyanData ; // RGB ( 0,255,255 )
// 画笔
CPen m_GridPen ;
// 数据点位图画刷
CBrush m_clrBrush ;
// 网格开始位置
int nGridStarPosX , nGridStarPosY ; // 0,0
// 锁定显示范围
bool bLock ; // true---锁定
// 控件上显示的字体
CFont m_SmallFont ;
// Y轴刻度的颜色
COLORREF m_clrAxisScaleY ; // RGB ( 0,255,255 )
// 是否显示Y轴刻度数字
int nShowFormatDataText ; // 0---不显示
// 控件标题
TCHAR szTitls [MAX_PATH * sizeof ( TCHAR ) + 1] ; // NULL
// 曲线单位
TCHAR szUints [32 * sizeof ( TCHAR ) + 1] ; // NULL
private:
// 关键代码
CRITICAL_SECTION g_cs ;
public: // 公共方法
// 设置画线的类型
void SetPlotType ( int nType )
{
nPlotType = nType ;
bfInit = false ;
}
// 设置网格间距
void SetGridResolutionX ( int nGridReluX ) { nGridResolutionX = nGridReluX ; }
void SetGridResolutionY ( int nGridReluY ) { nGridResolutionY = nGridReluY ; }
// 设置网格滚动速度
// 正值为从左向右滚动,0不动
void SetGridScrollSpeedX ( int nSpeedX ) { nGridScrollSpeedX = nSpeedX ; }
// 正值为从上到下滚动,0不动
void SetGridScrollSpeedY ( int nSpeedY ) { nGridScrollSpeedY = nSpeedY ; }
// 数据点面积大小(即:一个数据点占据的像素数)
bool SetPlotGranulatrity ( int nPlotGrltiy )
{
nPlotGranulatrity = nPlotGrltiy ;
nPlotData = m_Size.cx / nPlotGranulatrity ;
pfData = new float [ nPlotData ] ;
if ( pfData )
{
// 初始化数据为 0
ZeroMemory ( pfData , sizeof ( pfData ) * nPlotData ) ;
return true ;
}
return true ;
}
// 网格线宽度
void SetGridLineWidth ( int nWidth ) { nGridLineWidth = nWidth ; }
// 背景色
void SetGridBackClr ( COLORREF clr ) { m_clrDarkBack = clr ; }
// 前景色
void SetGridLineClr ( COLORREF clr )
{
m_clrDarkLine = clr ;
m_GridPen.CreatePen ( PS_SOLID , nGridLineWidth , m_clrDarkLine ) ;
m_clrDarkLine = clr ;
}
// 数据范围
void SetRang ( float fL , float fH )
{
fLow = fL ;
fHigh = fH ;
}
// 创建画笔
void SetLinePen ( int nWidth , COLORREF clr )
{
nGridLineWidth = nWidth ;
m_clrDarkLine = clr ;
m_GridPen.CreatePen ( PS_SOLID , nGridLineWidth , m_clrDarkLine ) ;
}
// 锁定数据显示范围
void LockRang ( bool bfLock = true ) ;
void LockRang ( float fMin = 0.0 , float fMax = 100.0 ) ;
// Y轴刻度颜色,bfShow==0显示刻度数字,1仅在左上角显示1000(k/sec)/2000样子的数字,2显示最大最小,3显示全部
void SetAxisScaleClrY ( COLORREF clr = RGB ( 0 , 255 , 255 ) )
{
m_clrAxisScaleY = clr ;
}
// 标题
void SetTitle ( PCTSTR pctTitle = NULL )
{
_stprintf ( szTitls , _TEXT ( "%s" ) , pctTitle ) ;
}
// 数据单位例:SetUnit( _TEXT ( "(k/sec)" ) ) ;
void SetUnit ( PCTSTR pctUint = NULL )
{
_stprintf ( szUints , _TEXT ( "%s" ) , pctUint ) ;
}
// 是否显示数据标题、颜色、单位
/*
nShow = 0 ; 不显示任何文本
nShow = 1 ; 仅仅显示标题
nShow = 2 ; 仅仅显示标题和流量总计
nShow = 3 ; 显示标题和最小值
nShow = 4 ; 显示标题、最小值和中值
*/
void ShowTitle ( int nShow = 1 )
{
nShowFormatDataText = nShow ;
}
private: // BAR属性
// BAR颜色
COLORREF m_clrUp ;
COLORREF m_clrDown ;
public: // BAR方法
// BAR 颜色
// clrUp -------- 顶部颜色
// clrDown -------- 底部颜色
// bfRfastness -------- 红色分量是否固定不变 false ---- 渐变
// bfGfastness -------- 红色分量是否固定不变 false ---- 渐变
// bfBfastness -------- 红色分量是否固定不变 false ---- 渐变
void SetBarColor ( COLORREF clrUp , COLORREF clrDown , bool bfRfastness = false , bool bfGfastness = false , bool bfBfastness = false ) ;
// 设置数据
void SetData ( float fData ) ;
private: // LINE属性
// 曲线颜色
COLORREF m_clrLinePen ; // RGB(0,255,0)
// 曲线宽度
int nLineWidth ;
public: // LINE方法
// 曲线颜色
void SetLineColorWidth ( COLORREF clrLine = RGB ( 0 , 255 , 0 ) , int nWidth = 1 )
{
nLineWidth = nWidth ;
m_clrLinePen = clrLine ;
}
void SetLineColor ( COLORREF clrLine = RGB ( 0 , 255 , 0 ) ) { m_clrLinePen = clrLine ; }
void SetLineWidth ( int nWidth = 1 ) { nLineWidth = nWidth ; }
const COLORREF GetLineColor () { return m_clrLinePen ; }
const int GetLineWidth () { return nLineWidth ; }
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnTimer(UINT nIDEvent);
protected:
virtual void PreSubclassWindow();
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
};
//////////////////////////////////// end ////////////////////////////////////
//////////////////////////////////// MulticolorPlotBtn.cpp ////////////////////////////////////
#include "StdAfx.h"
#include ".\multicolorplotbtn.h"
#include "MemDC.h"
#define GRID_TIMER 1
//网格刷新速率
#define GRID_UPDATE_SPEED 50
typedef struct
{
float fx ;
float fy ;
} DATA_POINT ;
DATA_POINT * g_DataPoint ;
CMulticolorPlotBtn::CMulticolorPlotBtn()
: nPlotType(BAR)
, bfInit(false)
, nGridResolutionX(10)
, nGridResolutionY(10)
, nGridScrollSpeedX(-1)
, nGridScrollSpeedY(0)
, nPlotGranulatrity(2)
, nGridLineWidth(1)
, m_clrDarkBack(RGB(0,0,75))
, m_clrDarkLine(RGB(32,64,32))
, nPlotData(0)
, pfData(NULL)
, fLow(0.0)
, fHigh(100.0)
, fScaleY(1.0)
, m_clrCyanData(RGB(0,255,255))
, nGridStarPosX(0)
, nGridStarPosY(0)
, bLock(true)
, m_clrAxisScaleY(RGB(0,255,255))
, nShowFormatDataText(0)
, m_clrLinePen(RGB(0,255,0))
, nLineWidth(1)
{
// 关键代码
// 初始化关键代码的 C_S 结构
InitializeCriticalSection ( & g_cs ) ;
// 初始化标题
_stprintf ( szTitls , _TEXT ( "%s" ) , _TEXT ( "" ) ) ;
// 初始化单位
_stprintf ( szUints , _TEXT ( "%s" ) , _TEXT ( "" ) ) ;
}
CMulticolorPlotBtn::~CMulticolorPlotBtn(void)
{
if ( pfData )
{
delete [] pfData ;
}
// 释放关键代码
DeleteC