#include "stdafx.h"
#include "../Verbpo.h"
#include "TrendChart.h"


INT CTrendChart::m_i32PointNum = 180;
INT CTrendChart::m_i32Height=380;
INT CTrendChart::m_i32Width = 120*4+100;

// CTrendChart

IMPLEMENT_DYNAMIC(CTrendChart, CStatic)

CTrendChart::CTrendChart()
{
	int i = 0;
	
	m_i16WaveDate = new INT16[CTrendChart::m_i32PointNum];
	m_pCPonit = new CPoint[CTrendChart::m_i32PointNum];
	
	m_i16WaveDateLength = 0;
	penLine = new CPen(PS_SOLID, 1, RGB(15, 200, 130));
	penBKLine = new CPen(PS_SOLID, 1, RGB(15, 100, 200));

	font.CreateFont(20,                                          //   nHeight     
		0,                                                   //   nWidth     
		0,                                                   //   nEscapement   
		0,                                                   //   nOrientation     
		FW_NORMAL,                                   //   nWeight     
		FALSE,                                           //   bItalic     
		FALSE,                                           //   bUnderline     
		0,                                                   //   cStrikeOut     
		ANSI_CHARSET,                             //   nCharSet     
		OUT_DEFAULT_PRECIS,                 //   nOutPrecision     
		CLIP_DEFAULT_PRECIS,               //   nClipPrecision     
		DEFAULT_QUALITY,                       //   nQuality     
		DEFAULT_PITCH | FF_SWISS,     //   nPitchAndFamily       
		_T("宋体"));

	m_i32Type = 0;
	//UnitLength_Pixel = (Height - 4) / 10;
}

CTrendChart::~CTrendChart()
{
	int i = 0;
	
	delete m_i16WaveDate;
	delete m_pCPonit;
	
	delete penLine;
	delete penBKLine;

	font.DeleteObject();
}


BEGIN_MESSAGE_MAP(CTrendChart, CStatic)
	ON_WM_PAINT()
	ON_WM_CREATE()
	ON_WM_DESTROY()
END_MESSAGE_MAP()



// CTrendChart 消息处理程序

int CTrendChart::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	CString str;
	INT i, j;
	// 获得控件区域
	GetClientRect(&m_rectCtrl);
	//客户区DC，可以绘画及显示
	CClientDC pDC(this);

	//依附窗口DC创建兼容内存DC
	dcMem.CreateCompatibleDC(&pDC);
	//创建兼容位图
	bmp.CreateCompatibleBitmap(&pDC, CTrendChart::m_i32Width, CTrendChart::m_i32Height);
	dcMem.SelectObject(&bmp); //将位图选择进内存DC
	dcMem.FillSolidRect(0, 0, CTrendChart::m_i32Width, CTrendChart::m_i32Height, RGB(0, 0, 0));

	//依附窗口DC创建兼容内存DC
	dcMemBK.CreateCompatibleDC(&pDC);
	//创建兼容位图
	bmpBK.CreateCompatibleBitmap(&pDC, CTrendChart::m_i32Width, CTrendChart::m_i32Height);
	dcMemBK.SelectObject(&bmpBK); //将位图选择进内存DC
	dcMemBK.FillSolidRect(0, 0, CTrendChart::m_i32Width, CTrendChart::m_i32Height, RGB(0, 0, 0));

	//清空所有图像
	dcMemBK.FillSolidRect(0, 0, CTrendChart::m_i32Width, CTrendChart::m_i32Height, RGB(0, 0, 0));

	dcMemBK.SelectObject(penBKLine);
	
	//画竖线
	
	for (i = 0; i <=120 * 4; i += 120)
	{
		for (j = 1; j <=50*5; j += 2)
		{
			dcMemBK.MoveTo(i + 70, CTrendChart::m_i32Height-(j + 50));
			dcMemBK.LineTo(i + 70, CTrendChart::m_i32Height - (j + 1 + 50));
		}
	}
	for (i = 0; i <=50 * 5; i += 50)
	{
		for (j = 1; j <=120 * 4; j += 2)
		{
			dcMemBK.MoveTo(j + 70, CTrendChart::m_i32Height - (i + 50));
			dcMemBK.LineTo(j + 70 + 1, CTrendChart::m_i32Height - (i + 1 + 50));
		}
	}

	//画Y轴
	dcMemBK.MoveTo(70, CTrendChart::m_i32Height - 50);
	dcMemBK.LineTo(70, CTrendChart::m_i32Height - 50 * 5 - 80);

	//画Y轴箭头
	dcMemBK.MoveTo(65, m_i32Height - 50 * 5 - 70);
	dcMemBK.LineTo(70, m_i32Height - 50 * 5 - 80);
	dcMemBK.MoveTo(75, m_i32Height - 50 * 5 - 70);
	dcMemBK.LineTo(70, m_i32Height - 50 * 5 - 80);

	//画X轴
	dcMemBK.MoveTo(70, CTrendChart::m_i32Height - 50);
	dcMemBK.LineTo(90 + 120 * 4, CTrendChart::m_i32Height - 50);

	//画X轴箭头
	dcMemBK.MoveTo(90 + 120 * 4 - 10, CTrendChart::m_i32Height - 50+5);
	dcMemBK.LineTo(90 + 120 * 4, CTrendChart::m_i32Height - 50);
	dcMemBK.MoveTo(90 + 120 * 4 - 10, CTrendChart::m_i32Height - 50 - 5);
	dcMemBK.LineTo(90 + 120 * 4, CTrendChart::m_i32Height - 50);
	
	dcMem.SelectObject(&font);
	dcMem.SetTextColor(RGB(0, 255, 0));

	m_i32DestroyFlag = 0;
	/*str = "I";
	dcMem.DrawText(str, CRect(0, m_i32Y1 - 10, 30, m_i32Y1 + 10), 0);

	str = "II";
	dcMem.DrawText(str, CRect(0, m_i32Y2 - 10, 30, m_i32Y2 + 10), 0);*/
	//	return CStatic::OnCreate(lpCreateStruct);
	return TRUE;
}

void CTrendChart::OnDestroy()
{
	CStatic::OnDestroy();

	m_i32DestroyFlag = 1;
	// TODO:  在此处添加消息处理程序代码

	dcMem.DeleteDC(); 
	bmp.DeleteObject(); 

	dcMemBK.DeleteDC(); 
	bmpBK.DeleteObject(); 
	
}


void CTrendChart::OnPaint()
{
	CPaintDC pDC(this);

	pDC.BitBlt(m_rectCtrl.left, m_rectCtrl.top, m_rectCtrl.Width(), m_rectCtrl.Height(), &dcMem, 0, 0, SRCCOPY);
	//	CStatic::OnPaint();
}

void CTrendChart::Set_UnitLength_Value(int Value)
{
	UnitLength_Value = Value;

}

void CTrendChart::draw_wave()
{
	CPen *pOldPen;

	dcMem.BitBlt(m_rectCtrl.left, m_rectCtrl.top, m_rectCtrl.Width(), m_rectCtrl.Height(), &dcMemBK, 0, 0, SRCCOPY);

	
	switch(m_i32Type)
	{
	case DATA_TYPE_HR:
		Draw_HR_Chart();
		break;

	case DATA_TYPE_RR:
		Draw_RR_Chart();
		break;

	case DATA_TYPE_SPO2:
		Draw_SPO2_Chart();
		break;

	case DATA_TYPE_PR:
		Draw_PR_Chart();
		break;

	case DATA_TYPE_TEMP:
		Draw_TEMP_Chart();
		break;

	case DATA_TYPE_PVCs:
		Draw_PVCs_Chart();
		break;

	case DATA_TYPE_ST:
		Draw_ST_Chart();
		break;

	case DATA_TYPE_OXY:
		Draw_OXY_Chart();
		break;
	}


	/*int a_i32WaveDateLength = 0, a_i32WaveDateR, i = 0, y = 0, a_i32Temp = 0, j = 0, t = 0;
	int a_i32X = 0, a_p32LastX = 0, a_i32Y, a_p32LastY = 0;
	register int a_i32Height1_2 = 0, a_UnitLength_Pixel = UnitLength_Pixel, a_UnitLength_Value = UnitLength_Value;
	register int a_i32PointNum = 0;


	m_CSWaveData.Lock();
	if (m_i16WaveDateLength)
	{
		a_i32Height1_2 = m_i32Y1;
		m_pCPonit[0][a_i32PointNum].x = a_i32X;
		m_pCPonit[0][a_i32PointNum].y = m_i32Y1;
		a_i32PointNum++;
		for (i = 0; i < ST_PATTERN_LENGTH; i++)
		{
			a_i32Y = a_i32Height1_2 - ((m_i16WaveDate[0][i] - 2048)*a_UnitLength_Pixel / a_UnitLength_Value) / 2;
			if (a_i32Y >= m_i32YLimit1)
				a_i32Y = m_i32YLimit1 - 1;
			else if (a_i32Y < 0)
				a_i32Y = 0;

			a_i32X++;

			m_pCPonit[0][a_i32PointNum].x = a_i32X;
			m_pCPonit[0][a_i32PointNum].y = a_i32Y;
			a_i32PointNum++;
		}

		a_i32PointNum = 0;
		a_i32Height1_2 = m_i32Y2;
		m_pCPonit[0][a_i32PointNum].x = a_i32X;
		m_pCPonit[0][a_i32PointNum].y = m_i32Y2;
		a_i32PointNum++;
		for (i = 0; i < ST_PATTERN_LENGTH; i++)
		{
			a_i32Y = a_i32Height1_2 - ((m_i16WaveDate[1][i] - 2048)*a_UnitLength_Pixel / a_UnitLength_Value) / 2;
			if (a_i32Y >= m_i32YLimit2)
				a_i32Y = m_i32YLimit2 - 1;
			else if (a_i32Y < m_i32YLimit1 + 4)
				a_i32Y = m_i32YLimit1 + 4;

			a_i32X++;
			a_i32Y += m_i32Y2;
			m_pCPonit[1][a_i32PointNum].x = a_i32X;
			m_pCPonit[1][a_i32PointNum].y = a_i32Y;
			a_i32PointNum++;
		}
	}

	m_CSWaveData.Unlock();

	dcMem.FillSolidRect(40, 0, m_i32Width - 40, m_i32Height, RGB(0, 0, 0));
	pOldPen = dcMem.SelectObject(penECGLine);
	dcMem.Polyline(m_pCPonit[0], a_i32PointNum);
	dcMem.Polyline(m_pCPonit[1], a_i32PointNum);
	pOldPen = dcMem.SelectObject(penISO);
	a_i32Temp = m_i32ISO / 4;
	if (a_i32Temp > 124)
		a_i32Temp = 124;
	dcMem.MoveTo(165 - a_i32Temp, 0);
	dcMem.LineTo(165 - a_i32Temp, m_i32Height);

	pOldPen = dcMem.SelectObject(penST);
	a_i32Temp = m_i32ST / 4;
	if (a_i32Temp > 124)
		a_i32Temp = 124;
	dcMem.MoveTo(165 + a_i32Temp, 0);
	dcMem.LineTo(165 + a_i32Temp, m_i32Height);
	dcMem.SelectObject(penISO);*/

}

