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

📄 realtime.cpp

📁 一个多线程的网络数据采集系统(客户端)
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "stdafx.h"
#include "RealTime.h"

CALine::CALine()
{ 
	m_nInterval = 1000;//1s
	m_nColor = RGB(0, 0, 0);//黑色		
	m_nLineStyle = PS_SOLID;//实线	
	m_nLineWidth = 0;	
	m_sName	= "";		
	m_sDescription = "";	
	m_sUnit	= "";		
	m_dMin = 0;			
	m_dMax = 0;			
	m_dScaleLow	= 0;	
	m_dScaleHigh = 0;	
}

void CALine::AddValue(SPLINEDATA value)
{
	m_value.Add(value);
	if(m_value.GetCount() == 1)
	{
		m_dMin = m_dMax = value.value;
	}
	else
	{
		if(value.value < m_dMin)m_dMin = value.value;
		if(value.value > m_dMax)m_dMax = value.value;
	}
	m_dNowValue = value.value;
}

CRealTime::CRealTime()
{
    SetRatio(0, 0, 1, 1);
	m_nSize = 0;
	m_nBkColor = RGB(255, 255, 255);//白色
	m_nGridColor = RGB(192, 192, 192);//浅灰色
	m_nBorderColor = RGB(0, 0, 0);//黑色
	m_nTickColor = RGB(0, 0, 255);////亮蓝色
	m_nTitleColor = RGB(255, 0, 0);//亮红色
	m_nBoundaryColor = RGB(255, 0, 0);
	SetStringAlign(LEFT, TOP);
	m_bPrepared = false;
	m_bForward = true;
	m_bRealTime = false;
    m_bCursorlineFlag = true;

	m_SpeedLevel[0] = 500000;	// 500 seconds
	m_SpeedLevel[1] = 100000;	// 100 seconds
	m_SpeedLevel[2] = 50000;	// 50 seconds
	m_SpeedLevel[3] = 20000;	// 20 seconds
	m_SpeedLevel[4] = 10000;	// 10 seconds
	m_SpeedLevel[5] = 5000;		// 5 seconds
	m_SpeedLevel[6] = 2000;		// 2 seconds
	m_SpeedLevel[7] = 1000;		// 1 seconds
	m_SpeedLevel[8] = 500;		// 0.5 seconds
	m_SpeedLevel[9] = 200;		// 0.2 seconds
	m_SpeedLevel[10] = 100;		// 0.1 seconds
	m_SpeedLevel[11] = 50;		// 0.05 seconds
	m_SpeedLevel[12] = 20;		// 0.02 seconds
	m_SpeedLevel[13] = 10;		// 0.01 seconds

	m_gapLevel[0] = 0.01;//(100 cycle per unit)
	m_gapLevel[1] = 0.1;//(10 cycle per unit)
	m_gapLevel[2] = 1;//(1 cycle per unit)
	m_gapLevel[3] = 2;//(0.5 cycle per unit)
	m_gapLevel[4] = 5;//(0.2 cycle per unit)
	m_gapLevel[5] = 10;//(0.1 cycle per unit)
	m_gapLevel[6] = 20;//(0.05 cycle per unit)
	m_gapLevel[7] = 50;//(0.02 cycle per unit)
	m_gapLevel[8] = 100;//(0.01 cycle per unit)
	m_gapLevel[9] = 200;//(0.005 cycle per unit)
    
	m_nTimeInterval = 1000;
	m_nCursorTime = 0;
	m_nMaxTime = 0;
	m_dTotalYMin = 0;
	m_dTotalYMax = 0;
    m_nXTicks = 30;
	m_nXTicksInLarge = 5;
	m_nYTicks = 50;
	m_nYTicksInLarge = 5;
	m_nGridShow = BIGGRID;
//	m_nGridShow = SMALLGRID;
	m_redrawFlag = true;
	m_bControl = false;
	m_nOffset = 0;
	m_nYOffset = 0;
	m_nTick = 0;
	m_nPage = -1;
	m_nMaxPages = 0;
	m_X = 0;
	m_nYPages = 2;
	m_dYMin = -100;
	m_dYMax = 100;
	nEditLIndex = -1;
    nEditPIndex = -1;
 }

void CRealTime::PrepareEdit(CView *pView, CPoint point)
{
	int nF, nT, nIndex;
	nEditPIndex = -1;
	nIndex = GetEditCurve();
	if(nIndex < 0 || nIndex >= m_cLines.GetCount())return;
    m_pCurLine = &(m_cLines[nIndex]);
	int nCount = m_pCurLine->GetCounter();
	if(nCount == 0)return;
	nF = (int)(m_scale.xmin / m_pCurLine->m_nInterval);
	nT = (int)(m_scale.xmax / m_pCurLine->m_nInterval);
	if((nF * m_pCurLine->m_nInterval) != (DWORD)(m_scale.xmin))nF++;
	if(nT >= nCount)nT = nCount - 1;
	for(nIndex = nF; nIndex < nT + 1; nIndex++)
	{
		double value = (*m_pCurLine)[nIndex].value;
		if(value >= m_scale.ymin && value <= m_scale.ymax)
		{
            int x, y;
			x = (int)((nIndex * m_pCurLine->m_nInterval - m_scale.xmin) / m_scale.dx) + m_plotRect.left;
			y = (int)((m_scale.ymax - value) / m_scale.dy) + m_plotRect.top;
			CRect rect(x - 3, y - 3, x + 3, y + 3);
			CRgn rgn;
			rgn.CreateEllipticRgnIndirect(rect);
			if(rgn.PtInRegion(point))
			{
                nEditPIndex = nIndex;
				pView->SetCapture();
				return;
			}
		}
	}   
}

bool CRealTime::DoEditing(CView *pView, CPoint point)
{
    if(nEditLIndex != -1 && nEditPIndex != -1)
	{
		double value;
		if(m_plotRect.PtInRect(point))
		{
			value = m_scale.ymax - (point.y - m_plotRect.top) * m_scale.dy;
            m_nOffset = m_nYOffset = 0;
		}
		else
		{
			if(point.y <= m_plotRect.top)
			{
				m_nOffset = 0;
				m_nYOffset = m_nYTicksInLarge;
				value = m_scale.ymax + (m_scale.ymax - m_scale.ymin) / m_nYTicks;
			}
			else
			{
				m_nOffset = 0;
				m_nYOffset = 0 - m_nYTicksInLarge;
				value = m_scale.ymin - (m_scale.ymax - m_scale.ymin) / m_nYTicks;
			}
		}
		m_pCurLine = &(m_cLines[nEditLIndex]);
		m_pCurLine->SetValue(nEditPIndex, value);
		if(m_dTotalYMin > value)m_dTotalYMin = value;
		if(m_dTotalYMax < value)m_dTotalYMax = value;
		return true;
	}
	return false;
}

void CRealTime::FinishEditing(CView *pView, CPoint point)
{
	DoEditing(pView, point);
	if(nEditLIndex != -1 && nEditPIndex != -1)
	{
		if(pView->GetCapture() == pView)
		{
			::ReleaseCapture();
			nEditPIndex = -1;
		}
	}
}

bool CRealTime::SetXAxis(int XTicks, int XTicksInLarge)
{
    if(XTicksInLarge < 2 || XTicks <= XTicksInLarge)
		return false;
	m_nXTicks = XTicks;
	m_nXTicksInLarge = XTicksInLarge;
	return true;
}

bool CRealTime::SetYAxis(int YTicks, int YTicksInLarge)
{
	if(YTicksInLarge < 2 || YTicks <= YTicksInLarge)
		return false;
	m_nYTicks = YTicks;
	m_nYTicksInLarge = YTicksInLarge;
	return true;
}

bool CRealTime::InitialSetting(int XTicks, int XTicksInLarge, int YTicks,
							   int YTicksInLarge, DWORD SecsPerCycle, double ymin, double UnitsPerCycle)
{
	if (! SetXAxis(XTicks, XTicksInLarge))
	{
		m_bSetingFailed = true;
		return false;
	}
	if (! SetYAxis(YTicks, YTicksInLarge))
	{
		m_bSetingFailed = true;
		return false;
	}
    if(! SetRange(0, ymin, SecsPerCycle * XTicks, ymin + UnitsPerCycle * YTicks))
	{
		m_bSetingFailed = true;
		return false;
	}
	m_nCyclesPerSec = 1000 / (double)(SecsPerCycle);
	m_nUnitsPerCycle = UnitsPerCycle;
	int nPages = m_nYPages / 2;
	m_dYMin = m_scale.ymin - m_nUnitsPerCycle * m_nYTicks * (m_nYPages - nPages);
	m_dYMax = m_scale.ymin + m_nUnitsPerCycle * m_nYTicks * nPages;
	return m_bSetingFailed;
}

bool CRealTime::AdjustXSetting(double nUnitsPerCycle, int& nMax, int& nPos)
{
	if(nUnitsPerCycle > 0)
	{
        double temp;
		//计算新的总刻度数
		temp = m_nCursorTime / (nUnitsPerCycle * 1000);
		nMax = (int)temp;
		if(nMax < temp)nMax++;
		if(nMax < m_nXTicks)nMax = m_nXTicks;
		//计算显示页的新X轴范围
		temp = m_scale.xmin / (nUnitsPerCycle * 1000);
		nPos = (int)temp;
		if(nPos + m_nXTicks > nMax)
		{
			nPos = nMax - m_nXTicks;				
		}
		temp = nPos * (nUnitsPerCycle * 1000);
        m_bSetingFailed = ! SetXRange(temp, temp + nUnitsPerCycle * 1000 * m_nXTicks);
        if(! m_bSetingFailed)
		{
			m_nMaxPages = (nMax / m_nXTicks) + 1;
			nPos += m_nXTicks;
			m_nPage = nPos / m_nXTicks;
			m_nTick = nPos % m_nXTicks;
		    m_nCyclesPerSec = 1 / nUnitsPerCycle;
			m_redrawFlag = true;
		}
	}
	else
		m_bSetingFailed = false;
	return m_bSetingFailed;
}

bool CRealTime::AdjustYSetting(double nUnitsPerCycle)
{
	if(nUnitsPerCycle > 0)
	{
        double dTemp;
		bool bFlag = false;//true:表示负数,false:表示非负数
		DWORD temp1, temp2, nUnits;
		nUnits = nUnitsPerCycle * 100;
		//计算显示页的新Y轴范围
		dTemp = m_scale.ymin;
		if(dTemp < 0)
		{
			dTemp = 0 - dTemp;
			bFlag = true;
		}
		temp1 = (DWORD)((DWORD)dTemp * 100) + (DWORD)((dTemp - (int)dTemp) * 100);
		temp2 = temp1 / nUnits;
		if(bFlag && (temp1 % nUnits))temp2++;
		temp2 *= nUnits;
		dTemp = (temp2 / 100) + (temp2 % 100) * 0.01;
		if(bFlag)dTemp = 0 - dTemp;
        m_bSetingFailed = ! SetYRange(dTemp, dTemp + nUnitsPerCycle * m_nYTicks);
		if(! m_bSetingFailed)
		{
            int nPages = m_nYPages / 2;
			m_nUnitsPerCycle = nUnitsPerCycle;
			m_dYMin = m_scale.ymin - m_nUnitsPerCycle * m_nYTicks * (m_nYPages - nPages);
			m_dYMax = m_scale.ymin + m_nUnitsPerCycle * m_nYTicks * nPages;
			m_redrawFlag = true;
		}
	}
	else
		m_bSetingFailed = false;
	return m_bSetingFailed;
}

