📄 trendline.cpp
字号:
#include "StdAfx.h"
#include ".\trendline.h"
CTrendLine::CTrendLine(void)
: m_sName("")
, m_pValues(NULL)
, m_iRoomSize(0)
, m_iValueNum(0)
, m_iSheetSize(0)
, m_iSheetIndex(0)
, m_dMaxValue(0.0)
, m_dMinValue(0.0)
{
}
CTrendLine::~CTrendLine(void)
{
this->Destroy();
}
// 调整
void CTrendLine::Adjust(CTrendLine & trendLine)
{
double max = this->m_dMaxValue;
double min = this->m_dMinValue;
if(max < trendLine.m_dMaxValue)
{
max = trendLine.m_dMaxValue;
}
if(min > trendLine.m_dMinValue)
{
min = trendLine.m_dMinValue;
}
this->m_dMaxValue = max;
this->m_dMinValue = min;
trendLine.m_dMaxValue = max;
trendLine.m_dMinValue = min;
}
// 创建
void CTrendLine::Create(int size)
{
this->Destroy();
this->m_iRoomSize = size + size;
this->m_pValues = new VALUE[this->m_iRoomSize];
this->m_iValueNum = 0;
this->m_iSheetSize = 1;
this->m_iSheetIndex = 0;
}
// 清空
void CTrendLine::Clear()
{
this->m_dMaxValue = -1e200;
this->m_dMinValue = 1e200;
this->m_iValueNum = 0;
this->m_iSheetSize = 1;
this->m_iSheetIndex = 0;
}
// 销毁
void CTrendLine::Destroy()
{
if(this->m_pValues)
{
delete [] this->m_pValues;
this->m_pValues = NULL;
}
this->m_dMaxValue = -1e200;
this->m_dMinValue = 1e200;
this->m_iRoomSize = 0;
this->m_iValueNum = 0;
this->m_iSheetSize = 0;
this->m_iSheetIndex = 0;
}
// 添加值
void CTrendLine::AddValue(double value)
{
if(value > this->m_dMaxValue)
{
this->m_dMaxValue = value;
}
if(value < this->m_dMinValue)
{
this->m_dMinValue = value;
}
if(this->m_iSheetIndex >= this->m_iSheetSize - 1)
{
this->m_pValues[this->m_iValueNum].avg = value;
this->m_pValues[this->m_iValueNum].min = value;
this->m_pValues[this->m_iValueNum].max = value;
this->m_iValueNum++;
this->m_iSheetIndex = 0;
if(this->m_iValueNum >= this->m_iRoomSize)
{
int i;
int newIndex;
for(i = 0; i < this->m_iRoomSize; i += 2)
{
newIndex = i / 2;
this->m_pValues[newIndex].avg = (this->m_pValues[i].avg + this->m_pValues[i + 1].avg) / 2;
this->m_pValues[newIndex].max = this->m_pValues[i].max;
if(this->m_pValues[newIndex].max < this->m_pValues[i + 1].max)
{
this->m_pValues[newIndex].max = this->m_pValues[i + 1].max;
}
this->m_pValues[newIndex].min = this->m_pValues[i].min;
if(this->m_pValues[newIndex].min > this->m_pValues[i + 1].min)
{
this->m_pValues[newIndex].min = this->m_pValues[i + 1].min;
}
}
this->m_iValueNum = this->m_iRoomSize / 2;
this->m_iSheetSize *= 2;
this->m_iSheetIndex = 0;
}
}
else
{
VALUE * pv = &this->m_pValues[this->m_iValueNum - 1];
if(this->m_iSheetIndex == 0)
{
pv->avg = value;
pv->min = value;
pv->max = value;
}
else
{
pv->avg = (pv->avg * this->m_iSheetIndex + value) / (this->m_iSheetIndex + 1);
if(pv->max < value)
{
pv->max = value;
}
if(pv->min > value)
{
pv->min = value;
}
}
this->m_iSheetIndex++;
}
}
// 绘制
void CTrendLine::Draw(CDC * pDC, CRect & rect, CPen & penFleet, CPen & penDeep, bool rightCoord)
{
static const int LEFT = 60;
static const int RIGHT = 60;
static const int TOP = 20;
static const int BOTTOM = 20;
static CPen pgray(PS_SOLID, 1, RGB(192, 192, 192));
if(rightCoord)
{
CPen * pold = pDC->SelectObject(&pgray);
pDC->MoveTo(rect.right - RIGHT, rect.top + TOP);
pDC->LineTo(rect.right - RIGHT, rect.bottom - BOTTOM);
pDC->LineTo(rect.left + LEFT, rect.bottom - BOTTOM);
CString s;
s.Format("%lf", this->m_dMaxValue);
pDC->TextOut(rect.right - RIGHT + 2, rect.top + TOP - 8, s);
s.Format("%lf", this->m_dMinValue);
pDC->TextOut(rect.right - RIGHT + 2, rect.bottom - BOTTOM - 8, s);
pDC->TextOut(rect.left + LEFT, rect.bottom - BOTTOM + 2, "0");
s.Format("%d", this->m_iValueNum * this->m_iSheetSize + this->m_iSheetIndex);
pDC->TextOut(rect.right - RIGHT, rect.bottom - BOTTOM + 2, s);
pDC->TextOut(rect.right - RIGHT + 2, (rect.bottom + rect.top) / 2, m_sName);
pDC->SelectObject(pold);
}
else
{
CPen * pold = pDC->SelectObject(&pgray);
pDC->MoveTo(rect.left + LEFT, rect.top + TOP);
pDC->LineTo(rect.left + LEFT, rect.bottom - BOTTOM);
pDC->LineTo(rect.right - RIGHT, rect.bottom - BOTTOM);
CString s;
s.Format("%lf", this->m_dMaxValue);
pDC->TextOut(rect.left + 5, rect.top + TOP - 8, s);
s.Format("%lf", this->m_dMinValue);
pDC->TextOut(rect.left + 5, rect.bottom - BOTTOM - 8, s);
pDC->TextOut(rect.left + LEFT, rect.bottom - BOTTOM + 2, "0");
s.Format("%d", this->m_iValueNum * this->m_iSheetSize + this->m_iSheetIndex);
pDC->TextOut(rect.right - RIGHT, rect.bottom - BOTTOM + 2, s);
pDC->TextOut(rect.left + 5, (rect.bottom + rect.top) / 2, m_sName);
pDC->SelectObject(pold);
}
// 绘制
if(this->m_iValueNum > 1)
{
CPen * pold = pDC->SelectObject(&penFleet);
int w = rect.Width() - LEFT - RIGHT;
int h = rect.Height() - TOP - BOTTOM;
int i;
double dvalue = this->m_dMaxValue - this->m_dMinValue;
int x;
for(i = 0; i < m_iValueNum; i++)
{
if( this->m_pValues[i].min != this->m_pValues[i].avg
|| this->m_pValues[i].max != this->m_pValues[i].avg)
{
x = rect.left + LEFT + w * i / (m_iValueNum - 1);
pDC->MoveTo(x, int(rect.bottom - BOTTOM - h * (this->m_pValues[i].min - this->m_dMinValue) / dvalue));
pDC->LineTo(x, int(rect.bottom - BOTTOM - h * (this->m_pValues[i].max - this->m_dMinValue) / dvalue));
}
}
pDC->SelectObject(&penDeep);
x = rect.left + LEFT;
pDC->MoveTo(x, int(rect.bottom - BOTTOM - h * (this->m_pValues[0].avg - this->m_dMinValue) / dvalue));
for(i = 1; i < m_iValueNum; i++)
{
x = rect.left + LEFT + w * i / (m_iValueNum - 1);
pDC->LineTo(x, int(rect.bottom - BOTTOM - h * (this->m_pValues[i].avg - this->m_dMinValue) / dvalue));
}
pDC->SelectObject(pold);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -