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

📄 clplot.cpp

📁 c++系统开发实例精粹内附的80例源代码 环境:windows2000,c++6.0
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::DrawBasic(CDC * dc)
{
	CBrush brushctlBkColor(m_ctlBkColor);
	dc->FillRect(m_ctlRect,&brushctlBkColor);
	if(m_bctlBorder)
	{
		dc->DrawEdge(m_ctlRect,BDR_SUNKENINNER|BDR_SUNKENOUTER, BF_RECT); 
	}
	dc->Rectangle(m_plotRect);
	DrawLegendShadow(dc);
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::DrawPlot(CDC * dc)
{
	for(int s=0;s<MAXSERIES;s++)
	{
		if(m_series[s].m_bIAmInUse)
		{
			DrawSerie(dc, s);
		}
	}
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::DrawSerie(CDC *dc,int s)
{
	BOOL bMore=TRUE;
	BOOL bDraw;
	CPoint p;
	int ly;
	// lets get some serie parameters now and save the time of indexing during the loop
	long y = m_series[s].m_lbegin;
	long m = m_series[s].m_lend;
	long a = m_series[s].m_lNoValues;
	BOOL bRightAxis = m_series[s].m_bRightAxisAlign;
	CPen pen(m_series[s].m_iLineStyle, 1, m_series[s].m_color);
	CPen *old = dc->SelectObject(&pen);
	while(bMore){
		bDraw=FALSE;
		bMore=FALSE;
		ly=0;
		while(y != m && !bDraw){
			if(m_series[s].m_pvalues[y].dValue == m_dNoData)
			{
				bDraw = TRUE;
				bMore = TRUE;
			}else{
//				Scaling. We do scaling inline to save some time
				time_t valuetime = m_series[s].m_pvalues[y].ValueTime.GetTime();
				p.x = (int)(m_plotRect.left + ((valuetime-m_timeaxis.m_mintime.GetTime())/m_timeaxis.m_dSecondsPrPixel));
				
				if(bRightAxis){
					p.y = (int)(m_plotRect.bottom - ((m_series[s].m_pvalues[y].dValue-m_rightaxis.minrange)/m_rightaxis.m_dValuePrPixel));
				}else{
					p.y = (int)(m_plotRect.bottom - ((m_series[s].m_pvalues[y].dValue-m_leftaxis.minrange)/m_leftaxis.m_dValuePrPixel));
				}

				if((ly == 0 || p.x != pLineArray[ly].x || p.y != pLineArray[ly].y)
					&& (p.x >= m_plotRect.left && p.x <= m_plotRect.right))
				{
					pLineArray[ly].x = p.x;
					pLineArray[ly].y = p.y;
					ly++;
				}
			}
			y++;
			if(y > a)	// wrap list index ?
				y=0;
		}
		if(ly > 0){
			dc->Polyline(pLineArray, ly);
		}
	}
	dc->SelectObject(old);
}

//*******************************************************************************************************/
//*
//*******************************************************************************************************/
void clPlot::DrawGrid(CDC * dc)
{
	DrawXAxisGrid(dc);
	DrawYAxisGrid(dc);
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::DrawYAxisGrid(CDC * dc)
{
	double yGrid = m_leftaxis.minrange;
	double delta = 25.0 + (long)(((m_leftaxis.m_dValuePrPixel)))*25;
	if((long)delta%50 != 0 && delta > 20.0)
		delta +=25;
	double d10 = delta / 5.0;
	// todo: delta switch
	long diff = ((long)yGrid)%((long)delta);
	yGrid = yGrid - diff;
	CPen *old, pen(PS_SOLID, 1, m_gridColor);
	CPen stick(PS_SOLID,0,RGB(0,0,0));
	CPen mline(PS_SOLID,0,RGB(192,192,192));

	for( long sy = (long)((long)(m_leftaxis.minrange) - diff); sy < m_leftaxis.maxrange; sy+=(long)d10)
	{
		int off=3;
		if((long)sy%(long)delta == 0){
			off=5;
		}
		if(sy > m_leftaxis.minrange){
			int y = (int)(m_plotRect.bottom - ((sy-m_leftaxis.minrange)/m_leftaxis.m_dValuePrPixel));
			old = dc->SelectObject(&stick); 
			dc->MoveTo(CPoint(m_plotRect.left,y));
			dc->LineTo(CPoint(m_plotRect.left-off,y));
			dc->MoveTo(CPoint(m_plotRect.right,y));
			dc->LineTo(CPoint(m_plotRect.right+off,y));
			dc->SelectObject(old);

			old = dc->SelectObject(&mline);
			dc->MoveTo(CPoint(m_plotRect.left+1,y));
			dc->LineTo(CPoint(m_plotRect.right-1,y));
			dc->SelectObject(old);
		}
	}

	old = dc->SelectObject(&pen);
	while(yGrid <= m_leftaxis.maxrange)
	{
		double yy = m_plotRect.bottom - ((yGrid-m_leftaxis.minrange)/m_leftaxis.m_dValuePrPixel);
		int y = (int)yy;
		if(yGrid > m_leftaxis.minrange && yGrid<m_leftaxis.maxrange){
			dc->MoveTo(CPoint(m_plotRect.left+1,y));
			dc->LineTo(CPoint(m_plotRect.right-1,y));
		}

		char b[100];
		sprintf(b, "%.0f", yGrid);
		dc->DrawText(b, CRect(m_clientRect.left, y-m_TextHeight/2,m_plotRect.left-5,y+m_TextHeight/2), DT_RIGHT|DT_BOTTOM);

		double yr = (m_plotRect.bottom - yy) * m_rightaxis.m_dValuePrPixel + m_rightaxis.minrange;
		sprintf(b, "%.0f", yr);
		dc->DrawText(b, CRect(m_plotRect.right+5, y-m_TextHeight/2,m_clientRect.right,y+m_TextHeight/2), DT_LEFT|DT_BOTTOM);

		yGrid += delta;
	}
	dc->SelectObject(old);
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::DrawXAxisGrid(CDC * dc)
{
	long yGrid = m_timeaxis.m_mintime.GetTime();
	long delta = (long)(10.0 + (long)(((m_timeaxis.m_dSecondsPrPixel)))*10);
	long d10 = (long)(delta / 10.0);
	// todo: delta switch
	long diff = ((long)yGrid)%((long)delta);
	yGrid = yGrid - diff;
	CPen *old, pen(PS_SOLID, 1, m_gridColor);
	CPen stick(PS_SOLID,0,RGB(0,0,0));
	CPen mline(PS_SOLID,0,RGB(192,192,192));

	for( long sx = m_timeaxis.m_mintime.GetTime() - diff; sx < m_timeaxis.m_maxtime.GetTime(); sx+=d10)
	{
		int off=3;
		if((long)sx%(long)delta == 0){
			off=5;
		}
		if(sx > m_timeaxis.m_mintime.GetTime()){
			int x = (int)(m_plotRect.left + ((sx-m_timeaxis.m_mintime.GetTime())/m_timeaxis.m_dSecondsPrPixel));
//			int x = m_plotRect.right - ((sx-m_timeaxis.m_mintime.GetTime())/m_timeaxis.m_lSecondsPrPixel);
			old = dc->SelectObject(&stick); 
			dc->MoveTo(CPoint(x,m_plotRect.bottom));
			dc->LineTo(CPoint(x+off,m_plotRect.bottom));
			dc->SelectObject(old);

			old = dc->SelectObject(&mline);
			dc->MoveTo(CPoint(x,m_plotRect.bottom-1));
			dc->LineTo(CPoint(x,m_plotRect.top+1));
			dc->SelectObject(old);
		}
	}

	old = dc->SelectObject(&pen);
	while(yGrid <= m_timeaxis.m_maxtime.GetTime())
	{
		int x = (int)(m_plotRect.left + ((yGrid-m_timeaxis.m_mintime.GetTime())/m_timeaxis.m_dSecondsPrPixel));

		if(yGrid > m_timeaxis.m_mintime.GetTime() && yGrid<m_timeaxis.m_maxtime.GetTime()){
			dc->MoveTo(CPoint(x,m_plotRect.bottom-1));
			dc->LineTo(CPoint(x,m_plotRect.top+1));
		}

//		char b[100];
//		sprintf(b, "%.0f", yGrid);
//		dc->DrawText(b, CRect(m_clientRect.left, y-m_TextHeight/2,m_plotRect.left-5,y+m_TextHeight/2), DT_RIGHT|DT_BOTTOM);

		yGrid += delta;
	}
	dc->SelectObject(old);
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::DrawLegendShadow(CDC * dc)
{
	if(m_blegendBorder){
		CPen pen(PS_SOLID, 1, RGB(127,127,127));
		CPen *oPen = dc->SelectObject(&pen);
		CBrush *oBrush , brush(RGB(127,127,127));
		oBrush = dc->SelectObject(&brush);
		dc->Rectangle(CRect(m_legendRect.left+5,m_legendRect.top+5,m_legendRect.right+5, m_legendRect.bottom+5));
		dc->SelectObject(oBrush);
		dc->SelectObject(oPen);
	}
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::DrawLegend(CDC * dc)
{
	if(m_blegendBorder){
		CPen pen(PS_SOLID, 1, RGB(0,0,0));
		CPen *oPen = dc->SelectObject(&pen);
		CBrush *oBrush , brush(m_legendBkColor);
		oBrush = dc->SelectObject(&brush);
		dc->Rectangle(m_legendRect);
		dc->SelectObject(oBrush);
		dc->SelectObject(oPen);
	}
	int y = m_legendRect.top + 5;
	int dx = m_legendRect.left + (2*m_TextHeight);
	int mh = m_TextHeight/2;
	for(int x = 0; x< MAXLEGENDS;x++){
		if(m_primarylegends[x].m_bIAmInUse){
			CRect lRect(	dx + 5, y, m_legendRect.right - 5, y + m_TextHeight);
			CPen pen(m_primarylegends[x].m_istyle, 1, m_primarylegends[x].m_color);
			CPen *oPen = dc->SelectObject(&pen);
			dc->MoveTo(CPoint(m_legendRect.left+5, y + mh));
			dc->LineTo(CPoint(dx, y+mh));
			dc->SelectObject(oPen);

			dc->DrawText(m_primarylegends[x].m_szTitle, lRect, DT_LEFT);
			y+=m_TextHeight+1;
		}
	}
}

//*******************************************************************************************************/
//* Function:		clPlot::AddPoint
//*******************************************************************************************************/
BOOL clPlot::AddPoint(int serie, CTime &valuetime, double &value)
{
	if(m_series[serie].m_lNoValues < m_lMaxDataPrSerie){
		m_series[serie].AddPoint(valuetime, value);
		if(m_bAutoScrollX && valuetime > m_timeaxis.m_maxtime){
			time_t span = m_timeaxis.m_maxtime.GetTime() - m_timeaxis.m_mintime.GetTime();
			time_t mintime = valuetime.GetTime() - span;

			SetBXRange(CTime(mintime), valuetime);
		}
		return TRUE;
	}
	return FALSE;
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetBXRange(CTime &fromtime, CTime &totime, BOOL bMove)
{
	m_timeaxis.m_mintime = fromtime;
	m_timeaxis.m_maxtime = totime;
	if(!bMove){
		m_timeaxis.m_dSecondsPrPixel = ((double)(m_timeaxis.m_maxtime.GetTime() - m_timeaxis.m_mintime.GetTime()+1)) / (double)m_plotRect.Width();
	}
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetLYRange(double &minrange, double &maxrange)
{
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetRYRange(double &minrange, double &maxrange)
{
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetBXTitle(const char *title)
{
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetLYTitle(const char *title)
{
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetRYTitle(const char *title)
{
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::Reset()
{
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetSerie(int s, int style, COLORREF color, double minrange, double maxrange, const char *szTitle, BOOL Rightalign)
{
	m_series[s].m_bIAmInUse = TRUE;
	m_series[s].m_color = color;
	m_series[s].m_iLineStyle = style;
	m_series[s].m_bRightAxisAlign = Rightalign;
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::MoveWindow(CRect & Rect)
{
	m_ctlRect = Rect;
	GetParent()->ClientToScreen(m_ctlRect);
	ScreenToClient(m_ctlRect);
	ComputeRects(TRUE);
	CWnd::MoveWindow(Rect);
}

//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetLegend(int l, int style, COLORREF color, const char *text)
{
	m_primarylegends[l].m_bIAmInUse	= TRUE;
	m_primarylegends[l].m_color		= color;
	m_primarylegends[l].m_istyle		= style;
	m_primarylegends[l].m_szTitle		= text;
	CClientDC dc(this);
	CFont *oFont = dc.SelectObject(&m_font);
	int w = 0;
	int n=0;
	for(int x = 0; x< MAXLEGENDS;x++){
		if(m_primarylegends[x].m_bIAmInUse){
			n++;
			CSize z=dc.GetTextExtent(CString(m_primarylegends[x].m_szTitle));
			if(z.cx > w )
				w=z.cx;
//			m_TextHeight = z.cy;
		}
	}

	m_legendRect.right = m_legendRect.left + 10+(2*m_TextHeight) + w;
	m_legendRect.bottom = m_legendRect.top + 10 + (m_TextHeight*n);
	dc.SelectObject(oFont);
}

⌨️ 快捷键说明

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