www.pudn.com > TestLinearGrap.rar > LinearGraph.cpp
// LinearGraph.cpp : implementation file
//
#include "stdafx.h"
#include "TestLinearGrap.h"
#include "LinearGraph.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CLine::CLine()
{
m_pValues = NULL;
m_ValueCount = 0;
m_LineColr = RGB(128,128,0);
}
CLine::~CLine()
{
if ( m_pValues )
{
delete m_pValues;
m_pValues = NULL;
m_ValueCount = 0;
}
}
void CLine::ShiftValue(UINT nCount)
{
if ( nCount==0 )
return;
ASSERT(nCount0 )
{
UINT i;
for(i=nCount; i=nCount; i++)
m_pValues[i] = m_pValues[nCount-i];
for(i=nCount; im_pValues;
this->m_pValues = new SPosValue[nCount];
this->m_ValueCount = nCount;
for(UINT i=0; im_ValueCount;
}
void CLine::SetPosValue(UINT nPos, int nValue)
{
ASSERT( nPos< m_ValueCount );
m_pValues[nPos].bUnkownValue = FALSE;
m_pValues[nPos].value = nValue;
}
BOOL CLine::GetPosValue(UINT nPos, int &nValue)
{
ASSERT( nPosm_Colors[item] = colr;
}
void CLinearGraph::SetLines(UINT nLineCount, UINT nUnits)
{
if ( this->m_LineMap )
delete this->m_LineMap;
this->m_nLineCount = nLineCount;
this->m_nUints = nUnits;
if ( nLineCount>0 )
{
this->m_LineMap = new CLine[nLineCount];
for(UINT i=0; im_LineMap[i].SetLinePosCount(nUnits);
}
}
void CLinearGraph::SetLineColor(UINT iLineID, COLORREF colr)
{
ASSERT(iLineIDm_nLineCount);
this->m_LineMap[iLineID].m_LineColr = colr;
}
void CLinearGraph::SetDrawGrid(BOOL bDraw)
{
this->m_bDrawDrid = bDraw;
}
void CLinearGraph::SetGridSize(const CSize &size)
{
this->m_GridSize = size;
}
void CLinearGraph::SetValueUnit(int nWidth)
{
this->m_nYUnitWidth = nWidth;
}
void CLinearGraph::ShiftUnit(int nUnit)
{
this->m_nUintPos += nUnit;
for(UINT i=0; im_nLineCount; i++)
this->m_LineMap[i].ShiftValue(nUnit);
}
void CLinearGraph::SetCurLine(UINT nLineID)
{
ASSERT(nLineIDm_nLineCount);
this->m_CurLineID = nLineID;
}
void CLinearGraph::SetValue(int nValue)
{
this->m_LineMap[this->m_CurLineID].SetPosValue(this->m_nUints-1, nValue);
this->Invalidate();
}
void CLinearGraph::SetValue(UINT nLineID, int nValue)
{
ASSERT(nLineIDm_nLineCount);
(this->m_LineMap+nLineID)->SetPosValue(this->m_nUints-1, nValue);
this->Invalidate();
}
void CLinearGraph::OnPaint()
{
CPaintDC dc(this); // device context for painting
//绘制背影
CRect rt;
this->GetClientRect(&rt);
dc.FillSolidRect(&rt, this->m_Colors[BK_COLOR]);
//计算
int nWidthUnit = rt.Width()/m_nUints; //每平移一单元宽度
int nFirstPos = rt.Width() - (nWidthUnit*m_nUintPos)%m_GridSize.cx;
//绘制表格竖线
CPen pen(PS_SOLID, 1, this->m_Colors[GRID_LINE_COLOR]);
CPen *pOldPen = dc.SelectObject(&pen);
for(; nFirstPos>rt.left; nFirstPos-=m_GridSize.cx)
{
dc.MoveTo(nFirstPos, 0);
dc.LineTo(nFirstPos, rt.Height());
}
//绘制表格横线
nFirstPos = rt.bottom;
for(; nFirstPos>rt.top; nFirstPos-=m_GridSize.cy)
{
dc.MoveTo(0, nFirstPos);
dc.LineTo(rt.Width(), nFirstPos);
}
dc.SelectObject(pOldPen);
//绘制每一条曲线
int nValue;
for(UINT i=0; im_nLineCount; i++ )
{
BOOL bLastValueUnkown=TRUE;
CPen linepen(PS_SOLID, 1, this->m_LineMap[i].m_LineColr);
pOldPen = dc.SelectObject(&linepen);
//从右至左绘制曲线的每一个点
int xPos = rt.right;
for(int j=this->m_LineMap[i].GetLineCount()-1; j>0; j--, xPos-=nWidthUnit )
{
if ( this->m_LineMap[i].GetPosValue(j, nValue) )
{
//如果曲线上点未知,不绘制
nValue /= m_nYUnitWidth;
nValue = rt.Height() - nValue;
if ( nValue