void CRealTime::PrepareSketch(double& XUnits, double& YUnits)
{
    if(m_nMaxTime == 0)return;
	//调整Y轴显示范围和比例
	double nUnitsPerCycle, nSpan;
	DWORD nUnits;
	nSpan = m_dTotalYMax - m_dTotalYMin;
	nUnits = ((DWORD)(nSpan * 100)) / m_nYTicks;
	if(((DWORD)(nSpan * 100)) % m_nYTicks)nUnits++;
    int i, j;
	i = 1;
	while((j = nUnits / i) >= 10)
	{
		i *= 10;
	}
    if(nUnits % i)j++;
	j++;
	nUnits = j * i;
	nUnitsPerCycle = (double)(nUnits) / 100;
	m_scale.ymin = m_dTotalYMin - nUnitsPerCycle;
	AdjustYSetting(nUnitsPerCycle);
	YUnits = nUnitsPerCycle;
	//调整X轴显示范围和比例
//	if(m_nCursorTime > m_nXTicks * (DWORD)(1000 / m_nCyclesPerSec + 0.5))
//	{
        nUnits = m_nCursorTime / m_nXTicks;
		i = 1;
		while((j = nUnits / i) >= 10)
		{
			i *= 10;
		}
		if(nUnits % i)j++;
		nUnits = j * i;
		SetXRange(0, m_nXTicks * nUnits); 
		m_nMaxPages = 2;
		m_nPage = 1;
		m_nTick = 0;
		m_nCyclesPerSec = 1000.0 / nUnits;
//	}
	XUnits = 1 / m_nCyclesPerSec;
	m_redrawFlag = true;
	nEditLIndex = nEditPIndex = -1;
}

void CRealTime::DrawBkGround(CDC *pDC,CRect &rt)
{
	CBrush bkBrush(m_nBkColor);
	pDC->SetBkColor(m_nBkColor);
	pDC->FillRect(rt, &bkBrush);	
}

bool CRealTime::SetRatio(double xmin, double ymin, double xmax, double ymax)
{
	if (xmin < 0 || ymin < 0 || xmax > 1 || ymax >1 || xmax < xmin || ymax < ymin)
		return false;
	m_ratio.xmin = xmin;
	m_ratio.ymin = ymin;
	m_ratio.xmax = xmax;
	m_ratio.ymax = ymax;
	return true;
}

void CRealTime::GetPixelRect(RECT& rt)
{
	rt.left	= m_drawRect.left;
	rt.top = m_drawRect.top;
	rt.right = m_drawRect.right;
	rt.bottom = m_drawRect.bottom;
}

bool CRealTime::SetXRange(double xmin, double xmax)
{
    if (xmin < 0 || xmax <= xmin)return false;
	m_scale.xmin = xmin;
    m_scale.xmax = xmax;
    return true;
}

bool CRealTime::SetYRange(double ymin, double ymax)
{
	if(ymax <= ymin)return false;
	m_scale.ymin = ymin;
    m_scale.ymax = ymax;
	return true;
}

bool CRealTime::SetRange(double xmin, double ymin, double xmax, double ymax)
{
	if (xmin < 0 || xmax <= xmin || ymax <= ymin)
		return false;
	m_scale.xmin = xmin;
    m_scale.ymin = ymin;
    m_scale.xmax = xmax;
    m_scale.ymax = ymax;
	return true; 
}

void CRealTime::SetPixelRect(RECT& rt)
{
	LONG Width = rt.right - rt.left;
    LONG Height = rt.bottom - rt.top; 
	m_drawRect.left = (LONG)(rt.left + m_ratio.xmin * Width);
	m_drawRect.top = (LONG)(rt.top + m_ratio.ymin * Height);
	m_drawRect.right = (LONG)(rt.right - (1 - m_ratio.xmax) * Width);
	m_drawRect.bottom = (LONG)(rt.bottom - (1 - m_ratio.ymax) * Height);
}

void CRealTime::RecalcRects(CRect& rt)

⌨️ 快捷键说明

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