void CTrendChart::Set_Type(INT type)
{

	m_i32Type = type;
	m_i32AdjRange = 0;
}


void CTrendChart::Up_Adj_Range()
{
	if (m_i32AdjRange >= 1)
		m_i32AdjRange -= 1;
}


void CTrendChart::Down_Adj_Range()
{
	if (m_i32AdjRange < 3)
		m_i32AdjRange += 1;

}

void CTrendChart::Draw_HR_Chart()
{
	INT a_i32Data = 0, a_i32Data2=0,i=0,j=0;
	CString a_cs;
	//画Y轴坐标值
	if (m_i32AdjRange == 0)
	{
		a_i32Data = 70;
	}
	else if (m_i32AdjRange == 1)
	{
		a_i32Data = 50;
	}
	else if (m_i32AdjRange == 2)
	{
		a_i32Data = 30;
	}
	else if (m_i32AdjRange == 3)
	{
		a_i32Data = 10;
	}
	i = CTrendChart::m_i32Height - 50;
	UnitLength_Value = a_i32Data;
	for (j = 0; j <= 5; j++, i -= 50)
	{
		a_cs.Format(_T("%d"), a_i32Data2);
		dcMem.DrawText(a_cs, CRect(2, i - 10, 68, i + 10), 0);
		a_i32Data2 += a_i32Data;
	}
	a_cs.Format(_T("心律 RPM"));
	dcMem.DrawText(a_cs, CRect(5, 5, 100, 25), 0);
}

