Column chart and pie chart display data statistics, column chart data statistics
1 # include <Windows. h> 2 # include <tchar. h> 3 # include <math. h> 4 BOOLEAN InitWindowClass (HINSTANCE hInstance, int nCmdShow); 5 lresult callback WndProc (HWND, UINT, WPARAM, LPARAM); 6 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 7 {8 MSG; 9 if (! InitWindowClass (hInstance, nCmdShow) 10 {11 MessageBox (NULL, L "window creation failed! ", _ T (" create window "), NULL); 12 return 1; 13} 14 while (GetMessage (& msg, NULL, 0, 0 )) 15 {16 TranslateMessage (& msg); 17 DispatchMessage (& msg); 18} 19 return (int) msg. wParam; 20} 21 22 lresult callback WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 23 {24 HDC hDC; 25 PAINTSTRUCT ps; 26 HBRUSH hBrush; 27 HPEN hPen; 28 RECT clientRect; 29 static RECT oldClientRect = {0, 0, 0}; 30 flo At sita = 0; 31 int a [4] = {75, 50, 60, 90}, maxValue, I, xOrg, yOrg, deltaX, deltaY, xBegin, yBegin, xEnd, yEnd, s = 0; 32 int hatchBrushStyle [4] = {HS_BDIAGONAL, HS_FDIAGONAL, HS_CROSS, HS_DIAGCROSS}; // four Shadow styles 33 COLORREF colorIndex [4] = {RGB (255, 0, 0), RGB (0,255, 0), RGB (0, 0,255), RGB (255, 0,255)}; // four colors: 34 switch (message) 35 {36 case WM_PAINT: 37 maxValue = a [0]; 38 for (I = 0; I <4; I ++) 39 {40 s + = a [I]; 41 if (a [I]> maxValue) 42 maxValue = a [I]; 43} // calculate the total value of all data and the maximum value 44 hDC = BeginPaint (hWnd, & ps); 45 GetClientRect (hWnd, & clientRect ); // obtain the size of the user area 46 if (clientRect. right-clientRect. left) <300 | (clientRect. bottom-clientRect. top) <300) // determine the screen size 47 {48 MessageBox (hWnd, L ". The screen size cannot be drawn! ", L" error message ", 0); 49 // EndPaint (hWnd, & ps); // end drawing 50 break; 51} 52 hPen = (HPEN) getStockObject (BLACK_PEN); // set the paint brush to the pre-defined black paint brush 53 SelectObject (hDC, hPen); // select the paint brush 54 Rectangle (hDC, clientRect. left + 10, clientRect. top + 10, clientRect. right-10, clientRect. bottom-10); 55 MoveToEx (hDC, (clientRect. left + clientRect. right)/2, clientRect. top + 10, NULL); 56 LineTo (hDC, (clientRect. left + clientRect. Right)/2, clientRect. bottom-10); // The window is divided into the left and right parts in the middle of the window 57 // -------------------------- The following is the data distribution chart in the left half with a column chart ------------------------------ 58 xOrg = client. left + 60; 59 yOrg = clientRect. bottom-60; // coordinate origin of the column weight 60 xEnd = (clientRect. left + clientRect. right)/2-50; // the rightmost 61 yEnd = yOrg; 62 deltaX = (xEnd-xOrg-100)/4; // The unit for calculating the horizontal coordinates is 63 MoveToEx (hDC, xOrg, yOrg, NULL); 64 LineTo (h DC, xEnd, yEnd); // draw the horizontal axis 65 xEnd = xOrg; 66 yEnd = clientRect. top + 60; // The top 67 MoveToEx (hDC, xOrg, yOrg, NULL) of the coordinate axis; 68 LineTo (hDC, xEnd, yEnd ); // draw the vertical axis 69 deltaY = (yOrg-yEnd-100)/maxValue; // calculate the unit pixel 70 hPen = CreatePen (PS_SOLID, 1, RGB (127,127,127); // use gray as paint brush 71 SelectObject (hDC, hPen); // select paint brush 72 for (I = 0; I <4; I ++) 73 {74 hBrush = CreateHatchBrush (hatchBrushStyle [I], colorI Ndex [I]); // create a shadow image brush 75 SelectObject (hDC, hBrush); // select image brush 76 xBegin = xOrg + deltaX * I; 77 yBegin = yOrg; 78 xEnd = xBegin + deltaX; 79 yEnd = yOrg-a [I] * deltaY; 80 Rectangle (hDC, xBegin, yBegin, xEnd, yEnd ); // The column number of each part is 81} 82 // ------------------------------ the data distribution chart ------------------------------------ 83 xOrg = clientRect in the right part. left + (clientRect. right-clientRect. left) * 3/4 + 10; 84 yOrg = ClientRect. top + (clientRect. bottom-clientRect. top)/2 + 10; // xOrg, yOrg is the center coordinate of the right part 85 deltaX = deltaY = min (clientRect. right-clientRect. left)/4, (clientRect. bottom-clientRect. top)/2)-50; 86 xBegin = xOrg + 10; 87 yBegin = yOrg; 88 for (I = 0; I <4; I ++) 89 {90 hBrush = CreateSolidBrush (colorIndex [I]); // create a monochrome paint brush 91 SelectObject (hDC, hBrush ); // select image painter 92 sita = sita + 2*3.1415 * a [I] /S; 93 xEnd = xOrg + 10 * cos (sita); 94 yEnd = yOrg-10 * sin (sita); // calculates the coordinates of the end of the Pie chart 95 Pie (hDC, xOrg-deltaX, yOrg-deltaY, xOrg + deltaX, yOrg + deltaY, xBegin, yBegin, xEnd, yEnd); // 96 xBegin = xEnd; 97 yBegin = yEnd; // coordinates 98} 99 DeleteObject (hPen); 100 DeleteObject (hBrush); 101 EndPaint (hWnd, & ps ); // end drawing 102 break; 103 case WM_SIZE: // when the window size changes, refresh the window 104 InvalidateRect (hWnd, NULL, t Rue); 105 break; 106 case WM_DESTROY: 107 PostQuitMessage (0); // call PostQuitMessage to send the WM_QUIT message 108 break; 109 default: 110 return DefWindowProc (hWnd, message, wParam, lParam); // default system message processing function 111 break; 112} 113 return 0; 114} 115 BOOLEAN InitWindowClass (HINSTANCE hInstance, int nCmdShow) 116 {117 WNDCLASSEX wcex; 118 HWND hWnd; 119 TCHAR szWindowClass [] = L "window example"; 120 TCHAR szTitle [] = L "column chart and Pie Chart Display Data Statistics"; 121 wce X. cbSize = sizeof (WNDCLASSEX); 122 wcex. style = 0; 123 wcex. lpfnWndProc = WndProc; 124 wcex. cbClsExtra = 0; 125 wcex. cbWndExtra = 0; 126 wcex. hInstance = hInstance; 127 wcex. hIcon = LoadIcon (hInstance, MAKEINTRESOURCE (IDI_APPLICATION); 128 wcex. hCursor = LoadCursor (NULL, IDC_ARROW); 129 wcex. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); 130 wcex. lpszMenuName = NULL; 131 wcex. lpszClassName = szWindow Class; 132 wcex. hIconSm = LoadIcon (wcex. hInstance, MAKEINTRESOURCE (IDI_APPLICATION); 133 if (! RegisterClassEx (& wcex) 134 return FALSE; 135 hWnd = CreateWindow (136 szWindowClass, 137 szTitle, 138 WS_OVERLAPPEDWINDOW, 139 CW_USEDEFAULT, CW_USEDEFAULT, 140 CW_USEDEFAULT, CW_USEDEFAULT, 141 NULL, 142 NULL, 143 hInstance, 144 NULL145); 146 if (! HWnd) 147 return FALSE; 148 ShowWindow (hWnd, nCmdShow); 149 UpdateWindow (hWnd); 150 return TRUE; 151}