#include "StdAfx.h" #include "CPieChartView.h" #include "T2Archive.h" #include "URect.h" /*static*/ CPieChartView* CPieChartView::CreateCPieChartViewStream(T2Archive* inStream) { return new CPieChartView(inStream); } CPieChartView::CPieChartView(T2Archive* inStream) : mPie(NULL) { inStream->Read(&mTextTraits, sizeof(mTextTraits)); inStream->Read(&mBorderWidth, sizeof(mBorderWidth)); } /*virtual*/ CPieChartView::~CPieChartView() { } void CPieChartView::DrawSelf() { double pi = 3.14159265358979; CRect theFrameRect; GetClientRect(theFrameRect); if (!theFrameRect.IsRectEmpty()) { #line 47 _ASSERT(URect::Width(theFrameRect) == URect::Height(theFrameRect)); int unknownValue = 0; // not used for anything CWnd *parent = GetParent(); CDC *parentDC = parent->GetDC(); COLORREF theBackColor = parentDC->GetBkColor(); parent->ReleaseDC(parentDC); CDC *pDC = GetDC(); CPen thePen(PS_SOLID, 1, RGB(0, 0, 0)); CBrush theBrush(theBackColor); CPen *oldPen = (CPen *) pDC->SelectStockObject(NULL_PEN); CBrush *oldBrush = pDC->SelectObject(&theBrush); pDC->Ellipse(theFrameRect); CRect pieArea = theFrameRect; pieArea.DeflateRect(mBorderWidth, mBorderWidth); CPoint center = pieArea.CenterPoint(); if (mPie && mPie->totalValue != 0) { int i; int sumOfValue = 0; CPoint ptStart(center.x, pieArea.top); for (i = 0; i < mPie->sliceCount; i++) { if (mPie->slices[i].value == 0) continue; sumOfValue += mPie->slices[i].value; int angleEnd = (sumOfValue * 360) / mPie->totalValue; CPoint ptEnd = center; CSize size( cos((angleEnd * pi) / 180.0), sin((angleEnd * pi) / 180.0) ); ptEnd += size; CBrush brush(mPie->slices[i].color); pDC->SelectObject(brush); pDC->Pie(pieArea, ptStart, ptEnd); ptStart = ptEnd; } CSize textExtent = pDC->GetTextExtent("100%", 4); SIZE maxNumberSize = textExtent; int percentagePos = (URect::Width(pieArea) - maxNumberSize.cx) / 2; CPoint anotherCenter = center; pDC->SetTextColor(RGB(0, 0, 0)); pDC->SetBkMode(TRANSPARENT); int currAngle = 0; sumOfValue = 0; for (i = 0; i < mPie->sliceCount; i++) { if (mPie->slices[i].value == 0) continue; sumOfValue += mPie->slices[i].value; int endAngle = (sumOfValue * 360) / mPie->totalValue; int midAngle = (currAngle + endAngle) / 2; CString str((mPie->slices[i].value * 100) / mPie->totalValue, 1); str += '%'; CPoint textCenter = center; CSize sliceSize( percentagePos * cos((midAngle * pi) / 180.0), percentagePos * sin((midAngle * pi) / 180.0) ); textCenter += sliceSize; CRect drawRect(textCenter.x - maxNumberSize.cx / 2, textCenter.y - maxNumberSize.cy / 2, textCenter.x + maxNumberSize.cx / 2, textCenter.y + maxNumberSize.cy / 2); pDC->DrawText(str, drawRect, DT_CENTER); currAngle = endAngle; } } pDC->SelectStockObject(BLACK_PEN); pDC->SelectStockObject(NULL_BRUSH); pDC->Ellipse(pieArea); pDC->SelectObject(oldPen); pDC->SelectObject(oldBrush); ReleaseDC(pDC); } } void CPieChartView::CutPie(const Pie* inPie) { mPie = inPie; CRect rect; GetClientRect(rect); InvalidateRect(rect); }