원형 차트 그리기
직선 및 곡선 함수를 사용하여 원형 차트를 그릴 수 있습니다. 원형 차트를 그리는 데 사용되는 기본 함수는 원형 중심, 원형 반경, 시작 각도 및 스윕 각도의 좌표를 제공해야 하는 AngleArc 함수입니다. 다음 스크린샷은 사용자가 이러한 값을 입력하는 데 사용할 수 있는 대화 상자를 보여줍니다.
원형 차트스크린샷
위에 표시된 값은 다음 원형 차트를 생성합니다.
애플리케이션의 리소스 스크립트에 있는 대화 상자 템플릿(. RC) 파일은 다음과 같이 이전 대화 상자의 특성(높이, 포함된 컨트롤 및 스타일)을 지정합니다.
AngleArc DIALOG 6, 18, 160, 100
STYLE WS_DLGFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION
CAPTION "Pie Chart"
FONT 8, "MS Sans Serif"
BEGIN
EDITTEXT IDD_X, 18, 22, 25, 12, ES_AUTOHSCROLL
LTEXT "X", 102, 4, 24, 9, 8
EDITTEXT IDD_Y, 18, 39, 25, 12, ES_AUTOHSCROLL
LTEXT "Y", 104, 5, 42, 12, 8
LTEXT "Center", 105, 19, 11, 23, 8
EDITTEXT IDD_RADIUS, 103, 9, 32, 12, ES_AUTOHSCROLL
EDITTEXT IDD_STARTANGLE, 103, 31, 32, 12, ES_AUTOHSCROLL
EDITTEXT IDD_SWEEPANGLE, 103, 53, 32, 12, ES_AUTOHSCROLL
LTEXT "Radius", 109, 73, 11, 25, 8
LTEXT "Start Angle", 110, 59, 33, 42, 8
LTEXT "Sweep Angle", 111, 55, 55, 43, 8
PUSHBUTTON "OK", IDD_OK, 9, 82, 40, 14
PUSHBUTTON "Cancel", IDD_CANCEL, 110, 82, 40, 14
END
애플리케이션의 원본 파일에 있는 대화 상자 프로시저는 다음 단계에 따라 데이터(가운데 좌표, 호 반경, 시작 및 스윕 각도)를 검색합니다.
- 애플리케이션 정의 ClearBits 함수는 사용자 입력을 받는 배열을 0으로 초기화합니다.
- 애플리케이션 정의 GetStrLngth 함수는 사용자가 입력한 문자열의 길이를 검색합니다.
- 애플리케이션 정의 RetrieveInput 함수는 사용자가 입력한 값을 검색합니다.
다음 샘플 코드는 대화 상자 프로시저를 보여줍니다.
void ClearBits(LPTSTR, int);
int GetStrLngth(LPTSTR);
DWORD RetrieveInput(LPTSTR, int);
BOOL CALLBACK ArcDlgProc(HWND hdlg, UINT uMsg, WPARAM wParam,
LPARAM lParam)
{
CHAR chInput[4]; // receives control-window input
int cch; // array-size and count variable
switch (uMsg)
{
case WM_INITDIALOG:
return FALSE;
case WM_COMMAND:
switch (wParam)
{
// If the user pressed the OK button, retrieve the
// data that was entered in the various AngleArc
// controls.
case IDD_OK:
// Retrieve the x-coordinate of the arc's center.
ClearBits(chInput, sizeof(chInput));
GetDlgItemText(hdlg, IDD_X, chInput,
sizeof(chInput));
cch = GetStrLngth(chInput);
nX = (int)RetrieveInput(chInput, cch);
// Retrieve the y-coordinate of the arc's center.
ClearBits(chInput, sizeof(chInput));
GetDlgItemText(hdlg, IDD_Y, chInput,
sizeof(chInput));
cch = GetStrLngth(chInput);
nY = (int)RetrieveInput(chInput, cch);
// Retrieve the radius of the arc.
ClearBits(chInput, sizeof(chInput));
GetDlgItemText(hdlg, IDD_RADIUS, chInput,
sizeof(chInput));
cch = GetStrLngth(chInput);
dwRadius = (DWORD) RetrieveInput(chInput, cch);
// Retrieve the start angle.
ClearBits(chInput, sizeof(chInput));
GetDlgItemText(hdlg, IDD_STARTANGLE, chInput,
sizeof(chInput));
cch = GetStrLngth(chInput);
xStartAngle = (float) RetrieveInput(chInput, cch);
// Retrieve the sweep angle.
ClearBits(chInput, sizeof(chInput));
GetDlgItemText(hdlg, IDD_SWEEPANGLE, chInput,
sizeof(chInput));
cch = GetStrLngth(chInput);
xSweepAngle = (float) RetrieveInput(chInput, cch);
EndDialog(hdlg, FALSE);
return TRUE;
// If user presses the CANCEL button, close the
// dialog box.
case IDD_CANCEL:
EndDialog(hdlg, FALSE);
return TRUE;
} // end switch (wParam)
break;
default:
return FALSE;
} // end switch (message)
UNREFERENCED_PARAMETER(lParam);
}
void ClearBits(LPTSTR cArray, int iLength)
{
int i;
for (i = 0; i < iLength; i++)
cArray[i] = 0;
}
int GetStrLngth(LPTSTR cArray)
{
int i = 0;
while (cArray[i++] != 0);
return i - 1;
}
DWORD RetrieveInput(LPTSTR cArray, int iLength)
{
int i, iTmp;
double dVal, dCount;
dVal = 0.0;
dCount = (double) (iLength - 1);
// Convert ASCII input to a floating-point value.
for (i = 0; i < iLength; i++)
{
iTmp = cArray[i] - 0x30;
dVal = dVal + (((double)iTmp) * pow(10.0, dCount--));
}
return (DWORD) dVal;
}
원형 차트의 각 섹션을 그리려면 사용자가 입력한 값을 AngleArc 함수에 전달합니다. 현재 브러시를 사용하여 원형 차트를 채우려면 경로 대괄호에 AngleArc 호출을 삽입합니다. 다음 코드 샘플에서는 정의된 경로 대괄호 및 AngleArc대한 호출을 보여 냅니다.
int nX;
int nY;
DWORD dwRadius;
float xStartAngle;
float xSweepAngle;
hdc = GetDC(hwnd);
BeginPath(hdc);
SelectObject(hdc, GetStockObject(GRAY_BRUSH));
MoveToEx(hdc, nX, nY, (LPPOINT) NULL);
AngleArc(hdc, nX, nY, dwRadius, xStartAngle, xSweepAngle);
LineTo(hdc, nX, nY);
EndPath(hdc);
StrokeAndFillPath(hdc);
ReleaseDC(hwnd, hdc);