summaryrefslogtreecommitdiff
path: root/src/T2DLL/CPieChartView.cpp
blob: 4ee4cf288e29375f24030b20f28725657690d140 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#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);
}