📄 clplot.cpp
字号:
//
//*******************************************************************************************************/
void clPlot::DrawBasic(CDC * dc)
{
CBrush brushctlBkColor(m_ctlBkColor);
dc->FillRect(m_clientRect, &brushctlBkColor);
dc->Rectangle(m_plotRect);
}
//*******************************************************************************************************/
// Modified for this competition
//*******************************************************************************************************/
void clPlot::DrawPlot(CDC * dc)
{
for ( int s=0; s < SERIENUMBER; s++ )
DrawSerie(dc, s);
}
void clPlot::DrawSerie(CDC *dc, int s)
{
int begin = m_series[s].m_lbegin;
int end = m_series[s].m_lend;
CPoint p;
CPoint *pLineArray;
pLineArray = new CPoint[MAXSIRIEDATA];
int ly = 0;
int height = ( m_plotRect.bottom - m_plotRect.top ) / 50 * 50;
while ( begin != end )
{
time_t valuetime = m_series[s].m_pvalues[begin].ValueTime.GetTime();
p.x = (int)(m_plotRect.left + ((valuetime-m_timeaxis.m_mintime.GetTime()) /
m_timeaxis.m_dSecondsPrPixel));
double temp = ((double)(m_series[s].m_pvalues[begin].dValue)) / (m_series[s].m_nMaxValue - m_series[s].m_nMinValue);
p.y = (int)( m_plotRect.bottom - height*temp );
if ( p.x >= m_plotRect.left && p.x <= m_plotRect.right )
{
pLineArray[ly].x = p.x;
pLineArray[ly].y = p.y;
ly++;
}
begin = ( begin + 1 ) % MAXSIRIEDATA;
}
if ( ly > 0)
{
CPen pen(m_series[s].m_iLineStyle, 1, m_series[s].m_color);
CPen *old = dc->SelectObject(&pen);
dc->Polyline(pLineArray, ly);
dc->SelectObject(old);
}
delete []pLineArray;
}
//*******************************************************************************************************/
//* 画网格及坐标轴
//*******************************************************************************************************/
void clPlot::DrawGrid(CDC * dc)
{
DrawXAxisGrid(dc);
DrawYAxisGrid(dc);
}
//*******************************************************************************************************/
// 画横向网格及坐标值
// Author: Bill Chen
//*******************************************************************************************************/
void clPlot::DrawYAxisGrid(CDC * dc)
{
CPen stick(PS_SOLID, 0, RGB(0,0,0));
CPen mline(PS_SOLID, 0, RGB(192,192,192));
CPen *old;
int delt = m_plotRect.Height() / 50; // 无论比例如何, 横向 50 条线
int sx = m_plotRect.left;
int ex = m_plotRect.right;
int y = m_plotRect.bottom - delt;
for ( int i = 1; y >= m_plotRect.top; i++, y -= delt )
{
if ( i%5 == 0 )
{
old = dc->SelectObject(&stick);
dc->MoveTo(CPoint(sx-2, y));
dc->LineTo(CPoint(ex, y));
dc->SelectObject(old);
}
else
{
old = dc->SelectObject(&mline);
dc->MoveTo(sx, y);
dc->LineTo(ex, y);
dc->SelectObject(old);
}
}
}
//*******************************************************************************************************/
// 画纵向网格
// Author: Bill Chen
//*******************************************************************************************************/
void clPlot::DrawXAxisGrid(CDC * dc)
{
CPen stick(PS_SOLID, 0, RGB(0,0,0));
CPen mline(PS_SOLID, 0, RGB(192,192,192)); //粗线
CPen *old;
int delt = m_plotRect.Width() / 60; // 无论比例如何, 纵向 60 条线
int x = m_plotRect.right - delt; // x, changed
int sy = m_plotRect.bottom; // y start
int ey = m_plotRect.top; // y end
UINT oldMode = dc->SetTextAlign( TA_CENTER | TA_TOP );
for ( int i = 1; x >= m_plotRect.left; i++, x -= delt )
{
if ( i%6 == 0 ) // 每间隔 10 条线, 画一条粗线
{
old = dc->SelectObject(&stick);
dc->MoveTo(CPoint(x, sy+2));
dc->LineTo(CPoint(x, ey));
dc->SelectObject(old);
CString str;
str.Format( "-%d", i * m_nSpanTime / 60 );
//UINT oldMode = dc->SetTextAlign( TA_CENTER | TA_TOP );
dc->TextOut( x, sy+5, str );
//dc->SetTextAlign ( oldMode );
}
else
{
old = dc->SelectObject(&mline);
dc->MoveTo(x, sy);
dc->LineTo(x, ey);
dc->SelectObject(old);
}
}
dc->TextOut( m_plotRect.right, sy+5, "Current" );
dc->SetTextAlign ( oldMode );
oldMode = dc->SetTextAlign( TA_RIGHT | TA_TOP );
dc->TextOut( m_plotRect.left-5, sy+5, "Time axis(s):" );
dc->SetTextAlign ( oldMode );
}
//*******************************************************************************************************/
//* Function: clPlot::AddPoint
//*******************************************************************************************************/
BOOL clPlot::AddPoint(int serie, CTime &valuetime, double value)
{
m_series[serie].AddPoint( valuetime, value );
if ( value > (1.1*m_series[serie].m_nMaxValue) )
m_series[serie].m_nMaxValue = (((int)(value))/10+1)*10;
if ( m_bAutoScrollX && (valuetime > m_timeaxis.m_maxtime) )
{
CTimeSpan span = m_timeaxis.m_maxtime - m_timeaxis.m_mintime;
CTime mintime = valuetime - span;
SetBXRange( mintime, valuetime );
}
return TRUE;
}
//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetBXRange(CTime &fromtime, CTime &totime, BOOL bMove)
{
m_timeaxis.m_mintime = fromtime;
m_timeaxis.m_maxtime = totime;
if ( !bMove ) // bMove = true;
{
m_timeaxis.m_dSecondsPrPixel =
( (double)(m_timeaxis.m_maxtime.GetTime() -
m_timeaxis.m_mintime.GetTime()) ) /
(double)m_plotRect.Width();
}
}
//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::SetSerie(int s,
int style,
COLORREF color,
int minrange,
int maxrange,
const char *szTitle
)
{
m_series[s].m_iLineStyle = style;
m_series[s].m_color = color;
m_series[s].m_nMinValue = minrange;
m_series[s].m_nMaxValue = maxrange/10*10;
if ( maxrange <= minrange )
maxrange = (minrange/10+1)*10;
m_series[s].m_strTitle = szTitle;
}
//*******************************************************************************************************/
//*******************************************************************************************************/
void clPlot::MoveWindow(CRect & Rect)
{
m_clientRect = Rect;
GetParent()->ClientToScreen(m_clientRect);
ScreenToClient(m_clientRect);
ComputeRects(TRUE);
CWnd::MoveWindow(Rect);
}
void clPlot::WriteLegend(CDC *dc, int s)
{
int delt = ( m_plotRect.bottom - m_plotRect.top ) / 50;
int x = m_iMright + (s+1)*COORWIDTH - 5;
int y = m_plotRect.bottom - delt*5;
int cor = m_series[s].m_nMaxValue/10;
COLORREF oldColor = dc->SetTextColor(m_series[s].m_color);
UINT oldMode = dc->SetTextAlign( TA_RIGHT | TA_BASELINE );
for ( int i =1 ; i <= 10; i++, y -= delt*5 )
{
CString str;
str.Empty();
str.Format("%d", cor*i);
dc->TextOut(x, y, str);
}
dc->TextOut(x, y, m_series[s].m_strTitle);
dc->SetTextAlign( oldMode );
dc->SetTextColor( oldColor );
}
// * * Added by Bill Chen
void clPlot::ClearData()
{
m_series[SERIENUMBER];
for ( int i=0; i<SERIENUMBER; i++ )
m_series[i].m_lbegin = m_series[i].m_lend;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -