www.pudn.com > DrawGrInDlg.rar > Pie.cpp


// Pie.cpp: implementation of the CPie class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "Pie.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
CPie::CPie() 
{ 
	CutPie = FALSE; 
	WithText = FALSE; 
} 
 
CPie::~CPie() 
{ 
 
} 
void CPie::DrawPie(CDC* pDC,int LeftTopx,int LeftTopy,int RightBottomx,int RightBottomy) 
{ 
	int nWidth=30; 
	if(CutPie == FALSE) 
	{ 
		DrawNormalPie(pDC,LeftTopx+25,LeftTopy+50,RightBottomx-nWidth-25,RightBottomy-50); 
	} 
	else 
	{ 
		DrawCutPie(pDC,LeftTopx+25,LeftTopy+50,RightBottomx-nWidth-25,RightBottomy-50); 
	} 
	DrawLegend(pDC,RightBottomx-nWidth-5,LeftTopy+50,nWidth,CBaseGr::BOTTOM); 
	DrawTitle(pDC,LeftTopx+25,LeftTopy+50,RightBottomx-nWidth-25,RightBottomy-50,CBaseGr::TOP); 
} 
void CPie::DrawPieText(CDC* pDC,double sub,double sum,int Startx,int Starty,Align Mode,COLORREF FontColor) 
{ 
	CFont m_font; 
	m_font.CreateFont( 
		  -12,                      // nHeight 
		  0,                      // nWidth 
		   0,                      // nEscapement 
		   0,                      // nOrientation 
			FW_NORMAL,             // nWeight 
			FALSE,                 // bItalic 
			FALSE,                 // bUnderline 
		   FALSE,                      // cStrikeOut 
		OEM_CHARSET,              // nCharSet 
		OUT_DEFAULT_PRECIS,        // nOutPrecision 
		CLIP_DEFAULT_PRECIS,       // nClipPrecision 
		DEFAULT_QUALITY,           // nQuality 
		DEFAULT_PITCH | FF_DONTCARE,  // nPitchAndFamily 
		_T("ËÎÌå"));  
	CFont *pOldFont = pDC->SelectObject(&m_font); 
 
	pDC->SetTextColor(FontColor); 
//	DrawText(pDC->GetSafeHdc(),Value,-1,&rcString,DT_RIGHT | DT_SINGLELINE); 
	CString Value = _T(""); 
	if(sum!=0.0) 
	{ 
		Value.Format(_T("%2d%%"),(int)(sub/sum*100+0.5)); 
	} 
	switch(Mode) 
	{ 
	case LEFT: 
		pDC->ExtTextOut(Startx+2,Starty,ETO_OPAQUE,NULL,Value,NULL); 
		break; 
	case RIGHT: 
		pDC->ExtTextOut(Startx-15,Starty,ETO_OPAQUE,NULL,Value,NULL); 
		break; 
	case TOP: 
		pDC->ExtTextOut(Startx-7,Starty-15,ETO_OPAQUE,NULL,Value,NULL); 
		break; 
	case BOTTOM: 
		pDC->ExtTextOut(Startx-7,Starty+15,ETO_OPAQUE,NULL,Value,NULL); 
		break; 
	} 
	pDC->SelectObject(pOldFont); 
} 
void CPie::DrawNormalPie(CDC* pDC,int LeftTopx,int LeftTopy,int RightBottomx,int RightBottomy) 
{ 
	int i=0; 
	double ValueSum=0.0; 
	double ValueSumb=0.0; 
	double AngleS=0.0; 
	double AngleE=0.0; 
	for(i=0;i (RightBottomy-LeftTopy)) 
			{ 
				DrawArc(pDC,LeftTopx+(RightBottomx-LeftTopx)/2,LeftTopy+(RightBottomy-LeftTopy)/2,(RightBottomy-LeftTopy)/2,AngleS,AngleE,m_Legend.GetAt(i).Graph.LineSize,m_Legend.GetAt(i).Graph.LineColor,m_Legend.GetAt(i).Graph.FillColor); 
				Startx = (int)(LeftTopx+(RightBottomx-LeftTopx)/2+(RightBottomy-LeftTopy)/2*cos(AngleS+(AngleE-AngleS)/2)); 
				Starty = (int)(LeftTopy+(RightBottomy-LeftTopy)/2+(RightBottomy-LeftTopy)/2*sin(AngleS+(AngleE-AngleS)/2)); 
			} 
			else 
			{ 
				DrawArc(pDC,LeftTopx+(RightBottomx-LeftTopx)/2,LeftTopy+(RightBottomy-LeftTopy)/2,(RightBottomx-LeftTopx)/2,AngleS,AngleE,m_Legend.GetAt(i).Graph.LineSize,m_Legend.GetAt(i).Graph.LineColor,m_Legend.GetAt(i).Graph.FillColor); 
				Startx = (int)(LeftTopx+(RightBottomx-LeftTopx)/2+(RightBottomx-LeftTopx)/2*cos(AngleS+(AngleE-AngleS)/2)); 
				Starty = (int)(LeftTopy+(RightBottomy-LeftTopy)/2+(RightBottomx-LeftTopx)/2*sin(AngleS+(AngleE-AngleS)/2)); 
			} 
			if(WithText) 
			{ 
				if(((AngleE+AngleS)/2 < 90*PI/180 && 
					(AngleE+AngleS)/2 >=0) || 
					((AngleE+AngleS)/2 > 270*PI/180 && 
					(AngleE+AngleS)/2 <=360*PI/180))  
				DrawPieText(pDC,m_Legend.GetAt(i).Value,ValueSum,Startx,Starty,LEFT); 
				else if((AngleE+AngleS)/2 > 90*PI/180 && 
					(AngleE+AngleS)/2 < 270*PI/190)  
				DrawPieText(pDC,m_Legend.GetAt(i).Value,ValueSum,Startx,Starty,RIGHT); 
				else if((AngleS+AngleE)/2 == 90*PI/180) 
				DrawPieText(pDC,m_Legend.GetAt(i).Value,ValueSum,Startx,Starty,TOP); 
				else if((AngleS+AngleE)/2 == 270*PI/180) 
				DrawPieText(pDC,m_Legend.GetAt(i).Value,ValueSum,Startx,Starty,BOTTOM); 
			} 
		} 
	} 
} 
void CPie::DrawCutPie(CDC* pDC,int LeftTopx,int LeftTopy,int RightBottomx,int RightBottomy) 
{ 
	int i=0; 
	double ValueSum=0.0; 
	double ValueSumb=0.0; 
	double AngleS=0.0; 
	double AngleE=0.0; 
	for(i=0;i (RightBottomy-LeftTopy)) 
			{ 
				DrawCutArc(pDC,LeftTopx+(RightBottomx-LeftTopx)/2,LeftTopy+(RightBottomy-LeftTopy)/2,(RightBottomy-LeftTopy)/2,AngleS,AngleE,m_Legend.GetAt(i).Graph.LineSize,m_Legend.GetAt(i).Graph.LineColor,m_Legend.GetAt(i).Graph.FillColor); 
			} 
			else 
			{ 
				DrawCutArc(pDC,LeftTopx+(RightBottomx-LeftTopx)/2,LeftTopy+(RightBottomy-LeftTopy)/2,(RightBottomx-LeftTopx)/2,AngleS,AngleE,m_Legend.GetAt(i).Graph.LineSize,m_Legend.GetAt(i).Graph.LineColor,m_Legend.GetAt(i).Graph.FillColor); 
			} 
		} 
	} 
} 
void CPie::DrawCutArc(CDC* pDC,int pCoCx,int pCoCy,int nRadius,double AngleS,double AngleE,int LineSize,COLORREF LineColor,COLORREF FillColor) 
{ 
	int CutRadius = 5; 
	pCoCx -= CutRadius; 
	pCoCy -= CutRadius; 
	POINT PointS,PointE,PointBase; 
	PointBase.x = pCoCx - nRadius; 
	PointBase.y = pCoCy; 
	if(AngleS > AngleE) 
	{ 
		double conv = AngleS; 
		AngleS = AngleE; 
		AngleE = conv; 
	} 
	int Detax = (int)(CutRadius*cos(AngleS+(AngleE-AngleS)/2)); 
	int Detay = (int)(CutRadius*sin(AngleS+(AngleE-AngleS)/2)); 
	PointS.x = pCoCx + (int)(nRadius*cos(AngleS)) + Detax; 
	PointS.y = pCoCy + (int)(nRadius*sin(AngleS)) + Detay; 
	PointE.x = pCoCx + (int)(nRadius*cos(AngleE)) + Detax; 
	PointE.y = pCoCy + (int)(nRadius*sin(AngleE)) + Detay; 
 
	double AngleTmp = AngleS; 
	double DetaAngle = PI/180; 
	int nCount = (int)((AngleE - AngleS)/DetaAngle); 
	CPoint *ArcP = new CPoint[nCount+2]; 
	(*(ArcP+0)).x = pCoCx + Detax; 
	(*(ArcP+0)).y = pCoCy + Detay; 
	int i = 1; 
	while(AngleTmp <= AngleE) 
	{ 
		(*(ArcP+i)).x = pCoCx + (int)(nRadius*cos(AngleTmp)) + Detax; 
		(*(ArcP+i)).y = pCoCy + (int)(nRadius*sin(AngleTmp)) + Detay; 
		AngleTmp += DetaAngle; 
		i++; 
	} 
	(*(ArcP+nCount)) = PointE; 
	(*(ArcP+nCount+1)) = (*(ArcP+0)); 
	DrawPolygon(pDC,ArcP,nCount+2,LineSize,LineColor,FillColor); 
	delete []ArcP; 
}