void CTrendChart::Draw_RR_Chart()
{
	INT a_i32Data = 0, a_i32Data2 = 0, i = 0, j = 0;
	CString a_cs;
	//画Y轴坐标值
	if (m_i32AdjRange == 0)
	{
		a_i32Data = 30;
	}
	else if (m_i32AdjRange == 1)
	{
		a_i32Data = 20;
	}
	else if (m_i32AdjRange == 2)
	{
		a_i32Data =	15;
	}
	else if (m_i32AdjRange == 3)
	{
		a_i32Data = 10;
	}
	i = CTrendChart::m_i32Height - 50 ;
	UnitLength_Value = a_i32Data;
	for (j = 0; j <= 5; j++, i -= 50)
	{
		a_cs.Format(_T("%d"), a_i32Data2);
		dcMem.DrawText(a_cs, CRect(2, i - 10, 68, i + 10), 0);
		a_i32Data2 += a_i32Data;
	}

	a_cs.Format(_T("呼吸率 RPM"));
	dcMem.DrawText(a_cs, CRect(5, 5, 150, 25), 0);
}

void CTrendChart::Draw_SPO2_Chart()
{
	INT a_i32Data = 0, a_i32Data2 = 0, i = 0, j = 0;
	CString a_cs;
	//画Y轴坐标值
	if (m_i32AdjRange == 0)
	{
		a_i32Data = 20;
	}
	else if (m_i32AdjRange == 1)
	{
		a_i32Data = 18;
	}
	else if (m_i32AdjRange == 2)
	{
		a_i32Data = 15;
	}
	else if (m_i32AdjRange == 3)
	{
		a_i32Data = 10;
	}
	i = CTrendChart::m_i32Height - 50;
	UnitLength_Value = a_i32Data;
	for (j = 0; j <= 5; j++, i -= 50)
	{
		a_cs.Format(_T("%d"), a_i32Data2);
		dcMem.DrawText(a_cs, CRect(2, i - 10, 68, i + 10), 0);
		a_i32Data2 += a_i32Data;
	}

	a_cs.Format(_T("血氧饱和度 %s"),_T("%"));
	dcMem.DrawText(a_cs, CRect(5, 5, 150, 25), 0);
}

void CTrendChart::Draw_PR_Chart()
{
	INT a_i32Data = 0, a_i32Data2 = 0, i = 0, j = 0;
	CString a_cs;
	//画Y轴坐标值
	if (m_i32AdjRange == 0)
	{
		a_i32Data = 30;
	}
	else if (m_i32AdjRange == 1)
	{
		a_i32Data = 20;
	}
	else if (m_i32AdjRange == 2)
	{
		a_i32Data = 15;
	}
	else if (m_i32AdjRange == 3)
	{
		a_i32Data = 10;
	}
	i = CTrendChart::m_i32Height - 50;
	UnitLength_Value = a_i32Data;
	for (j = 0; j <= 5; j++, i -= 50)
	{	
		a_cs.Format(_T("%d"), a_i32Data2);
		dcMem.DrawText(a_cs, CRect(2, i - 10, 68, i + 10), 0);
		a_i32Data2 += a_i32Data;
	}

	a_cs.Format(_T("脉率 RPM"));
	dcMem.DrawText(a_cs, CRect(5, 5, 100, 25), 0);
}

void CTrendChart::Draw_TEMP_Chart()
{
	INT a_i32Data = 0, a_i32Data2 = 0,  i = 0, j = 0;
	float a_f32Data = 0.0f, a_f32Data2 = 0.0f;
	CString a_cs;
	//画Y轴坐标值
	if (m_i32AdjRange == 0)
	{
		a_f32Data = 9.0f;
	}
	else if (m_i32AdjRange == 1)
	{
		a_f32Data = 8.0f;
	}
	else if (m_i32AdjRange == 2)
	{
		a_f32Data = 7.0f;
	}
	else if (m_i32AdjRange == 3)
	{
		a_f32Data = 6.0f;
	}
	i = CTrendChart::m_i32Height - 50;
	UnitLength_Value = (INT)(a_f32Data*10);
	for (j = 0; j <= 5; j++, i -= 50)
	{
		
		a_cs.Format(_T("%2.1f"), a_f32Data2);
		dcMem.DrawText(a_cs, CRect(2, i - 10, 68, i + 10), 0);
		a_f32Data2 += a_f32Data;
	}
	a_cs.Format(_T("体温 ℃"));
	dcMem.DrawText(a_cs, CRect(2, 5, 100, 25), 0);

}

void CTrendChart::Draw_PVCs_Chart()
{
	INT a_i32Data = 0, a_i32Data2 = 0, i = 0, j = 0;
	CString a_cs;
	//画Y轴坐标值
	if (m_i32AdjRange == 0)
	{
		a_i32Data = 15;
	}
	else if (m_i32AdjRange == 1)
	{
		a_i32Data = 10;
	}
	else if (m_i32AdjRange == 2)
	{
		a_i32Data = 8;
	}
	else if (m_i32AdjRange == 3)
	{
		a_i32Data = 4;
	}
	i = CTrendChart::m_i32Height - 50;
	UnitLength_Value = a_i32Data;
	for (j = 0; j <= 5; j++, i -= 50)
	{
		a_cs.Format(_T("%d"), a_i32Data2);
		dcMem.DrawText(a_cs, CRect(2, i - 10, 68, i + 10), 0);
		a_i32Data2 += a_i32Data;
	}

	a_cs.Format(_T("PVCs"));
	dcMem.DrawText(a_cs, CRect(5, 5, 100, 25), 0);

}


void CTrendChart::Draw_ST_Chart()
{

	INT a_i32Data = 0, a_i32Data2 = 0, i = 0, j = 0;
	float a_f32Data = 0.0f, a_f32Data2 = 0.0f;
	CString a_cs;
	//画Y轴坐标值
	if (m_i32AdjRange == 0)
	{
		a_f32Data = 1.6f;
		a_f32Data2 = -4.0f;
	}
	else if (m_i32AdjRange == 1)
	{
		a_f32Data = 0.8f;
		a_f32Data2 = -2.0f;
	}
	else if (m_i32AdjRange == 2)
	{
		a_f32Data = 0.4f;
		a_f32Data2 = -1.0f;
	}
	else if (m_i32AdjRange == 3)
	{
		a_f32Data = 0.2f;
		a_f32Data2 = -0.5f;
	}
	i = CTrendChart::m_i32Height - 50;
	UnitLength_Value = (INT)(a_f32Data * 10);
	for (j = 0; j <=5; j++, i -= 50)
	{
		a_cs.Format(_T("%2.1f"), a_f32Data2);
		dcMem.DrawText(a_cs, CRect(2, i - 10, 68, i + 10), 0);
		a_f32Data2 += a_f32Data;
	}
	a_cs.Format(_T("ST"));
	dcMem.DrawText(a_cs, CRect(2, 5, 150, 25), 0);
}


void CTrendChart::Draw_OXY_Chart()
{
	INT a_i32Data = 0, a_i32Data2 = 0, i = 0, j = 0;
	CString a_cs;
	//画Y轴坐标值
	if (m_i32AdjRange == 0)
	{
		a_i32Data = 20;
	}
	else if (m_i32AdjRange == 1)
	{
		a_i32Data = 15;
	}
	else if (m_i32AdjRange == 2)
	{
		a_i32Data = 10;
	}
	else if (m_i32AdjRange == 3)
	{
		a_i32Data = 5;
	}
	i = CTrendChart::m_i32Height - 50;
	UnitLength_Value = a_i32Data;
	for (j = 0; j <= 5; j++, i -= 50)
	{		
		a_cs.Format(_T("%d"), a_i32Data2);
		dcMem.DrawText(a_cs, CRect(2, i - 10, 68, i + 10), 0);
		a_i32Data2 += a_i32Data;
	}

	a_cs.Format(_T("氧浓度 %s"), _T("%"));
	dcMem.DrawText(a_cs, CRect(5, 5, 100, 25), 0);

}
